服务器点位
NODE |
IP |
mgr_node0 |
192.165.26.200 |
mgr_node1 |
192.165.25.201 |
mgr_node2 |
192.165.26.202 |
proxysql |
192.165.26.199 |
修改主机名
hostnamectl set-hostname mgr_node0
hostnamectl set-hostname mgr_node1
hostnamectl set-hostname mgr_node2
hostnamectl set-hostname proxysql
在所有节点修改/etc/hosts
192.165.26.200 mgr_node0
192.165.26.201 mgr_node1
192.165.26.202 mgr_node2
192.165.26.199 proxysql
运行uuidgen获取uuid
uuidgen
修改所有节点的my.cnf
[mysqld]
user=mysql
port=3306
basedir=/usr/local/mysql
datadir=/data
max_connections=200
character-set-server=utf8mb4
default-storage-engine=INNODB
server_id=200
log-bin=/usr/local/mysql/log/mysql-bin.log
log_slave_updates=ON
relay_log=relay-log
binlog_format=ROW
binlog_checksum=NONE
gtid_mode=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
slave_preserve_commit_order=ON
transaction_write_set_extraction=XXHASH64
loose-group_replication_group_name=<a uuid>
loose-group_replication_start_on_boot=off
loose-group_replication_local_address=<ip:port>
loose-group_replication_group_seeds=<ip:port,ip:port>
loose-group_replication_ip_whitelist=<ip,net,...>
loose-group_replication_bootstrap_group=off
loose-group_replication_single_primary_mode=ture
loose-group_replication_enforce_update_everywhere_checks=false
loose-group_replication_single_primary_mode=ture
loose-group_replication_enforce_update_everywhere_checks=false
多主运行
每个mysql节点均运行
mysql> INSTALL PLUGIN group_replication SONAME 'group_replication.so';
mysql> create user 'repl'@'%' identified by 'p@ssw0rd';
mysql> grant replication slave on *.* to 'repl'@'%';
在第一个节点执行
mysql> change master to master_user='repl',master_password='p@ssw0rd' for channel 'group_replication_recovery';
mysql> set global group_replication_bootstrap_group=on;
mysql> start group_replication;
mysql> set global group_replication_bootstrap_group=off;
在其他节点执行
mysql> change master to master_user='repl',master_password='p@ssw0rd' for channel 'group_replication_recovery';
mysql> set global group_replication_allow_local_disjoint_gtids_join=on;
mysql> start group_replication;
查看是否搭建成功
mysql> select * from performance_schema.replication_group_members;
单主运行
每个MySQL节点运行
mysql> INSTALL PLUGIN group_replication SONAME 'group_replication.so';
mysql> create user 'repl'@'%' identified by 'p@ssw0rd';
mysql> grant replication slave on *.* to 'repl'@'%';
mysql> group_replication_enforce_update_everywhere_checks=off;
mysql> set global group_replication_single_primary_mode=on;
在主节点运行
mysql> change master to master_user='repl',master_password='p@ssw0rd' for channel 'group_replication_recovery';
mysql> set global group_replication_bootstrap_group=on;
mysql> start group_replication;
mysql> set global group_replication_bootstrap_group=off;
在从节点运行
mysql> change master to master_user='repl',master_password='p@ssw0rd' for channel 'group_replication_recovery';
mysql> set global group_replication_allow_local_disjoint_gtids_join=on;
mysql> start group_replication;
查询主节点
mysql> show variables like 'server_uuid';
mysql> show status like 'group_replication_primary_member';
group_replication_primary_member如果返回结果为空则为多主,如果不为空则显示的id的设备为主
ProxySQL配置(读写分离)
在集群主节点运行
mysql> create user monitor@'%' identified by 'p@ssw0rd';
mysql> grant all privileges on *.* to monitor@'%' with grant option;
mysql> create user proxysql@'%' identified by 'p@ssw0rd';
mysql> grant all privileges on *.* to proxysql@'%' with grant option;
配置主写从读
主节点:
mysql> set global read_only=0;
从节点
mysql> set global read_only=1;
下载proxysql的rpm包后利用yum安装
启动ProxySQL
$ systemctl start proxysql
$ netstat -anlp|grep proxysql
利用MySQL客户端登录proxysql
mysql -uadmin -padmin -h 127.0.0.1 -P 6032 --prompt 'proxysql>'
配置监控账号
proxysql> set mysql-monitor_username='monitor';
proxysql> set mysql-monitor_password='p@ssw0rd';
或者
proxysql> UPDATE global_variables SET variable_value='monitor' WHERE variable_name='mysql-monitor_username';
proxysql> UPDATE global_variables SET variable_value='p@ssw0rd' WHERE variable_name='mysql-monitor_password';
配置默认组信息
组名 |
id |
writer_hostgroup |
10 |
backup_writer_hostgroup |
20 |
reader_hostgroup |
30 |
offline_hostgroup |
40 |
proxysql> insert into mysql_group_replication_hostgourps(writer_hostgroup,backup_writer_hostgroup,reader_hostgroup,offline_hostgroup,active,writer_is_also_reader)values(10,20,30,40,1,1);
配置写组用户
insert into mysql_users(username,password,default_hostgroup)values('proxysql','p@ssw0rd',10);
主节点定义为写组10,从节点定义为只读组30
insert into mysql_servers(hostgroup_id,hostname,port,weight,max_connections,max_replication_lag,comment)values (10,'192.165.26.200',3306,1,3000,10,'mgr_node0');
insert into mysql_servers(hostgroup_id,hostname,port,weight,max_connections,max_replication_lag,comment)values (30,'192.165.26.201',3306,2,3000,10,'mgr_node1');
insert into mysql_servers(hostgroup_id,hostname,port,weight,max_connections,max_replication_lag,comment)values (30,'192.165.26.202',3306,2,3000,10,'mgr_node2');
insert into mysql_servers(hostgroup_id,hostname,port,weight,max_connections,max_replication_lag,comment)values (30,'192.165.26.200',3306,1,3000,10,'mgr_node0');
配置分离参数
insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply)values(1,1,'^SELECT.*FOR UPDATE$',10,1);
insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply)values(2,1,'^SELECT',30,1);
生效
save mysql users to disk;
save mysql servers to disk;
save mysql query rules to disk;
save mysql variables to disk;
save admin variables to disk;
load mysql users to runtime;
load mysql servers to runtime;
load mysql query rules to runtime;
load mysql variables to runtime;
load admin variables to runtime;
在MGR主节点执行 addition_to_sys.sql
USE sys;
DELIMITER $$
CREATE FUNCTION IFZERO(a INT, b INT)
RETURNS INT
DETERMINISTIC
RETURN IF(a = 0, b, a)$$
CREATE FUNCTION LOCATE2(needle TEXT(10000), haystack TEXT(10000), offset INT)
RETURNS INT
DETERMINISTIC
RETURN IFZERO(LOCATE(needle, haystack, offset), LENGTH(haystack) + 1)$$
CREATE FUNCTION GTID_NORMALIZE(g TEXT(10000))
RETURNS TEXT(10000)
DETERMINISTIC
RETURN GTID_SUBTRACT(g, '')$$
CREATE FUNCTION GTID_COUNT(gtid_set TEXT(10000))
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE result BIGINT DEFAULT 0;
DECLARE colon_pos INT;
DECLARE next_dash_pos INT;
DECLARE next_colon_pos INT;
DECLARE next_comma_pos INT;
SET gtid_set = GTID_NORMALIZE(gtid_set);
SET colon_pos = LOCATE2(':', gtid_set, 1);
WHILE colon_pos != LENGTH(gtid_set) + 1 DO
SET next_dash_pos = LOCATE2('-', gtid_set, colon_pos + 1);
SET next_colon_pos = LOCATE2(':', gtid_set, colon_pos + 1);
SET next_comma_pos = LOCATE2(',', gtid_set, colon_pos + 1);
IF next_dash_pos < next_colon_pos AND next_dash_pos < next_comma_pos THEN
SET result = result +
SUBSTR(gtid_set, next_dash_pos + 1,
LEAST(next_colon_pos, next_comma_pos) - (next_dash_pos + 1)) -
SUBSTR(gtid_set, colon_pos + 1, next_dash_pos - (colon_pos + 1)) + 1;
ELSE
SET result = result + 1;
END IF;
SET colon_pos = next_colon_pos;
END WHILE;
RETURN result;
END$$
CREATE FUNCTION gr_applier_queue_length()
RETURNS INT
DETERMINISTIC
BEGIN
RETURN (SELECT sys.gtid_count( GTID_SUBTRACT( (SELECT
Received_transaction_set FROM performance_schema.replication_connection_status
WHERE Channel_name = 'group_replication_applier' ), (SELECT
@@global.GTID_EXECUTED) )));
END$$
CREATE FUNCTION gr_member_in_primary_partition()
RETURNS VARCHAR(3)
DETERMINISTIC
BEGIN
RETURN (SELECT IF( MEMBER_STATE='ONLINE' AND ((SELECT COUNT(*) FROM
performance_schema.replication_group_members WHERE MEMBER_STATE != 'ONLINE') >=
((SELECT COUNT(*) FROM performance_schema.replication_group_members)/2) = 0),
'YES', 'NO' ) FROM performance_schema.replication_group_members JOIN
performance_schema.replication_group_member_stats USING(member_id));
END$$
CREATE VIEW gr_member_routing_candidate_status AS SELECT
sys.gr_member_in_primary_partition() as viable_candidate,
IF( (SELECT (SELECT GROUP_CONCAT(variable_value) FROM
performance_schema.global_variables WHERE variable_name IN ('read_only',
'super_read_only')) != 'OFF,OFF'), 'YES', 'NO') as read_only,
sys.gr_applier_queue_length() as transactions_behind, Count_Transactions_in_queue as 'transactions_to_cert' from performance_schema.replication_group_member_stats;$$
DELIMITER ;
查看各节点状态
mysql> SELECT * FROM sys.gr_member_routing_candidate_status;
proxysql> select * from mysql_server_group_replication_log order by time_start_us desc limit 6\G;
proxysql> SELECT * FROM monitor.mysql_server_connect_log ORDER BY time_start_us DESC LIMIT 10\G;
proxysql> SELECT* FROM monitor.mysql_server_ping_log ORDER BY time_start_us DESC LIMIT 10;