mysql双主复制搭建实验

时间:2022-09-15 08:48:35


一.基本信息

版本:10.1.12-MariaDB
binlog格式: ROW
事务级别:READ-COMMITTED
主库:10.16.24.107/10.16.24.108

二.搭建步骤

1.在10.16.24.108上安装mysql(步骤略),并作为第一个主库;
2.关闭10.16.24.108上的mysql,以冷copy方式搭建从库,并用change master指向主库(步骤略)
3.实现两个主库相互复制,需要更改相关配置:
a.在两个库的my.cnf文件中增加如下参数:log_slave_updates,并重启mysql实例,保证两个库应用relay log的日志操作也会记录到binlog中.
b.两个自增变量设置,并更改my.cnf文件
 设置107上的自增变量设置:
 set global auto_increment_increment=2 
 set global auto_increment_offset=1
 设置108上的自增变量设置:
 set global auto_increment_increment=2 
 set global auto_increment_offset=2
c.设置启动mysql实例时,禁止自动启动slave
  在my.cnf中加入skip-slave-start
4.107和108上都要建立复制帐号
grant replication slave on *.* to repl@'10.16.24%' identified by "replsafe";

5.在107上查看master信息
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000012 |     2433 |              |                  |
+------------------+----------+--------------+------------------+
在108上执行:
change master to
master_host='10.16.24.107',
master_port=3307,
master_user='repl',
master_password='replsafe',
master_log_file='mysql-bin.000012',
master_log_pos=2433;

6.启动slave ,查看状态
show salve status\G,显示同步正常,基本上没问题

三.数据同步测试
先在107上建一个库test107:
(product)root@localhost [(none)]> create database test107;
Query OK, 1 row affected (0.04 sec)
再查看108上是否有同步:
(product)root@localhost [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| log                |
| mysql              |
| performance_schema |
| test               |
| test107            |
| test2              |
| zengxuewen         |
+--------------------+
8 rows in set (0.00 sec)

同样在108上建一个库test108:
(product)root@localhost [(none)]> create database test108;

再查看107上是否有同步:
(product)root@localhost [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| log                |
| mysql              |
| performance_schema |
| test               |
| test107            |
| test108            |
| test2              |
| zengxuewen         |
+--------------------+
9 rows in set (0.00 sec)

四.主键冲突测试
 
107操作:
use test;
(product)root@localhost [test]> create table dm107 (col int not null auto_increment primary key);
Query OK, 0 rows affected (0.05 sec)

(product)root@localhost [test]> insert into dm107 values (null),(null),(null);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

(product)root@localhost [test]> select * from dm107;
+-----+
| col |
+-----+
|   1 |
|   3 |
|   5 |
+-----+
3 rows in set (0.00 sec)

108上操作:
(product)root@localhost [test]> create table dm108 (col int not null auto_increment primary key);
Query OK, 0 rows affected (0.01 sec)

(product)root@localhost [test]> insert into dm108 values (null),(null),(null);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

(product)root@localhost [test]> select * from dm108;
+-----+
| col |
+-----+
|   2 |
|   4 |
|   6 |
+-----+
3 rows in set (0.00 sec)

说明不存主键冲突问题。