MySQL 8.X 主從配置


Desc

✏️ 本文以基於GTID的復制示例

GTID(Global Transaction ID)是對於一個已提交事務的唯一編號,並且是一個全局(主從復制)唯一的編號。

它的官方定義如下:

GTID = source_id :transaction_id

7E11FA47-31CA-19E1-9E56-C43AA21293967:29

什么是sever_uuid,和Server-id 區別?

核心特性: 全局唯一,具備冪等性

GTID核心參數

重要參數:

gtid-mode=on --啟用gtid類型,否則就是普通的復制架構

enforce-gtid-consistency=true --強制GTID的一致性

log-slave-updates=1 --slave更新是否記入日志

MySQL 8.x版本安裝參考

 

Master端配置

✏️ 1.編輯配置文件

# vim /etc/my.cnf
[mysqld]

datadir=/data/mysql
socket=/var/lib/mysql/mysql.sock

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

port=3306
server-id=100
gtid-mode=on    #啟用gtid類型,否則就是普通的復制架構
log-slave-updates    #slave更新是否記入日志
enforce-gtid-consistency    #強制GTID的一致性
log_bin=/data/mysql/mybinlog
max_connections=1000

✏️ 2.創建主從復制專用賬戶

# mysql -uroot -pNewPass#123

mysql> create user 'slave'@'192.168.3.%' identified with mysql_native_password by 'Mslave#q818';
Query OK, 0 rows affected (2.23 sec)

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

✏️ ​3.查看master的日志及其偏移量

mysql> show master status;
+-----------------+----------+--------------+------------------+------------------------------------------+
| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        |
+-----------------+----------+--------------+------------------+------------------------------------------+
|mybinlog.000003  |     195  |              |                  | 11cdb65b-e125-11ea-904c-fa39b0d35500:1-6 |
+-----------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)

 

Slave端配置

✏️ 1.編輯配置文件

# vim /etc/my.cnf
[mysqld]

datadir=/data/mysql
socket=/var/lib/mysql/mysql.sock

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

port=3306
server-id=106
gtid-mode=on
log-slave-updates
enforce-gtid-consistency
skip-slave-start
relay_log=/data/mysql/relay.log
max_connections=1000
read-only=1

✏️ 2.進入mysql進行配置連接

mysql> change master to master_host='192.168.3.95', \
      master_user='slave', \
      master_password='Mslave#q818', \
      master_port=3306, \
      master_auto_position=1;

mysql> start slave;

✏️ 3.查看slave狀態

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.3.95
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mybinlog.000003
          Read_Master_Log_Pos: 195
               Relay_Log_File: relay.000003
                Relay_Log_Pos: 407
        Relay_Master_Log_File: mybinlog.000003
             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: 195
              Relay_Log_Space: 2379
              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: 100
                  Master_UUID: 11cdb65b-e125-11ea-904c-fa39b0d35500
             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: 11cdb65b-e125-11ea-904c-fa39b0d35500:1-6
            Executed_Gtid_Set: 11cdb65b-e125-11ea-904c-fa39b0d35500:1-6,
8ee47a46-e12a-11ea-a237-fa55fa771000: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 (0.00 sec)

ERROR: 
No query specified

 

測試驗證略

✏️ 普通主從配置參考


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM