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

一、主库、从库同步测试

[root@Master-Mysql ~]# /usr/local/mysql/bin/mysql -uroot -p
mysql> show processlist\G
  State: Master has sent all binlog to slave; waiting for binlog to be updated
mysql> create database helloworld;
mysql> use hitest;
mysql> insert into test(id,name) values(3,'doit');
mysql> grant select,insert,update,delete on *.* to byrd@'192.168.199.%' identified by 'admin';
mysql> create user 'def'@'localhost' identified by 'admin';
mysql> select user,host from mysql.user;
+------+---------------+
| user | host          |
+------+---------------+
| byrd | 192.168.199.% |
| def  | localhost     |
+------+---------------+
7 rows in set (0.00 sec)
#mysql> grant all on *.* to 'imbyrd'@'localhost' identified by 'admin';    #主库建立一个用户imbyrd,密码为admin
############上面主库############主库从库分隔符############下面从库############
[root@Slave-Mysql ~]# /usr/local/mysql/bin/mysql -uroot -p    #下面是从库,上面是主库哦!!!
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| helloworld         |
mysql> use hitest;
mysql> select * from test;
+----+--------+
| id | name   |
+----+--------+
|  1 | zy     |
|  2 | binghe |
|  3 | doit   |
+----+--------+
mysql> show grants for byrd@'192.168.199.%';
+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Grants for byrd@192.168.199.%                                                                                                                               |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
| GRANT SELECT, INSERT, UPDATE, DELETE, REPLICATION SLAVE ON *.* TO 'byrd'@'192.168.199.%' IDENTIFIED BY PASSWORD '*01A6717B58FF5C7EAFFF6CB7C96F7428EA65FE4C' |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.03 sec)
mysql> select user,host from mysql.user;
+------+---------------+
| user | host          |
+------+---------------+
| root | 127.0.0.1     |
| byrd | 192.168.199.% |
| root | ::1           |
| root | localhost     |
+------+---------------+
7 rows in set (0.00 sec)

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

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

mysql> create database hiworld;
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| hitest             |
| hiworld            |
+--------------------+
8 rows in set (0.00 sec)
mysql> grant all on *.* to byrd@'192.168.199.%' identified by 'admin';
mysql> show grants for byrd@'192.168.199.%';
+--------------------------------------------------------------------------------------------------------------------------+
| Grants for byrd@192.168.199.%                                                                                            |
+--------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'byrd'@'192.168.199.%' IDENTIFIED BY PASSWORD '*01A6717B58FF5C7EAFFF6CB7C96F7428EA65FE4C' |
+--------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> use hitest;
mysql> insert into test(id,name) values(6,'six');
mysql> select * from test;
+----+---------+
| id | name    |
+----+---------+
|  6 | six     |
+----+---------+
6 rows in set (0.02 sec)
############上面主库############主库从库分隔符############下面从库############
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| hitest             |
| hiworld            |
+--------------------+
12 rows in set (0.15 sec)
mysql> show grants for byrd@'192.168.199.%';
+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Grants for byrd@192.168.199.%                                                                                                                               |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
| GRANT SELECT, INSERT, UPDATE, DELETE, REPLICATION SLAVE ON *.* TO 'byrd'@'192.168.199.%' IDENTIFIED BY PASSWORD '*01A6717B58FF5C7EAFFF6CB7C96F7428EA65FE4C' |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> use hitest;
mysql> select * from test;
+----+---------+
| id | name    |
+----+---------+
|  6 | six     |
+----+---------+
6 rows in set (0.04 sec)

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

主库mysql-bin内容:

[root@Master-Mysql data]# /usr/local/mysql/bin/mysqlbinlog mysql-bin.000016
create database hiworld
/*!*/;
GRANT ALL PRIVILEGES ON *.* TO 'byrd'@'192.168.199.%' IDENTIFIED BY PASSWORD '*01A6717B58FF5C7EAFFF6CB7C96F7428EA65FE4C'
/*!*/;
use `hitest`/*!*/;
insert into test(id,name) values(6,'six')
/*!*/;
CREATE USER 'def'@'localhost' IDENTIFIED BY PASSWORD '*4ACFE3202A5FF5CF467898FC58AAB1D615029441'
/*!*/;
从库mysqld-relay-bin内容:

create database hiworld
/*!*/;
GRANT ALL PRIVILEGES ON *.* TO 'byrd'@'192.168.199.%' IDENTIFIED BY PASSWORD '*01A6717B58FF5C7EAFFF6CB7C96F7428EA65FE4C'
/*!*/;
use `hitest`/*!*/;
insert into test(id,name) values(6,'six')
/*!*/;
CREATE USER 'def'@'localhost' IDENTIFIED BY PASSWORD '*4ACFE3202A5FF5CF467898FC58AAB1D615029441'
/*!*/;

