mysql双主复制实践

mysql双主复制实践

一、原理介绍

要讲清楚mysql的双主复制,先说一下主从复制的原理:

主点只负责写,节点只负责读,当用户写请求到达后,被调到主节点,写操作完成后将记录在主节点的二进制日志中,从节点启动I/O线程从主节点处请求主节点的二进制日志,主节点通过dump线程接收到此请求后将二进制日志发送给从节点。而后从节点将收到的二进制日志通过SQL线程在中继日志中,实现事件重放。

主节点:

启动dump线程:为每个从节点启动一个dump线程,用于发送二进制文件

从节点:

启动I/O线程:从主节点请求二进制日志,并保存在中继日志中

启动SQL线程:从中继日志中读取日志事件,实现重放

而什么是双主呢?双主即两个mysql服务器互为主从关系,即在A服务器眼里自己是主服务器而B是从服务器,在B服务器眼里自己是主服务器而A是从服务器。

接下来以两台服务器来演示整个配置过程

A服务器:node1 192.168.50.138

B服务器:node2 192.168.50.139

二、配置A服务器

配置步骤:

1、配置唯一serverID,启用二进制日志,启用中继日志

[root@node1 ~]# vim /etc/my.cnf

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
skip_name_resolve = on      #跳过主机名反解
innodb_file_per_table = on  #没张表使用一个表空间来存储
log_bin=mysql-bin           #开启二进制日志,命名为mysql-bin
server_id = 1               #serverID号为1
relay_log = relay-log       #开启中继日志,命名为relay-log
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

2、创建具有相应权限的用户账号

权限:REPLICATION SLAVE      ,     REPLICATION CLIENT

MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'repl-node1'@'192.168.50.139' IDENTIFIED BY '123456';

三、配置B服务器

1、配置唯一serverID,启用二进制日志,启用中继日志

[root@node2 ~]# vim /etc/my.cnf

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
skip_name_resolve = on
innodb_file_per_table = on
log-bin=mysql-bin
server_id = 2                #注意一定要是全局唯一的serverID
relay_log = relay-log

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

2、创建具有相应权限的用户账号

权限:REPLICATION SLAVE      ,     REPLICATION CLIENT

MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'repl-node2'@'192.168.50.138' IDENTIFIED BY '123456';

四、配置互为主从

分别查看A、B主机的master状态,和起始位置

A:

MariaDB [(none)]> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |      425 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

B:

MariaDB [(none)]> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |      425 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

将A作为从节点指向B为主节点

MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.50.139', MASTER_USER='repl-node2', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin.000003', MASTER_LOG_POS = 425;
Query OK, 0 rows affected (0.08 sec)

将B作为从节点指向A为主节点

MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.50.138', MASTER_USER='repl-node1', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin.000003', MASTER_LOG_POS = 425;

现在来看看ASLAVE状态

A:

MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.50.139   #指明主节点是node2
                  Master_User: repl-node2
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003  #指明主节点的二进制文件
          Read_Master_Log_Pos: 425               #事件起始位置
               Relay_Log_File: relay-log.000001  #从节点自己的中继日志
                Relay_Log_Pos: 4                 #中继日志起始位置
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: No                #还未启动IO线程
            Slave_SQL_Running: No                #还未启动SQL线程
              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: 425
              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

分别开启各复制线程

MariaDB [(none)]> START SLAVE;    #A和B中都执行
Query OK, 0 rows affected (0.00 sec)

现在看看A中有哪些数据

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+

在A中创建个mydb数据库,在数据库中创建student表,看B中能否实现同步复制

MariaDB [(none)]> CREATE DATABASE mydb;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> USE mydb;
Database changed
MariaDB [mydb]> CREATE TABLE student (name VARCHAR(30) NOT NULL,class INT NOT NULL);
Query OK, 0 rows affected (0.01 sec)
MariaDB [mydb]> INSERT INTO student VALUES ('xiaoming',10),('xiaogouzi',100);
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

在B中进行查看

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

MariaDB [(none)]> USE mydb
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [mydb]> SELECT * FROM student;
+-----------+-------+
| name      | class |
+-----------+-------+
| xiaoming  |    10 |
| xiaogouzi |   100 |
+-----------+-------+
2 rows in set (0.00 sec)

B中已经能够看到从A中执行的操作,现在从B中插入一行数据,看A中是否能同步到

MariaDB [mydb]> INSERT INTO student VALUES ('laowang',50);
Query OK, 1 row affected (0.00 sec)

在A中查看

MariaDB [mydb]> SELECT * FROM student;
+-----------+-------+
| name      | class |
+-----------+-------+
| xiaoming  |    10 |
| xiaogouzi |   100 |
| laowang   |    50 |
+-----------+-------+
3 rows in set (0.00 sec)

成功!!!


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值