Mysql读写分离和组复制

读写分离

环境介绍

主机名 ip 服务名
Server1 172.25.1.1 mysql(master)
Server2 172.25.1.2 mysql(slave)
Server3 172.25.1.3 mysql-proxy
  1. 配置主从复制
  2. 开启远程连接权限(server1)
    mysql> grant all on *.* to root@'%' identified by 'Redhat123...';

因为在server1和server2上配置有主从复制,所以只需要的server1中进行配置,配置信息会自动同步到server2中

安装mysql-proxy

解压tar包

[root@server3 ~]# tar xf mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz
[root@server3 ~]# mv mysql-proxy-0.8.5-linux-el6-x86-64bit /usr/local/mysql-proxy

配置mysql-proxy.conf

[root@server3 ~]# mkdir -p /usr/local/mysql-proxy/{conf,logs}
[root@server3 ~]# vim /usr/local/mysql-proxy/conf/mysql-proxy.conf
[mysql-proxy]
user=root
proxy-address=172.25.1.3:3306
proxy-backend-addresses=172.25.1.1:3306   #写服务器
proxy-read-only-backend-addresses=172.25.1.2:3306   #只读服务器
proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua  #lua脚本
plugins=proxy  #插件
log-file=/usr/local/mysql-proxy/logs/mysql-proxy.log
daemon=true
keepalive=true
log-level=debug

修改rw-splitting.lua

[root@server3 ~]# vim /usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua

Mysql读写分离和组复制_第1张图片

修改最大和最小连接数为了测试方便

启动mysql-proxy

[root@server3 ~]# /usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/mysql-proxy.conf 
2018-08-18 20:37:47: (critical) mysql-proxy-cli.c:326: loading config from '/usr/local/mysql-proxy/conf/mysql-proxy.conf' failed: permissions of /usr/local/mysql-proxy/conf/mysql-proxy.conf aren't secure (0660 or stricter required)
2018-08-18 20:37:47: (message) Initiating shutdown, requested from mysql-proxy-cli.c:328
2018-08-18 20:37:47: (message) shutting down normally, exit code is: 1

解决方法

[root@server3 ~]# chmod 660 /usr/local/mysql-proxy/conf/mysql-proxy.conf

查看是否启动成功

[root@server3 ~]# /usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/mysql-proxy.conf 
[root@server3 ~]# netstat -lntup|grep 3306
tcp        0      0 10.10.10.3:3306             0.0.0.0:*                   LISTEN      1175/mysql-proxy  

测试

(1)客户端连接数据库

[root@server4 ~]# yum install -y mysql                 ###下载一个连接的命令
[root@server4 ~]# mysql -h 172.25.1.3 -uroot -pRedhat123...
[root@foundation1 mysql]# mysql -h 172.25.1.3 -uroot -pGAOyu123...
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.17-log MySQL Community Server (GPL)

Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> create database haproxy;  #创建写操作
Query OK, 1 row affected (0.01 sec)

(2)在server1中查看数据

### haproxy库已经被添加进来,因为server1与server2为主从服务关系,因此如果server1和server2都有数据,说明数据是写到了server1中,从server1同步到server2中,server2没有同步到server1的能力
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| haproxy            |
| linux              |
| mysql              |
| performance_schema |
| redhat             |
| repl               |
| sys                |
+--------------------+
8 rows in set (0.00 sec)

Mysql读写分离和组复制_第2张图片

mysql组复制

Mysql读写分离和组复制_第3张图片

(1)安装MySQL(server1,server2,server3)

[root@server3 ~]# ls
mysql-community-client-5.7.23-1.el6.x86_64.rpm
mysql-community-common-5.7.23-1.el6.x86_64.rpm
mysql-community-libs-5.7.23-1.el6.x86_64.rpm
mysql-community-libs-compat-5.7.23-1.el6.x86_64.rpm
mysql-community-server-5.7.23-1.el6.x86_64.rpm
mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz

[root@server3 ~]# yum install -y *

(2)启动并进行初始化

[root@server3 ~]# /etc/init.d/mysqld restart
[root@server3 ~]# grep "password" /var/log/mysqld.log   #查找初始密码进行初始化
[root@server3 ~]# mysql_secure_installation

(3)配置my.cnf

<1>server1

server_id=1
gtid_mode=ON
enforce_gtid_consistency=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW

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_local_address= "172.25.1.1:24901"
loose-group_replication_group_seeds= "172.25.1.1:24901,172.25.1.2:24901,172.25.1.3:24901"
loose-group_replication_bootstrap_group=off

loose-group_replication_single_primary_mode=FALSE
loose-group_replication_enforce_update_everywhere_checks=TRUE
loose-group_replication_ip_whitelist='172.25.1.0/24'

<2>server2

server_id=2
gtid_mode=ON
enforce_gtid_consistency=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW

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_local_address= "172.25.1.2:24901"
loose-group_replication_group_seeds= "172.25.1.1:24901,172.25.1.2:24901,172.25.1.3:24901"
loose-group_replication_bootstrap_group=off

