当前位置:首页 > Mysql教程 > 列表

Mysql主从同步排除指定数据库

发布:smiling 来源: PHP粉丝网  添加日期:2015-04-18 10:04:06 浏览: 评论:0 

主从同步时有时我们希望排除一些记录不同步了,那么这个要如何来配置了呢?下面小编来为各位介绍一篇Mysql主从同步忽略特定库的方法吧.

一、主库、从库同步测试

  1. [root@Master-Mysql ~]# /usr/local/mysql/bin/mysql -uroot -p 
  2. mysql> show processlist\G 
  3.   State: Master has sent all binlog to slave; waiting for binlog to be updated 
  4. mysql> create database helloworld; 
  5. mysql> use hitest; 
  6. mysql> insert into test(id,namevalues(3,'doit'); 
  7. mysql> grant select,insert,update,delete on *.* to byrd@'192.168.199.%' identified by 'admin'
  8. mysql> create user 'def'@'localhost' identified by 'admin'
  9. mysql> select user,host from mysql.user
  10. +------+---------------+ 
  11. user | host          | 
  12. +------+---------------+ 
  13. | byrd | 192.168.199.% | 
  14. | def  | localhost     | 
  15. +------+---------------+ 
  16. rows in set (0.00 sec) 
  17. #mysql> grant all on *.* to 'imbyrd'@'localhost' identified by 'admin';    #主库建立一个用户imbyrd,密码为admin 
  18. ############上面主库############主库从库分隔符############下面从库############ 
  19. [root@Slave-Mysql ~]# /usr/local/mysql/bin/mysql -uroot -p    #下面是从库,上面是主库哦!!! 
  20. mysql> show databases; 
  21. +--------------------+ 
  22. Database           | 
  23. +--------------------+ 
  24. | helloworld         | 
  25. mysql> use hitest; 
  26. mysql> select * from test; 
  27. +----+--------+ 
  28. | id | name   | 
  29. +----+--------+ 
  30. |  1 | zy     | 
  31. |  2 | binghe | 
  32. |  3 | doit   | 
  33. +----+--------+ 
  34. mysql> show grants for byrd@'192.168.199.%'
  35. +-------------------------------------------------------------------------------------------------------------------------------------------------------------+ 
  36. | Grants for byrd@192.168.199.%                                                                                                                               | 
  37. +-------------------------------------------------------------------------------------------------------------------------------------------------------------+ 
  38. GRANT SELECTINSERTUPDATEDELETE, REPLICATION SLAVE ON *.* TO 'byrd'@'192.168.199.%' IDENTIFIED BY PASSWORD '*01A6717B58FF5C7EAFFF6CB7C96F7428EA65FE4C' | 
  39. +-------------------------------------------------------------------------------------------------------------------------------------------------------------+ 
  40. 1 row in set (0.03 sec)  --phpfensi.com 
  41. mysql> select user,host from mysql.user
  42. +------+---------------+ 
  43. user | host          | 
  44. +------+---------------+ 
  45. | root | 127.0.0.1     | 
  46. | byrd | 192.168.199.% | 
  47. | root | ::1           | 
  48. | root | localhost     | 
  49. +------+---------------+ 
  50. rows in set (0.00 sec) 

结论:主库、从库同步正常.

二、主库、从库权限同步测试(此次只在从库的my.cnf增加了replicate-wild-ignore-table=mysql.%)

  1. mysql> create database hiworld; 
  2. mysql> show databases; 
  3. +--------------------+ 
  4. Database           | 
  5. +--------------------+ 
  6. | hitest             | 
  7. | hiworld            | 
  8. +--------------------+ 
  9. rows in set (0.00 sec) 
  10. mysql> grant all on *.* to byrd@'192.168.199.%' identified by 'admin'
  11. mysql> show grants for byrd@'192.168.199.%'
  12. +--------------------------------------------------------------------------------------------------------------------------+ 
  13. | Grants for byrd@192.168.199.%                                                                                            | 
  14. +--------------------------------------------------------------------------------------------------------------------------+ 
  15. GRANT ALL PRIVILEGES ON *.* TO 'byrd'@'192.168.199.%' IDENTIFIED BY PASSWORD '*01A6717B58FF5C7EAFFF6CB7C96F7428EA65FE4C' | 
  16. +--------------------------------------------------------------------------------------------------------------------------+ 
  17. 1 row in set (0.00 sec) 
  18. mysql> use hitest; 
  19. mysql> insert into test(id,namevalues(6,'six'); 
  20. mysql> select * from test; 
  21. +----+---------+ 
  22. | id | name    | 
  23. +----+---------+ 
  24. |  6 | six     | 
  25. +----+---------+ 
  26. rows in set (0.02 sec) 
  27. ############上面主库############主库从库分隔符############下面从库############ 
  28. mysql> show databases; 
  29. +--------------------+ 
  30. Database           | 
  31. +--------------------+ 
  32. | hitest             | 
  33. | hiworld            | 
  34. +--------------------+ 
  35. 12 rows in set (0.15 sec) 
  36. mysql> show grants for byrd@'192.168.199.%'
  37. +-------------------------------------------------------------------------------------------------------------------------------------------------------------+ 
  38. | Grants for byrd@192.168.199.%                                                                                                                               | 
  39. +-------------------------------------------------------------------------------------------------------------------------------------------------------------+ 
  40. GRANT SELECTINSERTUPDATEDELETE, REPLICATION SLAVE ON *.* TO 'byrd'@'192.168.199.%' IDENTIFIED BY PASSWORD '*01A6717B58FF5C7EAFFF6CB7C96F7428EA65FE4C' | 
  41. +-------------------------------------------------------------------------------------------------------------------------------------------------------------+ 
  42. 1 row in set (0.00 sec) 
  43. mysql> use hitest; 
  44. mysql> select * from test; 
  45. +----+---------+ 
  46. | id | name    | 
  47. +----+---------+ 
  48. |  6 | six     | 
  49. +----+---------+ 
  50. rows in set (0.04 sec) 

结论:从库在my.cnf增加replicate-wild-ignore-table=mysql.%后权限未同步.

主库mysql-bin内容:

  1. [root@Master-Mysql data]# /usr/local/mysql/bin/mysqlbinlog mysql-bin.000016 
  2. create database hiworld 
  3. /*!*/; 
  4. GRANT ALL PRIVILEGES ON *.* TO 'byrd'@'192.168.199.%' IDENTIFIED BY PASSWORD '*01A6717B58FF5C7EAFFF6CB7C96F7428EA65FE4C' 
  5. /*!*/; 
  6. use `hitest`/*!*/; 
  7. insert into test(id,namevalues(6,'six'
  8. /*!*/; 
  9. CREATE USER 'def'@'localhost' IDENTIFIED BY PASSWORD '*4ACFE3202A5FF5CF467898FC58AAB1D615029441' 
  10. /*!*/; 

从库mysqld-relay-bin内容:

  1. create database hiworld 
  2. /*!*/; 
  3. GRANT ALL PRIVILEGES ON *.* TO 'byrd'@'192.168.199.%' IDENTIFIED BY PASSWORD '*01A6717B58FF5C7EAFFF6CB7C96F7428EA65FE4C' 
  4. /*!*/; 
  5. use `hitest`/*!*/; 
  6. insert into test(id,namevalues(6,'six'
  7. /*!*/; 
  8. CREATE USER 'def'@'localhost' IDENTIFIED BY PASSWORD '*4ACFE3202A5FF5CF467898FC58AAB1D615029441' 
  9. /*!*/; 

结论:

①:当从服务器注释掉replicate-wild-ignore-table=mysql.%内容后(且mysql服务重新启动),之后的所有同步恢复正常,但是在注释后的授权是无法恢复的,如果想要重新授权,需要在主服务器上重新执行授权命令.

②:从库配置文件增加replicate-wild-ignore-table=mysql.%后,对授权、增加用户、虽然记录到mysqld-relay-bin中,但是会进行过滤,而对增加数据库则进行同步;

备注:Prior to MySQL 5.5.32, this option caused any statements containing fully qualified table names not to be logged if there was no default database specified (that is, when SELECT DATABASE() returned NULL). In MySQL 5.5.32 and later, when there is no default database, no --binlog-ignore-db options are applied, and such statements are always logged. (Bug #11829838, Bug #60188)

Tags: Mysql主从同步 Mysql同步排除

分享到: