ITPub博客

首页 > 数据库 > MySQL > 单向同步复制需要注意的几个参数及常用命令

单向同步复制需要注意的几个参数及常用命令

原创 MySQL 作者:Michael_DD 时间:2015-08-03 16:25:06 0 删除 编辑
单向同步复制需要注意的几个参数及常用命令



log-bin=mysql-bin  默认是所有库都写二进制日志


binlog-do-db=db_name    需要写二进制日志的库
binlog-ignore-db=db_name    不需要写二进制日志的库


replicate-do-db=db_name      需要同步复制的库
replicate-ignore-db=db_name   不需要同步复制的库


replicate-wild-do-table=db_name.%    需要同步复制的表
replicate-wild-ignore-table=db_name.%  不需要同步复制的表,默认其他所有同步




binlog-do-db, binlog-ignore-db, replicate-do-db 和 replicate-ignore-db 来过滤复制(某些数据库), 尽管有些使用, 他们是危险的. 
对于很多的实例,有更安全的替换方案 replicate-wild-do-table=db_name.% 
(具体原因:网上很多资料可以查到)


配置文件:
master端:
[mysqld]
port = 3306
server-id = 1
replicate_wild_do_table=centreon_status.%
log-bin=mysql-bin




slave端:
[mysqld]
port = 3306
server-id = 2
master-host=192.168.9.146
master-user=replication 
master-password=123456
replicate_wild_do_table=centreon_status.%
log-bin=mysql-bin






Master端:
mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000015 |  4386841 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)


查看日志:
mysql> SHOW BINLOG EVENTS in 'mysql-bin.000015' from 4386841;
+------------------+---------+-------------+-----------+-------------+-------------------------------------------------------------------+
| Log_name         | Pos     | Event_type  | Server_id | End_log_pos | Info                                                              |
+------------------+---------+-------------+-----------+-------------+-------------------------------------------------------------------+
| mysql-bin.000015 | 4386841 | Query       |         1 |     4386928 | BEGIN                                                             |
| mysql-bin.000015 | 4386928 | Table_map   |         1 |     4387029 | table_id: 146 (centreon_status.nagios_programstatus)              |
| mysql-bin.000015 | 4387029 | Update_rows |         1 |     4387245 | table_id: 146 flags: STMT_END_F                                   |
| mysql-bin.000015 | 4387245 | Xid         |         1 |     4387272 | COMMIT /* xid=23872 */                                            |
| mysql-bin.000015 | 4387272 | Query       |         1 |     4387359 | BEGIN                                                             |
| mysql-bin.000015 | 4387359 | Table_map   |         1 |     4387460 | table_id: 146 (centreon_status.nagios_programstatus)              |
| mysql-bin.000015 | 4387460 | Update_rows |         1 |     4387676 | table_id: 146 flags: STMT_END_F                                   |
| mysql-bin.000015 | 4387676 | Xid         |         1 |     4387703 | COMMIT /* xid=23876 */                                            |
| mysql-bin.000015 | 4387703 | Query       |         1 |     4387790 | BEGIN                                                             |
| mysql-bin.000015 | 4387790 | Table_map   |         1 |     4387923 | table_id: 140 (centreon_status.nagios_hoststatus)                 |
| mysql-bin.000015 | 4387923 | Update_rows |         1 |     4388673 | table_id: 140 flags: STMT_END_F                                   |
| mysql-bin.000015 | 4388673 | Xid         |         1 |     4388700 | COMMIT /* xid=23878 */                                            |
| mysql-bin.000015 | 4388700 | Query       |         1 |     4388787 | BEGIN                                                             |
| mysql-bin.000015 | 4388787 | Table_map   |         1 |     4388874 | table_id: 123 (centreon_status.nagios_customvariablestatus)       |
| mysql-bin.000015 | 4388874 | Update_rows |         1 |     4388968 | table_id: 123 flags: STMT_END_F                                   |
| mysql-bin.000015 | 4388968 | Xid         |         1 |     4388995 | COMMIT /* xid=23879 */                                            |
| mysql-bin.000015 | 4388995 | Query       |         1 |     4389082 | BEGIN                                                             |
| mysql-bin.000015 | 4389082 | Table_map   |         1 |     4389169 | table_id: 123 (centreon_status.nagios_customvariablestatus)       |
| mysql-bin.000015 | 4389169 | Update_rows |         1 |     4389271 | table_id: 123 flags: STMT_END_F                                   |
| mysql-bin.000015 | 4389271 | Xid         |         1 |     4389298 | COMMIT /* xid=23880 */                                            |
| mysql-bin.000015 | 4389298 | Query       |         1 |     4389385 | BEGIN                                                             |
| mysql-bin.000015 | 4389385 | Table_map   |         1 |     4389472 | table_id: 123 (centreon_status.nagios_customvariablestatus)       |
| mysql-bin.000015 | 4389472 | Update_rows |         1 |     4389586 | table_id: 123 flags: STMT_END_F                                   |
| mysql-bin.000015 | 4389586 | Xid         |         1 |     4389613 | COMMIT /* xid=23881 */                                            |




Slave端:
mysql> stop slave;
mysql> CHANGE MASTER TO MASTER_HOST='192.168.9.146', MASTER_PORT=3306, MASTER_USER='replication', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin.000015', MASTER_LOG_POS=2762078;
mysql> start slave;
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.9.146
                  Master_User: replication
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000015
          Read_Master_Log_Pos: 4502237
               Relay_Log_File: mysqld-relay-bin.000002
                Relay_Log_Pos: 1740410
        Relay_Master_Log_File: mysql-bin.000015
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: centreon_status.%
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 4502237
              Relay_Log_Space: 1740566
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
1 row in set (0.00 sec)


ERROR: 
No query specified


mysql> 








同步时需要初始化数据,这时候可以把主库的表锁住,这样就不会出现初始化数据不一致:


备份master数据库数据
mysql> flush tables with read lock;  //不要退出这个终端,否则这个锁就不生效了。从服务器的数据库建好后。在主服务器执行解锁
 
同时要记录下mysql-bin.000003和1271
mysql> show master status;
+------------------+----------+--------------+-------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB              |
+------------------+----------+--------------+-------------------------------+
| mysql-bin.000002 |      106 | blog,www     | mysql,test,information_schema |
+------------------+----------+--------------+-------------------------------+
1 row in set (0.01 sec)
取得快照并记录日志名和偏移量
 
开启另一个终端对主服务器数据目录做快照。
#cd /usr/local/mysql/data
#tar -zcvf backup.tar.gz  blog
 
此时在主库解开table的锁定
mysql> unlock tables;




告诉主服务器,如果当前的数据库(即USE选定的数据库)是db_name,应将更新记录到二进制日志中。其它所有没有明显指定的数据库  被忽略。如果使用该选项,你应确保只对当前的数据库进行 ...


例如你设置了
binlog-do-db=db1

然后你执行了以下语句
use db1;
create database db2; 
第二句语句并不会记入log
即使当前数据库是db1,因为考虑的是语句中的数据库名;

相反,除了CREATE DATABASE, ALTER DATABASE, and DROP DATABASE 以外的语句是对当前数据库有关的。

例如你设置了
binlog-do-db=db1

然后你执行了以下语句
use db2;
insert into db1.table1 values(1);
这条语句是不会计入log的,因为当前db是db2。而与操作db无关。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29500582/viewspace-1760551/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2014-02-22

  • 博文量
    326
  • 访问量
    2671974