loose-group_replication_single_primary_mode=FALSE
loose-group_replication_enforce_update_everywhere_checks=TRUE
loose-group_replication_ip_whitelist='172.25.1.0/24'

<3>server3

server_id=3
gtid_mode=ON
enforce_gtid_consistency=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW

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_local_address= "172.25.1.3:24901"
loose-group_replication_group_seeds= "172.25.1.1:24901,172.25.1.2:24901,172.25.1.3:24901"
loose-group_replication_bootstrap_group=off

loose-group_replication_single_primary_mode=FALSE
loose-group_replication_enforce_update_everywhere_checks=TRUE
loose-group_replication_ip_whitelist='172.25.1.0/24'

(4)配置mysql

[root@server1 ~]# /etc/init.d/mysqld restart
[root@server1 ~]# mysql -p
mysql> SET SQL_LOG_BIN=0;             ###关闭日志
mysql> GRANT REPLICATION SLAVE ON *.* TO repl@'%' identified by 'Redhat123...';
mysql> FLUSH PRIVILEGES;
mysql> RESET MASTER;
mysql> SET SQL_LOG_BIN=1;
mysql> CHANGE MASTER TO MASTER_USER='repl', MASTER_PASSWORD='Redhat123...' FOR CHANNEL 'group_replication_recovery';
mysql> INSTALL PLUGIN group_replication SONAME 'group_replication.so';          ###安装
mysql> SHOW PLUGINS;                 ###查看
mysql> SET GLOBAL group_replication_bootstrap_group=ON;
mysql> START GROUP_REPLICATION;
mysql> SET GLOBAL group_replication_bootstrap_group=OFF;
mysql> SELECT * FROM performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | a3035dc1-3109-11e9-8e3f-525400b3177a | server1     |        3306 | ONLINE       |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
1 row in set (0.00 sec)

[root@server2 ~]# /etc/init.d/mysqld restart
[root@server2 ~]# mysql -p
mysql> SET SQL_LOG_BIN=0;             ###关闭日志
mysql> GRANT REPLICATION SLAVE ON *.* TO repl@'%' identified by 'Redhat123...';
mysql> FLUSH PRIVILEGES;
mysql> RESET MASTER;
mysql> SET SQL_LOG_BIN=1;
mysql> CHANGE MASTER TO MASTER_USER='repl', MASTER_PASSWORD='Redhat123...' FOR CHANNEL 'group_replication_recovery';
mysql> INSTALL PLUGIN group_replication SONAME 'group_replication.so';          ###安装
mysql> SHOW PLUGINS;                 ###查看
mysql> START GROUP_REPLICATION;
mysql> SELECT * FROM performance_schema.replication_group_members; 
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | a3035dc1-3109-11e9-8e3f-525400b3177a | server1     |        3306 | ONLINE       |
| group_replication_applier | aa3e6a35-3109-11e9-a349-525400c7b4a0 | server2     |        3306 | ONLINE       |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
2 rows in set (0.00 sec)

[root@server3 ~]# /etc/init.d/mysqld restart
[root@server3 ~]# mysql -p
mysql> SET SQL_LOG_BIN=0;             ###关闭日志
mysql> GRANT REPLICATION SLAVE ON *.* TO repl@'%' identified by 'Redhat123...';
mysql> FLUSH PRIVILEGES;
mysql> RESET MASTER;
mysql> SET SQL_LOG_BIN=1;
mysql> CHANGE MASTER TO MASTER_USER='repl', MASTER_PASSWORD='Redhat123...' FOR CHANNEL 'group_replication_recovery';
mysql> INSTALL PLUGIN group_replication SONAME 'group_replication.so';          ###安装
mysql> SHOW PLUGINS;                 ###查看
mysql> START GROUP_REPLICATION;
mysql> SELECT * FROM performance_schema.replication_group_members; 
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | a3035dc1-3109-11e9-8e3f-525400b3177a | server1     |        3306 | ONLINE       |
| group_replication_applier | aa3e6a35-3109-11e9-a349-525400c7b4a0 | server2     |        3306 | ONLINE       |
| group_replication_applier | cc4e13be-3106-11e9-9f75-525400f1946d | server3     |        3306 | ONLINE       |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
3 rows in set (0.00 sec)


(5)设置白名单

如果在my.cnf配置中没有加入白名单的配置,启动组复制将会失败,我们可以通过下面的命令来解决!!!
日志会报错:Connection attempt from IP address 172.25.1.1 refused. Address
is not in the IP whitelist.

mysql> STOP GROUP_REPLICATION;
mysql> SET GLOBAL group_replication_ip_whitelist='172.25.1.0/24'
mysql> START GROUP_REPLICATION;

(6) 测试

在组复制中没有master和slave只分,他们有可以做为对方的主或对方的从

<1>使用server3添加一个数据库

mysql> CREATE DATABASE redhat;
Query OK, 1 row affected (0.02 sec)

<2>在server1中查看

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| redhat             |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

你可能感兴趣的:(Mysql读写分离和组复制)