1 MySQL 5.7.27 MGR 多主环境

基础信息如下:
centos 6.5/vbox
实例名 A B C
IP 10.15.7.29 10.15.7.28 10.15.7.27
实例端口号 3306 3306 3306
Server-ID 2019080601 2019080602 2019080603
通讯端口号 33061 33061 33061
MySQL Versoin 5.7.27 5.7.27 5.7.27
MGR参数配置方式 修改配置文件 修改配置文件 修改配置文件  

简单命令

cat >> /etc/profile << EOF
export HISTTIMEFORMAT='%F %T '
EOF
cat >> /etc/hosts << EOF
10.15.7.29 mgr1
10.15.7.28 mgr2
10.15.7.27 mgr3
EOF
sed -i 's/=enforcing/=disabled/' /etc/selinux/config
cat >> /etc/security/limits.conf << EOF
mysql soft nproc 65535
mysql hard nproc 65535
mysql soft nofile 65535
mysql hard nofile 65535
EOF
sed -i '$a vm.swappiness = 0' /etc/sysctl.conf
echo deadline > /sys/block/sda/queue/scheduler
sysctl -p
groupadd mysql
useradd -g mysql mysql
passwd mysql
cat >> /home/mysql/.bash_profile << EOF
export LANG=en_US.UTF-8
export PATH=/usr/local/mysql/bin:$PATH
export MYSQL_PS1="(\u@\h:\p) [\d]> "
EOF
source /home/mysql/.bash_profile
cat >> /etc/sysconfig/clock << EOF
ZONE=Asia/Shanghai
UTC=false
ARC=false
EOF
rm /etc/localtime
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
date
ntpdate 182.92.12.11
service iptables stop
chkconfig iptables off vim /boot/grub/grub.conf
numa=off cat >> /etc/resolv.conf << EOF
nameserver 114.114.114.114
EOF
yum -y install lrzsz ls -l /dev/cdrom |grep cdrom
mount /dev/cdrom /mnt/
yum install -y gcc gcc-* make cmake gcc-c++ libaio libaio-devel bison autoconf automake ncurses-devel libmcrypt* libtool-ltdl-devel*
yum install -y openssl openssl-devel ncurses ncurses-devel mkdir -p /data/mysqldata/{3306/{data,tmp,binlog,slave,log/iblog},backup,scripts}
tar -zxvf mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
cd /usr/local/
ln -s mysql-5.7.27-linux-glibc2.12-x86_64 mysql
chown -R mysql:mysql /usr/local/mysql/
chown -R mysql:mysql /data/mysqldata/ [root@mgr1 local]# shutdown -h now
[root@mgr1 ~]# cat /etc/redhat-release
CentOS release 6.5 (Final)

拷贝到另外2台虚拟机,并进行相应的修改
vim /etc/hosts
vim /etc/udev/rules.d/70-persistent-net.rules
用eth1的mac地址分别替换eth0,并删除1
start_udev
vim /etc/sysconfig/network-scripts/ifcfg-eth0
修改HWADDR=下面的mac的eth0和uuid
nmcli con list
nmcli dev list
init 6

mysqld --defaults-file=/data/mysqldata/3306/my.cnf --datadir=/data/mysqldata/3306/data --basedir=/usr/local/mysql --user=mysql --initialize
cat /data/mysqldata/3306/log/mysql-error.log |grep "root@localhost"|awk -F " " '{print $11}'
mkdir -p /data/mysqldata/loadfile
/usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysqldata/3306/my.cnf &
/usr/local/mysql/bin/mysql -uroot -p'/UrpTQb(1;Qc' -S /data/mysqldata/3306/mysql.sock

--单主模式

