0
点赞
收藏
分享

微信扫一扫

mysql双主同步的实现

ZGtheGreat 2021-09-19 阅读 29
日记本

#mysql双主同步的实现

##环境描述

``Centos7.3版本``

``192.190.10.153 安装keepalived、mysql``

``192.190.10.154 安装keepalived、mysql``

##环境部署

### 1.部署

``---------------192.190.10.153服务器操作记录---------------``

```linux

在my.cnf文件的[mysqld]配置区域添加下面内容:

[root@SpinfoServer ~]# vim /opt/Spinfo/mysql/my.cnf

server-id = 1   // mysql标示,不能出现重复

log-bin = mysql-bin // 开启log-bin二进制日志文件

sync_binlog = 1

binlog_ignore_db = mysql,information_schema // 不进行同步的数据库

binlog_checksum = none

binlog_format = mixed

auto-increment-increment = 2

auto-increment-offset = 1

slave-skip-errors = all

(注:修改完成后需重启mysql)

// 进入mysql

数据同步授权(iptables防火墙开启3306端口)这样I/O线程就可以以这个用户的身份连接到主服务器,并且读取它的二进制日志。

MariaDB [test]> grant replication slave,replication client on *.* to 'root'@'%' identified by "root";

Query OK, 0 rows affected (0.00 sec)

MariaDB [test]> flush privileges;

Query OK, 0 rows affected (0.00 sec)

最好将库锁住,仅仅允许读,以保证数据一致性;待主主同步环境部署后再解锁;

锁住后,就不能往表里写数据,但是重启mysql服务后就会自动解锁!

MariaDB [test]> flush tables with read lock;  //注意该参数设置后,如果自己同步对方数据,同步前一定要记得先解锁!

Query OK, 0 rows affected (0.00 sec)

查看log bin日志和pos值位置

MariaDB [test]> show master status;

+------------------+----------+--------------+--------------------------+

| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB        |

+------------------+----------+--------------+--------------------------+

| mysql-bin.000159 |      245 |              | mysql,information_schema |

+------------------+----------+--------------+--------------------------+

1 row in set (0.00 sec)

```

``---------------192.190.10.154服务器操作记录---------------``

```linux

在my.cnf文件的[mysqld]配置区域添加下面内容:

[root@SpinfoServer ~]# vim /opt/Spinfo/mysql/my.cnf

server-id = 2

log-bin = mysql-bin

sync_binlog = 1

binlog_ignore_db = mysql,information_schema

binlog_checksum = none

binlog_format = mixed

auto-increment-increment = 2

auto-increment-offset = 2

slave-skip-errors = all

(注:修改完成后需重启mysql)

// 进入mysql

MariaDB [test]> grant replication slave,replication client on *.* to 'root'@'%' identified by "root";

Query OK, 0 rows affected (0.00 sec)

MariaDB [test]> flush privileges;

Query OK, 0 rows affected (0.00 sec)

MariaDB [test]> flush tables with read lock;

Query OK, 0 rows affected (0.00 sec)

MariaDB [test]> show master status;

+------------------+----------+--------------+--------------------------+

| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB        |

+------------------+----------+--------------+--------------------------+

| mysql-bin.000162 |      245 |              | mysql,information_schema |

+------------------+----------+--------------+--------------------------+

1 row in set (0.00 sec)

```

``---------------192.190.10.153服务器做同步操作---------------``

