MySQL数据库集群技术主从自动协商详细讲解

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)
相关推荐
热爱嵌入式的小许12 分钟前
Linux基础项目开发1:量产工具——显示系统
linux·运维·服务器·韦东山量产工具
vvvae12342 小时前
分布式数据库
数据库
雪域迷影3 小时前
PostgreSQL Docker Error – 5432: 地址已被占用
数据库·docker·postgresql
bug菌¹3 小时前
滚雪球学Oracle[4.2讲]:PL/SQL基础语法
数据库·oracle
逸巽散人4 小时前
SQL基础教程
数据库·sql·oracle
韩楚风4 小时前
【linux 多进程并发】linux进程状态与生命周期各阶段转换,进程状态查看分析,助力高性能优化
linux·服务器·性能优化·架构·gnu
陈苏同学4 小时前
4. 将pycharm本地项目同步到(Linux)服务器上——深度学习·科研实践·从0到1
linux·服务器·ide·人工智能·python·深度学习·pycharm
Ambition_LAO4 小时前
解决:进入 WSL(Windows Subsystem for Linux)以及将 PyCharm 2024 连接到 WSL
linux·pycharm
月空MoonSky4 小时前
Oracle中TRUNC()函数详解
数据库·sql·oracle
momo小菜pa4 小时前
【MySQL 06】表的增删查改
数据库·mysql