A实例
ALTER USER 'root'@'localhost' IDENTIFIED BY '***';
flush privileges;
grant ALL PRIVILEGES ON *.* TO 'system'@'%' IDENTIFIED BY "***";
grant ALL PRIVILEGES ON *.* TO 'system'@'127.0.0.1' IDENTIFIED BY "****";
create user repl@'%' identified by '***';
grant replication slave on *.* to repl@'%';
flush privileges;
(root@localhost:mysql.sock) [(none)]> show global variables like '%gtid%';
+----------------------------------+------------------------------------------+
| Variable_name | Value |
+----------------------------------+------------------------------------------+
| binlog_gtid_simple_recovery | ON |
| enforce_gtid_consistency | ON |
| gtid_executed | b773de0e-b7d1-11e9-94a2-0800271e631f:1-7 |
| gtid_executed_compression_period | 1000 |
| gtid_mode | ON |
| gtid_owned | |
| gtid_purged | |
| session_track_gtids | OFF |
+----------------------------------+------------------------------------------+
(root@localhost:mysql.sock) [(none)]> reset master;
(root@localhost:mysql.sock) [(none)]> install PLUGIN group_replication SONAME 'group_replication.so';
(root@localhost:mysql.sock) [(none)]> show global variables like 'group%';
(root@localhost:mysql.sock) [(none)]> show plugins; (root@localhost:mysql.sock) [(none)]> set global group_replication_bootstrap_group=ON;
Query OK, 0 rows affected (0.00 sec)
(root@localhost:mysql.sock) [(none)]> change master to master_user='repl',master_password='***' for channel 'group_replication_recovery';
Query OK, 0 rows affected, 2 warnings (0.05 sec)
(root@localhost:mysql.sock) [(none)]> start group_replication;
Query OK, 0 rows affected (2.26 sec)
(root@localhost:mysql.sock) [(none)]> set global group_replication_bootstrap_group=OFF;
Query OK, 0 rows affected (0.00 sec)
(root@localhost:mysql.sock) [(none)]> select * from performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | b773de0e-b7d1-11e9-94a2-0800271e631f | mgr1 | 3306 | ONLINE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
[mysql@mgr1 data]$ ll /data/mysqldata/3306/slave/
total 20
-rw-r----- 1 mysql mysql 138 Aug 6 06:42 mysqld-relay-bin-group_replication_applier.index
-rw-r----- 1 mysql mysql 70 Aug 6 06:42 mysqld-relay-bin-group_replication_recovery.index
-rw-r----- 1 mysql mysql 218 Aug 6 06:42 relaylog-group_replication_applier.000001
-rw-r----- 1 mysql mysql 558 Aug 6 06:42 relaylog-group_replication_applier.000002
-rw-r----- 1 mysql mysql 150 Aug 6 06:42 relaylog-group_replication_recovery.000001
A实例
[mysql@mgr1 3306]$ mysql -usystem -p -h127.0.0.1 -P3306
create database mgr;
use mgr
create table tb1(id int primary key auto_increment not null,name varchar(100));
insert into tb1(name) select @@server_id;
insert into tb1(name) select @@server_id;
insert into tb1(name) select @@server_id;
insert into tb1(name) select @@server_id;
insert into tb1(name) select @@server_id;
(system@127.0.0.1:3306) [mgr]> select * from tb1;
+----+------------+
| id | name |
+----+------------+
| 4 | 2019080601 |
| 11 | 2019080601 |
| 18 | 2019080601 |
| 25 | 2019080601 |
| 32 | 2019080601 |
+----+------------+
5 rows in set (0.00 sec)
(system@127.0.0.1:3306) [mgr]> show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000001 | 2059 | | | aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:1-8 |
+------------------+----------+--------------+------------------+------------------------------------------+

对实例B 进行操作

创建用户
reset master;
show global variables like 'group%';
install PLUGIN group_replication SONAME 'group_replication.so';
change master to master_user='repl',master_password='***' for channel 'group_replication_recovery';
(root@localhost:mysql.sock) [(none)]> start group_replication;
Query OK, 0 rows affected (5.92 sec) 2019-08-06T06:51:20.431756+08:00 3 [Warning] Plugin group_replication reported: '[GCS] Automatically adding IPv4 localhost address to the whitelist. It is mandatory that it is added.'
2019-08-06T06:51:25.483441+08:00 29 [Warning] Storing MySQL user name or password information in the master info repository is not secure and is therefore not recommended. Please consider using the USER and PASSWORD connection options for START SLAVE; see the 'START SLAVE Syntax' in the MySQL Manual for more information. mysql -uroot -p -S /data/mysqldata/3306/mysql.sock
/usr/local/mysql/bin/mysqladmin -uroot -p -S /data/mysqldata/3306/mysql.sock shutdown
/usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysqldata/3306/my.cnf &
(system@127.0.0.1:3306) [mgr]> select * from performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | b773de0e-b7d1-11e9-94a2-0800271e631f | mgr1 | 3306 | ONLINE |
| group_replication_applier | c23fd354-b7d1-11e9-94ce-080027c2b864 | mgr2 | 3306 | ONLINE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+

