MySQL主从自动协商
需求
- 实验2与上一个实验需求基本相同。master1 作为主mysql,slave1 作为从mysql。 不同之处,使用了 "gtid_mode=ON enforce_gtid_consistency=1" 该属性自动记录position位置。不需要手动指定了。
环境
主服务器配置
-
部署一台新mysql服务器。准备好域名解析。
-
准备数据
sql
mysql> create database master1db;
Query OK, 1 row affected (0.05 sec)
mysql> use master1db;
Database changed
mysql> create table master1db.master1tab(name char(50));
Query OK, 0 rows affected (0.03 sec)
mysql> insert into master1db.master1tab values (1111);
Query OK, 1 row affected (0.02 sec)
mysql> insert into master1db.master1tab values (2222);
Query OK, 1 row affected (0.00 sec)
- 开启二进制日志
bash
[root@service ~]# vim /etc/my.cnf
server-id=1
log_bin
gtid_mode=ON
enforce_gtid_consistency=1
[root@service ~]# systemctl restart mysqld
- 创建复制用户
sql
mysql> create user 'rep'@'192.168.142.%' identified by 'Wyxbuke00.';
Query OK, 0 rows affected (0.03 sec)
mysql> grant replication slave,replication client on *.* to 'rep'@'192.168.142.%' ;
Query OK, 0 rows affected (0.00 sec)
- 备份master数据库的数据
bash
[root@service ~]# mysqldump -p'Wyxbuke00.' --all-databases --single-transaction --master-data=2 --flush-logs > `date +%F`-mysql-all.sql
[root@service ~]# ll
总用量 446108
-rw-r--r--. 1 root root 0 2月 22 16:12 2024-02-22
-rw-r--r--. 1 root root 1288580 2月 22 16:10 2024-02-22-mysql-all.sql
-rw-r--r--. 1 root root 0 2月 23 10:20 2024-02-23
-rw-r--r--. 1 root root 1288900 2月 23 10:21 2024-02-23-mysql-all.sql
[root@service ~]# scp 2024-02-23-mysql-all.sql slave1:/tmp
root@slave1's password:
2024-02-23-mysql-all.sql 100% 1259KB 48.4MB/s 00:00
观察二进制日志分割点
-- CHANGE MASTER TO MASTER_LOG_FILE='service-bin.000003', MASTER_LOG_POS=157;
- 准备数据2
sql
mysql> insert into master1db.master1tab values (6666666666);
Query OK, 1 row affected (0.02 sec)
mysql> use master1db;
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 master1db.master1tab;
+------------+
| name |
+------------+
| 1111 |
| 2222 |
| 33333333 |
| 44444 |
| 55555 |
| 6666666666 |
+------------+
6 rows in set (0.00 sec)
从服务器配置
- 重置从服务器数据库
bash
[root@server ~]# systemctl stop mysqld
[root@server ~]# rm -rf /var/lib/mysql/*
[root@server ~]# systemctl start mysqld
[root@server ~]# grep password /var/log/mysqld.log
[Server] A temporary password is generated for root@localhost: GsH5i*E&!m*V
[root@server ~]# mysqladmin -uroot -p'GsH5i*E&!m*V' password 'Wyxbuke00.'
- 测试rep用户是否可用
bash
[root@server ~]# mysql -h master1 -urep -p'Wyxbuke00.'
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 15
Server version: 8.0.36 MySQL Community Server - GPL
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
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>
- 启动二进制日志,服务器ID,GTID
bash
vim /etc/my.cnf
server-id=2
log_bin
gtid_mode=ON
enforce_gtid_consistency=1
[root@server ~]# systemctl restart mysqld
- 还原恢复手动同步数据
bash
[root@server ~]# ll /tmp
total 1260
-rw-r--r--. 1 root root 1288580 Feb 22 16:35 2024-02-22-mysql-all.sql
-rw-r--r--. 1 root root 0 Feb 23 10:06 2024-02-23-10
mysql> set sql_log_bin=0;
Query OK, 0 rows affected (0.00 sec)
mysql> source /tmp/2024-02-23-10;
mysql> source /tmp/2024-02-23-mysql-all.sql;
mysql> select * from master1db.master1tab;
+------------+
| name |
+------------+
| 1111 |
| 2222 |
| 33333333 |
| 44444 |
| 55555 |
| 6666666666 |
+------------+
6 rows in set (0.00 sec)
- 设置主服务器
sql
mysql> change master to master_host='master1',master_user='rep',master_password='Wyxbuke00.',master_auto_position=1;
Query OK, 0 rows affected, 7 warnings (0.03 sec)
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: master1
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: service-bin.000007
Read_Master_Log_Pos: 197
Relay_Log_File: server-relay-bin.000002
Relay_Log_Pos: 377
Relay_Master_Log_File: service-bin.000007
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 197
Relay_Log_Space: 588
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: d5ddcb15-d063-11ee-a4cc-000c2904e4be
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set: d5ddcb15-d063-11ee-a4cc-000c2904e4be:1
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:
1 row in set, 1 warning (0.00 sec)
- 返回主服务器更新数据,到从服务器查看数据
sql
主服务器
mysql> insert into master1db.master1tab values (777788);
Query OK, 1 row affected (0.00 sec)
从服务器
mysql> select * from master1db.master1tab;
+------------+
| name |
+------------+
| 1111 |
| 2222 |
| 33333333 |
| 44444 |
| 55555 |
| 6666666666 |
| 777788 |
+------------+
7 rows in set (0.01 sec)