目录
MySQL-9.1.0 主从复制
1 实验准备工作
1.1 下载并安装官方MySQL的rpm包
1.2 设置DNS解析
1.3 修改密码
2 GTID模式实现主从复制
2.1 增加配置文件内容
2.2 创建主从复制账号
MASTER
SLAVE1
SLAVE2
2.3 实现GTID的自动定位
SLAVE1 && SLAVE2
3 导入数据查看是否成功
3.1 主服务器导入SQL脚本
3.2 两个从服务器查看是否复制同步
本章节实现MySQL主从复制GTID模式
主机 | IP地址 | 端口 |
---|---|---|
mysql-master | 192.168.239.100 | 3306 |
mysql-slave1 | 192.168.239.110 | 3306 |
mysql-slave2 | 192.168.239.120 | 3306 |
[root@master mysql_rpm]# yum localinstall mysql-community-server-9.1.0-1.el7.x86_64.rpm \
mysql-community-common-9.1.0-1.el7.x86_64.rpm \
mysql-community-client-9.1.0-1.el7.x86_64.rpm \
mysql-community-icu-data-files-9.1.0-1.el7.x86_64.rpm \
mysql-community-libs-9.1.0-1.el7.x86_64.rpm \
mysql-community-client-plugins-9.1.0-1.el7.x86_64.rpm
[root@master ~]# cat >> /etc/hosts < 192.168.239.100 master
> 192.168.239.110 slave1
> 192.168.239.120 slave2
> EOF
[root@slave1 ~]# cat >> /etc/hosts < 192.168.239.100 master
> 192.168.239.110 slave1
> 192.168.239.120 slave2
> EOF
[root@slave2 ~]# cat >> /etc/hosts < 192.168.239.100 master
> 192.168.239.110 slave1
> 192.168.239.120 slave2
> EOF
MASTER SLAVE1 SLAVE2 均是以下这样设置
# 改变密码策略
[root@master ~]# cat >> /etc/my.cnf < alter user 'root'@'localhost' identified by '123456';
mysql> flush privileges;
[root@master ~]# cat >> /etc/my.cnf <> /etc/my.cnf <> /etc/my.cnf <
mysql> set sql_log_bin=0; # 关闭二进制SQL日志写入
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'sql_log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| sql_log_bin | OFF |
+---------------+-------+
mysql> CREATE USER 'repl'@'%' IDENTIFIED BY '123456';
# 赋予所有库所有表 repl 用户 REPLICATION SLAVE 的权限
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';
mysql> FLUSH PRIVILEGES;
mysql> set sql_log_bin=1;
mysql> set sql_log_bin=0;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'sql_log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| sql_log_bin | OFF |
+---------------+-------+
mysql> CREATE USER 'repl'@'%' IDENTIFIED BY '123456';
mysql> FLUSH PRIVILEGES;
mysql> set sql_log_bin=1;
mysql> set sql_log_bin=0;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'sql_log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| sql_log_bin | OFF |
+---------------+-------+
mysql> CREATE USER 'repl'@'%' IDENTIFIED BY '123456';
mysql> FLUSH PRIVILEGES;
mysql> set sql_log_bin=1;
mysql> SET SQL_LOG_BIN=0; # 关闭语句记录
# 指定主服务器的IP端口以及授权过的用户repl,并开启自动定位
mysql> CHANGE REPLICATION SOURCE TO
SOURCE_HOST='192.168.239.100',
SOURCE_PORT=3306,
SOURCE_USER='repl',
SOURCE_PASSWORD='123456',
GET_SOURCE_PUBLIC_KEY=1, # 信任证书
SOURCE_AUTO_POSITION=1; # 开启自动定位功能
mysql> START REPLICA;
# 假如说失败需要执行 STOP REPLICA; 停止复制
# 之后再执行 RESET REPLICA; 删除配置的语句
# 查看连接 master 是否正常
mysql> SHOW REPLICA STATUS\G
*************************** 1. row ***************************
Replica_IO_State: Waiting for source to send event
Source_Host: 192.168.239.100
Source_User: repl
Source_Port: 3306
Connect_Retry: 60
Source_Log_File: mysql-bin.000001
Read_Source_Log_Pos: 158
Relay_Log_File: slave2-relay-bin.000002
Relay_Log_Pos: 375
Relay_Source_Log_File: mysql-bin.000001
Replica_IO_Running: Yes # IO表示链接网络没有问题
Replica_SQL_Running: Yes # SQL表示本地同步没有问题,如有问题基本上就是配置文件的问题
mysql> SET SQL_LOG_BIN=0; # 开启语句记录
[root@master ~]# mysql -uroot -p123456
mysql> create database gtid;
Query OK, 1 row affected (0.00 sec)
mysql> use gtid
Database changed
mysql> source gtid.sql;
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> show tables;
+----------------------------------------+
| Tables_in_gtid |
+----------------------------------------+
| aaa |
| abi_http_log |
| act_app_appdef |
| act_app_databasechangelog |
| act_app_databasechangeloglock |
+----------------------------------------+
[root@ slave1 && slave2]# mysql -uroot -p123456
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| gtid |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.01 sec)
mysql> use gtid
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> show tables
+----------------------------------------+
| Tables_in_gtid |
+----------------------------------------+
| aaa |
| abi_http_log |
| act_app_appdef |
| act_app_databasechangelog |
| act_app_databasechangeloglock |
+----------------------------------------+