加入第三个节点c

创建用户
reset master;
show global variables like 'group%';
install PLUGIN group_replication SONAME 'group_replication.so';
change master to master_user='repl',master_password='***' for channel 'group_replication_recovery';
(root@localhost:mysql.sock) [(none)]> start group_replication;
Query OK, 0 rows affected (3.52 sec)
(system@127.0.0.1:3306) [mgr]> select * from performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | b773de0e-b7d1-11e9-94a2-0800271e631f | mgr1 | 3306 | ONLINE |
| group_replication_applier | c23fd354-b7d1-11e9-94ce-080027c2b864 | mgr2 | 3306 | ONLINE |
| group_replication_applier | c52d7921-b7d1-11e9-9438-0800278aac9d | mgr3 | 3306 | ONLINE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
c节点上查询
(root@localhost:mysql.sock) [mgr]> select *from tb1;
+----+------------+
| id | name |
+----+------------+
| 7 | 2018080801 |
| 14 | 2018080801 |
| 21 | 2018080801 |
| 28 | 2018080801 |
| 35 | 2018080801 |
+----+------------+
(root@localhost:mysql.sock) [mgr]> select count(*) from tb2;
+----------+
| count(*) |
+----------+
| 1 |
+----------+

A实例上进行查询

(system@127.0.0.1:3306) [mgr]> SELECT * FROM mysql.slave_relay_log_info;
+-----------------+-----------------------------------------------------------------------+---------------+-----------------+----------------+-----------+-------------------+----+----------------------------+
| Number_of_lines | Relay_log_name | Relay_log_pos | Master_log_name | Master_log_pos | Sql_delay | Number_of_workers | Id | Channel_name |
+-----------------+-----------------------------------------------------------------------+---------------+-----------------+----------------+-----------+-------------------+----+----------------------------+
| 7 | /data/mysqldata/3306/slave/relaylog-group_replication_applier.000002 | 1480 | | 65 | 0 | 16 | 1 | group_replication_applier |
| 7 | /data/mysqldata/3306/slave/relaylog-group_replication_recovery.000001 | 4 | | 0 | 0 | 0 | 1 | group_replication_recovery |
+-----------------+-----------------------------------------------------------------------+---------------+-----------------+----------------+-----------+-------------------+----+----------------------------+
(system@127.0.0.1:3306) [mgr]> show global variables like 'group_replication_single_primary_mode';
+---------------------------------------+-------+
| Variable_name | Value |
+---------------------------------------+-------+
| group_replication_single_primary_mode | ON |
+---------------------------------------+-------+

单主模式下,对实例C进行插入测试

(root@localhost:mysql.sock) [mgr]> insert into tb1(name) select @@server_id;
ERROR 1290 (HY000): The MySQL server is running with the --super-read-only option so it cannot execute this statement

切换为多主,跟上一篇是一样的,先关闭mgr,然后在关闭参数group_replication_single_primary_mode,再重启加入mgr

2 ProxySQL

官方下载
https://www.percona.com/downloads/proxysql/

