WL#6404: Add rewrite-db option to mysqlbinlog on RBR

Affects: Server-5.7   —   Status: Complete

Add rewrite-db option to mysqlbinlog so a RBR log can be replayed to a different
database name.

I propose to add an option to mysqlbinlog utility so that we can apply RBR 
to a different DB name. 
The RBR statements include the db_name embedded in them. To rewrite the RBR 
statements, on a different db name when specified, we are including this
new option. With the current implementation we cannot apply a row event in 
a different database. 

This option " rewrite-db " will two value, the from_db and to_db name. 
Now while reading an ROW event from the mysqlbinlog we need to replace the 
events of from_db to 'to_db' as specified by the new option.

One of the use of this option is consider that we have a sharding 
implementation. One shard is one database, one server may have many shards. 
We regularly clone shards, when we do this we will seed a new database with a 
snapshot of the parent database. We then keep the clone shard up to date by 
applying the binlogs from the parent shard.

With statement based replication we can do something like this:

mysqlbinlog --database=shard1 binlog.123456 > logs.to.apply
mysql -h new_host shard1_clone < logs.to.apply 

We use MIXED replication at the moment, so if we hit a RBR event for one shard 
while we're trying to apply the logs to another shard (database) this approach 
will fail, hence the feature.
The new option can be used for applying the ROW based event on to a new database
which is not possible as of now.

The new option introduced " --rewrite-db=from_db->to_db "  is followed by two
database name which is actaully from_db and to_db database names. This will 
replace the database name of the RBR event to the new ' to_db '.

shell> mysqlbinlog --rewrite-db=from_db,to_db binlog.000001 > /tmp/statements.sql
shell> mysql -u root -p -e "source /tmp/statements.sql"

Incase the user wishes to rewrite multiple databases and rewrite each one to a
different destination database the syntax should be :

shell> mysqlbinlog --rewrite-db="from_db1->to_db1" 
--rewrite-db="from_db2->to_db2" binlog.000001 > /tmp/statements.sql

shell> mysql -u root -p -e "source /tmp/statements.sql"

This will help is applying all ROW events of the binlog file to be applied to 
the.
The following changes will be done
===================================


IN SERVER CODE
---------------

1. Function to extract the to_db name from the list prepared which contains the    
   from_db , to_db pairs.

+*/
+bool get_binlog_rewrite_db(const char* db)
+{


2. Added functionality to modify the log_event buffer to incorporate the new 
   database name :

+bool rewrite_buffer(char **buf, int event_len, 
+                    const Format_description_log_event 
+                          *description_event)

3. Modified the code to suppress the use database command in the binlog 

+  
+  Suppress if the --rewrite-db  option in use. 
+  Skip otherwise.
+*/
+  else if (db && !option_rewrite_set)



IN MYSQLBINLOG CLIENT
----------------------

1. Add a new option for mysqlbinlog client

+  {"rewrite-db", OPT_REWRITE_DB, "Rewrite the rowbased event to point "
+   "to a new database so that the event can be applied to a new database",
+    &rewrite, &rewrite, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},

Ensured that the syntax for the rewrite-db is used properly by the user 

+  case OPT_REWRITE_DB:
+  {
....
....
....
+                                           // I_List ie. binlog_rewrite_db.
+    break;                                 
+  }