MySQL8.4.2使用自带方法开启主从模式(1 主 2 从)

 ·  2025-01-15

1、前置条件

192.168.3.200
192.168.3.201
192.168.3.202

root@xdc-virtual-machine:/home/xdc# mysql --version
mysql  Ver 8.4.2 for Linux on x86_64 (MySQL Community Server - GPL)

2、主服务器配置
修改/etc/mysql/my.cnf

[mysqld]
server-id = 1 #需保持唯一
log-bin = mysql-bin
binlog-format = ROW

3、查看主服务器日志文件名称及坐标位置

SHOW BINARY LOG STATUS;
8.0.39:show master status;
返回示例
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 |      339 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

4、主服务器用户创建

-- 创建用户 slave
CREATE USER 'slave'@'%' IDENTIFIED BY '12345678';
-- 授权
GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%';

GRANT ALL PRIVILEGES ON *.* TO 'slave'@'%'WITH GRANT OPTION;

-- 刷新权限
FLUSH PRIVILEGES;

5、从服务器1配置
修改/etc/mysql/my.cnf

[mysqld]
server-id = 2                      # 唯一的服务器 ID,与主服务器不同
relay-log = relay-log              # 启用 relay log
log-bin = mysql-bin                # 从库也需要二进制日志(用于级联复制)
read_only = 1                      # 设置只读模式(super 用户除外)

6、配置从库链接主库
(1)、mysql -p 进入 mysql 命令行界面
(2)、执行

CHANGE REPLICATION SOURCE TO
     SOURCE_HOST='192.168.3.200',
     SOURCE_PORT=3306,
     SOURCE_USER='slave',
     SOURCE_PASSWORD='12345678',
     SOURCE_LOG_FILE='mysql-bin.000004',
     SOURCE_LOG_POS=339,
     SOURCE_SSL = 1;

8.0.39
建立链接

change master to master_host="192.168.3.200",master_port=3306,master_user="slave",master_password="12345678",master_log_file="binlog.000001",master_log_pos=683;

开启链接
start slave;
查看状态
show slave status;
SHOW SLAVE STATUS\G
其他:
STOP SLAVE;
RESET SLAVE;
START SLAVE;
(3)、开启

 START REPLICA;

(4)、检查状态

 SHOW REPLICA STATUS\G;

(5)、如出现报错信息需进行手动排查错误
UUID相同:
停止服务:sudo systemctl stop mysql
删除配置:sudo rm /var/lib/mysql/auto.cnf
重启服务:sudo systemctl start mysql
检查 UUID:SHOW VARIABLES LIKE 'server_uuid';
按上续方法继续执行开启及检查命令即可直至无报错信息即可检查同步效果
数据不同步问题:
1、停止复制:STOP REPLICA;
2、重置复制:RESET REPLICA;
3、开启:START REPLICA;
成功:

mysql>  SHOW REPLICA STATUS\G;
*************************** 1. row ***************************
             Replica_IO_State: Waiting for source to send event
                  Source_Host: 192.168.3.200
                  Source_User: slave
                  Source_Port: 3306
                Connect_Retry: 60
              Source_Log_File: mysql-bin.000004
          Read_Source_Log_Pos: 1032
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 1021
        Relay_Source_Log_File: mysql-bin.000004
           Replica_IO_Running: Yes
          Replica_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_Source_Log_Pos: 1032
              Relay_Log_Space: 1226
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Source_SSL_Allowed: Yes
           Source_SSL_CA_File: 
           Source_SSL_CA_Path: 
              Source_SSL_Cert: 
            Source_SSL_Cipher: 
               Source_SSL_Key: 
        Seconds_Behind_Source: 0
Source_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Source_Server_Id: 1
                  Source_UUID: 175f955e-d259-11ef-b2a2-000c2952a796
             Source_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
    Replica_SQL_Running_State: Replica has read all relay log; waiting for more updates
           Source_Retry_Count: 10
                  Source_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Source_SSL_Crl: 
           Source_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Source_TLS_Version: 
       Source_public_key_path: 
        Get_Source_public_key: 0
            Network_Namespace: 
1 row in set (0.00 sec)

 
评论
Jan. All Rights Reserved. Theme Jasmine by Kent Liao.
蜀ICP备2023017538号-1