[root@mysqmgr1 opt]# rpm -ivh proxysql-1.4.14-1.1.el7.x86_64.rpm
warning: proxysql-1.4.14-1.1.el7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 8507efa5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:proxysql-1.4.14-1.1.el7 ################################# [100%]
ProxySQL默认配置文件为/etc/proxysql.cnf,只在第一次启动的时候有用,后续的所有配置都是通过对SQLite数据库的操作,并且不会更新到proxysql中,而是存储在/var/lib/proxysql/proxysql.db中
[root@mgr1 opt]# proxysql --version ##版本查询
ProxySQL version 1.4.14-percona-1.1, codename Truls
[root@mgr1 opt]# rpm -ql proxysql ##查看具体安装内容
/etc/init.d/proxysql ##启动脚本
/etc/logrotate.d/proxysql-logrotate
/etc/proxysql-admin.cnf ##配置文件
/etc/proxysql.cnf ##默认配置文件
/usr/bin/proxysql ##执行文件
/usr/bin/proxysql-admin
/usr/bin/proxysql-status
/usr/bin/proxysql_galera_checker ##ProxySQL调度程序检查pxc_maint_mode参数状态,持续检测各个节点的状态
/usr/bin/proxysql_node_monitor
/usr/share/doc/proxysql-1.4.14
/usr/share/doc/proxysql-1.4.14/LICENSE
/var/lib/proxysql
/var/run/proxysql
/var/lib/proxysql/proxysql.db ##SQLite数据文件
/var/lib/proxysql/proxysql.log ##日志文件
备注:如果存在proxysql.db数据文件,则ProxySQL启动过程中将不会读取proxysql.cnf配置文件的内容来初始化ProxySQL

启动

[root@mgr1 opt]# /etc/init.d/proxysql start
Starting ProxySQL: 2019-08-06 09:43:31 [INFO] Using config file /etc/proxysql.cnf
DONE!
[root@mgr1 opt]# netstat -tunlp|grep proxysql
tcp 0 0 0.0.0.0:6032 0.0.0.0:* LISTEN 11904/proxysql
tcp 0 0 0.0.0.0:6033 0.0.0.0:* LISTEN 11904/proxysql
[root@mgr1 opt]# ss -tunlp|grep proxysql
tcp LISTEN 0 128 *:6032 *:* users:(("proxysql",11904,20))
tcp LISTEN 0 128 *:6033 *:* users:(("proxysql",11904,19))
启动了6032和6033两个端口,默认管理端口是6032,客户端服务端口是6033,默认的用户名密码都是 admin,通过mysql的客户端可以登录
[mysql@mgr1 ~]$ mysql -uadmin -padmin -h127.0.0.1 -P6032
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
(admin@127.0.0.1:6032) [(none)]> show databases;
+-----+---------------+-------------------------------------+
| seq | name | file |
+-----+---------------+-------------------------------------+
| 0 | main | |
| 2 | disk | /var/lib/proxysql/proxysql.db |
| 3 | stats | |
| 4 | monitor | |
| 5 | stats_history | /var/lib/proxysql/proxysql_stats.db |
+-----+---------------+-------------------------------------+

使用默认的admin用户(密码也为admin)登录ProxySQL管理界面
[mysql@mgr1 ~]$ mysql -uadmin -padmin -h127.0.0.1 -P6032

main:内存配置数据库,即MEMORY,表里存放后端db实例、用户验证、路由规则等信息。
mysql_servers—后端可以连接MySQL服务器的列表。
mysql_users—配置后端数据库的账号和监控的账号。
mysql_query_rules—指定Query路由到后端不同服务器的规则列表。
注:表名以runtime_开头的表示ProxySQL当前运行的配置内容,不能通过DML语句修改。
只能修改对应的不以 runtime开头的表,然后“LOAD”使其生效,“SAVE”使其存到硬盘以供下次重启加载。
disk库—持久化磁盘的配置。
stats库—统计信息的汇总。
monitor库—一些监控的收集信息,包括数据库的健康状态等

配置 ProxySQL 监控
创建ProxySQL监控账号和对外用户访问ProxySQL

(root@localhost:mysql.sock) [test]> create user 'monitor'@'%' identified by 'monitor';
(root@localhost:mysql.sock) [test]> grant all privileges on *.* to 'monitor'@'%' with grant option;
(root@localhost:mysql.sock) [test]> create user 'proxysql'@'%' identified by 'proxysql';
(root@localhost:mysql.sock) [test]> grant all privileges on *.* to 'proxysql'@'%' with grant option;
(root@localhost:mysql.sock) [test]> flush privileges;

ProxySQL的多层配置系统
ProxySQL 有一套很完整的配置系统,方便 DBA 对线上的操作。整套配置系统分为三层,最顶层为 RUNTIME,中间层为 MEMORY 和最底层,也就是持久层的 DISK和 CONFIG FILE。

