ProxySQL 读写分离实践
前言
ProxySQL是一个高性能的MySQL中间件,拥有强大的规则引擎。具有以下特性:
- 连接池,而且是 multiplexing
- 主机和用户的最大连接数限制
- 自动下线后端DB
- 延迟超过阀值
- ping 延迟超过阀值
- 网络不通或宕机
- 强大的规则路由引擎
- 实现读写分离
- 查询重写
- sql流量镜像
- 支持prepared statement
- 支持Query Cache
- 支持负载均衡,与gelera结合自动failover
官方参考文档 https://github.com/sysown/proxysql/wiki
生产环境建议架构如下:
注意, MHA 的VIP 已经不需要了,因为ProxySQL 可以检查发现并剔除故障的机器,对应用是透明的。
环境介绍
10.180.3.1 Proxy1
10.180.2.163 MHA-M1
10.180.2.164 MHA-S1
10.180.2.165 MHA-S2
软件下载
推荐使用percona 的源,使用yum install
[root@Proxy1 ~]# yum list proxysql |grep proxysql
proxysql.x86_64 1.3.9-1.1.el6 percona-release-x86_64
安装完后,在/etc 有两个初始化的文件
proxysql-admin.cnf proxysql.cnf , 里面配置有默认的IP 和端口号, 可以根据需要进行修改
使用service proxysql start 启动服务
启动之后,可以看到这些文件:proxysql.db是SQLITE的数据文件,proxysql配置,如后端数据库的账号、密码、路由等存储在这个数据库里面,proxysql.log是日志文件。proxysql.pid这个pid文件。proxysql.cnf是ProxySQL的一些静态配置项,比如一些启动选项,sqlite的数据目录等等。配置文件只在第一次启动的时候读取进行初始化,后面只读取db文件。
[root@Proxy1 init.d]# ps -ef|grep proxysql
root : pts/ :: mysql -uadmin -p -S /tmp/proxysql_admin.sock
proxysql Jul17 ? :: /usr/bin/proxysql -c /etc/proxysql.cnf -D /var/lib/proxysql
proxysql Jul17 ? :: /usr/bin/proxysql -c /etc/proxysql.cnf -D /var/lib/proxysql
和MySQL的很相似,我们启动一个进程,然后fork出一个子进程,父进程负责监控子进程运行状况如果挂了则拉起来,子进程负责执行真正的任务
库的说明
root@proxysql1:~# mysql -uadmin -padmin -h127.0.0. -P6032
...
...
admin@127.0.0.1 : (none) ::>show databases;
+-----+---------+-------------------------------+
| seq | name | file |
+-----+---------+-------------------------------+
| | main | |
| | disk | /var/lib/proxysql/proxysql.db |
| | stats | |
| | monitor | |
+-----+---------+-------------------------------+
main
内存配置数据库,表里存放后端db实例、用户验证、路由规则等信息。表名以runtime_
开头的表示proxysql当前运行的配置内容,不能通过dml语句修改,只能修改对应的不以 runtime_ 开头的(在内存)里的表,然后LOAD
使其生效,SAVE
使其存到硬盘以供下次重启加载。disk
是持久化到硬盘的配置,sqlite数据文件。stats
是proxysql运行抓取的统计信息,包括到后端各命令的执行次数、流量、processlist、查询种类汇总/执行时间等等。monitor
库存储 monitor 模块收集的信息,主要是对后端db的健康/延迟检查
至于库里面每一个表和字段的说明,请详细阅读
http://seanlook.com/2017/04/10/mysql-proxysql-install-config/
配置ProxySQL
登入ProxySQL,把MySQL主从的信息添加进去。将主库master也就是做写入的节点放到HG 300中,salve节点做读放到HG 3000。在proxysql输入命令:
mysql -uadmin -padmin -h127.0.0.1 -P6032
配置后端DB 的读写分组
admin@localhost:proxysql_admin.sock [(none)]>insert into mysql_servers(hostgroup_id,hostname,port,weight,max_connections,max_replication_lag,comment) values (,'10.180.2.163',,,,,'proxysql'); admin@localhost:proxysql_admin.sock [(none)]>insert into mysql_servers(hostgroup_id,hostname,port,weight,max_connections,max_replication_lag,comment) values (,'10.180.2.163',,,,,'proxysql'); admin@localhost:proxysql_admin.sock [(none)]>insert into mysql_servers(hostgroup_id,hostname,port,weight,max_connections,max_replication_lag,comment) values (,'10.180.2.164',,,,,'proxysql'); admin@localhost:proxysql_admin.sock [(none)]>insert into mysql_servers(hostgroup_id,hostname,port,weight,max_connections,max_replication_lag,comment) values (,'10.180.2.165',,,,,'proxysql');
admin@localhost:proxysql_admin.sock [(none)]>select * from mysql_servers order by hostgroup_id;
+--------------+--------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+----------+
| hostgroup_id | hostname | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+--------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+----------+
| | 10.180.2.163 | | ONLINE | | | | | | | proxysql |
| | 10.180.2.163 | | ONLINE | | | | | | | proxysql |
| | 10.180.2.164 | | ONLINE | | | | | | | proxysql |
| | 10.180.2.165 | | ONLINE | | | | | | | proxysql |
+--------------+--------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+----------+
注意, 这里为什么要把master 也加到读 的HG 呢,这是为了防止所有的slave 都宕机仍然可以通过master 进行读,平时配置的weight 可以相对小一些
配置后端MySQL用户
这个用户需要先在后端MySQL里真实存在,一个是监控账号、一个是程序账号。
a) 监控账号
root@localhost:mysql3306.sock [(none)]>GRANT SUPER, REPLICATION CLIENT ON *.* TO 'proxysql'@'%' identified by 'proxysql';
注意:由于需要执行show slave status的命令来获得延迟时间,所以需要权限SUPER 和 REPLICATION CLIENT。并且需要设置mysql_servers.max_replication_lag的值,由于mysql_servers.max_replication_lag仅适用于从,但也可以将其配置为所有主机,无论是从还是主(不会有任何影响)。
b)程序账号
GRANT ALL ON `sbtest`.* TO 'sbuser'@'%' identified by 'sbuser';
配置user 和HG 的对应关系 和监控账号
Admin 的后台:
admin@localhost:proxysql_admin.sock [(none)]>insert into mysql_users(username,password,active,default_hostgroup,transaction_persistent)values
-> ('sbuser','sbuser',,,);
Query OK, row affected (0.00 sec)
admin@localhost:proxysql_admin.sock [(none)]>select * from mysql_users\G;
*************************** . row ***************************
username: sbuser
password: sbuser
active:
use_ssl:
default_hostgroup:
default_schema: NULL
schema_locked:
transaction_persistent:
fast_forward:
backend:
frontend:
max_connections:
row in set (0.00 sec)
设置健康监测账号监控后台MySQL 节点
admin@localhost:proxysql_admin.sock [(none)]>set mysql-monitor_username='proxysql';
Query OK, row affected (0.00 sec) admin@localhost:proxysql_admin.sock [(none)]>set mysql-monitor_password='proxysql';
Query OK, row affected (0.00 sec)
加载并保存配置
admin@localhost:proxysql_admin.sock [(none)]>load mysql servers to runtime;
Query OK, rows affected (0.01 sec) admin@localhost:proxysql_admin.sock [(none)]>load mysql users to runtime;
Query OK, rows affected (0.00 sec) admin@localhost:proxysql_admin.sock [(none)]>load mysql variables to runtime;
Query OK, rows affected (0.01 sec) admin@localhost:proxysql_admin.sock [(none)]>save mysql servers to disk;
Query OK, rows affected (0.03 sec) admin@localhost:proxysql_admin.sock [(none)]>save mysql users to disk;
Query OK, rows affected (0.02 sec) admin@localhost:proxysql_admin.sock [(none)]>save mysql variables to disk;
Query OK, rows affected (0.01 sec)
连接Proxy尝试做业务
sbuser@proxy1: [sbtest]> use sbtest
Database changed, warnings
sbuser@proxy1: [sbtest]> create table t1 ( id int);
Query OK, rows affected (0.08 sec) sbuser@proxy1: [sbtest]> insert into t1 select ;
Query OK, row affected (0.05 sec)
Records: Duplicates: Warnings: sbuser@proxy1: [sbtest]> select * from t1;
+------+
| id |
+------+
| |
+------+
row in set (0.00 sec)
从Stats 库,你可以看到有类似审计的功能
admin@localhost:proxysql_admin.sock [(none)]>select* from stats.stats_mysql_query_digest;
+-----------+------------+----------+--------------------+--------------------------+------------+------------+------------+----------+----------+----------+
| hostgroup | schemaname | username | digest | digest_text | count_star | first_seen | last_seen | sum_time | min_time | max_time |
+-----------+------------+----------+--------------------+--------------------------+------------+------------+------------+----------+----------+----------+
| | sbtest | sbuser | 0x3765930C7143F468 | select * from t1 | | | | | | |
| | sbtest | sbuser | 0x3C44D988579DAFFA | insert into t1 values(?) | | | | | | |
+-----------+------------+----------+--------------------+--------------------------+------------+------------+------------+----------+----------+----------+
默认是没有配置读写分离的,所以所有的读写都分配到了master 的HG 上面,下面我们配置读写分离规则
读写分离规则
除select * from tb for update的select全部发送到slave,其他的的语句发送到master。
admin@localhost:proxysql_admin.sock [(none)]>insert into mysql_query_rules(active,match_pattern,destination_hostgroup,apply) values
-> (,'^select.*for updates',,);
Query OK, row affected (0.01 sec) admin@localhost:proxysql_admin.sock [(none)]>insert into mysql_query_rules(active,match_pattern,destination_hostgroup,apply) values (,'^select',,);
Query OK, row affected (0.00 sec) admin@localhost:proxysql_admin.sock [(none)]>load mysql query rules to runtime;
Query OK, rows affected (0.00 sec) admin@localhost:proxysql_admin.sock [(none)]>save mysql query rules to disk;
Query OK, rows affected (0.02 sec) admin@localhost:proxysql_admin.sock [(none)]>select rule_id,active,match_pattern,destination_hostgroup,apply from runtime_mysql_query_rules;
+---------+--------+----------------------+-----------------------+-------+
| rule_id | active | match_pattern | destination_hostgroup | apply |
+---------+--------+----------------------+-----------------------+-------+
| | | ^select.*for updates | | |
| | | ^select | | |
+---------+--------+----------------------+-----------------------+-------+
rows in set (0.00 sec)
再次进行读写测试
先清空stats 表
select * from stats_mysql_query_digest_reset;
admin@localhost:proxysql_admin.sock [(none)]>select * from stats_mysql_query_digest;
+-----------+------------+----------+--------------------+-----------------------------+------------+------------+------------+----------+----------+----------+
| hostgroup | schemaname | username | digest | digest_text | count_star | first_seen | last_seen | sum_time | min_time | max_time |
+-----------+------------+----------+--------------------+-----------------------------+------------+------------+------------+----------+----------+----------+
| | sbtest | sbuser | 0x3DCE919B79C9576C | select * from t1 where id=? | | | | | | |
| | sbtest | sbuser | 0x3765930C7143F468 | select * from t1 | | | | | | |
| | sbtest | sbuser | 0x3C44D988579DAFFA | insert into t1 values(?) | | | | | | |
+-----------+------------+----------+--------------------+-----------------------------+------------+------------+------------+----------+----------+----------+
可以看到读已经分发到slave 上,写还是在master,但是怎么知道发送到了3000HG的哪台slave 上?
下面配置和MHA 的集成
mysql_replication_hostgroups 表的主要作用是监视指定主机组中所有服务器的read_only值,并且根据read_only的值将服务器分配给写入器或读取器主机组,定义 hostgroup 的主从关系。ProxySQL monitor 模块会监控 HG 后端所有servers 的 read_only 变量,如果发现从库的 read_only 变为0、主库变为1,则认为角色互换了,自动改写 mysql_servers 表里面 hostgroup 关系,达到自动 Failover 效果。
admin@localhost:proxysql_admin.sock [(none)]>insert into mysql_replication_hostgroups(writer_hostgroup,reader_hostgroup,comment) values (,,'read_write_split');
Query OK, row affected (0.00 sec) admin@localhost:proxysql_admin.sock [(none)]>load mysql servers to runtime;
Query OK, rows affected (0.01 sec) admin@localhost:proxysql_admin.sock [(none)]>save mysql servers to disk;
Query OK, rows affected (0.06 sec) admin@localhost:proxysql_admin.sock [(none)]>select * from runtime_mysql_replication_hostgroups;
+------------------+------------------+------------------+
| writer_hostgroup | reader_hostgroup | comment |
+------------------+------------------+------------------+
| | | read_write_split |
+------------------+------------------+------------------+
row in set (0.00 sec)
手工在线切换MHA:
[root@MHA-S2 mha]# masterha_master_switch --master_state=alive --conf=/etc/mha/app1.cnf --new_master_host=MHA-S1 --new_master_port= --orig_master_is_new_slave
Fri Sep :: - [info] MHA::MasterRotate version 0.57.
Fri Sep :: - [info] Starting online master switch..
Fri Sep :: - [info]
Fri Sep :: - [info] * Phase : Configuration Check Phase..
Fri Sep :: - [info]
Fri Sep :: - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Fri Sep :: - [info] Reading application default configuration from /etc/mha/app1.cnf..
Fri Sep :: - [info] Reading server configuration from /etc/mha/app1.cnf..
Fri Sep :: - [info] GTID failover mode =
Fri Sep :: - [info] Current Alive Master: MHA-M1(10.180.2.163:)
Fri Sep :: - [info] Alive Slaves:
Fri Sep :: - [info] MHA-S1(10.180.2.164:) Version=5.7.-log (oldest major version between slaves) log-bin:enabled
Fri Sep :: - [info] GTID ON
Fri Sep :: - [info] Replicating from MHA-M1(10.180.2.163:)
Fri Sep :: - [info] Primary candidate for the new Master (candidate_master is set)
Fri Sep :: - [info] MHA-S2(10.180.2.165:) Version=5.7.-log (oldest major version between slaves) log-bin:enabled
Fri Sep :: - [info] GTID ON
Fri Sep :: - [info] Replicating from MHA-M1(10.180.2.163:) It is better to execute FLUSH NO_WRITE_TO_BINLOG TABLES on the master before switching. Is it ok to execute on MHA-M1(10.180.2.163:)? (YES/no): yes
Fri Sep :: - [info] Executing FLUSH NO_WRITE_TO_BINLOG TABLES. This may take long time..
Fri Sep :: - [info] ok.
Fri Sep :: - [info] Checking MHA is not monitoring or doing failover..
Fri Sep :: - [info] Checking replication health on MHA-S1..
Fri Sep :: - [info] ok.
Fri Sep :: - [info] Checking replication health on MHA-S2..
Fri Sep :: - [info] ok.
Fri Sep :: - [info] MHA-S1 can be new master.
Fri Sep :: - [info]
From:
MHA-M1(10.180.2.163:) (current master)
+--MHA-S1(10.180.2.164:)
+--MHA-S2(10.180.2.165:) To:
MHA-S1(10.180.2.164:) (new master)
+--MHA-S2(10.180.2.165:)
+--MHA-M1(10.180.2.163:) Starting master switch from MHA-M1(10.180.2.163:) to MHA-S1(10.180.2.164:)? (yes/NO): yes
Fri Sep :: - [info] Checking whether MHA-S1(10.180.2.164:) is ok for the new master..
Fri Sep :: - [info] ok.
Fri Sep :: - [info] MHA-M1(10.180.2.163:): SHOW SLAVE STATUS returned empty result. To check replication filtering rules, temporarily executing CHANGE MASTER to a dummy host.
Fri Sep :: - [info] MHA-M1(10.180.2.163:): Resetting slave pointing to the dummy host.
Fri Sep :: - [info] ** Phase : Configuration Check Phase completed.
Fri Sep :: - [info]
Fri Sep :: - [info] * Phase : Rejecting updates Phase..
Fri Sep :: - [info]
Fri Sep :: - [info] Executing master ip online change script to disable write on the current master:
Fri Sep :: - [info] /usr/local/bin/master_ip_online_change --command=stop --orig_master_host=MHA-M1 --orig_master_ip=10.180.2.163 --orig_master_port= --orig_master_user='root' --new_master_host=MHA-S1 --new_master_ip=10.180.2.164 --new_master_port= --new_master_user='root' --orig_master_ssh_user=root --new_master_ssh_user=root --orig_master_is_new_slave --orig_master_password=xxx --new_master_password=xxx
Unknown option: orig_master_ssh_user
Unknown option: new_master_ssh_user
Unknown option: orig_master_is_new_slave
Unknown option: orig_master_password
Unknown option: new_master_password
Fri Sep :: Set read_only on the new master.. ok.
Fri Sep :: Waiting all running threads are disconnected.. (max milliseconds)
{'Time' => '','Command' => 'Binlog Dump GTID','db' => undef,'Id' => '','Info' => undef,'User' => 'repl','State' => 'Master has sent all binlog to slave; waiting for more updates','Host' => 'MHA-S2:44384'}
{'Time' => '','Command' => 'Binlog Dump GTID','db' => undef,'Id' => '','Info' => undef,'User' => 'repl','State' => 'Master has sent all binlog to slave; waiting for more updates','Host' => 'MHA-S1:52664'}
Fri Sep :: Waiting all running threads are disconnected.. (max milliseconds)
{'Time' => '','Command' => 'Binlog Dump GTID','db' => undef,'Id' => '','Info' => undef,'User' => 'repl','State' => 'Master has sent all binlog to slave; waiting for more updates','Host' => 'MHA-S2:44384'}
{'Time' => '','Command' => 'Binlog Dump GTID','db' => undef,'Id' => '','Info' => undef,'User' => 'repl','State' => 'Master has sent all binlog to slave; waiting for more updates','Host' => 'MHA-S1:52664'}
{'Time' => '','Command' => 'Sleep','db' => undef,'Id' => '','Info' => undef,'User' => 'proxysql','State' => '','Host' => '10.180.3.1:58934'}
Fri Sep :: Waiting all running threads are disconnected.. (max milliseconds)
{'Time' => '','Command' => 'Binlog Dump GTID','db' => undef,'Id' => '','Info' => undef,'User' => 'repl','State' => 'Master has sent all binlog to slave; waiting for more updates','Host' => 'MHA-S2:44384'}
{'Time' => '','Command' => 'Binlog Dump GTID','db' => undef,'Id' => '','Info' => undef,'User' => 'repl','State' => 'Master has sent all binlog to slave; waiting for more updates','Host' => 'MHA-S1:52664'}
{'Time' => '','Command' => 'Sleep','db' => undef,'Id' => '','Info' => undef,'User' => 'proxysql','State' => '','Host' => '10.180.3.1:58934'}
Fri Sep :: Set read_only= on the orig master.. ok.
Fri Sep :: Waiting all running queries are disconnected.. (max milliseconds)
{'Time' => '','Command' => 'Binlog Dump GTID','db' => undef,'Id' => '','Info' => undef,'User' => 'repl','State' => 'Master has sent all binlog to slave; waiting for more updates','Host' => 'MHA-S2:44384'}
{'Time' => '','Command' => 'Binlog Dump GTID','db' => undef,'Id' => '','Info' => undef,'User' => 'repl','State' => 'Master has sent all binlog to slave; waiting for more updates','Host' => 'MHA-S1:52664'}
Disabling the VIP on old master: MHA-M1
Fri Sep :: Killing all application threads..
Fri Sep :: done.
Fri Sep :: - [info] ok.
Fri Sep :: - [info] Locking all tables on the orig master to reject updates from everybody (including root):
Fri Sep :: - [info] Executing FLUSH TABLES WITH READ LOCK..
Fri Sep :: - [info] ok.
Fri Sep :: - [info] Orig master binlog:pos is -binlog.:.
Fri Sep :: - [info] Waiting to execute all relay logs on MHA-S1(10.180.2.164:)..
Fri Sep :: - [info] master_pos_wait(-binlog.:) completed on MHA-S1(10.180.2.164:). Executed events.
Fri Sep :: - [info] done.
Fri Sep :: - [info] Getting new master's binlog name and position..
Fri Sep :: - [info] -binlog.:
Fri Sep :: - [info] All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='MHA-S1 or 10.180.2.164', MASTER_PORT=, MASTER_AUTO_POSITION=, MASTER_USER='repl', MASTER_PASSWORD='xxx';
Fri Sep :: - [info] Executing master ip online change script to allow write on the new master:
Fri Sep :: - [info] /usr/local/bin/master_ip_online_change --command=start --orig_master_host=MHA-M1 --orig_master_ip=10.180.2.163 --orig_master_port= --orig_master_user='root' --new_master_host=MHA-S1 --new_master_ip=10.180.2.164 --new_master_port= --new_master_user='root' --orig_master_ssh_user=root --new_master_ssh_user=root --orig_master_is_new_slave --orig_master_password=xxx --new_master_password=xxx
Unknown option: orig_master_ssh_user
Unknown option: new_master_ssh_user
Unknown option: orig_master_is_new_slave
Unknown option: orig_master_password
Unknown option: new_master_password
Fri Sep :: Set read_only= on the new master.
Enabling the VIP - 10.180.2.168/ on the new master - MHA-S1
Fri Sep :: - [info] ok.
Fri Sep :: - [info]
Fri Sep :: - [info] * Switching slaves in parallel..
Fri Sep :: - [info]
Fri Sep :: - [info] -- Slave switch on host MHA-S2(10.180.2.165:) started, pid:
Fri Sep :: - [info]
Fri Sep :: - [info] Log messages from MHA-S2 ...
Fri Sep :: - [info]
Fri Sep :: - [info] Waiting to execute all relay logs on MHA-S2(10.180.2.165:)..
Fri Sep :: - [info] master_pos_wait(-binlog.:) completed on MHA-S2(10.180.2.165:). Executed events.
Fri Sep :: - [info] done.
Fri Sep :: - [info] Resetting slave MHA-S2(10.180.2.165:) and starting replication from the new master MHA-S1(10.180.2.164:)..
Fri Sep :: - [info] Executed CHANGE MASTER.
Fri Sep :: - [info] Slave started.
Fri Sep :: - [info] End of log messages from MHA-S2 ...
Fri Sep :: - [info]
Fri Sep :: - [info] -- Slave switch on host MHA-S2(10.180.2.165:) succeeded.
Fri Sep :: - [info] Unlocking all tables on the orig master:
Fri Sep :: - [info] Executing UNLOCK TABLES..
Fri Sep :: - [info] ok.
Fri Sep :: - [info] Starting orig master as a new slave..
Fri Sep :: - [info] Resetting slave MHA-M1(10.180.2.163:) and starting replication from the new master MHA-S1(10.180.2.164:)..
Fri Sep :: - [info] Executed CHANGE MASTER.
Fri Sep :: - [info] Slave started.
Fri Sep :: - [info] All new slave servers switched successfully.
Fri Sep :: - [info]
Fri Sep :: - [info] * Phase : New master cleanup phase..
Fri Sep :: - [info]
Fri Sep :: - [info] MHA-S1: Resetting slave info succeeded.
Fri Sep :: - [info] Switching master to MHA-S1(10.180.2.164:) completed successfully.
切换完再次查看mysql_servers的HG 分组
admin@localhost:proxysql_admin.sock [(none)]>select * from runtime_mysql_servers;
+--------------+--------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+----------+
| hostgroup_id | hostname | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+--------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+----------+
| | 10.180.2.165 | | ONLINE | | | | | | | proxysql |
| | 10.180.2.163 | | ONLINE | | | | | | | proxysql |
| | 10.180.2.164 | | ONLINE | | | | | | | proxysql |
| | 10.180.2.164 | | ONLINE | | | | | | | proxysql |
+--------------+--------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+----------+
rows in set (0.00 sec)
可以看到S1 已经自动变到300的写分组,但是发现weight 没有变化,这样切换后新的master 承受的读请求比原来会大
进行读写的测试
admin@localhost:proxysql_admin.sock [(none)]>select * from stats_mysql_query_digest;
+-----------+------------+----------+--------------------+-----------------------------+------------+------------+------------+----------+----------+----------+
| hostgroup | schemaname | username | digest | digest_text | count_star | first_seen | last_seen | sum_time | min_time | max_time |
+-----------+------------+----------+--------------------+-----------------------------+------------+------------+------------+----------+----------+----------+
| | sbtest | sbuser | 0xE07EF74E5090A618 | show create table t1 | | | | | | |
| | sbtest | sbuser | 0x74A739578E179F19 | show processlist | | | | | | |
| | sbtest | sbuser | 0x99531AEFF718C501 | show tables | | | | | | |
| | sbtest | sbuser | 0x3DCE919B79C9576C | select * from t1 where id=? | | | | | | |
| | sbtest | sbuser | 0x3765930C7143F468 | select * from t1 | | | | | | |
| | sbtest | sbuser | 0x3C44D988579DAFFA | insert into t1 values(?) | | | | | | |
+-----------+------------+----------+--------------------+-----------------------------+------------+------------+------------+----------+----------+----------+
rows in set (0.00 sec)
可以看出读写分离正常。
下面进行master 意外宕机测试
Fri Sep :: - [info] Reading application default configuration from /etc/mha/app1.cnf..
Fri Sep :: - [info] Reading server configuration from /etc/mha/app1.cnf..
Fri Sep :: - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Fri Sep :: - [info] Reading application default configuration from /etc/mha/app1.cnf..
Fri Sep :: - [info] Reading server configuration from /etc/mha/app1.cnf..
orking directory. Check for details, and consider setting --workdir separately.
Fri Sep :: - [info] GTID failover mode =
Fri Sep :: - [info] Dead Servers:
Fri Sep :: - [info] Alive Servers:
Fri Sep :: - [info] MHA-M1(10.180.2.163:)
Fri Sep :: - [info] MHA-S1(10.180.2.164:)
Fri Sep :: - [info] MHA-S2(10.180.2.165:)
Fri Sep :: - [info] Alive Slaves:
Fri Sep :: - [info] MHA-M1(10.180.2.163:) Version=5.7.-log (oldest major version between slaves) log-bin:enabled
Fri Sep :: - [info] GTID ON
Fri Sep :: - [info] Replicating from MHA-S1(10.180.2.164:)
Fri Sep :: - [info] MHA-S2(10.180.2.165:) Version=5.7.-log (oldest major version between slaves) log-bin:enabled
Fri Sep :: - [info] GTID ON
Fri Sep :: - [info] Checking slave configurations..
Fri Sep :: - [info] Checking replication filtering settings..
Fri Sep :: - [info] binlog_do_db= , binlog_ignore_db=
Fri Sep :: - [info] Replication filtering check ok.
Fri Sep :: - [info] GTID (with auto-pos) is supported. Skipping all SSH and Node package checking.
Fri Sep :: - [info] Checking SSH publickey authentication settings on the current master..
Fri Sep :: - [info] HealthCheck: SSH to MHA-S1 is reachable.
Fri Sep :: - [info]
MHA-S1(10.180.2.164:) (current master)
+--MHA-M1(10.180.2.163:)
+--MHA-S2(10.180.2.165:) Fri Sep :: - [info] Checking master_ip_failover_script status:
Fri Sep :: - [info] /usr/local/bin/master_ip_failover --command=status --ssh_user=root --orig_master_host=MHA-S1 --orig_master_ip=10.180.2.164 --orig_master_port=
IN SCRIPT TEST====/sbin/ifconfig eth1: down==/sbin/ifconfig eth1: 10.180.2.168/=== Checking the Status of the script.. OK
Fri Sep :: - [info] OK.
Fri Sep :: - [warning] shutdown_script is not defined.
Fri Sep :: - [info] Set master ping interval seconds.
Fri Sep :: - [info] Set secondary check script: /usr/local/bin/masterha_secondary_check -s MHA-S1 -s MHA-S2
Fri Sep :: - [info] Starting ping health check on MHA-S1(10.180.2.164:)..
Fri Sep :: - [info] Ping(SELECT) succeeded, waiting until MySQL doesn't respond..
Fri Sep :: - [warning] Got error on MySQL select ping: (MySQL server has gone away)
Fri Sep :: - [info] Executing SSH check script: exit
Fri Sep :: - [info] HealthCheck: SSH to MHA-S1 is reachable.
Monitoring server MHA-S1 is reachable, Master is not reachable from MHA-S1. OK.
Monitoring server MHA-S2 is reachable, Master is not reachable from MHA-S2. OK.
Fri Sep :: - [info] Master is not reachable from all other monitoring servers. Failover should start.
Fri Sep :: - [warning] Got error on MySQL connect: (Lost connection to MySQL server at 'reading initial communication packet', system error: )
Fri Sep :: - [warning] Connection failed time(s)..
Fri Sep :: - [warning] Got error on MySQL connect: (Lost connection to MySQL server at 'reading initial communication packet', system error: )
Fri Sep :: - [warning] Connection failed time(s)..
Fri Sep :: - [warning] Got error on MySQL connect: (Lost connection to MySQL server at 'reading initial communication packet', system error: )
Fri Sep :: - [warning] Connection failed time(s)..
Fri Sep :: - [warning] Master is not reachable from health checker!
Fri Sep :: - [warning] Master MHA-S1(10.180.2.164:) is not reachable!
Fri Sep :: - [warning] SSH is reachable.
Fri Sep :: - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Fri Sep :: - [info] Reading application default configuration from /etc/mha/app1.cnf..
Fri Sep :: - [info] Reading server configuration from /etc/mha/app1.cnf..
Fri Sep :: - [info] GTID failover mode =
Fri Sep :: - [info] Dead Servers:
Fri Sep :: - [info] MHA-S1(10.180.2.164:)
Fri Sep :: - [info] Alive Servers:
Fri Sep :: - [info] MHA-M1(10.180.2.163:)
Fri Sep :: - [info] MHA-S2(10.180.2.165:)
Fri Sep :: - [info] Alive Slaves:
Fri Sep :: - [info] MHA-M1(10.180.2.163:) Version=5.7.-log (oldest major version between slaves) log-bin:enabled
Fri Sep :: - [info] GTID ON
Fri Sep :: - [info] Replicating from MHA-S1(10.180.2.164:)
Fri Sep :: - [info] MHA-S2(10.180.2.165:) Version=5.7.-log (oldest major version between slaves) log-bin:enabled
Fri Sep :: - [info] GTID ON
Fri Sep :: - [info] Replicating from MHA-S1(10.180.2.164:)
Fri Sep :: - [info] Checking slave configurations..
Fri Sep :: - [info] Checking replication filtering settings..
Fri Sep :: - [info] Replication filtering check ok.
Fri Sep :: - [info] Master is down!
Fri Sep :: - [info] Terminating monitoring script.
Fri Sep :: - [info] Got exit code (Master dead).
Fri Sep :: - [info]
Fri Sep :: - [info] * Phase : Configuration Check Phase..
Fri Sep :: - [info]
Fri Sep :: - [info] GTID failover mode =
Fri Sep :: - [info] Dead Servers:
Fri Sep :: - [info] MHA-S1(10.180.2.164:)
Fri Sep :: - [info] Checking master reachability via MySQL(double check)...
Fri Sep :: - [info] ok.
Fri Sep :: - [info] Alive Servers:
Fri Sep :: - [info] MHA-M1(10.180.2.163:)
Fri Sep :: - [info] MHA-S2(10.180.2.165:)
Fri Sep :: - [info] Alive Slaves:
Fri Sep :: - [info] MHA-M1(10.180.2.163:) Version=5.7.-log (oldest major version between slaves) log-bin:enabled
Fri Sep :: - [info] GTID ON
Fri Sep :: - [info] Replicating from MHA-S1(10.180.2.164:)
Fri Sep :: - [info] MHA-S2(10.180.2.165:) Version=5.7.-log (oldest major version between slaves) log-bin:enabled
Fri Sep :: - [info] GTID ON
Fri Sep :: - [info] Replicating from MHA-S1(10.180.2.164:)
Fri Sep :: - [info] Starting GTID based failover.
Fri Sep :: - [info]
Fri Sep :: - [info] ** Phase : Configuration Check Phase completed.
Fri Sep :: - [info]
Fri Sep :: - [info] * Phase : Dead Master Shutdown Phase..
Fri Sep :: - [info]
Fri Sep :: - [info] Forcing shutdown so that applications never connect to the current master..
Fri Sep :: - [info] Executing master IP deactivation script: IN SCRIPT TEST====/sbin/ifconfig eth1: down==/sbin/ifconfig eth1: 10.180.2.168/=== Disabling the VIP on old master: MHA-S1
Fri Sep :: - [info] done.
Fri Sep :: - [warning] shutdown_script is not set. Skipping explicit shutting down of the dead master.
Fri Sep :: - [info] * Phase : Dead Master Shutdown Phase completed.
Fri Sep :: - [info]
Fri Sep :: - [info] * Phase : Master Recovery Phase..
Fri Sep :: - [info]
Fri Sep :: - [info] * Phase 3.1: Getting Latest Slaves Phase..
Fri Sep :: - [info]
Fri Sep :: - [info] The latest binary log file/position on all slaves is -binlog.:
Fri Sep :: - [info] Retrieved Gtid Set: 1c2dc99f-7b57-11e7-a280-005056b665cb:
Fri Sep :: - [info] Latest slaves (Slaves that received relay log files to the latest):
Fri Sep :: - [info] MHA-M1(10.180.2.163:) Version=5.7.-log (oldest major version between slaves) log-bin:enabled
Fri Sep :: - [info] GTID ON
Fri Sep :: - [info] Replicating from MHA-S1(10.180.2.164:)
Fri Sep :: - [info] MHA-S2(10.180.2.165:) Version=5.7.-log (oldest major version between slaves) log-bin:enabled
Fri Sep :: - [info] GTID ON
Fri Sep :: - [info] Replicating from MHA-S1(10.180.2.164:)
Fri Sep :: - [info] The oldest binary log file/position on all slaves is -binlog.:
Fri Sep :: - [info] Retrieved Gtid Set: 1c2dc99f-7b57-11e7-a280-005056b665cb:
Fri Sep :: - [info] Oldest slaves:
Fri Sep :: - [info] MHA-M1(10.180.2.163:) Version=5.7.-log (oldest major version between slaves) log-bin:enabled
Fri Sep :: - [info] GTID ON
Fri Sep :: - [info] GTID ON
Fri Sep :: - [info] Replicating from MHA-S1(10.180.2.164:)
Fri Sep :: - [info]
Fri Sep :: - [info] Searching new master from slaves..
Fri Sep :: - [info] Candidate masters from the configuration file:
Fri Sep :: - [info] Non-candidate masters:
Fri Sep :: - [info] New master is MHA-M1(10.180.2.163:)
Fri Sep :: - [info] Starting master failover..
Fri Sep :: - [info]
From:
MHA-S1(10.180.2.164:) (current master)
+--MHA-M1(10.180.2.163:)
+--MHA-S2(10.180.2.165:) To:
MHA-M1(10.180.2.163:) (new master)
+--MHA-S2(10.180.2.165:)
Fri Sep :: - [info]
Fri Sep :: - [info] * Phase 3.3: New Master Recovery Phase..
Fri Sep :: - [info]
Fri Sep :: - [info] Waiting all logs to be applied..
Fri Sep :: - [info] -binlog.:
Fri Sep :: - [info] Master Recovery succeeded. File:Pos:Exec_Gtid_Set: -binlog., , 1c2dc99f-7b57-11e7-a280-005056b665cb:-,
a5757eae--11e7-82c7-005056b662d3:-
Fri Sep :: - [info] Executing master IP activate script:
Unknown option: new_master_user
Unknown option: new_master_password IN SCRIPT TEST====/sbin/ifconfig eth1: down==/sbin/ifconfig eth1: 10.180.2.168/=== Enabling the VIP - 10.180.2.168/ on the new master - MHA-M1
Fri Sep :: - [info] OK.
Fri Sep :: - [info] Setting read_only= on MHA-M1(10.180.2.163:)..
Fri Sep :: - [info] ok.
Fri Sep :: - [info] ** Finished master recovery successfully.
Fri Sep :: - [info] * Phase : Master Recovery Phase completed.
Fri Sep :: - [info]
Fri Sep :: - [info] * Phase : Slaves Recovery Phase..
Fri Sep :: - [info]
Fri Sep :: - [info]
Fri Sep :: - [info] * Phase 4.1: Starting Slaves in parallel..
Fri Sep :: - [info]
Fri Sep :: - [info]
Fri Sep :: - [info] Log messages from MHA-S2 ...
Fri Sep :: - [info]
Fri Sep :: - [info] Resetting slave MHA-S2(10.180.2.165:) and starting replication from the new master MHA-M1(10.180.2.163:)..
Fri Sep :: - [info] Executed CHANGE MASTER.
Fri Sep :: - [info] Slave started.
Fri Sep :: - [info] gtid_wait(1c2dc99f-7b57-11e7-a280-005056b665cb:-,
a5757eae--11e7-82c7-005056b662d3:-) completed on MHA-S2(10.180.2.165:). Executed events.
Fri Sep :: - [info] End of log messages from MHA-S2.
Fri Sep :: - [info] -- Slave on host MHA-S2(10.180.2.165:) started.
Fri Sep :: - [info] All new slave servers recovered successfully.
Fri Sep :: - [info]
Fri Sep :: - [info] * Phase : New master cleanup phase..
Fri Sep :: - [info]
Fri Sep :: - [info] Resetting slave info on the new master..
Fri Sep :: - [info] MHA-M1: Resetting slave info succeeded.
Fri Sep :: - [info] Master failover to MHA-M1(10.180.2.163:) completed successfully.
Fri Sep :: - [info] Deleted server2 entry from /etc/mha/app1.cnf .
Fri Sep :: - [info] ----- Failover Report ----- app1: MySQL Master failover MHA-S1(10.180.2.164:) to MHA-M1(10.180.2.163:) succeeded Master MHA-S1(10.180.2.164:) is down! Check MHA Manager logs at MHA-S2:/var/log/masterha/app1/manager.log for details. Started automated(non-interactive) failover.
Invalidated master IP address on MHA-S1(10.180.2.164:)
Selected MHA-M1(10.180.2.163:) as a new master.
MHA-M1(10.180.2.163:): OK: Applying all logs succeeded.
MHA-M1(10.180.2.163:): OK: Activated master IP address.
MHA-S2(10.180.2.165:): OK: Slave started, replicating from MHA-M1(10.180.2.163:)
MHA-M1(10.180.2.163:): Resetting slave info succeeded.
Master failover to MHA-M1(10.180.2.163:) completed successfully.
Fri Sep :: - [info] Sending mail..
可以看到宕机的master 被踢出ProxySQL,设置为SHUNNED
admin@localhost:proxysql_admin.sock [(none)]>select * from runtime_mysql_servers;
+--------------+--------------+------+---------+--------+-------------+-----------------+---------------------+---------+----------------+----------+
| hostgroup_id | hostname | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+--------------+------+---------+--------+-------------+-----------------+---------------------+---------+----------------+----------+
| | 10.180.2.165 | | ONLINE | | | | | | | proxysql |
| | 10.180.2.163 | | ONLINE | | | | | | | proxysql |
| | 10.180.2.164 | | SHUNNED | | | | | | | proxysql |
| | 10.180.2.163 | | ONLINE | | | | | | | proxysql |
+--------------+--------------+------+---------+--------+-------------+-----------------+---------------------+---------+----------------+----------+
rows in set (0.00 sec)
读写测试依然是正常的
admin@localhost:proxysql_admin.sock [(none)]>select * from stats_mysql_query_digest;
+-----------+------------+----------+--------------------+-----------------------------+------------+------------+------------+----------+----------+----------+
| hostgroup | schemaname | username | digest | digest_text | count_star | first_seen | last_seen | sum_time | min_time | max_time |
+-----------+------------+----------+--------------------+-----------------------------+------------+------------+------------+----------+----------+----------+
| | sbtest | sbuser | 0x16D22096575C5170 | select count(?) from t1 | | | | | | |
| | sbtest | sbuser | 0x02033E45904D3DF0 | show databases | | | | | | |
| | sbtest | sbuser | 0xE07EF74E5090A618 | show create table t1 | | | | | | |
| | sbtest | sbuser | 0x74A739578E179F19 | show processlist | | | | | | |
| | sbtest | sbuser | 0x99531AEFF718C501 | show tables | | | | | | |
| | sbtest | sbuser | 0x3DCE919B79C9576C | select * from t1 where id=? | | | | | | |
| | sbtest | sbuser | 0x3765930C7143F468 | select * from t1 | | | | | | |
| | sbtest | sbuser | 0xC67C49B8E6DB18A6 | show tables. | | | | | | |
| | sbtest | sbuser | 0x3C44D988579DAFFA | insert into t1 values(?) | | | | | | |
+-----------+------------+----------+--------------------+-----------------------------+------------+------------+------------+----------+----------+----------+
rows in set (0.00 sec)
总结:
至此,ProxySQL的基本安装和简单配置实现读写分离已经分享完毕,想更深入了解的朋友可以详细参考一下博客:
http://www.cnblogs.com/zhoujinyi/p/6829983.html
http://www.cnblogs.com/zhoujinyi/p/6838685.html
http://seanlook.com/2017/04/10/mysql-proxysql-install-config/
ProxySQL 读写分离实践的更多相关文章
- MySQL ProxySQL读写分离实践
目的 在上一篇文章MySQL ProxySQL读写分离使用初探里初步介绍了ProxySQL的使用,本文继续介绍它的一些特点和DBProxy的性能差异.深入一些去了解ProxySQL,通过测试来说明Pr ...
- mysqlfailover高可用与proxysql读写分离配置
proxysql官方推荐两种高可用方案: 1.MHA+proxysql 2.mysqlrpladmin+proxysql MySQLfailover工具包含在mysqlrpladmin工具中,所以两者 ...
- MHA+ProxySQL 读写分离高可用
文档结构如下: 1.ProxySQL说明 ProxySQL是mysql的一款中间件的产品,是灵活的mysql代理层,可以实现读写分离,支持query路由器的功能,支持动态指定sql进行缓存,支持动态加 ...
- MySQL ProxySQL读写分离使用初探
目的 在美团点评DBProxy读写分离使用说明文章中已经说明了使用目的,本文介绍ProxySQL的使用方法以及和DBProxy的性能差异.具体的介绍可以看官网的相关说明,并且这个中间件也是percon ...
- Proxysql读写分离配置
ProxySQL是Percona主推的读写分离中间件,下载地址为: https://www.percona.com/downloads/proxysql/ 一.安装 1:下载 wget https:/ ...
- ProxySQL读写分离
我们首先看一下自己的环境: MHA已经搭建: master: slave: slave: MHA manager在172.16.16.34,配置文件如下: [root@localhost bin]# ...
- ProxySQL读写分离代理
实现ProxySQL反向代理Mysql读写分离 简介 ProxySQL相当于小型的数据库,在磁盘上有存放数据库的目录:ProxySQL用法和mysql相似 启动ProxySQL后会有两个监听端口: 6 ...
- MySQL主从复制与读写分离实践
MySQL主从复制(Master-Slave)与读写分离(MySQL-Proxy)实践 目录: 介绍 MySQL的安装与配置 MySQL主从复制 MySQL读写分离 编译安装lua 安装配置MySQ ...
- ProxySQL 读写分离方法
转载自:https://www.jianshu.com/p/597b840bf70c (使用正则表达式实现基本的读/写分离) 在这一部分,我将通过一个示例来演示如何通过正则表达式来实现读/写分离. 首 ...
随机推荐
- nuxt 的一些报错和插件推荐
1.项目有启动报这个错误的,是因为node版本太低了,nuxt要求node版本要达到8.0.0以上 . 2.插件推荐
- wipefs进程
wipefs进程是啥,占用了百分之90多的cpu wipefs进程是啥,占用了百分之90多的cpu,把这个进程干掉了,过了一天又自动启动了,很多朋友应该遇到过类似的问题. wipefs是linux自带 ...
- [poj3252]Round Numbers_数位dp
Round Numbers poj3252 题目大意:求一段区间内Round Numbers的个数. 注释:如果一个数的二进制表示中0的个数不少于1的个数,我们就说这个数是Round Number.给 ...
- Find The Multiply
Find The Multiply poj-1426 题目大意:给你一个正整数n,求任意一个正整数m,使得n|m且m在十进制下的每一位都是0或1. 注释:n<=200. 想法:看网上的题解全是b ...
- 关于Unity中如何代码动态修改天空盒
在Unity中动态修改天空盒有两种方法: 一.为每个Texture建立天空盒材质球,需要更换时直接将对应材质球作为天空盒,缺点是建立的材质球太多 private void ChangeSkybox(M ...
- vs连接Oracle 客户端库时引发 BadImageFormatException
报错:Oracle 客户端库时引发 BadImageFormatException如果在安装 32 位 Oracle 客户端组件的情况下以 64 位模式? 解决方案:http://www.cnblog ...
- bash下常用快捷键
Ctrl-A 相当于HOME键,用于将光标定位到本行最前面Ctrl-E 相当于End键,即将光标移动到本行末尾Ctrl-B 相当于左箭头键,用于将光标向左移动一格Ctrl-F 相当于右箭头键,用于将光 ...
- nodejs安装和卸载
Ubuntu 上安装 Node.js 方式一:直接安装 一.安装 1.$ sudo apt-get install nodejs2.$ sudo apt-get install npm二.升级 ...
- alpha冲刺总结随笔
前言:前面乱乱糟糟整了一路,到最后终于可以稳定下来了.安安心心做个总结,然后把之后要做的事情都理清楚好了. 新学长似乎是个正经[并不]大腿. 看起来也不用都是一个人或者跟陈华学长两个人对半开了[突然摸 ...
- 【iOS】swift init构造器
这几天在使用 Swift 重写原来的一个运动社交应用 SportJoin. 为什么要重写呢? 首先因为实在找不到设计师给我作图; 其次, 我也闲不下来, 想找一些项目做, 所以只好将原来的代码重写了. ...