1 Star 0 Fork 0

louislivi / SwooleMysqlPool

加入 Gitee
与超过 600 万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未指定开源许可证,未经作者的许可,此代码仅用于学习,不能用于其他用途。
克隆/下载
MySqlServer.php 1.89 KB
一键复制 编辑 Web IDE 原始数据 按行查看 历史
louislivi 提交于 2018-09-13 16:11 . Create MySqlServer.php
<?php
require dirname(__FILE__) . '/MySQLPool.php';
use Swoole\Coroutine\Pool\MySQLPool;
$1_connect = [
'host' => '1',
'user' => 'root',
'password' => '123456',
'database' => 'db1',
'charset' => 'utf8mb4', //指定字符集
];
$2_connect = [
'host' => '2',
'user' => 'root',
'password' => '123456',
'database' => 'db2',
'charset' => 'utf8mb4', //指定字符集
];
$3_connect = [
'host' => '3',
'user' => 'root',
'password' => '123456',
'database' => 'db3',
'charset' => 'utf8mb4', //指定字符集
];
$server = new \Swoole\Server("127.0.0.1", 9510, SWOOLE_BASE);
$server->set([
'worker_num' => 8,
'daemonize' => 1,
'max_coro_num' => 16000,
'log_file' => '/var/www/mysql.log',
]);
$server->on('connect', function ($server, $fd){});
$server->on('receive', function ($server, $fd, $from_id,$data) use($1_connect,$2_connect,$3_connect){
$data = json_decode($data,true);
if (isset($data['database']) && $data['query']){
MySQLPool::init([
'1_connect' => [
'serverInfo' => $1_connect,
'maxSpareConns' => 10,
'maxConns' => 20
],
'2_connect' => [
'serverInfo' => $2_connect,
'maxSpareConns' => 10,
'maxConns' => 20
],
'3_connect' => [
'serverInfo' => $3_connect,
'maxSpareConns' => 10,
'maxConns' => 20
],
]);
$swoole_mysql = MySQLPool::fetch($data['database']);
$swoole_mysql = MySQLPool::reconnect($swoole_mysql,$data['database']);
$ret = $swoole_mysql->query($data['query'],60);
MySQLPool::recycle($swoole_mysql);
if ($server->exist($fd)){
$server->send($fd, json_encode($ret));
}
}
});
$server->on('close', function ($server, $fd) {});
$server->start();

评论 ( 0 )

你可以在登录后,发表评论

PHP
1
https://gitee.com/louislivi/swoole_mysqlpool.git
git@gitee.com:louislivi/swoole_mysqlpool.git
louislivi
swoole_mysqlpool
SwooleMysqlPool
master

搜索帮助

182229 41614e54 1850385 182230 7885ed45 1850385