让PHP像C罗一样操作MySQL之ProxySQL

ProxySQL 简介

ProxySQL是一个高性能的MySQL中间件,是灵活强大的MySQL代理层。像C罗一样的强大,可以实现读写分离,支持Query路由功能,支持动态指定某个SQL进行cache,支持动态加载配置、故障切换和一些SQL的过滤功能。现在由percona支持。

特性

  • 连接池
  • 主机和用户的最大连接数限制
  • 自动下线后端DB
  • 强大的规则路由引擎
  • 支持prepared statement
  • 支持Query Cache
  • 支持负载均衡

ProxySQL 安装

相关网站

http://www.proxysql.com/ (官网)
https://www.percona.com/live/e17/sessions/utilizing-proxysql-for-connection-pooling-in-php
https://github.com/sysown/proxysql(git地址)
https://github.com/sysown/proxysql/wiki (帮助手册)

Ubuntu包安装

wget https://github.com/sysown/proxysql/releases/download/v1.4.9/proxysql_1.4.9-ubuntu16_amd64.deb
dpkg -i proxysql_1.4.9-ubuntu16_amd64.deb
  • 查看服务是否安装成功
root@bd-All-Series:/var/run# proxysql -h
High Performance Advanced Proxy for MySQL

USAGE: proxysql [OPTIONS]

OPTIONS:

-c, --config ARG             Configuraton file
-D, --datadir ARG            Datadir
-e, --exit-on-error          Do not restart ProxySQL if crashes
-f, --foreground             Run in foreground
-h, -help, --help, --usage   Display usage instructions.
-M, --no-monitor             Do not start Monitor Module
-n, --no-start               Starts only the admin service
-r, --reuseport              Use SO_REUSEPORT
-S, --admin-socket ARG       Administration Unix Socket
-V, --version                Print version
--idle-threads               Create auxiliary threads to handle idle connections
--initial                    Rename/empty database file
--reload                     Merge config file into database file
--sqlite3-server             Enable SQLite3 Server


ProxySQL rev. 1.4.9-3-gd9fd599 -- Wed May 30 11:59:03 2018
Copyright (C) 2013-2017 René Cannaò
This program is free and without warranty

  • 配置文件路径
    /etc/proxysql.cnf
  • 启动和停止命令
service proxysql start  #systemctl start proxysql
service proxysql stop   #systemctl stop proxysql
  • 连接后台
root@bd-All-Series:/var/run# mysql -u admin -padmin -h 127.0.0.1 -P6032 --prompt='Admin> '
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 165106
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

Admin> 

与mysql相勾结

修改配置文件

datadir="/var/lib/proxysql"

admin_variables=
{
    admin_credentials="admin:admin"
    mysql_ifaces="0.0.0.0:6032"
}

mysql_variables=
{
    threads=4
    max_connections=2048
    default_query_delay=0
    default_query_timeout=36000000
    have_compress=true
    poll_timeout=2000
    interfaces="/var/run/proxysql.sock"
    default_schema="information_schema"
    stacksize=1048576
    server_version="5.5.30"
    connect_timeout_server=3000
    monitor_username="monitor"
    monitor_password="monitor"
    monitor_history=600000
    monitor_connect_interval=60000
    monitor_ping_interval=10000
    monitor_read_only_interval=1500
    monitor_read_only_timeout=500
    ping_interval_server_msec=120000
    ping_timeout_server=500
    commands_stats=true
    sessions_sort=true
    connect_retries_on_failure=10
}

mysql_servers =
(
    { address="127.0.0.1" , port=3306 , hostgroup=0 }
)

mysql_users:
(
    { username = "root" , password = "root" , default_hostgroup = 0 , active = 1 }
)

mysql_query_rules:
(

)

scheduler=
(

)

mysql_replication_hostgroups=
(

)

在MySQL中增加proxysql的监听用户,注意是在MySQL中添加

use mysql;
CREATE USER 'monitor'@'host' IDENTIFIED BY 'monitor';
grant all privileges on mq.* to monitor@localhost identified by 'monitor';
flush privileges;

重启proxysql服务

  • 当我们修改了配置文件需要重新加载配置文件(存放在sqlite)
service proxysql initial
#其他相关命令
proxysql --initial                    #Rename/empty database file
proxysql --reload                     #Merge config file into database file

查看是否成功启动

root@bd-All-Series:/home/bd/soft# ps -ef |grep -v 'grep' | grep proxy
root     29165     1  0 16:20 ?        00:00:00 proxysql --initial -c /etc/proxysql.cnf -D /var/lib/proxysql
root     29166 29165  0 16:20 ?        00:00:24 proxysql --initial -c /etc/proxysql.cnf -D /var/lib/proxysql

代码测试

编写测试例子

<?php
//这里采用Unix Domain Socket形式连接proxysql,对应proxysql的配置文件
$host = '127.0.0.1';
$user = 'root';
$password = 'root';
$database = 'yiiadmin';
$charset = 'utf8';
$socket = '/var/run/proxysql.sock';

$dsn = "mysql:dbname={$database};charset={$charset}";

if (empty($_GET['proxysql'])) {
    $dsn .= ";host={$host}";
} else {
    $dsn .= ';unix_socket=/var/run/proxysql.sock';
}
echo $dsn .'<br/>';
$dbh = new PDO($dsn, $user, $password);

$sql = 'SELECT * FROM admin LIMIT 10';

$value = $dbh->query($sql);

foreach ($value as $v) {
    var_dump($v);
}
?>

ab测试

查看proxysql对MySQL的监控日志(在proxysql中查看)

Admin> SELECT * FROM monitor.mysql_server_ping_log limit 10;
+-----------+------+------------------+----------------------+------------+
| hostname  | port | time_start_us    | ping_success_time_us | ping_error |
+-----------+------+------------------+----------------------+------------+
| 127.0.0.1 | 3306 | 1531477104768035 | 233                  | NULL       |
| 127.0.0.1 | 3306 | 1531477114768168 | 231                  | NULL       |
| 127.0.0.1 | 3306 | 1531477124768243 | 235                  | NULL       |
| 127.0.0.1 | 3306 | 1531477134768373 | 217                  | NULL       |
| 127.0.0.1 | 3306 | 1531477144768454 | 241                  | NULL       |
| 127.0.0.1 | 3306 | 1531477154768586 | 234                  | NULL       |
| 127.0.0.1 | 3306 | 1531477164768663 | 235                  | NULL       |
| 127.0.0.1 | 3306 | 1531477174768792 | 235                  | NULL       |
| 127.0.0.1 | 3306 | 1531477184768866 | 240                  | NULL       |
| 127.0.0.1 | 3306 | 1531477194768994 | 214                  | NULL       |
+-----------+------+------------------+----------------------+------------+
10 rows in set (0.00 sec)
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,036评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,046评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,411评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,622评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,661评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,521评论 1 304
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,288评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,200评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,644评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,837评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,953评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,673评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,281评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,889评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,011评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,119评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,901评论 2 355

推荐阅读更多精彩内容