数据库
最后更新于:2022-04-02 02:21:08
[TOC]
## 安装(默认已安装)
`composer require easyswoole/mysqli`
## 配置信息
在 `dev.php` 数组后添加配置信息
```
'MYSQL' => [
'host' => '',
'port' => 3306,
'user' => '',
'password' => '',
'database' => '',
'timeout' => 30,
'charset' => 'utf8mb4',
'connect_timeout' => '5',//连接超时时间
],
```
## 设置线程池
### 注册mysql
EasySwoole的全局initialize 事件中
```
use EasySwoole\EasySwoole\Config as GConfig;
use EasySwoole\MysqliPool\Mysql;
use EasySwoole\Mysqli\Config;
$configData = GConfig::getInstance()->getConf('MYSQL');
$config = new Config($configData);
/**
这里注册的名字叫mysql,你可以注册多个,比如mysql2,mysql3
*/
$poolConf = Mysql::getInstance()->register('mysql',$config);
$mysql = Mysql::getInstance()->register('mysql1',$config);
```
### 服务启动后任意位置使用
```
$db = \EasySwoole\MysqliPool\Mysql::getInstance()->pool('mysql')::defer();
var_dump($db->rawQuery('select version()'));
$db = \EasySwoole\MysqliPool\Mysql::defer('mysql');
var_dump($db->rawQuery('select version()'));
```
## 原生注册 线程池
> [官网](https://www.easyswoole.com/Cn/Components/mysqlPool.html#mark-H2-14)
';