[Mysql] 主从复制环境搭建步骤详解

Posted Jan丶X

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Mysql] 主从复制环境搭建步骤详解相关的知识,希望对你有一定的参考价值。

一.架构图


软件版本:Linux、mysql Server version: 5.6.15

2.2 重启Mysql


 ***************************************主机操作******************************************

2.3 在主机上创建master用户,从机创建slave用户,注意,master用户需要有赋予slave复制的权限

2.4 登录master用户

GRANT REPLICATION SLAVE ON *.* to 'slave'@'%' identified by 'jan';
flush privileges;

2.5 停止对数据库的DDL和DML操作或lock起所有的数据库表,记录当前master的状态

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

***************************************从机操作******************************************

2.6 执行以下语句指定master的IP,日志,日志记录位置信息,并启动slave

mysql> stop slave;
Query OK, 0 rows affected (0.03 sec)
 
mysql> change master to
    ->       master_host='192.168.232.128',
    ->       master_user='master',
    ->       master_password='jan',
    ->       master_log_file='mysql-bin.000005',
    ->       master_log_pos=120;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
 
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

2.7 查看slave状态

mysql> show slave status\\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.232.128
                  Master_User: master
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000005
          Read_Master_Log_Pos: 120
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 283
        Relay_Master_Log_File: mysql-bin.000005
             Slave_IO_Running: Yes   //此状态必须为YES
            Slave_SQL_Running: Yes   //此状态必须为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: 120
              Relay_Log_Space: 460
              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: 2b913f38-8dd4-11e5-aa5d-000c29271917
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           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
1 row in set (0.00 sec)

从上述信息可知已经成功搭建主从库并联通了,但在搭建过程中遇到一些错误:

Slave_IO_Running:Yes

Slave_SQL_Running:No

解决方法:master用户没有配置权限给slave用户,授权即可

若还是无法解决问题,可查找binlog日志

show binlog event in 'master-bin.000005'\\G ;



三.测试



3.1 创建数据库

master:

mysql> create database testdb;
Query OK, 1 row affected (0.00 sec)

slave:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| testdb             |
+--------------------+
4 rows in set (0.00 sec)

3.2 更新数据

master:

mysql> create table a(id int(10),name varchar(20));
Query OK, 0 rows affected (0.02 sec)
 
mysql> insert into a values(1,'jan'),(2,'lulu');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

slave:

mysql> select * from a;
+------+------+
| id   | name |
+------+------+
|    1 | jan  |
|    2 | lulu |
+------+------+
2 rows in set (0.01 sec)




以上是关于[Mysql] 主从复制环境搭建步骤详解的主要内容,如果未能解决你的问题,请参考以下文章

生产环境配置mysql主从复制

linux Mysql 主从复制 原理介绍和步骤详解

Linux-中级篇搭建MySQL的主从复制

基于Docker的Mysql主从复制搭建

mysql主从复制环境搭建

Mysql实现主从复制(环境搭建)