replicate-rewrite-db:
Tells the slave to translate the default database (that is, the one selected by USE) to to_name if it was from_name on the master.Only statements involving tables are affected (not statements such as CREATE DATABASE, DROP DATABASE, and ALTER DATABASE), and only if from_name is the default database on the master.This does not work for cross-database updates.To specify multiple rewrites, use this option multiple times.The server uses the first one with a from_name value that matches.The database name translation is done before the --replicate-* rules are tested.
If you use this option on the command line and the “>” character is special to your command interpreter, quote the option value.For example:
shell> mysqld --replicate-rewrite-db="olddb->newdb"
比如,master与slave同步fandb这个库,但因为需要slave需要将fandb改名为dudb,master不能改,就需要这个参数
在Slave端的my.cnf中加入
replicate-rewrite-db=fandb->dudb
在Slave端的my.cnf中加入
replicate-rewrite-db=fandb->dudb
重启Slave库,然后首先要创建dudb这个库
加入Master端执行
(mysql@localhost)[fandb]> create table rewrite asselect*from ab;
如果Slave没有dudb.ab这个表,就会报错
[ERROR]Slave SQL:Error'Table 'dudb.ab' doesn't exist' on query. Default database: 'dudb'. Query: 'create table rewrite as select * from ab', Error_code: 1146
2015-07-10 09:44:58 48962 [Warning] Slave: Table 'dudb.ab' doesn't exist Error_code:1146
2015-07-1009:44:5848962[ERROR]Error running query, slave SQL thread aborted.Fix the problem, and restart the slave SQL thread with "SLAVE START".We stopped at log 'mysql-bin.000003' position 3973
然后sql_thread也会被停止
mysql> show slave status\G
***************************1. row ***************************
Slave_IO_State:Waitingfor master to send event
Master_Host:192.168.134.132
Master_User: repl
Master_Port:3306
Connect_Retry:60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos:4089
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos:283
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running:Yes
Slave_SQL_Running:No
这时需要手动create table dudb.ab as select * from fandb.ab
然后在start slave sql_thread
如果有多个库需要换名字,那么需要多次指定这个参数
replicate-rewrite-db=fandb->dudb
replicate-rewrite-db=a->b