mysql实现读写分离(proxy)与高可用(MGR)
---恢复内容开始---
实验目的是MySQL高可用且根据规则实现读写分离
首先准备三台MySQL服务器,版本是二进制的mysql-5.7.27
第一台:192.168.110.113
第二台:192.168.110.112
第三台:192.168.110.110
三台机器配置/etc/hosts,三台机器的目录都是/data,第一,二台机器设置为种子节点
如果三台机器是新装的数据库,不用先进行mysqldump更新至一致,如果有数据且不小的话建议先mysqldump更新至一致
在第一台上面,设置/etc/my.cnf
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# changes to the binary log between backups.
# log_bin
basedir = /usr/local/mysql
datadir = /data
# port = .....
# server_id = .....
# socket = .....
pid-file = /data/rabbitmq4.pid
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
socket=/tmp/mysql.sock
gtid_mode=on # 必须
enforce_gtid_consistency=on # 必须
log-bin=/data/master-bin # 必须
binlog_format=row # 必须
binlog_checksum=none # 必须
master_info_repository=TABLE # 必须
relay_log_info_repository=TABLE # 必须
relay_log=/data/relay-log # 必须,如果不给,将采用默认值
log_slave_updates=ON # 必须
sync-binlog=1 # 建议
log-error=/data/error.log
#pid-file=/data/mysqld.pid
loose-group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa" # 必须
loose-group_replication_start_on_boot=off # 建议设置为OFF
loose-group_replication_member_weigth = 40 # 非必需,mysql 5.7.20才开始支持该选项
loose-group_replication_local_address="192.168.110.113:20001" # 必须,下一行也必须
loose-group_replication_group_seeds="192.168.110.113:20001,192.168.110.112:20002"
master_user='repl',
master_password='P@ssword1!'
for channel 'group_replication_recovery';
mysql> start group_replication;
mysql> set @@global.group_replication_bootstrap_group=off;
datadir=/data
basedir=/usr/local/mysql
socket=/tmp/mysql.sock
gtid_mode=on # 必须
enforce_gtid_consistency=on # 必须
log-bin=/data/master-bin # 必须
binlog_format=row # 必须
binlog_checksum=none # 必须
master_info_repository=TABLE # 必须
relay_log_info_repository=TABLE # 必须
relay_log=/data/relay-log # 必须,如果不给,将采用默认值
log_slave_updates=ON # 必须
sync-binlog=1 # 建议
log-error=/data/error.log
pid-file=/data/mysqld.pid
loose-group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa" # 必须
loose-group_replication_start_on_boot=off # 建议设置为OFF
loose-group_replication_member_weigth = 20 # 非必需,mysql 5.7.20才开始支持该选项
loose-group_replication_local_address="192.168.110.112:20002" # 必须,下一行也必须
loose-group_replication_group_seeds="192.168.110.113:20001,192.168.110.112:20002"
master_user='repl',
master_password='P@ssword1!'
for channel 'group_replication_recovery';
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# changes to the binary log between backups.
#log_bin= mysql-bin
#server_id= 1
# These are commonly set, remove the # and set as required.
basedir = /usr/local/mysql
datadir = /data
#character_set_database = utf8
#character_set_server = utf8
# port = .....
# server_id = .....
socket = /tmp/mysql.sock
#pid = /data-mysql/docker.pid
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
server-id=120 # 必须
gtid_mode=on
enforce_gtid_consistency=on
log-bin=/data/master-bin
binlog_format=row
binlog_checksum=none
master_info_repository=TABLE
relay_log_info_repository=TABLE
relay_log=/data/relay-log
log_slave_updates=ON
sync-binlog=1
transaction_write_set_extraction=XXHASH64
loose-group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
loose-group_replication_start_on_boot=off
loose-group_replication_member_weigth = 30
loose-group_replication_local_address="192.168.110.110:20003"
loose-group_replication_group_seeds="192.168.110.113:20001,192.168.110.112:20002"
log-error=/data/error.log
pid-file=/data/docker.pid
master_user='repl',
master_password='P@ssword1!'
for channel 'group_replication_recovery';
mysql> start group_replication;
mysql> set @@global.group_replication_bootstrap_group=off;
[proxysql_repo]
name= ProxySQL
baseurl=http://repo.proxysql.com/ProxySQL/proxysql-1.4.x/centos/\$releasever
gpgcheck=1
gpgkey=http://repo.proxysql.com/ProxySQL/repo_pub_key
EOF
runtime_
表,修改后必须执行LOAD ... TO RUNTIME
才能加载到RUNTIME生效,执行save ... to disk
才能将配置持久化保存到磁盘insert into mysql_servers(hostgroup_id,hostname,port) values(10,'192.168.110.112',3306);
insert into mysql_servers(hostgroup_id,hostname,port) values(10,'192.168.110.110',3306);
save mysql servers to disk;
mysql> create user monitor@'192.168.110.%' identified by 'P@ssword1!';
mysql> grant replication client on *.* to monitor@'192.168.110.%';
set mysql-monitor_password='P@ssword1!';
save mysql variables to disk;
save mysql servers to disk;
在master节点上执行:(只需master执行即可,会复制给两个slave)
grant all on *.* to root@'192.168.110.%' identified by 'P@ssword1!';
grant all on *.* to sqlsender@'192.168.110.%' identified by 'P@ssword1!';
回到proxysql上
insert into mysql_users(username,password,default_hostgroup) values('root','P@ssword1!',10);
insert into mysql_users(username,password,default_hostgroup) values('sqlsender','P@ssword1!',10);
load mysql users to runtime;
save mysql users to disk;
插入两个规则,目的是将select语句分离到hostgroup_id=20
的读组,但由于select语句中有一个特殊语句SELECT...FOR UPDATE
它会申请写锁,所以应该路由到hostgroup_id=10
的写组。
insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply)
VALUES (1,1,'^SELECT.*FOR UPDATE$',10,1),
(2,1,'^SELECT',20,1);
load mysql query rules to runtime;
save mysql query rules to disk;
至此读写分离规则生效
以上,组复制是2016年官方推出的高可用功能,并且5.7.20后才支持这个功能,所以旧版本的MySQL不能用,旧版本的要用这个功能只能更新到新版,但是要结合应用程序代码是否能被新版本的数据库兼容