Skip to content

Commit

Permalink
for open
Browse files Browse the repository at this point in the history
  • Loading branch information
GXhua committed Jun 26, 2017
1 parent 962e602 commit 9aacef7
Show file tree
Hide file tree
Showing 9 changed files with 418 additions and 186 deletions.
151 changes: 21 additions & 130 deletions Core/MysqlProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ private function createTable() {

public function init() {

$this->serv = new \swoole_server('0.0.0.0', '9536', SWOOLE_BASE, SWOOLE_SOCK_TCP);
$this->serv = new \swoole_server('0.0.0.0', PORT, SWOOLE_BASE, SWOOLE_SOCK_TCP);
$this->serv->set([
'worker_num' => 1,
'task_worker_num' => 2,
'worker_num' => WORKER_NUM,
'task_worker_num' => TASK_WORKER_NUM,
'dispatch_mode' => 2,
'open_length_check' => 1,
'open_tcp_nodelay'=>true,
'log_file' => '/tmp/sqlproxy.log',
'daemonize' => 0,
'open_tcp_nodelay' => true,
'log_file' => SWOOLE_LOG,
'daemonize' => DAEMON,
'package_length_func' => 'mysql_proxy_get_length'
]
);
Expand All @@ -98,120 +98,9 @@ public function getConfig() {
// $env = get_cfg_var('env.name') ? get_cfg_var('env.name') : 'product';
// $jsonConfig = \CloudConfig::get("platform/proxy_shequ", "test");
// $config = json_decode($jsonConfig, true);
// $config = array(
// 'app_chelun_topic' => array(
// 'master' => array(
// 'host' => '192.168.1.254',
// 'port' => 10120,
// 'user' => 'chelun_topic',
// 'password' => 'F9lUez9A',
// 'database' => 'app_chelun_topic',
// 'charset' => 'utf8mb4',
// )
// ), 'infra_chelun' => array(
// 'master' => array(
// 'host' => '192.168.1.221',
// 'port' => 10121,
// 'user' => 'ro_infra',
// 'password' => 'Ui7$F4D#F5Sf',
// 'database' => 'infra_chelun',
// 'charset' => 'utf8mb4',
// )),
// 'app_chelun' => array(
// 'master' => array(
// 'host' => '192.168.1.41',
// 'port' => 3306,
// 'user' => 'chelun_user',
// 'password' => 'wK22KQucCiQe',
// 'database' => 'app_chelun',
// 'charset' => 'utf8mb4',
// )));
$config = array(
'eguanjia' => array(
'master' => array(
'host' => '10.10.2.73',
'port' => 3306,
'user' => 'root',
'password' => 'woshiguo35',
'database' => 'eguanjia',
'charset' => 'utf8',
),
'slave' => array(
array(
'host' => '10.10.2.73',
'port' => 3306,
'user' => 'root',
'password' => 'woshiguo35',
'database' => 'eguanjia',
'charset' => 'utf8',
),
),
)
);
// no use
// $config = array(
// 'chelun' => array(//test is tes db
// 'master' => array(
// 'host' => '10.10.1.23',
// 'port' => 3306,
// 'user' => 'chelun',
// 'password' => 'chelun',
// 'database' => 'chelun',
// 'charset' => 'utf8mb4',
// ),
// 'slave' => array(
// array(
// 'host' => '10.10.1.23',
// 'port' => 3306,
// 'user' => 'chelun',
// 'password' => 'chelun',
// 'database' => 'chelun',
// 'charset' => 'utf8mb4',
// ),
// ),
// ),
// 'chelun_home' => array(//test is tes db
// 'master' => array(
// 'host' => '10.10.1.23',
// 'port' => 3306,
// 'user' => 'chelun',
// 'password' => 'chelun',
// 'database' => 'chelun_home',
// 'charset' => 'utf8mb4',
// ),
// 'slave' => array(
// array(
// 'host' => '10.10.1.23',
// 'port' => 3306,
// 'user' => 'chelun',
// 'password' => 'chelun',
// 'database' => 'chelun_home',
// 'charset' => 'utf8mb4',
// ),
// ),
// )
// , 'spider' => array(//test is tes db
// 'master' => array(
// 'host' => '10.10.1.23',
// 'port' => 3306,
// 'user' => 'chelun',
// 'password' => 'chelun',
// 'database' => 'spider',
// 'charset' => 'utf8mb4',
// ),
// 'slave' => array(
// array(
// 'host' => '10.10.1.23',
// 'port' => 3306,
// 'user' => 'chelun',
// 'password' => 'chelun',
// 'database' => 'spider',
// 'charset' => 'utf8mb4',
// ),
// ),
// )
// );
$this->targetConfig = $config;


$this->targetConfig = MYSQL_CONF;
}

public function start() {
Expand Down Expand Up @@ -273,18 +162,20 @@ public function OnReceive($serv, $fd, $from_id, $data) {
}

public function OnResult($binaryData, $fd) {
// $end = microtime(true) * 1000;
if (isset($this->client[$fd])) {//有可能已经关闭了
$this->serv->send($fd, $binaryData);
// $logData = array(
// 'start' => $this->client[$fd]['start'],
// 'size' => strlen($binaryData),
// 'end' => $end,
// 'sql' => $this->client[$fd]['sql'],
// 'datasource' => $this->client[$fd]['datasource'],
// 'client_ip' => $this->client[$fd]['client_ip'],
// );
// $this->serv->task($logData);
if (RECORD_QUERY) {
$end = microtime(true) * 1000;
$logData = array(
'start' => $this->client[$fd]['start'],
'size' => strlen($binaryData),
'end' => $end,
'sql' => $this->client[$fd]['sql'],
'datasource' => $this->client[$fd]['datasource'],
'client_ip' => $this->client[$fd]['client_ip'],
);
$this->serv->task($logData);
}
}
}

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#mysql-proxy
简介:mysqlproxy 通过c扩展实现了mysql协议,应用层逻辑用php+swoole编写,业务代码只需要将配置文件的ip和端口改成proxy的ip和端口即可、
103 changes: 48 additions & 55 deletions config.php
Original file line number Diff line number Diff line change
@@ -1,80 +1,73 @@
<?php

/*
* 是否开启记录日志功能,开启后每次查询都会发送给REDIS_HOST所在的redis,用于生成web管理界面 统计慢查询等
* 开启后会降低30%左右的性能
*/
define("RECORD_QUERY", false);
/*
* 记录客户端sql查询的redis机器
*/
define("REDIS_HOST", "127.0.0.1");
define("REDIS_PORT", "6379");

/*
* redis key
*/
define("REDIS_SLOW", "sqlslow");
define("REDIS_BIG", "sqlbig");


/*
* swoole table的key
*/
define("MYSQL_CONN_KEY", "proxy_conn_key");
define("MYSQL_CONN_REDIS_KEY", "proxy_connection");

/*
* 错误码定义
*/
define("ERROR_CONN", 10001);
define("ERROR_AUTH", 10002);
define("ERROR_QUERY", 10003);
define("ERROR_PREPARE", 10004);

// no use
$shequ_test = array(
'chelun' => array(//test is tes db
'master' => array(
'host' => '10.10.1.23',
'port' => 3306,
'user' => 'chelun_test',
'password' => '4OX36HnN',
'database' => 'chelun',
'charset' => 'utf8mb4',
),
'slave' => array(
array(
'host' => '10.10.1.23',
'port' => 3306,
'user' => 'chelun_test',
'password' => '4OX36HnN',
'database' => 'chelun',
'charset' => 'utf8mb4',
),
),
),
'chelun_home' => array(//test is tes db
'master' => array(
'host' => '10.10.1.23',
'port' => 3306,
'user' => 'chelun_test',
'password' => '4OX36HnN',
'database' => 'chelun_home',
'charset' => 'utf8mb4',
),
'slave' => array(
array(
'host' => '10.10.1.23',
'port' => 3306,
'user' => 'chelun_test',
'password' => '4OX36HnN',
'database' => 'chelun_home',
'charset' => 'utf8mb4',
),
),
)
, 'spider' => array(//test is tes db

/*
* swoole server通用配置信息
*/
define("WORKER_NUM", 1);
//task 用于上报查询用
define("TASK_WORKER_NUM", 1);
//SWOOLE server的 日志
define("SWOOLE_LOG", "/tmp/sqlproxy.log");
//是否守护进程方式运行
define("DAEMON", 0);
//mysql proxy绑定的端口
define("PORT", "9536");


/*
* mysql数据源配置
*/
$config = array(
'eguanjia' => array(
'master' => array(
'host' => '10.10.1.23',
'host' => '10.10.2.73',
'port' => 3306,
'user' => 'chelun_test',
'password' => '4OX36HnN',
'database' => 'spider',
'charset' => 'utf8mb4',
'user' => 'root',
'password' => 'woshiguo35',
'database' => 'eguanjia',
'charset' => 'utf8',
),
'slave' => array(
array(
'host' => '10.10.1.23',
'host' => '10.10.2.73',
'port' => 3306,
'user' => 'chelun_test',
'password' => '4OX36HnN',
'database' => 'spider',
'charset' => 'utf8mb4',
'user' => 'root',
'password' => 'woshiguo35',
'database' => 'eguanjia',
'charset' => 'utf8',
),
),
)
);
define("MYSQL_CONF", $config);
2 changes: 1 addition & 1 deletion cpp_moudle/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PHPX_INCLUDE_DIR = "/src/PHP-X"
PHPX_INCLUDE_DIR = "./PHP-X"
PHP_INCLUDE = `php-config --includes`
PHP_INCLUDE_DIR = `php-config --include-dir`
PHP_EXTENSION_DIR = `php-config --extension-dir`
Expand Down
1 change: 1 addition & 0 deletions cpp_moudle/PHP-X
Submodule PHP-X added at a2f95f
Binary file modified cpp_moudle/mysql_proxy.so
Binary file not shown.
7 changes: 7 additions & 0 deletions cpp_moudle/nbproject/private/private.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project-private xmlns="http://www.netbeans.org/ns/project-private/1">
<code-assistance-data xmlns="http://www.netbeans.org/ns/make-project-private/1">
<code-model-enabled>true</code-model-enabled>
</code-assistance-data>
<data xmlns="http://www.netbeans.org/ns/make-project-private/1">
<activeConfTypeElem>0</activeConfTypeElem>
<activeConfIndexElem>0</activeConfIndexElem>
</data>
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group/>
</open-files>
</project-private>
Loading

0 comments on commit 9aacef7

Please sign in to comment.