成都网站建设设计

将想法与焦点和您一起共享

mysql主从复制及维护--单主、双主

本文讲述了MySQL单主、双主模式的配置方法、注意事项、和维护的一些事项。

成都创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站设计、成都网站建设、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的曲阜网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!


1. 单主配置实例

1.1 整体流程:

  1. master与slave时间同步
  2. master开启二进制记录功能并授权一个特定的用户对二进制文件有复制权限
  3. slave开启中继日志功能,执行同步语句,开启同步功能。
    • 注 :版本号不一致时,master版本可比slave版本低

1.2 master端配置

编辑/etc/my.cnf,在mysqld配置段中添加以下内容(需重启服务)

server-id=1
log_bin=/data/mysql/logs/mysql-bin

启动数据库并做授权:

MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'node2'@'192.168.200.%' IDENTIFIED BY 'node2pass';
MariaDB [(none)]> FLUSH PRIVILEGES;

1.3 master端注意事项

  1. /data/mysql/logs目录需存在,且属主、属组为运行mysql的用户(因mysql需在该目录创建、写入文件)
  2. mysql-bin可自行定义。
  3. server-id后面的数值唯一,各节点之间不能重复。

1.4 slave端配置

编辑/etc/my.cnf,在mysqld配置段中添加以下内容(需重启服务)

server-id=2
relay-log=/data/mysql/logs/relay-bin

启动服务后设置主节点并启动

MariaDB [(none)]> change master to master_host='192.168.200.41',master_port=3306,master_user='node2',master_password='node2pass',master_log_file='mysql-bin.000003', master_log_pos =4;
MariaDB [(none)]> START SLAVE;
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.200.41
                  Master_User: node2
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 927
               Relay_Log_File: relay-bin.000003
                Relay_Log_Pos: 604
        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: 927
              Relay_Log_Space: 1783
              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

查看show slave status时,Slave_IO_Running和Slave_SQL_Running状态为yes时即成功。可在主节点创建库或者表查看从节点是否也有。

1.5 slave端注意事项

  1. /data/mysql/logs目录需存在,且属主、属组为运行mysql的用户(因mysql需在该目录创建、写入文件)
  2. relay-bin名字可自行定义。
  3. server-id后面的数值唯一,不能重复。
  4. change master时,若master_log_pos的位置不正确,可能会导致Slave I/O thread失败。错误日志如下:
    180603 16:46:49 [Note] Slave I/O thread: connected to master 'node2@192.168.200.41:3306',replication started in log 'mysql-bin.000003' at position 760
    180603 16:46:49 [ERROR] Error reading packet from server: binlog truncated in the middle of event; consider out of disk space on master; the first event 'mysql-bin.000003' at 760, the last event read from 'mysql-bin.000003' at 760, the last byte read from 'mysql-bin.000003' at 769. ( server_errno=1236)
    180603 16:46:49 [ERROR] Slave I/O: Got fatal error 1236 from master when reading data from binary log: 'binlog truncated in the middle of event; consider out of disk space on master; the first event 'mysql-bin.000003' at 760, the last event read from 'mysql-bin.000003' at 760, the last byte read from 'mysql-bin.000003' at 769.', Error_code: 1236

    解决方法:
    通过show binary logs查看当前的二进制文件有哪些,再通过show binlog events in 'BINLOG FILE NAME',找到正确的POS位置,并在change master时使用正确的POS位置。

    MariaDB [(none)]> show binary logs;
    +------------------+-----------+
    | Log_name         | File_size |
    +------------------+-----------+
    | mysql-bin.000001 |     30355 |
    | mysql-bin.000002 |   1038814 |
    | mysql-bin.000003 |       491 |
    +------------------+-----------+
    3 rows in set (0.00 sec)
    MariaDB [(none)]> show binlog events in 'mysql-bin.000003';
    +------------------+-----+-------------+-----------+-------------+--------------------------------------------------------------------------------------------------+
    | Log_name         | Pos | Event_type  | Server_id | End_log_pos | Info                                                                                             |
    +------------------+-----+-------------+-----------+-------------+--------------------------------------------------------------------------------------------------+
    | mysql-bin.000003 |   4 | Format_desc |         1 |         245 | Server ver: 5.5.56-MariaDB, Binlog ver: 4                                                        |
    | mysql-bin.000003 | 245 | Query       |         1 |         416 | grant replication slave,replication client on *.* to lxk@'192.168.200.%' identified by 'lxkpass' |
    | mysql-bin.000003 | 416 | Query       |         1 |         491 | flush privileges                                                                                 |
    +------------------+-----+-------------+-----------+-------------+--------------------------------------------------------------------------------------------------+
    3 rows in set (0.00 sec)

    如上代码:选择Pos(开始位置)或者End_log_pos(结束位置)都可以。


2. 双主配置实例

2.1 整体流程:

  1. 节点1与节点2时间同步
  2. 节点1与节点2开启二进制日志及中继日志功能并授权一个特定的用户对二进制文件有复制权限
  3. 节点1与节点2执行同步语句,开启同步功能。
    注 :
    版本号不一致时,master版本可比slave版本低

2.2 配置文件:

节点1:编辑/etc/my.cnf,在mysqld配置段添加以下内容保存退出:

log_bin=/data/mysql/logs/mysql-bin
relay_log=/data/mysql/logs/relay-log

注:要确保mysql对/data/mysql/logs目录有读写权限
节点2:编辑/etc/my.cnf,在mysqld配置段添加以下内容保存退出:

