首页 » 数据库 » MySQL从入门到删库之主从复制

MySQL从入门到删库之主从复制

2020-01-02 Darius 数据库

所有的关系型数据库都存在一个通病性能差,在企业中如果用户量特别大,将所有的数据都存放在一台服务器上,其性能是远远达不到要求的。所以需要使用一些手段来解决其性能的问题。 提升性能的方式有向上扩展以及向外扩展
向上扩展(Scale Up):使用更新更好的硬件,但硬件在怎么更新也有其性能的极限。盲目的向上扩展无法结局根本的问题
向外扩展(Scale Out):就是使用多台机器分摊压力来提供服务
主从复制就是拿多个数据库服务器,组合成一个服务器的集合对外共同服务实现性能的提升,逻辑上使用的时对外扩展的方式(Scale out)来提升服务器的性能。

新主机搭建主从复制搭建

服务器类型 ip地址
192.168.73.133
192.168.73.145

主服务器操作

1.在主服务器上启用二进制日志

[root@localhost ~]# vim /etc/mysql/my.cnf 
log-bin=/data/bin/mysql-bin
binlog-format=row
server-id=1

2.创建二进制日志目录

[root@localhost ~]# mkdir /data/bin
[root@localhost ~]# chown -R mysql.mysql /data/bin

3.重启服务

[root@localhost ~]# service mysqld restart
Restarting mysqld (via systemctl):                         [  OK  ]

4.创建一个用来让从服务器复制数据的账号

MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO repluser@'192.168.73.%' IDENTIFIED BY 'centos';
Query OK, 0 rows affected (0.00 sec)

5.查看主服务器正在使用的二进制日志

MariaDB [(none)]> SHOW MASTER LOGS;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |       515 |    #当前二进制文件及位置需要记录,从服务器设置时需要
+------------------+-----------+
1 row in set (0.00 sec)

从服务器操作

1.修改配置文件

server-id       = 2     #server-id改为和主服务器不同
read-only               #设置为只读
#log-bin=mysql-bin      #将二进制日志关闭

2.启动MySQL服务器

[root@localhost ~]# service mysqld restart
Starting mysqld (via systemctl):                           [  OK  ]

3.关联主服务器

MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.73.133',MASTER_USER='repluser',MASTER_PASSWORD='centos',MASTER_PORT=3306,MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=515;
Query OK, 0 rows affected (0.01 sec)

4.查看从服务器状态

MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.73.133
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 515
               Relay_Log_File: localhost-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: No
            Slave_SQL_Running: No
              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: 515
              Relay_Log_Space: 256
              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: NULL
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: 0
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
                   Using_Gtid: No
                  Gtid_IO_Pos: 
      Replicate_Do_Domain_Ids: 
  Replicate_Ignore_Domain_Ids: 
                Parallel_Mode: conservative
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: 
1 row in set (0.00 sec)

ERROR: No query specified

5.启动线程

MariaDB [(none)]> START SLAVE;
Query OK, 0 rows affected (0.00 sec)

测试

在主服务器上导入一个hellodb数据库

[root@localhost ~]# mysql < hellodb_innodb.sql 

从服务器上查看是否同步成功

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| hellodb            |      #已经有hellodb库
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

主服务器已有数据的情况下搭建主从

拥有一台已经有数据的mysql服务器,追加一台从服务器

服务器 IP地址
Master 192.168.73.148
Slave 192.168.73.149

Master中的数据

[root@localhost ~]# mysql -e "SHOW DATABASES;"
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hellodb            |
| mysql              |
| performance_schema |
| test               |
| test1              |
+--------------------+

配置主服务器

1.修改主服务器配置文件

[root@localhost ~]# vim /etc/my.cnf
[mysqld]
log-bin=/data/bin/mysql-bin
binlog-format=row
[root@localhost ~]# systemctl restart mariadb

2.创建复制账号

[root@localhost ~]# mysql -e "GRANT REPLICATION SLAVE ON *.* TO 'repluser'@'192.168.73.%' IDENTIFIED BY 'centos';"

3.备份主节点中的所有数据

[root@localhost ~]# mysqldump -A --single-transaction -F --master-data=1 > /data/all.sql
[root@localhost ~]# vim /data/all.sql

4.将备份数据传送给从节点

[root@localhost ~]# scp /data/all.sql 192.168.73.149:/data

配置从服务器

1.修改配置文件

[root@localhost ~]# vim /etc/my.cnf
[mysqld]
server-id=2
read-only

2.清空MySQL数据库

[root@localhost ~]# rm -rf /var/lib/mysql/*

3.修改备份数据将chang master to 加入文件中

CHANGE MASTER TO
  MASTER_HOST='192.168.73.148',
  MASTER_USER='repluser',
  MASTER_PASSWORD='centos',
  MASTER_PORT=3306,
  MASTER_LOG_FILE='mysql-bin.000005',
  MASTER_LOG_POS=245;

