#####Mysql的组复制(全同步复制)######

###Mysql的组复制(全同步复制)####

master :westos1 172.25.40.11
slave:westos2 172.25.40.12
slave:westos3 172.25.40.13

1.基于之前的实验,所有的mysql关闭,把mysql数据删除确保实验环境纯净 ##注意 :删除数据之前先复制uuid /var/lib/mysql/auto.cnf

主数据库:
[root@westos1 mysql]# systemctl stop mysqld
[root@westos1 mysql]# cat /var/lib/mysql/auto.cnf 
[auto]
server-uuid=32e426a2-9e02-11e9-93da-525400807d36
[root@westos1 mysql]# rm -fr /var/lib/mysql/*
从库:
[root@westos2 mysql]# systemctl stop mysqld
[root@westos2 mysql]# rm -fr /var/lib/mysql/*

westos3:

[root@westos3 mysql]#  tar xf mysql-5.7.24-1.el7.x86_64.rpm-bundle.tar
[root@westos3 mysql]# yum install -y mysql-community-client-5.7.24-1.el7.x86_64.rpm mysql-community-common-5.7.24-1.el7.x86_64.rpm mysql-community-libs-5.7.24-1.el7.x86_64.rpm mysql-community-libs-compat-5.7.24-1.el7.x86_64.rpm mysql-community-server-5.7.24-1.el7.x86_64.rpm

2.修改配置文件
##官网可以查阅 https://dev.mysql.com/doc/refman/5.7/en/group-replication-configuring-instances.html

master节点:
[root@westos1 mysql]# vim /etc/my.cnf
server_id=1
gtid_mode=ON
enforce_gtid_consistency=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE #关闭binlog校验
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW #组复制依赖基于行的复制格式

transaction_write_set_extraction=XXHASH64
loose-group_replication_group_name="32e426a2-9e02-11e9-93da-525400807d36"       ##可以看/var/lib/mysql/auto.cnf
loose-group_replication_start_on_boot=off
loose-group_replication_local_address= "172.25.40.11:24901"
loose-group_replication_group_seeds= "172.25.40.11:24901,172.25.40.12:24901,172.25.40.13:24901"
loose-group_replication_bootstrap_group=off     ##插件是否自动引导,这个
选项一般都要off掉,只需要由发起组复制的节点开启,并只启动一次,如果是on>,下次再启动时,会生成一个同名的组,可能会发生脑裂
loose-group_replication_ip_whitelist="127.0.0.1,172.25.40.0/24"
loose-group_replication_enforce_update_everywhere_checks=ON
loose-group_replication_single_primary_mode=OFF  #后两行是开启多主模式的
参数
[root@westos1 mysql]# systemctl start mysqld启动mysqld

3.初始化数据库

[root@westos1 mysql]# cat /var/log/mysqld.log | grep password
2019-07-04T06:17:23.622901Z 1 [Note] A temporary password is generated for root@localhost: fbj#t,85R4QN
[root@westos1 mysql]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.24-log

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> alter user root@localhost identified by 'Westos+001'; 登录数据库,先修改密码
Query OK, 0 rows affected (0.34 sec)

4.)配置

mysql> SET SQL_LOG_BIN=0;  #关闭二进制日志,防止传到其他server上
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE USER rpl_user@'%' IDENTIFIED BY 'Westos+001';
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> SET SQL_LOG_BIN=1;##开启日志
Query OK, 0 rows affected (0.00 sec)

mysql>  CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='Westos+001' FOR CHANNEL 'group_replication_recovery';
Query OK, 0 rows affected, 2 warnings (0.73 sec)

mysql> INSTALL PLUGIN group_replication SONAME 'group_replication.so';
Query OK, 0 rows affected (0.62 sec)

mysql> SHOW PLUGINS;##查看插件
+----------------------------+----------+--------------------+----------------------+---------+
| Name                       | Status   | Type               | Library              | License |
+----------------------------+----------+--------------------+----------------------+---------+

| group_replication          | ACTIVE   | GROUP REPLICATION  | group_replication.so | GPL     |
+----------------------------+----------+--------------------+----------------------+---------+
46 rows in set (0.00 sec)

mysql> SET GLOBAL group_replication_bootstrap_group=ON;##组复制发起节点开启这个参数
Query OK, 0 rows affected (0.00 sec)

mysql> START GROUP_REPLICATION;
Query OK, 0 rows affected (2.90 sec)

mysql> SET GLOBAL group_replication_bootstrap_group=OFF;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT * FROM performance_schema.replication_group_members;##查看server1是否online
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | 631a65fe-9e23-11e9-bc4e-525400807d36 | westos1     |        3306 | ONLINE       |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
1 row in set (0.00 sec)

###注意:先不要添加数据等slave节点都配置好, 最后测试的时候再添加
加入数据

slave节点:

