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


测试验证略

✏️ 普通主从配置参考

人生是条无名的河,是浅是深都要过; 人生是杯无色的茶,是苦是甜都要喝; 人生是首无畏的歌,是高是低都要唱。