mysql --ignore-table,做Mysql主从时,注意使用replicate_wild_do_table和replicate-wild-ignore-table...

使用replicate_do_db和replicate_ignore_db时有一个隐患,跨库更新时会出错。

如在Master(主)服务器上设置 replicate_do_db=test(my.conf中设置)

use mysql;

update test.table1 set ......

那么Slave(从)服务器上第二句将不会被执行

如Master设置 replicate_ignore_db=mysql

use mysql;

update test.table1 set ......

那么Slave上第二句会被忽略执行

原因是设置replicate_do_db或replicate_ignore_db后,MySQL执行sql前检查的是当前默认数据库,所以跨库更新语句在Slave上会被忽略。

可以在Slave上使用 replicate_wild_do_table 和 replicate_wild_ignore_table 来解决跨库更新的问题,如:

replicate_wild_do_table=test.%

replicate_wild_ignore_table=mysql.%

这样就可以避免出现上述问题了

---------------------华丽丽的分界线------------------------

完整版:

作者: Baron Schwartz

Why MySQL’s binlog-do-db option is dangerous

为什么 MySQL的 binlog-do-db 选项是危险的.

I see a lot of people filtering replication with binlog-do-db, binlog-ignore-db, replicate-do-db, and replicate-ignore-db. Although there are uses for these, they are dangerous and in my opinion, they are overused. For many cases, there's a safer alternative.

我发现很多人通过 binlog-do-db, binlog-ignore-db, replicate-do-db 和 replicate-ignore-db 来过滤复制(某些数据库), 尽管有些使用, 但是,在我看来,他们是危险的,并且他们被滥用了. 对于很多的实例,有更安全的替换方案.

The danger is simple: they don't work the way you think they do. Consider the following scenario: you set binlog-ignore-db to "garbage" so data in the garbage database (which doesn't exist on the slave) isn't replicated. (I'll come back to this in a second, so if you already see the problem, don't rush to the comment form.)

为什么危险很简单: 他们并不像你想的那样工作. 想象如下的场景: 你设置了 binlog-ignore-db = garbage, 所以 garbage数据库(在slave上不存在这个数据库) 中的数据不会被复制,(待会儿我再讲这个,如果你已经发现问题了,不要急于到评论表单)

Now you do the following:

现在做下面的事情:

$ mysql

mysql> delete from garbage.junk;

mysql> use garbage;

mysql> update production.users set disabled = 1 where user = "root";

You just broke replication, twice. Once, because your slave is going to execute the first query and there's no such table "garbage.junk" on the slave. The second time, silently, because the update to production.users isn't replicated, so now the root user isn't disabled on the slave.

复制会broke2次, 第一次,因为 slave尝试着去之西你给第一条语句,但是slave上并没有这样的表"garbage.junk" , 第二次, 隐含的, 因为 对 production.users不会被 复制,因为 root帐号并没有在slave上被禁用掉.

Why? Because binlog-ignore-db doesn't do what you think. The phrase I used earlier, "data in the garbage database isn't replicated," is a fallacy. That's not what it does. In fact, it filters out binary logging for statements issued from connections whose default database is "garbage." In other words, filtering is not based on the contents of the query -- it is based on what database you USE.

为什么? 因为 binlog-ignore-db 并不像你想的那样执行, 我之前说的, "在garbage数据库中的数据不会被复制" 是错的, 实际上(数据库)并没有这么做.事实上, 他是通过默认的数据库为“garbage" 的连接, 过滤二进制的(SQL)语句日志的. 换句话说, 过滤不是基于 查询的字符串的, 而实际于你used的数据库.

The other configuration options I mentioned work similarly. The binlog-do-db and binlog-ignore-db statements are particularly dangerous because they keep statements from ever being written to the binary log, which means you can't use the binary log for point-in-time recovery of your data from a backup.

其他我提到的配置选项也都类似. binlog-do-db 和 binlog-ignore-db 语句是特别危险的,因为他们将语句写入了二进制日志. 意味着你不能使用二进制日志从备份恢复指定时间的数据.

In a carefully controlled environment, these options can have benefits, but I won't talk about that here. (We covered that in our book.)

在严格控制的环境中, 这些选项是很有用的,但是我不会谈论这些(这些包含在我们的书中),

The safer alternative is to configure filters on the slave, with options that actually operate on the tables mentioned in the query itself. These are replicate-wild-* options. For example, the safer way to avoid replicating data in the garbage database is to configure replicate-wild-ignore-table=garbage.%. There are still edge cases where that won't work, but it works in more cases and has fewer gotchas.

安全的替换方案是 在 slave上配置过滤, 使用基于查询中真正涉及到的表的选项, 这些是: replicate-wild-* 选项, 例如, 避免复制 garbage数据库中的数据的安全的方案是 配置: replicate-wild-ignore-table=garbage.%. 这样做仍然有一些特殊的情况, 不能正常工作,但可以在更多的情况下正常工作,并且会遇到更少的意外 (gotchas).

If you are confused, you should read the replication rules section of the manual until you know it by heart

如果你有些疑惑了,你应该去读一读手册上的复制规则一节,直到你真正明白为止.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL 主从复制中,可以使用 "start-position" 或 "start-datetime" 参数来指定从哪个位置或间点开始复制。这些参数可以在从服务器上的配置文件中设置。下面是具体步骤: 1. 打开从服务器上的配置文件 my.cnf 或 my.ini。 2. 找到 [mysqld] 段,添加以下参数: - 使用 "start-position" 参数: ``` replicate-do-db=database_name replicate-ignore-db=mysql replicate-wild-do-table=database_name.% master-info-file=/var/lib/mysql/master.info relay-log-info-file=/var/lib/mysql/relay-log.info relay-log=/var/lib/mysql/relay-log log-bin=mysql-bin log-slave-updates server-id=2 master-host=master_server_ip master-user=replication_user master-password=replication_password replicate-do-table=database_name.table_name replicate-do-table=database_name.table_name2 replicate-do-table=database_name.table_name3 replicate-do-table=database_name.table_name4 replicate-do-table=database_name.table_name5 relay-log-recovery=1 ``` 其中,"start-position" 参数可以在 "master-info-file" 文件中找到。 - 使用 "start-datetime" 参数: ``` replicate-do-db=database_name replicate-ignore-db=mysql replicate-wild-do-table=database_name.% master-info-file=/var/lib/mysql/master.info relay-log-info-file=/var/lib/mysql/relay-log.info relay-log=/var/lib/mysql/relay-log log-bin=mysql-bin log-slave-updates server-id=2 master-host=master_server_ip master-user=replication_user master-password=replication_password replicate-do-table=database_name.table_name replicate-do-table=database_name.table_name2 replicate-do-table=database_name.table_name3 replicate-do-table=database_name.table_name4 replicate-do-table=database_name.table_name5 relay-log-recovery=1 ``` 其中,"start-datetime" 参数可以在 "master-info-file" 文件中找到。 3. 保存并关闭文件,重启从服务器。 4. 在从服务器上执行以下命令启动复制: ``` CHANGE MASTER TO MASTER_HOST='master_server_ip', MASTER_USER='replication_user', MASTER_PASSWORD='replication_password', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=4; ``` 其中,"MASTER_LOG_FILE" 和 "MASTER_LOG_POS" 分别对应 "start-position" 或 "start-datetime" 的值。 注意:在使用 "start-datetime" 参数,需要保证主从服务器的区设置相同,否则可能会导致复制不一致的问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值