MySQL 8.0 GTID 搭建主从复制

一、环境介绍

虚拟机:Oracle VM VirtualBox 5.0.24
操作系统:CentOS Linux release 7.7.1908 (Core)
MySQL服务器:8.0.11 MySQL Community Server - GPL
软件下载地址
主从方式:基于GTID的搭建
主从同步方式:异步复制(也可使用半同步复制)
master_ip:192.168.56.101
slave1_ip:192.168.56.103
master主机名:mysql-101
slave1主机名:mysql-103

master配置文件:/etc/my.cnf

[mysqld]
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

basedir=/usr/local/mysql/mysql-8.0.11
datadir=/data/mysql/
port = 3306
socket = /tmp/mysql.sock
character-set-server=utf8

log-error = /data/mysql/mysqld.log
pid-file = /data/mysql/mysqld.pid

#GTID:
server_id=135                #服务器id
gtid_mode=on                 #开启gtid模式
enforce_gtid_consistency=on  #强制gtid一致性,开启后对于特定create table不被支持

#binlog
log_bin=master-binlog
log-slave-updates=1    
binlog_format=row            #强烈建议,其他格式可能造成数据不一致

#relay log
skip_slave_start=1
max_connect_errors=1000
default_authentication_plugin = 'mysql_native_password'

slave配置文件:/etc/my.cnf

[mysqld]
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

basedir=/data/mysql/mysql-8.0.11/
datadir=/data/mysql/mysql8.0_data/
port = 3306
socket = /tmp/mysql-8-0-11.sock
character-set-server=utf8

log-error = /data/mysql/mysql8.0_data/mysqld.log
pid-file = /data/mysql/mysql8.0_data/mysqld.pid
max_connect_errors=1000

#GTID:
gtid_mode=on
enforce_gtid_consistency=on
server_id=144

#binlog
log-bin=slave-binlog
log-slave-updates=1
binlog_format=row      #强烈建议,其他格式可能造成数据不一致

#relay log
skip_slave_start=1
default_authentication_plugin = 'mysql_native_password'

二、GTID搭建主从复制

1、主库执行

create user 'repl'@'%' identified by 'repl';
grant replication slave,replication client on *.* to 'repl'@'%';
flush privileges;

2、从库执行

  • 使用复制账号尝试远程连接
mysql -urepl -prepl -h192.168.56.101 -P 3306
  • 搭建主从复制
CHANGE MASTER TO  
      MASTER_HOST='192.168.56.101',    
      master_user='repl',    
      master_password='repl',    
      master_port=3306,    
      MASTER_AUTO_POSITION = 1;
  • 启动从库进程
start slave;
  • 查看从库启动状态
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.56.101
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-binlog.000002
          Read_Master_Log_Pos: 1135
               Relay_Log_File: mysql-103-relay-bin.000002
                Relay_Log_Pos: 1357
        Relay_Master_Log_File: master-binlog.000002
             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: 1135
              Relay_Log_Space: 1569
              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: 135
                  Master_UUID: 1c8f7f48-4a7c-11ea-ab18-0800278ffd3d
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave 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: 1c8f7f48-4a7c-11ea-ab18-0800278ffd3d:1-4
            Executed_Gtid_Set: 1c8f7f48-4a7c-11ea-ab18-0800278ffd3d:1-4,
846de782-4a7c-11ea-95ab-080027e75c4d:1
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
       Master_public_key_path: 
        Get_master_public_key: 0
1 row in set (0.00 sec)

三、错误异常处理

现象:
① Slave_IO_Running是Connecting状态,Slave_SQL_Running是Yes状态
② 数据库错误日志中显示账号无法连接到主库服务器
解决思路:
① 主从服务器间网络是否畅通
② 是否存在防火墙,过滤了数据库端口(命令:telnet 192.168.56.103 3306)
③ 复制链路配置的用户名和密码是否正确,是否有相应权限(查看权限命令:show grants for repuser@’%’\G)
④ 尝试使用复制账号在从服务器上面远程登录MySQL主库

你可能感兴趣的:(MySQL)