+-------------------------+
| RUNTIME |
+-------------------------+
/|\ |
| |
[1] | [2] |
| \|/
+-------------------------+
| MEMORY |
+-------------------------+ _
/|\ | |\
| | \
[3] | [4] | \ [5]
| \|/ \
+-------------------------+ +-------------------------+
| DISK | | CONFIG FILE |
+-------------------------+ +-------------------------+
(admin@127.0.0.1:6032) [main]> insert into mysql_servers(hostgroup_id,hostname,port) values (1,'10.15.7.29',3306);
(admin@127.0.0.1:6032) [main]> insert into mysql_servers(hostgroup_id,hostname,port) values (1,'10.15.7.28',3306);
(admin@127.0.0.1:6032) [main]> insert into mysql_servers(hostgroup_id,hostname,port) values (1,'10.15.7.27',3306);
(admin@127.0.0.1:6032) [main]> load mysql servers to runtime;
(admin@127.0.0.1:6032) [main]> save mysql servers to disk;

登录 ProxySQL 之后,凡是进行任何操作,都需要运行 load to runtime,从memory 加载到 runtime。然后再执行 save to disk 持久化到磁盘。

(admin@127.0.0.1:6032) [main]> select * from mysql_servers;
+--------------+------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| hostgroup_id | hostname | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 1 | 10.15.7.29 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
| 1 | 10.15.7.28 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
| 1 | 10.15.7.27 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
+--------------+------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
3 rows in set (0.02 sec)

为 ProxySQL 配置监控账号

(admin@127.0.0.1:6032) [main]> set mysql-monitor_username='monitor';
(admin@127.0.0.1:6032) [main]> set mysql-monitor_password='monitor';
(admin@127.0.0.1:6032) [main]> load mysql variables to runtime;
(admin@127.0.0.1:6032) [main]> save mysql variables to disk;
(admin@127.0.0.1:6032) [main]> select * from global_variables where variable_name in ('mysql-monitor_username','mysql-monitor_password');
+------------------------+----------------+
| variable_name | variable_value |
+------------------------+----------------+
| mysql-monitor_password | monitor |
| mysql-monitor_username | monitor |
+------------------------+----------------+
2 rows in set (0.03 sec)
(admin@127.0.0.1:6032) [main]> select * from monitor.mysql_server_connect_log order by time_start_us desc limit 6;
+------------+------+------------------+-------------------------+---------------+
| hostname | port | time_start_us | connect_success_time_us | connect_error |
+------------+------+------------------+-------------------------+---------------+
| 10.15.7.28 | 3306 | 1565112205391734 | 160613 | NULL |
| 10.15.7.27 | 3306 | 1565112204934198 | 14703 | NULL |
| 10.15.7.29 | 3306 | 1565112204459940 | 271504 | NULL |
| 10.15.7.28 | 3306 | 1565112154722343 | 163164 | NULL |
| 10.15.7.27 | 3306 | 1565112154063754 | 13523 | NULL |
| 10.15.7.29 | 3306 | 1565112153385738 | 190996 | NULL |
+------------+------+------------------+-------------------------+---------------+
6 rows in set (0.08 sec)
select * from monitor.mysql_server_ping_log order by time_start_us desc limit 6;

配置 ProxySQL 主从分组信息
这里会用到一张表 mysql_replication_hostgroups:
里面的 writer_hostgroup 是写入组的编号,reader_hostgroup 是读取组的编号。实验中使用 1作为写入组,2作为读取组编号

(admin@127.0.0.1:6032) [main]> insert into mysql_replication_hostgroups values (1,2,'proxy');
(admin@127.0.0.1:6032) [main]> load mysql servers to runtime;
(admin@127.0.0.1:6032) [main]> save mysql servers to disk;
(admin@127.0.0.1:6032) [main]> select * from mysql_replication_hostgroups;
+------------------+------------------+---------+
| writer_hostgroup | reader_hostgroup | comment |
+------------------+------------------+---------+
| 1 | 2 | proxy |
+------------------+------------------+---------+

---以前版本的表有check_type字段:可取值为:'read_only'、'innodb_read_only'、'super_read_only',默认值为'read_only'
ProxySQL会根据 server 的 read_only 的取值将服务器进行分组。read_only=0的server,master被分到编号为1的写组,read_only=1 的server,slave则被分到编号为 2的读组。

