阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

MySQL 8.X 主从配置

232次阅读
没有评论

共计 3474 个字符,预计需要花费 9 分钟才能阅读完成。

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

 

测试验证略

✏️ 普通主从 配置参考

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-28发表,共计3474字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中