/** * @brief 链接数据库 * @return boolean */ protected function _connect() { $dbObj = new \mysqli($this->_conf['host'], $this->_conf['user'], $this->_conf['password'], $this->_conf['dbName'], $this->_conf['port']); if ($dbObj->connect_error) { echo "Mysqli Error [connect_db_failed][errno]{$dbObj->connect_errno}" . "[error]{$dbObj->connect_error} [conf]" . serialize($this->_conf) . PHP_EOL; return false; } if (!empty($this->_conf['charset'])) { $dbObj->set_charset($this->_conf['charset']); } $dbSock = swoole_get_mysqli_sock($dbObj); if (!is_long($dbSock)) { echo "Mysqli Error [swoole_get_mysqli_sock]" . serialize($dbSock) . PHP_EOL; goto errorClose; } $ret = swoole_event_add($dbSock, array($this, 'onQueryResponse')); if (!is_long($ret)) { echo "Mysqli Error [swoole_event_add]" . serialize($ret) . PHP_EOL; goto errorClose; } $this->_idlePool[$dbSock] = compact('dbObj', 'dbSock'); $this->_connectionNum++; return $this; errorClose: $dbObj->close(); return false; }
/** * create mysql connection */ protected function createConnection() { $config = $this->config; $db = new \mysqli(); $db->connect($config['host'], $config['user'], $config['password'], $config['database'], $config['port']); $db_sock = swoole_get_mysqli_sock($db); swoole_event_add($db_sock, array($this, 'onSQLReady')); $this->idle_pool[] = array('object' => $db, 'socket' => $db_sock); $this->connection_num++; }
function onStart($serv) { $this->serv = $serv; for ($i = 0; $i < $this->pool_size; $i++) { $db = new mysqli(); $db->connect('172.16.1.19', 'test', 'test', '1188test'); $db_sock = swoole_get_mysqli_sock($db); swoole_event_add($db_sock, array($this, 'onSQLReady')); $this->idle_pool[] = array('mysqli' => $db, 'db_sock' => $db_sock, 'fd' => 0); } echo "Server: start.Swoole version is [" . SWOOLE_VERSION . "]\n"; }
/** * create mysql connection */ protected function createConnection() { $config = $this->config; $db = new \mysqli(); $db->connect($config['host'], $config['user'], $config['password'], $config['database'], $config['port']); if ($db->connect_error) { return [$db->connect_errno, $db->connect_error]; } if (!empty($config['charset'])) { $db->set_charset($config['charset']); } $db_sock = swoole_get_mysqli_sock($db); swoole_event_add($db_sock, array($this, 'onSQLReady')); $this->idle_pool[$db_sock] = array('object' => $db, 'socket' => $db_sock); $this->connection_num++; return 0; }
/** * [send 兼容Base类封装的send方法,调度器可以不感知client类型] * @param [type] $callback [description] * @return [type] [description] */ public function send(callable $callback) { if (!isset($this->db)) { echo " db not init \n"; //TODO do callback function to task return; } //TODO conf check $config = $this->conf; $this->callback = $callback; $this->calltime = microtime(true); $this->key = md5($this->calltime . $config['host'] . $config['port'] . rand(0, 10000)); $this->db->connect($config['host'], $config['user'], $config['password'], $config['database'], $config['port']); if (!empty($config['charset'])) { $this->db->set_charset($config['charset']); } $db_sock = swoole_get_mysqli_sock($this->db); swoole_event_add($db_sock, array($this, 'onSqlReady')); $this->doQuery($this->sql); }
<?php if (!function_exists('swoole_get_mysqli_sock')) { die("no async_mysql support\n"); } $db = new mysqli(); $db->connect('127.0.0.1', 'root', '123456', 'paopaogame'); $sql = "show tables"; $db->query($sql, MYSQLI_ASYNC); $readinfo = "read mysql back:" . $sql; $count = 0; swoole_event_add(swoole_get_mysqli_sock($db), function ($__db_sock) { global $db; global $readinfo; global $count; global $sql; // var_dump($__db_sock); $res = $db->reap_async_query(); var_dump($res->fetch_all(MYSQLI_ASSOC)); $db->query("SELECT * FROM paopaogame.paopaoaccount limit 0,2;", MYSQLI_ASYNC); echo $readinfo . " count=" . $count . "\n"; $count++; // echo "sleep "."1"." second"; sleep(1); }); echo "Finish\n";
/** * create mysql connection */ protected function createConnection() { $config = $this->config; $db = new \mysqli(); $db->connect($config['host'], $config['user'], $config['password'], $config['database'], $config['port']); if ($db->connect_error) { return [$db->connect_errno, $db->connect_error]; } if (!empty($config['charset'])) { $db->set_charset($config['charset']); } $db_sock = swoole_get_mysqli_sock($db); //内置客户端不需要加入EventLoop if (!$this->haveSwooleAsyncMySQL) { swoole_event_add($db_sock, array($this, 'onSQLReady')); } else { $db->sock = $db_sock; } //保存到空闲连接池中 $this->idle_pool[$db_sock] = array('object' => $db, 'socket' => $db_sock); //增加计数 $this->connection_num++; return 0; }
/** * create mysql connection */ public function createConnection() { $config = $this->config; $db = new \mysqli(); $db->connect($config['host'], $config['user'], $config['password'], $config['database'], $config['port']); if (!empty($config['charset'])) { $db->set_charset($config['charset']); } $db_sock = swoole_get_mysqli_sock($db); swoole_event_add($db_sock, array($this, "onSQLReady")); $this->idle_pool[$db_sock] = array('object' => $db, 'socket' => $db_sock); $this->connection_num++; }