```linux

MariaDB [test]> unlock tables;    //先解锁,将对方数据同步到自己的数据库中

MariaDB [test]> slave stop;

MariaDB [test]> change master to master_host='192.190.10.154',master_user='root',master_password='root',master_log_file='mysql-bin.000162',master_log_pos=245;

// 注:这里的host、log_file、log_pos都是另一台机器的

Query OK, 0 rows affected, 2 warnings (0.01 sec)

MariaDB [test]> start slave;

Query OK, 0 rows affected (0.01 sec)

查看同步状态,如下出现两个“Yes”,表明同步成功!

MariaDB [test]> show slave status \G;

*************************** 1. row ***************************

              Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.190.10.154

                  Master_User: root

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000162

          Read_Master_Log_Pos: 245

              Relay_Log_File: SpinfoServer-relay-bin.000007

                Relay_Log_Pos: 529

        Relay_Master_Log_File: mysql-bin.000162

            Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

            ......

            Master_Server_Id: 2

1 row in set (0.00 sec)

这样,master1就和master2实现了主从同步,即master1同步master2的数据。

```

``---------------192.190.10.154服务器做同步操作---------------``

```linux

MariaDB [test]> unlock tables;

MariaDB [test]> slave stop;

MariaDB [test]> change master to master_host='192.190.10.153',master_user='root',master_password='root',master_log_file='mysql-bin.000159',master_log_pos=245;

Query OK, 0 rows affected, 2 warnings (0.01 sec)

MariaDB [test]> start slave;

Query OK, 0 rows affected (0.01 sec)

MariaDB [test]> show slave status \G;

*************************** 1. row ***************************

              Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.190.10.153

                  Master_User: root

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000159

          Read_Master_Log_Pos: 245

              Relay_Log_File: SpinfoServer-relay-bin.000007

                Relay_Log_Pos: 529

        Relay_Master_Log_File: mysql-bin.000159

            Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

            ......

            Master_Server_Id: 1

1 row in set (0.00 sec)

这样,master2就和master1实现了主从同步,即master2也同步master1的数据。

```

### 2.验证

``-----------------主主同步效果验证---------------------``

```linux

1)在192.190.10.153数据库上写入新数据

MariaDB [test]> create table if not exists ha (

    -> id int(10) PRIMARY KEY AUTO_INCREMENT,

    -> name varchar(50) NOT NULL);

Query OK, 0 rows affected (0.04 sec)

MariaDB [test]> insert into ha values(1, "刘德华");

Query OK, 1 row affected (0.00 sec)

MariaDB [test]> insert into ha values(2, "周杰伦");

Query OK, 1 row affected (0.00 sec)

MariaDB [test]> select * from ha;

+----+-----------+

| id | name      |

+----+-----------+

|  1 | 刘德华    |

|  2 | 周杰伦    |

+----+-----------+

2 rows in set (0.00 sec)

2)在192.190.10.154上查看数据是否已经同步

MariaDB [test]> show tables;

+----------------+

| Tables_in_test |

+----------------+

| ha            |

| newtable      |

+----------------+

2 rows in set (0.01 sec)

MariaDB [test]> select * from ha;

+----+-----------+

| id | name      |

+----+-----------+

|  1 | 刘德华    |

|  2 | 周杰伦    |

+----+-----------+

2 rows in set (0.00 sec)

3)在192.190.10.154数据库上写入新数据

MariaDB [test]> insert into ha values(3, "林俊杰"),(4, "陈奕迅");

Query OK, 2 rows affected (0.01 sec)

Records: 2  Duplicates: 0  Warnings: 0

MariaDB [test]> select * from ha;

+----+-----------+

| id | name      |

+----+-----------+

|  1 | 刘德华    |

|  2 | 周杰伦    |

|  3 | 林俊杰    |

|  4 | 陈奕迅    |

+----+-----------+

4 rows in set (0.00 sec)

4)在192.190.10.153数据库上查看数据是否同步

MariaDB [test]> select * from ha;

+----+-----------+

| id | name      |

+----+-----------+

|  1 | 刘德华    |

|  2 | 周杰伦    |

|  3 | 林俊杰    |

|  4 | 陈奕迅    |

+----+-----------+

4 rows in set (0.00 sec)

(此处只做了添加数据时的验证,你也可以进行修改和删除的验证)

```

#####至此,Mysql主主同步环境已经实现。

举报

相关推荐

0 条评论