[root@westos2 mysql]# vim /etc/my.cnf
server_id=2   ##更改server_id
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="32e426a2-9e02-11e9-93da-525400807d36"
loose-group_replication_start_on_boot=off
loose-group_replication_local_address= "172.25.40.12:24901"  ##改成slavs节点的主机ip
loose-group_replication_group_seeds= "172.25.40.11:24901,172.25.40.12:24901,172.25.40.13:24901"
loose-group_replication_bootstrap_group=off
loose-group_replication_ip_whitelist="127.0.0.1,172.25.40.0/24"
loose-group_replication_enforce_update_everywhere_checks=ON
loose-group_replication_single_primary_mode=OFF
[root@westos2 mysql]# systemctl start mysqld  ##开启mysql
[root@westos2 mysql]# cat /var/log/mysqld.log | grep password初始化数据库
2019-07-04T06:37:32.599786Z 1 [Note] A temporary password is generated for root@localhost: 7!j*ZthYhN3%
[root@westos2 mysql]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.24-log

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> alter user root@localhost identified by 'Westos+001';   ##登录数据库,先修改密码
Query OK, 0 rows affected (0.05 sec)
mysql> SET SQL_LOG_BIN=0;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE USER rpl_user@'%' IDENTIFIED BY 'Westos+001';
Query OK, 0 rows affected (0.01 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> SET SQL_LOG_BIN=1;
Query OK, 0 rows affected (0.00 sec)

mysql> CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='Wesyos+001' FOR CHANNEL 'group_replication_recovery';
Query OK, 0 rows affected, 2 warnings (0.61 sec)

mysql> INSTALL PLUGIN group_replication SONAME 'group_replication.so';
Query OK, 0 rows affected (0.40 sec)

mysql> START GROUP_REPLICATION;   这里开启组复制可能会报错,查看mysql日志'
'cat /var/log/mysqld.log ,找到解决办法'
ERROR 3092 (HY000): The server is not configured properly to be an active member of the group. Please see more details on error log.
mysql> exit
Bye

查看日志找原因:[root@westos2 mysql]# cat /var/log/mysqld.log 
2019-07-04T06:43:31.131362Z 0 [ERROR] Plugin group_replication reported: 'The member contains transactions not present in the group. The member will now exit the group.'
2019-07-04T06:43:31.131372Z 0 [Note] Plugin group_replication reported: 'To force this member into the group you can use the group_replication_allow_local_disjoint_gtids_join option'
[root@westos2 mysql]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.7.24-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> STOP GROUP_REPLICATION;
Query OK, 0 rows affected (0.00 sec)

mysql> set global group_replication_allow_local_disjoint_gtids_join=on;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> START GROUP_REPLICATION;  恢复正常
Query OK, 0 rows affected, 1 warning (5.02 sec)

westos3:#westos2配置相同

[root@westos3 mysql]# vim /etc/my.cnf

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="32e426a2-9e02-11e9-93da-525400807d36"
loose-group_replication_start_on_boot=off
loose-group_replication_local_address= "172.25.40.13:24901"
loose-group_replication_group_seeds= "172.25.40.11:24901,172.25.40.12:24901,172.25.40.13:24901"
loose-group_replication_bootstrap_group=off
loose-group_replication_ip_whitelist="127.0.0.1,172.25.40.0/24"
loose-group_replication_enforce_update_everywhere_checks=ON
loose-group_replication_single_primary_mode=OFF
[root@westos3 mysql]# systemctl start mysqld
[root@westos3 mysql]# cat /var/log/mysqld.log | grep password
2019-07-04T06:53:28.608731Z 1 [Note] A temporary password is generated for root@localhost: JUmi0uSuwH>s
[root@westos3 mysql]# 
[root@westos3 mysql]# mysql_secure_installation   #初始化数据库密码

Securing the MySQL server deployment.

Enter password for user root: 

The existing password for the user account root has expired. Please set a new password.

New password: 

Re-enter new password: 
[root@westos3 mysql]# mysql -uroot -pWestos+001
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.24-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SET SQL_LOG_BIN=0;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE USER rpl_user@'%' IDENTIFIED BY 'Westos+001';
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> SET SQL_LOG_BIN=1;
Query OK, 0 rows affected (0.00 sec)

mysql> CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='Westos+001' FOR CHANNEL 'group_replication_recovery';
Query OK, 0 rows affected, 2 warnings (0.52 sec)

mysql>  INSTALL PLUGIN group_replication SONAME 'group_replication.so';
Query OK, 0 rows affected (0.62 sec)

mysql> set global group_replication_allow_local_disjoint_gtids_join=on;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql>  START GROUP_REPLICATION;
Query OK, 0 rows affected, 1 warning (6.47 sec)

配置好后在westos1上查看
在这里插入图片描述

看到3台都是online,表示正常
这时在任何一个节点都可以看到刚才插入的数据
在任何节点写入数据,其他节点也能看到
在westos1上插入数据:

###westos1:
mysql> create database westos;
Query OK, 1 row affected (0.13 sec)

mysql> use westos;
Database changed
mysql> create table test2 (t1 int primary key,t2 text not null);
Query OK, 0 rows affected (0.96 sec)

mysql> insert into test2 values (1,'tom');
Query OK, 1 row affected (0.56 sec)

mysql> select * from test2;
+----+-----+
| t1 | t2  |
+----+-----+
|  1 | tom |
+----+-----+
1 row in set (0.00 sec)

####westos2:
mysql> use westos;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql>  select * from test2;
+----+-----+
| t1 | t2  |
+----+-----+
|  1 | tom |
+----+-----+
1 row in set (0.00 sec)

mysql>  insert into test2 values (2,'lily');
Query OK, 1 row affected (0.61 sec)


####westos3:
mysql> use westos;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql>  select * from test2;
+----+------+
| t1 | t2   |
+----+------+
|  1 | tom  |
|  2 | lily |
+----+------+
2 rows in set (0.00 sec)

mysql>  insert into test2 values (3,'Bob');
Query OK, 1 row affected (0.48 sec)


#####westos1:
mysql> select * from test2;
+----+------+
| t1 | t2   |
+----+------+
|  1 | tom  |
|  2 | lily |
|  3 | Bob  |
+----+------+
3 rows in set (0.00 sec)

猜你喜欢

转载自blog.csdn.net/weixin_44821839/article/details/94613856