(admin@127.0.0.1:6032) [main]> select hostgroup_id,hostname,port,status,weight,comment from mysql_servers;
+--------------+------------+------+--------+--------+---------+
| hostgroup_id | hostname | port | status | weight | comment |
+--------------+------------+------+--------+--------+---------+
| 1 | 10.15.7.27 | 3306 | ONLINE | 1 | |
| 1 | 10.15.7.29 | 3306 | ONLINE | 1 | |
| 1 | 10.15.7.28 | 3306 | ONLINE | 1 | |
| 2 | 10.15.7.29 | 3306 | ONLINE | 1 | |
| 2 | 10.15.7.28 | 3306 | ONLINE | 1 | |
| 2 | 10.15.7.27 | 3306 | ONLINE | 1 | |
+--------------+------------+------+--------+--------+---------+

配置对外访问账号,默认指定主库,并对该用户开启事务持久化保护。
注:mysql_users 表中的 transaction_persistent 字段默认为 0
建议在创建完用户之后设置为1,避免发生脏读、幻读等现象命令如下:

(admin@127.0.0.1:6032) [main]> insert into mysql_users(username,password,default_hostgroup) values ('proxysql','proxysql',1);
(admin@127.0.0.1:6032) [main]> update mysql_users set transaction_persistent=1 where username='proxysql';
(admin@127.0.0.1:6032) [main]> load mysql users to runtime;
(admin@127.0.0.1:6032) [main]> save mysql users to disk;

配置读写分离策略
配置读写分离策略需要使用 mysql_query_rules 表。表中的 match_pattern 字段就是代表设置的规则,
destination_hostgroup 字段代表默认指定的分组,apply 代表真正执行应用规则。
把所有以 select 开头的语句全部分配到编号为 2的读组中。select for update 会产生一个写锁,
对数据查询的实效性要求高,把它分配到编号为 1的写组中,其他所有操作都会默认路由到写组中。

(admin@127.0.0.1:6032) [main]> insert into mysql_query_rules(active,match_pattern,destination_hostgroup, apply) VALUES(1,'^SELECT.*FOR UPDATE$',1,1);
(admin@127.0.0.1:6032) [main]> insert into mysql_query_rules(active,match_pattern,destination_hostgroup, apply) VALUES(1,'^SELECT',2,1);
(admin@127.0.0.1:6032) [main]> load mysql query rules to runtime;
(admin@127.0.0.1:6032) [main]> save mysql query rules to disk;
(admin@127.0.0.1:6032) [main]> select rule_id,active,match_pattern,destination_hostgroup,apply from mysql_query_rules;
+---------+--------+----------------------+-----------------------+-------+
| rule_id | active | match_pattern | destination_hostgroup | apply |
+---------+--------+----------------------+-----------------------+-------+
| 1 | 1 | ^SELECT.*FOR UPDATE$ | 1 | 1 |
| 2 | 1 | ^SELECT | 2 | 1 |
+---------+--------+----------------------+-----------------------+-------+
select * from stats_mysql_query_digest_reset;
(admin@127.0.0.1:6032) [main]> select * from stats_mysql_query_digest;
+-----------+--------------------+----------+--------------------+-----------------------------------+------------+------------+------------+----------+----------+----------+
| hostgroup | schemaname | username | digest | digest_text | count_star | first_seen | last_seen | sum_time | min_time | max_time |
+-----------+--------------------+----------+--------------------+-----------------------------------+------------+------------+------------+----------+----------+----------+
| 2 | information_schema | proxysql | 0x82A12D4C4E7B0A28 | select @@hostname | 5 | 1565143760 | 1565143774 | 4095 | 505 | 1577 |
| 2 | information_schema | proxysql | 0x3A53616B087A2CBC | select * from test.proxysql_test1 | 1 | 1565143752 | 1565143752 | 828 | 828 | 828 |
| 2 | information_schema | proxysql | 0x630BEE5AF5572801 | select * from db.tb | 1 | 1565143742 | 1565143742 | 2157 | 2157 | 2157 |
| 1 | information_schema | proxysql | 0x594F2C744B698066 | select USER() | 1 | 1565143734 | 1565143734 | 0 | 0 | 0 |
| 1 | information_schema | proxysql | 0x226CD90D52A2BA0B | select @@version_comment limit ? | 1 | 1565143734 | 1565143734 | 0 | 0 | 0 |
+-----------+--------------------+----------+--------------------+-----------------------------------+------------+------------+------------+----------+----------+----------+
5 rows in set (0.00 sec)

