前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MySQL 8.X 主从配置

MySQL 8.X 主从配置

作者头像
星哥玩云
发布2022-08-18 20:27:34
2000
发布2022-08-18 20:27:34
举报
文章被收录于专栏:开源部署开源部署

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.编辑配置文件

代码语言:javascript
复制
# 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.创建主从复制专用账户

代码语言:javascript
复制
# 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的日志及其偏移量

代码语言:javascript
复制
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.编辑配置文件

代码语言:javascript
复制
# 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进行配置连接

代码语言:javascript
复制
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状态

代码语言:javascript
复制
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

测试验证略

✏️ 普通主从配置参考

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Desc
  • Master端配置
  • Slave端配置
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档