4.启动MySQL服务,并导入备份

[root@localhost ~]# mysql < /data/all.sql 

5.查看从节点状态

MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.73.148
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000005
          Read_Master_Log_Pos: 245
               Relay_Log_File: mariadb-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000005
             Slave_IO_Running: No
            Slave_SQL_Running: No
              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: 245
              Relay_Log_Space: 245
              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: NULL
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: 0
1 row in set (0.00 sec)

6.启动线程

MariaDB [(none)]> START SLAVE;
Query OK, 0 rows affected (0.00 sec)

7.再次查看从节点状态,复制的2个线程已经启动

MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.73.148
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000005
          Read_Master_Log_Pos: 245
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 529
        Relay_Master_Log_File: mysql-bin.000005
             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: 245
              Relay_Log_Space: 825
              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
1 row in set (0.00 sec)

测试

主节点删test1库

MariaDB [(none)]> DROP DATABASE  test1;
Query OK, 1 row affected (0.01 sec)

从节点查看是否同步

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hellodb            |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)      #已经没有test1库

MySQL主从复制出错的解决方法

主从复制中若是出现错误可以通过几个方法来进行解决
1.如果主从复制时发生了主键冲突,从而阻止了主从复制,可以使用sql_slave_skip_counter这个变量来忽略错误将其排除
2.如果发生了较大的错误,可以考虑使用reset slave的方法重新配置从服务器来恢复错误
以下演示如何使用这两种方法解决错误,及相关操作的详细说明

reset slave的使用方法

环境准备搭建主从同步

主节点配置

1.修改配置文件

[root@Master ~]# vim /etc/my.cnf
[mysqld]
log-bin=/data/bin/mysql-bin
binlog-format=row
server-id=1

2.创建二进制日志目录

[root@Master ~]# mkdir /data/bin
[root@Master ~]# chown -R mysql.mysql /data/bin

3.启动mysqld服务

[root@Master ~]# systemctl start mariadb

4.查看主服务器日志位置

[root@Master ~]# mysql -e "SHOW MASTER LOGS;"
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |     26753 |
| mysql-bin.000002 |    921736 |
| mysql-bin.000003 |       245 |
+------------------+-----------+

5.创建一个用来复制数据的账户

[root@Master ~]# mysql -e "GRANT REPLICATION SLAVE ON *.* TO 'repluser'@'192.168.73.%' IDENTIFIED BY 'centos';"

从节点配置

1.修改配置文件

[root@Slave ~]# vim /etc/my.cnf
[mysqld]
read-only
server-id=2

2.启动服务

[root@Slave ~]# systemctl start mariadb

此处开始构建错误配置
以下所有CHANGE MASTER TO配置均为错误
3.配置CHANGE MASTER TO

MariaDB [(none)]> CHANGE MASTER TO
    ->   MASTER_HOST='master2.mycompany.com',
    ->   MASTER_USER='replication',
    ->   MASTER_PASSWORD='bigs3cret',
    ->   MASTER_PORT=3306,
    ->   MASTER_LOG_FILE='master2-bin.001',
    ->   MASTER_LOG_POS=4,
    ->   MASTER_CONNECT_RETRY=10;
Query OK, 0 rows affected (0.00 sec)

4.查看下SLAVE STATUS

MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: master2.mycompany.com
                  Master_User: replication
                  Master_Port: 3306
                Connect_Retry: 10
              Master_Log_File: master2-bin.001
          Read_Master_Log_Pos: 4
               Relay_Log_File: mariadb-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: master2-bin.001
             Slave_IO_Running: No
            Slave_SQL_Running: No
       ...以下省略...

5.启动复制线程

MariaDB [(none)]> START SLAVE;

6.再次查看SLAVE STATUS

MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
               Slave_IO_State: Connecting to master
                  Master_Host: master2.mycompany.com
                  Master_User: replication
                  Master_Port: 3306
                Connect_Retry: 10
              Master_Log_File: master2-bin.001
          Read_Master_Log_Pos: 4
               Relay_Log_File: mariadb-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: master2-bin.001
             Slave_IO_Running: Connecting
            Slave_SQL_Running: Yes
       ...以下省略...

线程已经正常启动
主服务器导入数据进行测试

[root@Master ~]# mysql < hellodb_innodb.sql 
[root@Master ~]# mysql -e "SHOW DATABASES;"
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hellodb            |
| mysql              |
| performance_schema |
| test               |
+--------------------+

从服务器查看是否同步(CHANGE MASTER TO信息不对怎么可能同步)

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

以下为错误解决方法

由于错误发生在CHANGE MASTER TO所以此处将CHANG MASTER TO部分纠正就行
1.首先将从服务器的复制线程停止