表stats_mysql_query_digest:SQL的执行次数、时间消耗等

(admin@127.0.0.1:6032) [main]> select hostgroup,schemaname,username,digest_text from stats_mysql_query_digest;
+-----------+--------------------+----------+-----------------------------------+
| hostgroup | schemaname | username | digest_text |
+-----------+--------------------+----------+-----------------------------------+
| 2 | information_schema | proxysql | select @@hostname |
| 2 | information_schema | proxysql | select * from test.proxysql_test1 |
| 2 | information_schema | proxysql | select * from db.tb |
| 1 | information_schema | proxysql | select USER() |
| 1 | information_schema | proxysql | select @@version_comment limit ? |
+-----------+--------------------+----------+-----------------------------------+
5 rows in set (0.00 sec)
(admin@127.0.0.1:6032) [main]> select hostgroup,schemaname,username,digest_text from stats_mysql_query_digest;
+-----------+--------------------+----------+-----------------------------------------+
| hostgroup | schemaname | username | digest_text |
+-----------+--------------------+----------+-----------------------------------------+
| 1 | test | proxysql | commit |
| 1 | test | proxysql | select * from proxysql_test1 for update |
| 1 | test | proxysql | show databases |
| 1 | test | proxysql | show tables |
| 2 | information_schema | proxysql | SELECT DATABASE() |
| 2 | information_schema | proxysql | select @@hostname |
| 2 | information_schema | proxysql | select * from test.proxysql_test1 |
| 2 | information_schema | proxysql | select * from db.tb |
| 1 | information_schema | proxysql | select USER() |
| 1 | information_schema | proxysql | select @@version_comment limit ? |
+-----------+--------------------+----------+-----------------------------------------+
[mysql@mysqmgr1 ~]$ mysql -uproxysql -pproxysql -h127.0.0.1 -P6033 -e 'select @@hostname'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| mgr1 |
+------------+
[mysql@mysqmgr1 ~]$ mysql -uproxysql -pproxysql -h127.0.0.1 -P6033 -e 'select @@hostname'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| mgr2 |
+------------+
[mysql@mysqmgr1 ~]$ mysql -uproxysql -pproxysql -h127.0.0.1 -P6033 -e 'select @@hostname'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| mgr3 |
+------------+

测试读请求负载均衡:

[mysql@mysqmgr1 ~]$ for i in {1..12}; do mysql -uproxysql -pproxysql -h127.0.0.1  -P6033 -e 'select @@hostname' -s -N; done
mysql: [Warning] Using a password on the command line interface can be insecure.
mgr3
mysql: [Warning] Using a password on the command line interface can be insecure.
mgr3
mysql: [Warning] Using a password on the command line interface can be insecure.
mgr3
mysql: [Warning] Using a password on the command line interface can be insecure.
mgr3
mysql: [Warning] Using a password on the command line interface can be insecure.
mgr1
mysql: [Warning] Using a password on the command line interface can be insecure.
mgr3
mysql: [Warning] Using a password on the command line interface can be insecure.
mgr1
mysql: [Warning] Using a password on the command line interface can be insecure.
mgr3
mysql: [Warning] Using a password on the command line interface can be insecure.
mgr1
mysql: [Warning] Using a password on the command line interface can be insecure.
mgr3
mysql: [Warning] Using a password on the command line interface can be insecure.
mgr3
mysql: [Warning] Using a password on the command line interface can be insecure.
mgr2

--故障切换

