当前位置:Gxlcms > 数据库问题 > MySQL 主从复制、主主复制、半同步复制

MySQL 主从复制、主主复制、半同步复制

时间:2021-07-01 10:21:17 帮助过:19人阅读

===============================================================================

概述:


===============================================================================

MySQL Replication:

  1.主从复制的目的和架构

Master/Slave(主/从)

  • Master: write/read

  • Slaves: read

主从复制的目的

  • 冗余:promte(提升为主),异地灾备

  • 负载均衡:转移一部分“读”请求;

  • 支援安全的备份操作;

  • 测试;

  ...

复制的方式:

同步复制:

  • 所谓的同步复制,意思是master的变化,必须等待slave-1,slave-2,...,slave-n完成后才能返回。这样,显然不可取,也不是MYSQL复制的默认设置。比如,在WEB前端页面上,用户增加了条记录,需要等待很长时间。

异步复制:

  • 如同AJAX请求一样。master只需要完成自己的数据库操作即可。至于slaves是否收到二进制日志,是否完成操作,不用关心。MYSQL的默认设置。

半同步复制:

  • master只保证slaves中的一个操作成功,就返回,其他slave不管。这个功能,是由google为MYSQL引入的。

主/从架构:

  • 一主多从;

  • 一从一主;

  • 级联复制;

  • 循环复制;

  • 双主复制;

  • 一从多主:每个主服务器提供不同的数据库;

 2.主从复制的工作流程

整体上来说,复制有3个步骤:

  • master将改变记录到二进制日志(binary log)中(这些记录叫做二进制日志事件,binary log events);

  • slave将master的binary log events拷贝到它的中继日志(relay log);

  • slave重做中继日志中的事件,将改变反映它自己的数据。

流程图:

技术分享

MySQL 复制配置

  1.主从复制配置步骤及操作演示

配置前注意事项:

  • 时间同步;

  • 复制的开始位置:

     ·从0开始;

     ·从备份中恢复到从节点后启动的复制;

  • 主从服务器mysqld程序版本不一致?

主服务器:

配置文件my.cnf

  • server_id=#

  • log_bin=log-bin

启动服务:

  • mysql> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO ‘USERNAME‘@‘HOST‘ IDENTIFIED BY ‘YOUR_PASSWORD‘;

  • mysql> FLUSH PRIVILEGES;

从服务器:

配置文件my.cnf

  • server_id=#

  • relay_log=relay-log 

启动服务:

  • mysql> CHANGE MASTER TO MASTER_HOST=‘HOST‘,MASTER_USER=‘USERNAME‘,MASTER_PASSWORD=‘YOUR_PASSWORD‘,MASTER_LOG_FILE=‘BINLOG‘,MASTER_LOG_POS=#;

  • mysql> START SLAVE [IO_THREAD|SQL_THREAD];

  • mysql> SHOW SLAVE STATUS;

 主从复制时应该注意的问题:

从服务设定为“只读”;

  • 在从服务器启动read_only,但仅对非SUPER权限的用户有效;

  • 阻止所有用户:

       mysql> FLUSH TABLES WITH READ LOCK;

      不建议使用,这样的话从服务器复制的数据就写不到磁盘中了

尽量确保复制时的事务安全

  • 在master节点启用参数:sync_binlog = ON   

       //每次提交立即将二进制事件从内存同步到二进制日志中

  • 如果用到的是InnoDB存储引擎:

     ·innodb_flush_logs_at_trx_commit=ON   //在事务提交时立即刷写日志

     ·innodb_support_xa=ON                 //支持分布式事务

从服务器意外中止时尽量避免自动启动复制线程

从节点:设置参数

  • sync_master_info=ON

  • sync_relay_log_info=ON

实验:配置实现主从复制

演示环境:

  • 两台CentOS 7的虚拟主机,一台做为mysql的主服务器,一台作为从服务器;

  • 两台服务器的MySQL的版本均为:mariadb-server-5.5.44-2.el7.centos.x86_64

操作步骤如下:

  1.首先在主从节点同步时间,编辑/etc/chrony,添加时间服务器即可,如下:

[root@slave ~]# vim /etc/chrony.conf 
 server 10.1.0.1 iburst
 
[root@master ~]# date;ssh 10.1.249.103 ‘date‘  //主从服务器同步时间如下
 Sun Dec  4 12:05:15 CST 2016
 Sun Dec  4 12:05:15 CST 2016

 2.编辑主节点服务器的配置文件/etc/my.cnf,并启动服务,如下:

[root@master ~]# vim /etc/my.cnf
  [mysqld]
  skip_name_resolve = ON
  innodb_file_per_table = ON
  log_bin=master-log          # 添加启动二进制日志文件
  server-id=1                 # server-id
