MySQL 5.7之主从复制搭建

  • A+
所属分类:Blog Database MySQL

MySQL5.7版本发版已经好久了,相对也比较稳定了,刚好今天搭好虚拟机环境,下面就来搭一套基于MySQL5.7版本的主从复制吧。

复制过程

MySQL 5.7之主从复制搭建
  • master将变化的binary log events记录到binary log中;
  • slave将master的binary log events拷贝到它的relay log中;
  • slave把relay log events重新应用到数据库。

准备环境

首先要基于主库数据搭建一个从库,方法有多种,比较常见的是mysqldump备份,然后去从库导入。我这因为数据量小,直接停库把主库的datadir目录的数据全量copy过去了,然后删除auto.cnf,不然会报错。

修改参数

在主库和从库的my.cnf里加入下面的参数,然后启动数据库看有没有问题

--master my.cnf
[mysqld]
server-id=1563306
log-bin=master-bin
log-bin-index=master-bin.index
--slave my.cnf
[mysqld]
server-id=1573306
relay-log=slave-relay-bin
relay-log-index=slave-relay-bin.index
log_slave_updates = 1
read_only = 1

创建用户

#在master创建复制用户
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO repl_user@'192.168.11.157' IDENTIFIED BY '123456';

开启复制

--master
mysql> show global variables like '%server_id%' ;
+----------------+---------+
| Variable_name  | Value   |
+----------------+---------+
| server_id      | 1563306 |
| server_id_bits | 32      |
+----------------+---------+
2 rows in set (0.01 sec)

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 |     1337 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

--slave
--connect master
CHANGE master TO master_host='192.168.11.156',master_user='repl_user',master_password='123456',master_port=3306,
master_log_file='master-bin.000004',master_log_pos=1337 ;

mysql> show global variables like '%server_id%' ;
+----------------+---------+
| Variable_name  | Value   |
+----------------+---------+
| server_id      | 1573306 |
| server_id_bits | 32      |
+----------------+---------+
2 rows in set (0.01 sec)

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.11.156
                  Master_User: repl_user
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 1337
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 485
        Relay_Master_Log_File: mysql-bin.000004
             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: 1337
              Relay_Log_Space: 686
              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: 1563306
                  Master_UUID: 7989ffdc-6f77-11e6-b7aa-0800277ae4d5
             Master_Info_File: /data/mysql3306/data/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: 
1 row in set (0.00 sec)

我们看到Slave_IO_Running跟Slave_SQL_Running都是Yes表明从库已经搭建好了,接下来试试在主库创建库跟表,看看从库有没有同步过来呢?

小结:要理解Master_Log_File,Read_Master_Log_Pos,Relay_Master_Log_File,Exec_Master_Log_Pos这几个参数的区别,对MySQL的主从切换、故障恢复有很大的帮助。

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: