mysql双向同步热备份设置

 

做之前baidu了很多资料,虽然是设置好了

 

但过程很不顺利啊,把步骤写下来,以免时间久了又忘了....

 

1、环境描述。
   主机:192.168.0.167(A)
   主机:192.168.0.251(B)
   MYSQL 版本为5.1.11
2、授权用户。(本人比较懒,直接用的root 跳过这一步)
A:
mysql> grant replication slave,file on *.* to 'backup_251'@'192.168.0.251' identified
 by '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
B:
mysql> grant replication slave,file on *.* to 'backup_167'@'192.168.0.167' identified
 by '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
然后都停止MYSQL 服务器。

 

3、配置文件。
在两个机器上的my.cnf,在windows下面就是my.ini文件了 里面都开启二进制日志 。

 

找到[mysqld]这个结点,在下面增加以下内容


A:
user = mysql
log-bin=mysql-bin
server-id       = 1
binlog-do-db=test   (需要同步的库,如果要同步整个数据库,就把这句去掉)
binlog-ignore-db=mysql (不需要同步的库)
replicate-do-db=test
replicate-ignore-db=mysql
slave-skip-errors=all
sync_binlog=1
auto_increment_increment=2 (因为是双向,自动增加的id会有冲突,把步长改为2 初始设为1)
auto_increment_offset=1

B:
user = mysql
log-bin=mysql-bin
server-id       = 2
binlog-do-db=test   (需要同步的库,如果要同步整个数据库,就把这句去掉)
binlog-ignore-db=mysql  (不需要同步的库)
replicate-do-db=test
replicate-ignore-db=mysql
slave-skip-errors=all
sync_binlog=1
auto_increment_increment=2   (因为是双向,自动增加的id会有冲突,把步长改为2 初始设为2)
auto_increment_offset=2
至于这些参数的说明具体看手册。

4、重新启动MYSQL服务器。

 

5、进入MYSQL的SHELL。


A:
mysql> flush tables with read lock/G
Query OK, 0 rows affected (0.00 sec)

mysql> show master status/G
*************************** 1. row ***************************
            File: mysql-bin.000007
        Position: 528
    Binlog_Do_DB: test
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)

 

 

B:
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)

mysql> show master status/G
*************************** 1. row ***************************
            File: mysql-bin.000004
        Position: 595
    Binlog_Do_DB: test
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)
然后备份自己的数据,保持两个机器的数据一致。
方法很多。完了后看下一步。


6、在各自机器上执行CHANGE MASTER TO命令。
A:
mysql> change master to
    -> master_host='192.168.0.251',
    -> master_user='backup_251',
    -> master_password='123456',
    -> master_log_file='mysql-bin.000004',
    -> master_log_pos=595;
Query OK, 0 rows affected (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)


B:
mysql> change master to
    -> master_host='192.168.0.167',
    -> master_user='backup_167',
    -> master_password='123456',
    -> master_log_file='mysql-bin.000007',
    -> master_log_pos=528;
Query OK, 0 rows affected (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

7、查看各自机器上的IO进程和 SLAVE进程是否都开启。
A:

mysql> show processlist/G
*************************** 1. row ***************************
     Id: 2
   User: repl
   Host: 192.168.0.251:54475
     db: NULL
Command: Binlog Dump
   Time: 1590
  State: Has sent all binlog to slave; waiting for binlog to be updated
   Info: NULL
*************************** 2. row ***************************
     Id: 3
   User: system user
   Host:
     db: NULL
Command: Connect
   Time: 1350
  State: Waiting for master to send event
   Info: NULL
*************************** 3. row ***************************
     Id: 4
   User: system user
   Host:
     db: NULL
Command: Connect
   Time: 1149
  State: Has read all relay log; waiting for the slave I/O thread to update it
   Info: NULL
*************************** 4. row ***************************
     Id: 5
   User: root
   Host: localhost
     db: test
Command: Query
   Time: 0
  State: NULL
   Info: show processlist
4 rows in set (0.00 sec)

B:

mysql> show processlist/G
*************************** 1. row ***************************
     Id: 1
   User: system user
   Host:
     db: NULL
Command: Connect
   Time: 2130
  State: Waiting for master to send event
   Info: NULL
*************************** 2. row ***************************
     Id: 2
   User: system user
   Host:
     db: NULL
Command: Connect
   Time: 1223
  State: Has read all relay log; waiting for the slave I/O thread to update it
   Info: NULL
*************************** 3. row ***************************
     Id: 4
   User: root
   Host: localhost
     db: test
Command: Query
   Time: 0
  State: NULL
   Info: show processlist
*************************** 4. row ***************************
     Id: 5
   User: repl2
   Host: 192.168.0.167:50718
     db: NULL
Command: Binlog Dump
   Time: 1398
  State: Has sent all binlog to slave; waiting for binlog to be updated
   Info: NULL
4 rows in set (0.00 sec)

 

 

8、释放掉各自的锁,然后进行插数据测试。
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)

插入之前两个机器表的对比:
A:

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t11_innodb     |
| t22            |
+----------------+
B:

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t11_innodb     |
| t22            |
+----------------+
从A机器上进行插入
A:
mysql> create table t11_replicas
    -> (id int not null auto_increment primary key,
    -> str varchar(255) not null) engine myisam;
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t11_replicas(str) values
    -> ('This is a master to master test table');
Query OK, 1 row affected (0.01 sec)

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t11_innodb     |
| t11_replicas   |
| t22            |
+----------------+
3 rows in set (0.00 sec)

mysql> select * from t11_replicas;
+----+---------------------------------------+
| id | str                                   |
+----+---------------------------------------+
|  1 | This is a master to master test table |
+----+---------------------------------------+
1 row in set (0.00 sec)


现在来看B机器:

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t11_innodb     |
| t11_replicas   |
| t22            |
+----------------+
3 rows in set (0.00 sec)

mysql> select * from t11_replicas;
+----+---------------------------------------+
| id | str                                   |
+----+---------------------------------------+
|  1 | This is a master to master test table |
+----+---------------------------------------+
1 row in set (0.00 sec)

现在反过来从B机器上插入数据:
B:

mysql> insert into t11_replicas(str) values('This is a test 2');
Query OK, 1 row affected (0.00 sec)

mysql> select * from t11_replicas;
+----+---------------------------------------+
| id | str                                   |
+----+---------------------------------------+
|  1 | This is a master to master test table |
|  2 | This is a test 2                      |
+----+---------------------------------------+
2 rows in set (0.00 sec)
我们来看A
A:
mysql> select * from t11_replicas;
+----+---------------------------------------+
| id | str                                   |
+----+---------------------------------------+
|  1 | This is a master to master test table |
|  2 | This is a test 2                      |
+----+---------------------------------------+
2 rows in set (0.00 sec)

好了。现在两个表互相为MASTER。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值