#================================================================================================= 
[root@master ~]# systemctl start mariadb.service  # 启动服务
[root@master ~]# ss -tnl
State      Recv-Q Send-Q   Local Address:Port                  Peer Address:Port              
LISTEN     0      25                   *:514                              *:*                  
LISTEN     0      50                   *:3306                             *:*                  
LISTEN     0      128                  *:22                               *:*                  
LISTEN     0      128          127.0.0.1:631                              *:*                  
LISTEN     0      100          127.0.0.1:25                               *:*      
#==================================================================================================
[root@master ~]# ll /var/lib/mysql/    # 查看数据库目录
total 132012
-rw-rw---- 1 mysql mysql    16384 Dec  3 21:47 aria_log.00000001
-rw-rw---- 1 mysql mysql       52 Dec  3 21:47 aria_log_control
drwx------ 2 mysql mysql     4096 Dec  3 14:48 hellodb
-rw-r----- 1 mysql mysql 18874368 Dec  3 21:47 ibdata1
-rw-r----- 1 mysql mysql 50331648 Dec  4 14:14 ib_logfile0
-rw-r----- 1 mysql mysql 50331648 Dec  3 14:44 ib_logfile1
-rw-rw---- 1 mysql mysql 15530898 Dec  3 21:47 master-log.000001   # 二进制日志文件
-rw-rw---- 1 mysql mysql       40 Dec  4 14:14 master-log.index
drwx------ 2 mysql mysql       49 Dec  3 14:48 mydb
drwx------ 2 mysql mysql     4096 Dec  3 14:48 mysql
srwxrwxrwx 1 mysql mysql        0 Dec  4 14:14 mysql.sock
drwx------ 2 mysql mysql     4096 Dec  3 14:44 performance_schema
drwx------ 2 mysql mysql       19 Dec  3 14:44 test
drwx------ 2 mysql mysql       79 Dec  3 14:48 testdb
drwx------ 2 mysql mysql    36864 Dec  3 14:48 ultrax
drwx------ 2 mysql mysql     8192 Dec  3 14:48 zabbix
#==================================================================================================
MariaDB [(none)]> SHOW MASTER STATUS;   # 查看主节点二进制日志的记录文件及Position
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-log.000001 |      245 |              |                  |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

 2.编辑从节点服务器的配置文件/etc/my.cnf,并启动服务,如下:

[root@slave ~]# vim /etc/my.cnf
 [mysqld]
  skip-name-resolve = ON
  innodb-file-per-table = ON
  relay-log=relay-log         # 中继日志
  server-id=2                 # server-id=2
#===================================================================================================
[root@slave ~]# systemctl start mariadb.service    # 启动服务
[root@slave ~]# ss -tnl
State      Recv-Q Send-Q   Local Address:Port                  Peer Address:Port              
LISTEN     0      50                   *:3306                             *:*                  
LISTEN     0      128                  *:22                               *:*                  
LISTEN     0      128          127.0.0.1:631                              *:*                  
LISTEN     0      100          127.0.0.1:25                               *:*                  
LISTEN     0      128          127.0.0.1:6010                             *:*                  
LISTEN     0      128          127.0.0.1:6011                             *:*  
#===================================================================================================
[root@slave ~]# ll /var/lib/mysql/    # 查看其数据库目录这时还没有发现中继日志relay-log,只有当复制数据后才会有
total 36904
-rw-rw---- 1 mysql mysql    16384 Nov 17 15:50 aria_log.00000001
-rw-rw---- 1 mysql mysql       52 Nov 17 15:50 aria_log_control
-rw-rw---- 1 mysql mysql 27262976 Dec  3 15:14 ibdata1
-rw-rw---- 1 mysql mysql  5242880 Dec  3 15:14 ib_logfile0
-rw-rw---- 1 mysql mysql  5242880 Nov 17 15:23 ib_logfile1
drwx------ 2 mysql mysql       19 Oct 30 20:23 mydb
drwx------ 2 mysql mysql     4096 Oct 30 20:02 mysql
srwxrwxrwx 1 mysql mysql        0 Dec  3 15:14 mysql.sock
drwx------ 2 mysql mysql     4096 Oct 30 20:02 performance_schema
drwx------ 2 mysql mysql        6 Oct 30 20:02 test
drwx------ 2 mysql mysql     8192 Nov 17 00:35 zabbix_proxy

 3.在主服务器上授权一个可以连接复制的用户账号,如下:

MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO ‘repluser‘@‘10.1.%.%‘ IDENTIFIED BY ‘replpass‘;
Query OK, 0 rows affected (0.01 sec)

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

MariaDB [(none)]> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-log.000001 |      494 |              |                  |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

 4.在从服务器上设置连接主服务器的相关信息,并启动复制

MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST=‘10.1.252.153‘,
    -> MASTER_USER=‘repluser‘,
    -> MASTER_PASSWORD=‘replpass‘,
    -> MASTER_LOG_FILE=‘master-log.000001‘, # 复制主节点的二进制的日志文件  
    -> MASTER_LOG_POS=245;                  # 复制主节点二进制日志开始的位置
Query OK, 0 rows affected (0.07 sec)

MariaDB [(none)]> SHOW SLAVE STATUS\G    # 查看slave从节点的信息,发现还没有启动
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 10.1.252.153         # 连接的主节点主机
                  Master_User: repluser             # 用户
                  Master_Port: 3306                 # 端口
                Connect_Retry: 60                   # 重试时间默认为60s
              Master_Log_File: master-log.000001    # 复制的二进制日志文件
          Read_Master_Log_Pos: 245                  # 复制的二进制日志文件起始位置
               Relay_Log_File: relay-log.000001     # 中继日志的日志文件
                Relay_Log_Pos: 4                    # 中继日志的起始位置
        Relay_Master_Log_File: master-log.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: 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                  # 是否基于ssl进行复制
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 0                   # 主节点的ID号
1 row in set (0.00 sec)

#=========================================================================================

MariaDB [(none)]> START SLAVE;         # 启动复制线程
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> SHOW SLAVE STATUS\G  # 再次查看slave状态
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.1.252.153
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-log.000001
          Read_Master_Log_Pos: 245
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 530
        Relay_Master_Log_File: master-log.000001
             Slave_IO_Running: Yes     # 已经启动从主节点复制数据到中继日志中
            Slave_SQL_Running: Yes                       

人气教程排行