mysql不同数据库同步到不同服务器

软件版本
系统版本:CentOS 6.4_x86_64
MysqlServer version: 5.5.30-log Source distribution
一个主库上有两个数据库,分别把这两个数据库同步到不同的服务器
解析:
Replication slave 1 should use --replicate-wild-do-table=databaseA.%.
Replication slave 2 should use --replicate-wild-do-table=databaseB.%.
1.规划主从机器和网络
master:10.10.54.57
slave1:10.10.54.56
slave2:10.10.54.58
2.修改master配置文件
vim /etc/my.cnf
=============================
[mysqld]
//为innodb的每个表单独创建表空间
innodb_file_per_table=1
log-bin=master-bin
binlog_format=row
server-id= 1 
===============================
##重启:/etc/init.d/mysqld restart
3.修改slave配置文件
vim /etc/my.cnf
=============================
[mysqld]
//为innodb的每个表单独创建表空间
innodb_file_per_table=1
log-bin=slave-bin
replicate-wild-do-table=employees.%
binlog_format=row
server-id= 10 
===============================
##重启:/etc/init.d/mysqld restart
vim /etc/my.cnf
=============================
[mysqld]
//为innodb的每个表单独创建表空间
innodb_file_per_table=1
log-bin=slave-bin
replicate-wild-do-table=tuge.%
binlog_format=row
server-id= 11
===============================
##重启:/etc/init.d/mysqld restart
4.在master上创建复制用户并授予权限
mysql> grant replication slave on *.* to 'xiao'@'10.10.54.%' identified by '123456';
5.在master上查看二进制日志文件和postion位置
mysql> show master status;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000001 |      215 |              |                  |
+-------------------+----------+--------------+------------------+
6.备份master上的数据,把备份master分别还原到从库上
slave1:
mysqldump -uroot -p --master-data=2 --single-transaction --flush-logs --database employees |mysql -uroot -p123456 -h10.10.54.56
slave2:
mysqldump -uroot -p --master-data=2 --single-transaction --flush-logs --database tuge |mysql -uroot -p123456 -h10.10.54.58
7.在slave1,2上change master操作
mysql>change master to master_host='10.10.54.57',master_user='xiao',master_password='123456',
master_log_file='master-bin.000001',master_log_pos=215;
6.在slave1,2上启动slave
mysql>start slave;
7.查看slave状态
salve1:mysql>show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.10.54.57
                  Master_User: xiao
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 215
               Relay_Log_File: xiao56-relay-bin.000002
                Relay_Log_Pos: 254
        Relay_Master_Log_File: master-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: employees.%
..............................................................
slave2:mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.10.54.57
                  Master_User: xiao
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 215
               Relay_Log_File: xiao58-relay-bin.000002
                Relay_Log_Pos: 254
        Relay_Master_Log_File: master-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: tuge.%
................................................................
8.验证
master:
mysql> use tuge;
mysql> create table haha(name varchar(20));
salve2:
mysql> show tables;
+----------------+
| Tables_in_tuge |
+----------------+
| haha           |
| info           |
+----------------+
master:
mysql> use employees;
mysql> create table titles(emp_no int not null);
slave1:
+---------------------+
| Tables_in_employees |
+---------------------+
| departments         |
| dept_emp            |
| dept_manager        |
| employees           |
| salaries            |
| titles              |
+---------------------+
注意:
1.--replicate-do-db选择二进制日志格式
binlog_format=row
2.存储引擎为innodb时,必须设置innodb_file_per_table=1
3.mysql 为了实现 replication 必须打开 bin-log 项,也是打开二进制的 mysql 日志记录选项。


你可能感兴趣的:(mysql,同步,复制,不同数据库)