log_bin=/data/mysql/logs/mysql-bin
relay_log=/data/mysql/logs/relay-log

注:要确保mysql对/data/mysql/logs目录有读写权限

节点1 IP:192.168.200.41 节点2 IP:192.168.200.42

2.3 数据库操作:

节点1与节点2都进行同样的操作以授权一个用户:

MariaDB [(none)]> grant replication slave,replication client on *.* to lxk@'192.168.200.%' identified by 'lxkpass';
Query OK, 0 rows affected (0.00 sec)

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

查看要复制的Pos位置:
节点1:

MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |      491 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> show binlog events in 'mysql-bin.000003';
+------------------+-----+-------------+-----------+-------------+--------------------------------------------------------------------------------------------------+
| Log_name         | Pos | Event_type  | Server_id | End_log_pos | Info                                                                                             |
+------------------+-----+-------------+-----------+-------------+--------------------------------------------------------------------------------------------------+
| mysql-bin.000003 |   4 | Format_desc |         1 |         245 | Server ver: 5.5.56-MariaDB, Binlog ver: 4                                                        |
| mysql-bin.000003 | 245 | Query       |         1 |         416 | grant replication slave,replication client on *.* to lxk@'192.168.200.%' identified by 'lxkpass' |
| mysql-bin.000003 | 416 | Query       |         1 |         491 | flush privileges                                                                                 |
+------------------+-----+-------------+-----------+-------------+--------------------------------------------------------------------------------------------------+
3 rows in set (0.00 sec)

节点2:

MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |      491 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> show binlog events in 'mysql-bin.000003';
+------------------+-----+-------------+-----------+-------------+--------------------------------------------------------------------------------------------------+
| Log_name         | Pos | Event_type  | Server_id | End_log_pos | Info                                                                                             |
+------------------+-----+-------------+-----------+-------------+--------------------------------------------------------------------------------------------------+
| mysql-bin.000003 |   4 | Format_desc |         2 |         245 | Server ver: 5.5.56-MariaDB, Binlog ver: 4                                                        |
| mysql-bin.000003 | 245 | Query       |         2 |         416 | grant replication slave,replication client on *.* to lxk@'192.168.200.%' identified by 'lxkpass' |
| mysql-bin.000003 | 416 | Query       |         2 |         491 | flush privileges                                                                                 |
+------------------+-----+-------------+-----------+-------------+--------------------------------------------------------------------------------------------------+
3 rows in set (0.00 sec)

因为为新数据库,两个节点的pos值相同,都为491,生产环境根据需要调整,一般位置的选取都是从授权后的位置开始。

执行同步语句:

节点1:

MariaDB [(none)]> change master to master_host='192.168.200.42',master_user='lxk',master_password='lxkpass',master_log_file='mysql-bin.000003',master_log_pos=491;
Query OK, 0 rows affected (0.27 sec)

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

节点2:

MariaDB [(none)]> change master to master_host='192.168.200.41',master_user='lxk',master_password='lxkpass',master_log_file='mysql-bin.000003',master_log_pos=491;
Query OK, 0 rows affected (0.01 sec)

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

2.4 查看状态

        MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.200.41       #主节点地址
                  Master_User: lxk                          #主节点用户名
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 491
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 529
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes                  #IO线程开启
            Slave_SQL_Running: Yes              #SQL线程开启

只要执行结果中slave_IO_Runing和Slave_SQL_Runing状态为Yes,即表示从节点连接主节点成功并成功开启复制功能。

2.5 测试:

  • 节点2创建一个数据库名为testdb
MariaDB [(none)]> create database testdb;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| testdb             |
+--------------------+
5 rows in set (0.00 sec)
  • 节点1查看本地数据库
MariaDB [(none)]> show databases;                    
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| testdb             |
+--------------------+
5 rows in set (0.00 sec)

3 监控和维护:

  1. 清理日志
    • 随着服务的运行,日志文件会越来越多,越来越大。二进制最好复制到其它节点(二进制文件一定要保存),再在mysql命令行下删除。通过PURGE的操作会反应到.index文件中。
MariaDB [testdb]> show binary logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |     30355 |
| mysql-bin.000002 |   1038814 |
| mysql-bin.000003 |      1364 |
+------------------+-----------+
3 rows in set (0.00 sec)

MariaDB [testdb]> purge binary logs to 'mysql-bin.000003';          #清空至mysql-bin.000003的日志
Query OK, 0 rows affected (0.01 sec)

MariaDB [testdb]> show binary logs;         #再次查看日志
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000003 |      1364 |
+------------------+-----------+
1 row in set (0.00 sec)

[root@node1 ~]# ls /data/mysql/logs/            #查看二进制日志保存目录,000003之前的都已经删除
mysql-bin.000003  mysql-bin.index  relay-log.000001  relay-log.000002  relay-log.index
  1. 复制监控

    • 判断从服务器是否落后于主服务器,通过show slave status\G查看。
    • Seconds_Behind_Master: 0 #表示未落后
  2. 如何确定主从节点数据是否一致

    • 双主模型中极易发生,可用procona-tools中的pt-table-checksum
  3. 主从数据不一致时的修复方法
    • 停掉复制线程再重新启动,从服务器就会启动复制,达到与主节点一致。

名称栏目:mysql主从复制及维护--单主、双主
网站地址:http://chengdu.cdxwcx.cn/article/gehspi.html