速查表之mysql8主从复制

环境准备: 两个mysql服务 or 两个服务器, 这里用docker环境
cat docker-compose.yml

version: "3"
services:
  mysql-m1:
    image: mysql:8.0.28
    volumes:
      - ${PWD}/mysql/data:/var/lib/mysql
      - ${PWD}/mysql/conf/master.conf:/etc/mysql/conf.d/master.cnf
    ports :
      - "3307:3306"
    restart: always
    environment:
      TZ: Asia/Shanghai
      MYSQL_ROOT_PASSWORD: root
  mysql-s1:
    image: mysql:8.0.28
    volumes:
      - ${PWD}/mysql/data:/var/lib/mysql
      - ${PWD}/mysql/conf/slave.conf:/etc/mysql/conf.d/slave.cnf
    ports :
      - "3307:3306"
    restart: always
    environment:
      TZ: Asia/Shanghai
      MYSQL_ROOT_PASSWORD: root

主库配置

[mysqld]
log-bin=mysql-bin#开启binlog
server-id=1
gtid_mode=on
enforce-gtid-consistency=on #强制gtid一致性,安全
log-slave-updates=on #在从服务器中记录传过来的主服务器修改日志
explicit_defaults_for_timestamp=true
lower_case_table_names=1
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
binlog-ignore-db=mysql
max_allowed_packet=128M

主库创建repl用户

mysql> create user repl@'%' identified WITH mysql_native_password by 'repl';
Query OK, 0 rows affected (0.02 sec)

mysql> grant replication slave on *.* to 'repl'@'%';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

开启gtid_modo为必须,否则会报如下错误:

  1. show slave status\G 时报错: The replication receiver thread cannot start because the master has GTID_MODE = ON and this server has GTID_MODE = OFF.

从库配置

server-id=2
gtid_mode=on
enforce-gtid-consistency=on
explicit_defaults_for_timestamp=true
lower_case_table_names=1
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION


binlog-ignore-db=mysql
max_allowed_packet=128M

配置文件不能为大写,否则报错
unknown variable ‘GTID_MODE=ON’.
配置gtid_mode=on时 enforce-gtid-consistency=on 为必须,否则报错
GTID_MODE = ON requires ENFORCE_GTID_CONSISTENCY = ON.

从库设置开始复制position

mysql> CHANGE MASTER TO MASTER_HOST='xxx.xxx.xxx.xxx',MASTER_PORT=3307,MASTER_USER='repl',MASTER_PASSWORD='repl',MASTER_LOG_FILE='mysql-bin.000005',MASTER_LOG_POS=197 ;

从库查看复制状态

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: xxx.xxx.xxx.xxx
                  Master_User: repl
                  Master_Port: 3307
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000005
          Read_Master_Log_Pos: 2850457
               Relay_Log_File: fbc4f27702f7-relay-bin.000005
                Relay_Log_Pos: 1546309
        Relay_Master_Log_File: mysql-bin.000005
             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: 2850457
              Relay_Log_Space: 2851232
              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: 54033806-af29-11ec-8fb2-0242ac150002
             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
           ...略

完成…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL 8 实现主从复制的过程包括以下几个步骤: 1. 配置主服务器: 首先,需要在主服务器上打开 binlog,并配置一个唯一的 server-id。这样主服务器才能将更新的日志发送给从服务器。在 MySQL 配置文件中,需要添加如下配置: ``` [mysqld] log-bin = mysql-bin server-id = 1 ``` 其中,`log-bin` 指定了 binlog 文件的名称,`server-id` 是主服务器的唯一标识。 2. 创建复制账户: 在主服务器上创建一个专门用于复制的账户,并赋予账户 REPLICATION SLAVE 权限。例如: ``` CREATE USER 'repl'@'slave_ip' IDENTIFIED BY 'password'; GRANT REPLICATION SLAVE ON *.* TO 'repl'@'slave_ip'; ``` 其中,`slave_ip` 是从服务器的 IP 地址,`password` 是账户的密码。 3. 备份数据: 在主服务器上对数据库进行备份,并记录当前的 binlog 文件名和位置。这样在从服务器配置时可以指定从哪个位置开始复制。 4. 配置从服务器: 在从服务器上,需要配置连接主服务器的信息和复制参数。通过修改 MySQL 配置文件,添加如下配置: ``` [mysqld] server-id = 2 ``` 然后在命令行或者 MySQL Workbench 中执行如下命令,连接主服务器并开始复制: ``` CHANGE MASTER TO MASTER_HOST='master_ip', MASTER_USER='repl', MASTER_PASSWORD='password', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=123; START SLAVE; ``` 其中,`master_ip` 是主服务器的 IP 地址,`mysql-bin.000001` 和 `123` 是在步骤 3 中备份数据时记录的 binlog 文件名和位置。 通过以上步骤,MySQL 8 主从复制就可以成功实现了。从服务器会不断从主服务器同步数据,并保持数据的一致性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值