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)

MySQL 5.7.27 MGR 单主/多主+ ProxySQL的更多相关文章

  1. MySQL+MGR 单主模式和多主模式的集群环境 - 部署手册 (Centos7.5)

    MySQL Group Replication(简称MGR)是MySQL官方于2016年12月推出的一个全新的高可用与高扩展的解决方案.MGR是MySQL官方在5.7.17版本引进的一个数据库高可用与 ...

  2. MySQL MGR 5.7.22 on centos 6.3 单主/多主搭建测试

    搭建Mysql Group Replication本次搭建采用3个实例,三个服务器,同一个网段,MGR的参数配置在配置文件中添加.注意通讯端口号的配置,它用于组成员之间的通讯使用请确定当前MySQL版 ...

  3. MySQL MGR 单主模式下master角色切换规则

    MGR单主模式,master节点可读可写,其余节点都是只读.当配置MGR为单主模式,非master节点自动开启super_read_only 当可读可写的节点异常宕机,会进行怎样的切换?在选择新的可写 ...

  4. mysql 8.0.18 mgr 搭建及其切换

    mysql 8.0.18 mgr 搭建及其切换 一.系统安装包 yum -y install make gcc-c++ cmake bison-devel ncurses-devel readline ...

  5. 【DB宝45】MySQL高可用之MGR+Consul架构部署

    目录 一.MGR+Consul架构简介 二.搭建MGR 2.1.申请3台MGR机器 2.2.3台主机安装MySQL环境 2.3.修改MySQL参数 2.4.重启MySQL环境 2.5.安装MGR插件( ...

  6. MySQL双主(主主)架构方案

    在企业中,数据库高可用一直是企业的重中之重,中小企业很多都是使用mysql主从方案,一主多从,读写分离等,但是单主存在单点故障,从库切换成主库需要作改动.因此,如果是双主或者多主,就会增加mysql入 ...

  7. MYSQL的分区字段,必须包含在主键字段内

    MYSQL的分区字段,必须包含在主键字段内   MYSQL的分区字段,必须包含在主键字段内 在对表进行分区时,如果分区字段没有包含在主键字段内,如表A的主键为ID,分区字段为createtime ,按 ...

  8. Delphi XE3通过ADOConnection 连接 MySQL 5.5.27 数据库

    Delphi XE3通过ADOConnection 连接 MySQL 5.5.27 数据库 unit Unit1; interface uses Winapi.Windows, Winapi.Mess ...

  9. MySQL简单查询详解-单表查询

    MySQL简单查询详解-单表查询 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查询的执行路径 一条SQL查询语句的执行过程大致如下图所示: 1>.客户端和服务端通过my ...

随机推荐

  1. docker容器内安装 rz、sz

    操作系统:ubuntu rz.sz命令找不到: 执行命令:apt-get update && apt-get install lrzsz

  2. 我的前端组件 ---- 16:9固定宽高比例的div

    目标:遇到一个需求,让图片在页面中,不管宽度如何变化.宽高保持16:9的比例. 实现: 方法一:这也是比较经典的一个方法,利用padding-bottom来实现. <!DOCTYPE html& ...

  3. 【leetcode】1247. Minimum Swaps to Make Strings Equal

    题目如下: You are given two strings s1 and s2 of equal length consisting of letters "x" and &q ...

  4. vue的组件创建和使用

    首先说一下vue组件 什么是组件? 在我的理解,vue的所有页面内容都是组件. 什么是父子组件? 因为所有的页面内容都是组件,那么怎么区分父子组件呢?其实很简单,现在有一个页面,在js里面的 comp ...

  5. idea 导入(非maven)web项目并发布到tomcat服务器

    IDEA 2017.1版本 web项目导入并发布到Tomcat服务器 1.点击编辑项目结构 2.点击project 将项目编译输出目录改成{项目目录}/OUT,并设置项目环境,编译版本 3.点击mod ...

  6. maven项目创建2

    添加依赖索引 但是默认是没有索引的,要手动创建索引 依赖范围 debug  配置 运行常见问题 处理办法,JDK重新安装 网络添加依赖网站

  7. Hexo + github pages + 阿里云绑定域名搭建个人博客

    申请域名 万网购买的域名,地址:https://wanwang.aliyun.com/domain/com?spm=5176.8142029.388261.137.LoKzy7 控制台进行解析 控制台 ...

  8. 微信小程序_(视图)简单的swiper容器

    swiper容器效果 官方文档:传送门 swiper容器可实现简单的轮播图效果 结构程序 Page({ /** * 页面的初始数据 */ data: { }, /** * 生命周期函数--监听页面加载 ...

  9. Redis的一点笔记

    Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. Redis 优势: 性能极高 – Redis能读的速度是110000次/s,写的速度是81000次/s . 丰富 ...

  10. python学习之路(8)

    定义函数 在Python中,定义一个函数要使用def语句,依次写出函数名.括号.括号中的参数和冒号:,然后,在缩进块中编写函数体,函数的返回值用return语句返回. 我们以自定义一个求绝对值的my_ ...