Mysql8主从复制搭建

我们知道Mysql的主从同步机制实现,还是先来一张主从同步的原理图(这里直接使用最经典的那一张),方便搭建已经遇到问题后的解决,这里忽略讲解binlog的格式,以及write log first等原理。

 实现一主多从读写分离的情况下,不推荐使用Docker搭建mysql,这里使用了官方Compressed TAR Archive搭建Mysql单节点的方式,详细可以参考:Mysql8 CentOS7 Compressed TAR Archive安装,这里默认已经搭建了一个主,根据需求搭建了多个从节点,并且已经设置好了 root的账号密码,继续:

一、主节点库

创建用户同步的数据的mysql用户、设置权限:

CREATE USER 'slave'@'%' IDENTIFIED BY 'Slave123';

GRANT REPLICATION SLAVE ON *.* To 'slave'@'%' WITH GRANT OPTION;

flush privileges;

 修改配置文件/etc/my.cnf ,增加下面的行(设置群服务id,设置binlog):

# bin log日志
log-bin=mysql-bin.log
# 服务id
server-id=1
#指定同步的数据库,如果不指定则同步全部数据库
# binlog-do-db=kevin-demo
#ignore-db
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=sys
binlog-ignore-db=performance_schema

重启mysql: service mysqld restart;

登陆mysql并且执行命令查看状态:show master status;

二、从节点库

修改配置文件 /etc/my.cnf, 配置添加如下:

# 指定bin log文件
log-bin=mysql-bin.log
# 服务id, 多个从节点挨着往上加
server-id=2

 重启mysql: service mysqld restart;

根据需要是否将从库设置为只读:flush tables with read lock;

根据上面 show master status现在的日志文件和位置设置下面的命令:

change master to master_host='47.94.140.208',master_user='slave',master_password='Slave123' ,MASTER_PORT=3306, master_log_file='mysql-bin.000001',master_log_pos=155;

SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1;

start slave;

使用同步命令查询: show slave status \G

mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 47.94.140.208
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 155
               Relay_Log_File: iZ2zehtz0jxwu2xvn85bmgZ-relay-bin.000002
                Relay_Log_Pos: 322
        Relay_Master_Log_File: mysql-bin.000001
             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: 1
          Exec_Master_Log_Pos: 155
              Relay_Log_Space: 548
              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: 1
                  Master_UUID: 42c2b936-c510-11ec-9696-00163e348a08
             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: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         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)

三、验证同步结果

一定保证slave数据库的 io线程和sql线程都在正常的工作,如下:

 如果发现从库sql线程没有执行,此时就需要检查相关库不一样的地方,或者将主库进行dump,并在从库执行保证一致,再在slave执行: stop slavestart slave

测试同步:在主节点上创建一个数据库如 : create table ...;

在从库上查询数据库是否已经同步:show tables;

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值