Docker mysql8.4.5搭建主从复制(mysql 8.4.5)

一.修改配置文件

路径一般是在/etc/my.cnf

1panel 路径

image

修改主服务器my.cnf

重启mysql服务生效

[mysqld]
server-id=101
#启用二进制日志
log-bin=mysql-bin

#gtid:
gtid_mode = on                  # 启用GTID,简化复制管理
enforce_gtid_consistency = on   # 强制gtid一致性,开启后对于特定create table不被支持

# 设置不要复制的数据库(可设置多个)
binlog-ignore-db=mysql
binlog-ignore-db=information_schema

#设置需要复制的数据库 需要复制的主数据库名字
#binlog-do-db=testdb

#设置logbin格式
binlog_format=STATEMENT

修改slave my.cnf

重启mysql服务生效

[mysqld]
server-id=102
#启用二进制日志
log-bin=mysql-bin

#gtid:
gtid_mode = on                  # 启用GTID,简化复制管理
enforce_gtid_consistency = on   # 强制gtid一致性,开启后对于特定create table不被支持

#设置logbin格式
binlog_format=STATEMENT

二,主库上:创建备份账号


[root@mysql ~]# mysql -u root -h 10.60.10.100 -p

-- 创建复制用户,并限制它只能从指定的 Slave IP 连接
CREATE USER 'repl_user'@'172.19.0.%' IDENTIFIED BY 'Repl_20250613';
 
-- 授予该用户 REPLICATION SLAVE 权限 (这是它读取二进制日志所需的最小权限)
GRANT REPLICATION SLAVE ON *.* TO 'repl_user'@'172.19.0.%';
 
-- 刷新权限
FLUSH PRIVILEGES;
 
-- (可选) 查看一下刚创建的用户的权限
SHOW GRANTS FOR 'repl_user'@'172.19.0.%';

--查看日志状态:
SHOW BINARY LOG STATUS;
 
EXIT;

image

三,主库上:导出指定库

--锁定数据库
mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)


-- 导出所有的数据库到 alldatabase.sql
[root@mysql ~]# mysqldump -uroot -p -h 10.60.10.100 --all-databases >alldatabase.sql


--解锁
mysql> UNLOCK TABLES;
Query OK, 0 rows affected (0.00 sec)

mysql> exit;
Bye

四 从库上导入数据库

image


[root@mysql ~]# mysql -u root -h 10.60.10.100 -P 3307 -p < alldatabase.sql

Enter password: 

刷新可以看到数据库已经有了

image

五 在从库启动复制

--执行sql对复制进行设置

CHANGE REPLICATION SOURCE TO SOURCE_HOST='172.19.0.3', SOURCE_USER='repl_user', SOURCE_PASSWORD='Repl_20250613', 
                   SOURCE_PORT=3306, SOURCE_LOG_FILE='mysql-bin.000003', SOURCE_LOG_POS=202599, SOURCE_SSL=1;

--启动复制

START replica;


--在从库上查看复制状态:
SHOW replica STATUS\G

image

mysql> SHOW replica STATUS\G
*************************** 1. row ***************************
             Replica_IO_State: Connecting to source
                  Source_Host: 172.19.0.3
                  Source_User: repl_user
                  Source_Port: 3306
                Connect_Retry: 60
              Source_Log_File: mysql-bin.000003
          Read_Source_Log_Pos: 158
               Relay_Log_File: 6f87995e2d2e-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Source_Log_File: mysql-bin.000003
           Replica_IO_Running: Connecting
          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: 158
              Relay_Log_Space: 158
              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: NULL
Source_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 1045
                Last_IO_Error: Error connecting to source 'repl_user@172.19.0.3:3306'. This was attempt 1/10, with a delay of 60 seconds between attempts. Message: Access denied for user 'repl_user'@'172.19.0.8' (using password: YES)
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Source_Server_Id: 0
                  Source_UUID:
             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: 250613 12:36:00
     Last_SQL_Error_Timestamp:
               Source_SSL_Crl:
           Source_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set: 0d6dfe7e-f4b9-11ef-854f-0242ac130002:1-155
                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)

mysql>

六,需要注意的地方:

1,show master status; 不能用了

# mysql 8.4版本前使用这条命令查看
show master status; 

# MySQL 8.4版本后使用这条命令查看
SHOW BINARY LOG STATUS;

2,change master to不能用了

# MSQL 8.23前
CHANGE MASTER TO MASTER_HOST='192.168.6.133', MASTER_USER='remote', MASTER_PASSWORD='yourpassword', MASTER_LOG_FILE='binlog.000003', MASTER_LOG_POS=158;

# MSQL 8.23后
CHANGE REPLICATION SOURCE TO SOURCE_HOST='192.168.6.133', SOURCE_USER='remote', SOURCE_PASSWORD='yourpassword', SOURCE_LOG_FILE='binlog.000003', SOURCE_LOG_POS=158;

CHANGE REPLICATION SOURCE TO SOURCE_HOST='192.168.6.136', SOURCE_USER='remote', SOURCE_PASSWORD='yourpassword', SOURCE_LOG_FILE='binlog.000004', SOURCE_LOG_POS=158,GET_SOURCE_PUBLIC_KEY=1;

3,start slave不能用了

# 开启同步
start replica ; #8.0.22之后 
start slave ; #8.0.22之前

4,show slave status不能用了

# 查看状态,\G表示行转列,便于查看
show replica status\G ; #8.0.22之后 
show slave status\G ; #8.0.22之前