MariaDB [(none)]> STOP SLAVE;
Query OK, 0 rows affected (17.48 sec)

2.将从服务器上的SLAVE信息重置

MariaDB [(none)]> RESET SLAVE;
Query OK, 0 rows affected (0.01 sec)

3.重新输入正确的CHANGE MASTER TO信息

MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.73.110',MASTER_USER='repluser',MASTER_PASSWORD='centos',MASTER_PORT=3306,MASTER_LOG_FILE='mysql-bin.000003',MASTER_LOG_POS=245;
Query OK, 0 rows affected (0.01 sec)

4.查看SLAVE STATUS;

MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.73.110
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 10
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 245
               Relay_Log_File: mariadb-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: No
            Slave_SQL_Running: No
              Replicate_Do_DB: 
# 此处信息已经改为正确

5.重新启动线程

MariaDB [(none)]> START SLAVE;
Query OK, 0 rows affected (0.00 sec)

6.再次查看SLAVE STATUS;

MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.73.110
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 10
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 7384     #已经有数据复制过来了
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 7668
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
# IO和SQL线程已经启动

7.查看下从节点内的库是否已经同步

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hellodb            |          #hellodb库已经从主节点中复制过来了
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.01 sec)

其他说明:
如果生产中,发生主从节点之间的数据偏差较大并且迟迟不能同步,可以考虑将从服务器全部清除从新配置从服务器。具体配置方法可以参考https://blog.51cto.com/11886307/2390636

关于sql_slave_skip_counter的使用方法

当发生主键冲突时,从服务器会卡在出错的位置不再进行服务,此种错误一般会出现在主主复制或者从服务器已经占用了某条记录的情况下,此时可以使用此选项来忽略错误。

构建错误

此处继续沿用刚才的主从复制环境
1.在从服务器上创建一条记录

MariaDB [(none)]> INSERT hellodb.teachers VALUE (5,'Li Xiaolong',30,'M');
Query OK, 1 row affected (0.00 sec)

2.在主服务器上也创建一条主键相同的记录

MariaDB [(none)]> INSERT hellodb.teachers VALUE (5,'Xiao Yan',20,'M');
Query OK, 1 row affected (0.00 sec)

3.返回从节点查看SLAVE STATUS

MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.73.110
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 10
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 7576
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 7668
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: No
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 1062
                   Last_Error: Could not execute Write_rows event on table hellodb.teachers; Duplicate entry '5' for key 'PRIMARY', Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY; the event's master log mysql-bin.000003, end_log_pos 7549
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 7384
              Relay_Log_Space: 8156
              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: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 1062
               Last_SQL_Error: Could not execute Write_rows event on table hellodb.teachers; Duplicate entry '5' for key 'PRIMARY', Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY; the event's master log mysql-bin.000003, end_log_pos 7549
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
1 row in set (0.00 sec)

4.从节点已经出错,在主节点继续添加记录

MariaDB [(none)]> INSERT hellodb.teachers VALUE (6,'Xiao Xuner',20,'M');
Query OK, 1 row affected (0.00 sec)

5.此时从节点已经不会再继续从主节点复制信息

MariaDB [(none)]> SELECT * FROM hellodb.teachers WHERE tid>4;
+-----+-------------+-----+--------+
| TID | Name        | Age | Gender |
+-----+-------------+-----+--------+
|   5 | Li Xiaolong |  30 | M      |   #此为刚才从节点添加的记录
+-----+-------------+-----+--------+
1 row in set (0.00 sec)

排错

1.使用sql_slave_skip_counter变量忽略错误

MariaDB [(none)]> SET GLOBAL sql_slave_skip_counter=1;
Query OK, 0 rows affected (0.00 sec)

2.停止线程并重新启动

MariaDB [(none)]> STOP SLAVE;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> START SLAVE;
Query OK, 0 rows affected (0.00 sec)

3.查看slave status状态,此时已经没有报错的信息

MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.73.110
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 10
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 7770
               Relay_Log_File: mariadb-relay-bin.000003
                Relay_Log_Pos: 529
        Relay_Master_Log_File: mysql-bin.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: 7770
              Relay_Log_Space: 8634
              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
1 row in set (0.00 sec)

4.在从服务器上查看teachers表

MariaDB [(none)]> SELECT * FROM hellodb.teachers WHERE tid>4;
+-----+-------------+-----+--------+
| TID | Name        | Age | Gender |
+-----+-------------+-----+--------+
|   5 | Li Xiaolong |  30 | M      |
|   6 | Xiao Xuner  |  20 | M      |    #此时刚才在主节点插入的6号记录已经复制过来
+-----+-------------+-----+--------+
2 rows in set (0.00 sec)
说点什么
好耶,沙发还空着ヾ(≧▽≦*)o
Loading...
Prev Post Next Post
已跳转到上次阅读的位置,从头阅读?