(admin@127.0.0.1:6032) [main]> select hostgroup_id,hostname,port,status,weight,comment from mysql_servers;
+--------------+------------+------+--------+--------+---------+
| hostgroup_id | hostname | port | status | weight | comment |
+--------------+------------+------+--------+--------+---------+
| 1 | 10.15.7.27 | 3306 | ONLINE | 1 | |
| 1 | 10.15.7.29 | 3306 | ONLINE | 1 | |
| 1 | 10.15.7.28 | 3306 | ONLINE | 1 | |
| 2 | 10.15.7.29 | 3306 | ONLINE | 1 | |
| 2 | 10.15.7.28 | 3306 | ONLINE | 1 | |
| 2 | 10.15.7.27 | 3306 | ONLINE | 1 | |
+--------------+------------+------+--------+--------+---------+
6 rows in set (0.00 sec) 7.29 mgr1 设置为只读状态
(system@127.0.0.1:3306) [(none)]> set global read_only=1;
Query OK, 0 rows affected (0.00 sec)
(admin@127.0.0.1:6032) [main]> select hostgroup_id,hostname,port,status,weight,comment from mysql_servers;
+--------------+------------+------+--------+--------+---------+
| hostgroup_id | hostname | port | status | weight | comment |
+--------------+------------+------+--------+--------+---------+
| 1 | 10.15.7.27 | 3306 | ONLINE | 1 | |
| 2 | 10.15.7.27 | 3306 | ONLINE | 1 | |
| 2 | 10.15.7.28 | 3306 | ONLINE | 1 | |
| 2 | 10.15.7.29 | 3306 | ONLINE | 1 | |
| 1 | 10.15.7.28 | 3306 | ONLINE | 1 | |
+--------------+------------+------+--------+--------+---------+
5 rows in set (0.00 sec)
ProxySQL变化,自动把mgr1 7.29踢出了1group组(write)
(system@127.0.0.1:3306) [(none)]> set global read_only=0; ##取消只读
Query OK, 0 rows affected (0.00 sec)
(admin@127.0.0.1:6032) [main]> select hostgroup_id,hostname,port,status,weight,comment from mysql_servers;
+--------------+------------+------+--------+--------+---------+
| hostgroup_id | hostname | port | status | weight | comment |
+--------------+------------+------+--------+--------+---------+
| 1 | 10.15.7.27 | 3306 | ONLINE | 1 | |
| 2 | 10.15.7.27 | 3306 | ONLINE | 1 | |
| 2 | 10.15.7.28 | 3306 | ONLINE | 1 | |
| 1 | 10.15.7.29 | 3306 | ONLINE | 1 | |
| 1 | 10.15.7.28 | 3306 | ONLINE | 1 | |
| 2 | 10.15.7.29 | 3306 | ONLINE | 1 | |
+--------------+------------+------+--------+--------+---------+
6 rows in set (0.00 sec)
可以看到,mgr1 7.29 主机又加入到group 1中(write)

最新文章

  1. EF架构~为IEnumerable接口添加增删查等操作,原因是IEnumerable导航属性更放心
  2. 【原】搭建Samba的简要过程
  3. Rem实现自适应初体验
  4. 关于mac book 的启动选项
  5. HTTP 和 HTTPS 协议
  6. Excel通过宏创建百万数据
  7. 【Hibernate】HQL new map
  8. 小型Mp3播放器
  9. C# 语言规范_版本5.0 (第18章 不安全代码)
  10. Servlet中的过滤器Filter用法
  11. PHP静态化技术
  12. webcron
  13. cocos 碰撞系统
  14. Google浏览器解决编码乱码问题
  15. xilinx_all_version.lic
  16. jQ如何选中被选中的单选按钮的值
  17. MYCAT扩容
  18. Python源码文件中带有中文时,输出乱码
  19. spring cloud Feign 使用 @RequestLine 注解遇到的问题
  20. shell随笔

热门文章

  1. oracle的监听控制
  2. 找到并更改启动时间(timeout)
  3. 使用GDI+加载32位的位图或者PNG图片(具有透明通道)
  4. Hihocoder1046K个串(线段树)(待解决)
  5. 【模板】manachar
  6. windows脚本复制文件(将u盘文件复制到固定路径)
  7. oracle-linux7 镜像地址 secuCRT 注册-linux内核
  8. JavaScript 正则表达式——预定义类,边界,量词,贪婪模式,非贪婪模式,分组,前瞻
  9. Nowcoder 北师校赛 B 外挂使用拒绝 ( k次前缀和、矩阵快速幂打表找规律、组合数 )
  10. Nowcoder Two Graphs ( 图的同构 )