一个主库上有2个数据库,分别把这两个数据库同步到不同的从服务器

(1)主从机
主机:10.10.54.86
从机1:10.10.54.85
从机2:10.10.54.87
数据库:a 同步到从1
数据库:b 同步到从2

(2)master配置,slave配置
master:
log-bin=master-bin
binlog_format=mixed
server-id       = 1

slave1:
log-bin=slave-bin
binlog_format=mixed
server-id       = 10


slave2:
log-bin=slave-bin
binlog_format=mixed
server-id       = 11

(3)在master创建复制用户并授予权限
mysql> grant replication slave on *.* to 'a'@'10.10.54.85' identified by 'a';
Query OK, 0 rows affected (0.00 sec)

mysql> grant replication slave on *.* to 'b'@'10.10.54.87' identified by 'b';
Query OK, 0 rows affected (0.02 sec)

(4)
mysql> show master status;
+-------------------+-----------+--------------+------------------+
| File              | Position  | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+-----------+--------------+------------------+
| master-bin.000014 | 379342358 |              |                  |
+-------------------+-----------+--------------+------------------+
1 row in set (0.00 sec)


(5)
从1:
[root@nan85 ~]# vim /etc/my.cnf 
replicate-do-db=a
从2:
[root@nan87 tmp]# vim /etc/my.cnf 
replicate-do-db=b

(6)
从1:
mysql> change master to master_host='10.10.54.86',master_user='a',master_password='a',master_log_file='master-bin.000014',master_log_pos=379342358;
Query OK, 0 rows affected (0.05 sec)

从2:
mysql> change master to master_host='10.10.54.86',master_user='b',master_password='b',master_log_file='master-bin.000014',master_log_pos=379342358;
Query OK, 0 rows affected (0.01 sec)

(6)
从1:
mysql> slave start;
Query OK, 0 rows affected (0.00 sec)

从2:
mysql> slave start;
Query OK, 0 rows affected (0.00 sec)

(7)
从1:
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.10.54.86
                  Master_User: a
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000014
          Read_Master_Log_Pos: 379342358
               Relay_Log_File: nan85-relay-bin.000002
                Relay_Log_Pos: 254
        Relay_Master_Log_File: master-bin.000014
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

从2:
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.10.54.86
                  Master_User: b
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000014
          Read_Master_Log_Pos: 379342358
               Relay_Log_File: nan87-relay-bin.000002
                Relay_Log_Pos: 254
        Relay_Master_Log_File: master-bin.000014
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

(8)测试
在主上:
mysql> drop database a;
Query OK, 0 rows affected (0.00 sec)

从1:显示数据库a删除
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| employees          |
| mysql              |
| performance_schema |
| test               |
+--------------------+

从2:数据库a还在
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| a                  |
| employees          |
| mysql              |
| performance_schema |
| test               |
+--------------------+

备注:经测试,在主机上对数据库a操作,在从1上数据库a有作用,而在主上对数据库b做操作,在从2上数据库a起作用。


你可能感兴趣的:(一个主库上有2个数据库,分别把这两个数据库同步到不同的从服务器)