Database
修改 apps/configs/db.php ,加入配置。
~~~
$db['master'] = array(
'type' => Swoole\Database::TYPE_MYSQLi,
'host' => "127.0.0.1",
'port' => 3306,
'dbms' => 'mysql',
'engine' => 'MyISAM',
'user' => "root",
'passwd' => "root",
'name' => "db_live",
'charset' => "utf8",
'setname' => true,
'persistent' => true,
);
return $db;
~~~
使用方式
单数据库,默认读取master配置
$this->db->query("select * from test");
多数据库,读取制定的数据库配置
~~~
$db['master'] = array(
'type' => Swoole\Database::TYPE_MYSQLi,
'host' => "127.0.0.1",
'port' => 3306,
'dbms' => 'mysql',
'engine' => 'MyISAM',
'user' => "root",
'passwd' => "root",
'name' => "swoole",
'charset' => "utf8",
'setname' => true,
'persistent' => false, //MySQL长连接
'use_proxy' => false, //启动读写分离Proxy
'slaves' => array(
array('host' => '127.0.0.1', 'port' => '3307', 'weight' => 100,),
array('host' => '127.0.0.1', 'port' => '3308', 'weight' => 99,),
array('host' => '127.0.0.1', 'port' => '3309', 'weight' => 98,),
),
);
$db['slave'] = array(
'type' => Swoole\Database::TYPE_MYSQLi,
'host' => "127.0.0.1",
'port' => 3306,
'dbms' => 'mysql',
'engine' => 'MyISAM',
'user' => "root",
'passwd' => "root",
'name' => "swoole",
'charset' => "utf8",
'setname' => true,
'persistent' => false, //MySQL长连接
);
$this->db('other_db_config')->query("select * from test");
~~~
驱动类型
swoole框架支持3种驱动类型:
Swoole\Database::TYPE_MYSQL,使用mysql扩展
Swoole\Database::TYPE_MYSQLi,使用mysqli扩展
Swoole\Database::TYPE_PDO,使用PDO扩展
配置选项
charset 制定数据库字符集
setname 在连接服务器成功后发送set names $charset
persistent 启用MySQL数据库长连接