结论

①:当从服务器注释掉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)

时间: 2024-12-31 01:23:26

Mysql主从同步排除指定数据库的相关文章

MySQL备份时排除指定数据库的方法_Mysql

使用mysqldump命令备份时候,--all-databases 可以备份所有的数据库. 使用ignore-table 还可以排除制定的表.但是,mysqldump没有参数可以排除数据库的. 要备份的数据库少的时候,可以通过mysqldump -uroot -p123456 --databases db1 db2 db3 > mysqldump.sql 这样来备份. 但是假如数据库有数十个的话,这样写起来很累人,也很low.解决办法还是有的,看下面: [下面演示用的mysql用户名的root,

mysql主从同步

MySQL编译安装 shell> groupadd mysql shell> useradd -g mysql mysql shell> gunzip < mysql-VERSION.tar.gz | tar -xvf - shell> cd mysql-VERSION shell> ./configure --prefix=/usr/local/mysql shell> make shell> make install shell> cp suppo

MySQL主从同步、读写分离配置步骤_Mysql

现在使用的两台服务器已经安装了MySQL,全是rpm包装的,能正常使用. 为了避免不必要的麻烦,主从服务器MySQL版本尽量保持一致; 环境:192.168.0.1 (Master) 192.168.0.2 (Slave) MySQL Version:Ver 14.14 Distrib 5.1.48, for pc-linux-gnu (i686) using readline 5.1 1.登录Master服务器,修改my.cnf,添加如下内容: server-id = 1 //数据库ID号,

怎样重配 重置mysql主从同步

重置mysql主从同步(MySQL Reset Master-Slave Replication) 在mysql主从同步的过程中,可能会因为各种原因出现主库与从库不同步的情况,网上虽然有一些解决办法,但是有时很难彻底解决,重置主从服务器也许不是最快的办法,但却是最安全有效的. 下面将自己重置主从同步的步骤总结一下,以备不时之需. master与slave均使用:centos6.0+mysql 5.1.61 ,假设有db1,db2两个数据库需要热备. 文中shell与mysql均使用root账号,

mysql 主从同步

mysql cmake install 过程我在这边就不说了,如果要看的朋友,点击我以前的博客 ,mysql version : 5.6.13 http://blog.csdn.net/wanglei_storage/article/details/48262141 下一篇是mysql 主从同步 + atlas读写分离,也就是目前比较热门的技术了 http://blog.csdn.net/wanglei_storage/article/details/48808777 好,下面进入正题.通过两台

Linux下MySQL主从同步监控shell脚本

说明: 操作系统:CentOS 目的:定时监控MySQL主从数据库是否同步,如果不同步,记录故障时间,并执行命令使主从恢复同步状态 1.创建脚本文件 vi /home/crontab/check_mysql_slave.sh   #编辑,添加下面代码 #!/bin/sh # check_mysql_slave status # author www.111cn.net ip=eth0  #网卡名称 mysql_binfile=/usr/local/mysql/bin/mysql mysql_us

mysql数据同步:不同数据库名,不同表名,字段名也不相同,怎么同步数据呢?

问题描述 mysql数据同步:不同数据库名,不同表名,字段名也不相同,怎么同步数据呢? 工作当中遇到了一个问题,本人刚入行菜鸟不知道怎么解决,想问问大虾: A服务器上有数据库db_a,库中有表tbl_a,表中有字段col_a, B服务器上有数据库db_b,库中有表tbl_b,表中有字段col_b, AB数据库名不同,表名不同,字段名也不同. 现在需要把A服务器上tbl_a表中col_a字段的数据 同步到 B服务器上tbl_b表中col_b字段. (也不知道我的描述各位看官能看懂不) 该怎么做呢?

centos 6.5设置mysql主从同步过程记录

在centos 6.5上设置了mysql主从功能,记录一下. 服务器1(主) IP:192.168.1.201 系统版本:centos 6.5 mysql版本:mysql 5.5 服务器2(从) IP:192.168.1.202 系统版本:centos 6.5 mysql版本:mysql 5.5 这里两台服务器的系统版本和mysql版本均一致,这也是官方推荐的做法.在开始设定之前,最好能确保主库和从库一致. 1.主库和从库创建同步用户 mysql> grant replication slave

Mysql主从同步备份策略分享_Mysql

环境:主从服务器上的MySQL数据库版本同为5.1.34主机IP:192.168.0.1从机IP:192.168.0.2一. MySQL主服务器配置1.编辑配置文件/etc/my.cnf# 确保有如下行server-id = 1log-bin=mysql-binbinlog-do-db=mysql  #需要备份的数据库名,如果备份多个数据库,重复设置这个选项即可binlog-ignore-db=mysql  #不需要备份的数据库名,如果备份多个数据库,重复设置这个选项即可log-slave-up