@@ -120,7 +120,7 @@ static my_bool verbose= 0, opt_no_create_info= 0, opt_no_data= 0,
120
120
opt_include_master_host_port = 0 ,
121
121
opt_events = 0 , opt_comments_used = 0 ,
122
122
opt_alltspcs = 0 , opt_notspcs = 0 , opt_drop_trigger = 0 ,
123
- opt_secure_auth = TRUE;
123
+ opt_skip_mysql_schema = 0 , opt_secure_auth = TRUE;
124
124
static my_bool insert_pat_inited = 0 , debug_info_flag = 0 , debug_check_flag = 0 ;
125
125
static ulong opt_max_allowed_packet , opt_net_buffer_length ;
126
126
static MYSQL mysql_connection ,* mysql = 0 ;
@@ -524,6 +524,9 @@ static struct my_option my_long_options[] =
524
524
{"dump-date" , OPT_DUMP_DATE , "Put a dump date to the end of the output." ,
525
525
& opt_dump_date , & opt_dump_date , 0 ,
526
526
GET_BOOL , NO_ARG , 1 , 0 , 0 , 0 , 0 , 0 },
527
+ {"skip_mysql_schema" , OPT_SKIP_MYSQL_SCHEMA , "Skip adding DROP DATABASE for mysql schema." ,
528
+ & opt_skip_mysql_schema , & opt_skip_mysql_schema , 0 , GET_BOOL , NO_ARG , 0 , 0 , 0 , 0 , 0 ,
529
+ 0 },
527
530
{"skip-opt" , OPT_SKIP_OPTIMIZATION ,
528
531
"Disable --opt. Disables --add-drop-table, --add-locks, --create-options, --quick, --extended-insert, --lock-tables, --set-charset, and --disable-keys." ,
529
532
0 , 0 , 0 , GET_NO_ARG , NO_ARG , 0 , 0 , 0 , 0 , 0 , 0 },
@@ -584,9 +587,9 @@ static void print_value(FILE *file, MYSQL_RES *result, MYSQL_ROW row,
584
587
int string_value );
585
588
static int dump_selected_tables (char * db , char * * table_names , int tables );
586
589
static int dump_all_tables_in_db (char * db );
587
- static int init_dumping_views (char * );
588
- static int init_dumping_tables (char * );
589
- static int init_dumping (char * , int init_func (char * ));
590
+ static int init_dumping_views (char * , my_bool );
591
+ static int init_dumping_tables (char * , my_bool );
592
+ static int init_dumping (char * , int init_func (char * , my_bool ));
590
593
static int dump_databases (char * * );
591
594
static int dump_all_databases ();
592
595
static char * quote_name (const char * name , char * buff , my_bool force );
@@ -4735,12 +4738,14 @@ View Specific database initalization.
4735
4738
SYNOPSIS
4736
4739
init_dumping_views
4737
4740
qdatabase quoted name of the database
4741
+ is_mysql_db TRUE if the db is mysql, else FALSE
4738
4742
4739
4743
RETURN VALUES
4740
4744
0 Success.
4741
4745
1 Failure.
4742
4746
*/
4743
- int init_dumping_views (char * qdatabase MY_ATTRIBUTE ((unused )))
4747
+ int init_dumping_views (char * qdatabase MY_ATTRIBUTE ((unused )),
4748
+ my_bool is_mysql_db MY_ATTRIBUTE ( (unused )))
4744
4749
{
4745
4750
return 0 ;
4746
4751
} /* init_dumping_views */
@@ -4752,13 +4757,14 @@ Table Specific database initalization.
4752
4757
SYNOPSIS
4753
4758
init_dumping_tables
4754
4759
qdatabase quoted name of the database
4760
+ is_mysql_db TRUE if the db is mysql, else FALSE
4755
4761
4756
4762
RETURN VALUES
4757
4763
0 Success.
4758
4764
1 Failure.
4759
4765
*/
4760
4766
4761
- int init_dumping_tables (char * qdatabase )
4767
+ int init_dumping_tables (char * qdatabase , my_bool is_mysql_db )
4762
4768
{
4763
4769
DBUG_ENTER ("init_dumping_tables" );
4764
4770
@@ -4775,7 +4781,7 @@ int init_dumping_tables(char *qdatabase)
4775
4781
if (mysql_query (mysql , qbuf ) || !(dbinfo = mysql_store_result (mysql )))
4776
4782
{
4777
4783
/* Old server version, dump generic CREATE DATABASE */
4778
- if (opt_drop_database )
4784
+ if (opt_drop_database && (! opt_skip_mysql_schema || ! is_mysql_db ) )
4779
4785
fprintf (md_result_file ,
4780
4786
"\n/*!40000 DROP DATABASE IF EXISTS %s*/;\n" ,
4781
4787
qdatabase );
@@ -4785,7 +4791,7 @@ int init_dumping_tables(char *qdatabase)
4785
4791
}
4786
4792
else
4787
4793
{
4788
- if (opt_drop_database )
4794
+ if (opt_drop_database && (! opt_skip_mysql_schema || ! is_mysql_db ) )
4789
4795
fprintf (md_result_file ,
4790
4796
"\n/*!40000 DROP DATABASE IF EXISTS %s*/;\n" ,
4791
4797
qdatabase );
@@ -4801,7 +4807,7 @@ int init_dumping_tables(char *qdatabase)
4801
4807
} /* init_dumping_tables */
4802
4808
4803
4809
4804
- static int init_dumping (char * database , int init_func (char * ))
4810
+ static int init_dumping (char * database , int init_func (char * , my_bool ))
4805
4811
{
4806
4812
if (is_ndbinfo (mysql , database ))
4807
4813
{
@@ -4825,14 +4831,15 @@ static int init_dumping(char *database, int init_func(char*))
4825
4831
char * qdatabase = quote_name (database ,quoted_database_buf ,opt_quoted );
4826
4832
my_bool freemem = FALSE;
4827
4833
char const * text = fix_identifier_with_newline (qdatabase , & freemem );
4834
+ my_bool is_mysql_db = !my_strcasecmp (charset_info , database , "mysql" );
4828
4835
4829
4836
print_comment (md_result_file , 0 , "\n--\n-- Current Database: %s\n--\n" ,
4830
4837
text );
4831
4838
if (freemem )
4832
4839
my_free ((void * )text );
4833
4840
4834
4841
/* Call the view or table specific function */
4835
- init_func (qdatabase );
4842
+ init_func (qdatabase , is_mysql_db );
4836
4843
4837
4844
fprintf (md_result_file ,"\nUSE %s;\n" , qdatabase );
4838
4845
check_io (md_result_file );
@@ -5839,24 +5846,20 @@ static int replace(DYNAMIC_STRING *ds_str,
5839
5846
5840
5847
@note: md_result_file should have been opened, before
5841
5848
this function is called.
5842
-
5843
- @param[in] flag If FALSE, disable binlog.
5844
- If TRUE and binlog disabled previously,
5845
- restore the session binlog.
5846
5849
*/
5847
5850
5848
- static void set_session_binlog (my_bool flag )
5851
+ static void set_session_binlog ()
5849
5852
{
5850
5853
static my_bool is_binlog_disabled = FALSE;
5851
5854
5852
- if (!flag && ! is_binlog_disabled )
5855
+ if (!is_binlog_disabled )
5853
5856
{
5854
5857
fprintf (md_result_file ,
5855
5858
"SET @MYSQLDUMP_TEMP_LOG_BIN = @@SESSION.SQL_LOG_BIN;\n" );
5856
5859
fprintf (md_result_file , "SET @@SESSION.SQL_LOG_BIN= 0;\n" );
5857
5860
is_binlog_disabled = 1 ;
5858
5861
}
5859
- else if ( flag && is_binlog_disabled )
5862
+ else
5860
5863
{
5861
5864
fprintf (md_result_file ,
5862
5865
"SET @@SESSION.SQL_LOG_BIN = @MYSQLDUMP_TEMP_LOG_BIN;\n" );
@@ -5894,7 +5897,7 @@ static my_bool add_set_gtid_purged(MYSQL *mysql_con)
5894
5897
{
5895
5898
if (opt_comments )
5896
5899
fprintf (md_result_file ,
5897
- "\n--\n-- GTID state at the beginning of the backup \n--\n\n" );
5900
+ "\n--\n-- GTID state at the end of the backup \n--\n\n" );
5898
5901
5899
5902
fprintf (md_result_file ,"SET @@GLOBAL.GTID_PURGED='" );
5900
5903
@@ -5917,79 +5920,98 @@ static my_bool add_set_gtid_purged(MYSQL *mysql_con)
5917
5920
5918
5921
/**
5919
5922
This function processes the opt_set_gtid_purged option.
5920
- This function also calls set_session_binlog() function before
5921
- setting the SET @@GLOBAL.GTID_PURGED in the output.
5923
+ This function when called with the flag as FALSE, just
5924
+ disables the binlog by calling set_session_binlog().
5925
+ Later when this function is called with the flag as TRUE,
5926
+ SET @@GLOBAL.GTID_PURGED is written in the output and the
5927
+ session binlog is restored if disabled previously.
5922
5928
5923
5929
@param[in] mysql_con the connection to the server
5930
+ @param[in] flag If FALSE, just disable binlog and not
5931
+ set the gtid purged as it will be set
5932
+ at a later point of time.
5933
+ If TRUE, set the gtid purged and
5934
+ restore the session binlog if disabled
5935
+ previously.
5924
5936
5925
5937
@retval FALSE successful according to the value
5926
5938
of opt_set_gtid_purged.
5927
5939
@retval TRUE fail.
5928
5940
*/
5929
5941
5930
- static my_bool process_set_gtid_purged (MYSQL * mysql_con )
5942
+ static my_bool process_set_gtid_purged (MYSQL * mysql_con , my_bool flag )
5931
5943
{
5932
- MYSQL_RES * gtid_mode_res ;
5933
- MYSQL_ROW gtid_mode_row ;
5934
- char * gtid_mode_val = 0 ;
5944
+ MYSQL_RES * gtid_mode_res ;
5945
+ MYSQL_ROW gtid_mode_row ;
5946
+ char * gtid_mode_val = 0 ;
5947
+ static int gtid_mode = -1 ;
5935
5948
char buf [32 ], query [64 ];
5936
5949
5937
5950
if (opt_set_gtid_purged_mode == SET_GTID_PURGED_OFF )
5938
5951
return FALSE; /* nothing to be done */
5939
5952
5940
5953
/*
5941
- Check if the server has the knowledge of GTIDs(pre mysql-5.6)
5942
- or if the gtid_mode is ON or OFF.
5954
+ Set gtid_mode, by fetching gtid_mode from server, if its not
5955
+ yet populated. gtid_mode is set to -1 if gtid_mode is not yet
5956
+ fetched from the server.
5943
5957
*/
5944
- my_snprintf (query , sizeof (query ), "SHOW VARIABLES LIKE %s" ,
5945
- quote_for_like ("gtid_mode" , buf ));
5958
+ if (gtid_mode < 0 )
5959
+ {
5960
+ /*
5961
+ Check if the server has the knowledge of GTIDs(pre mysql-5.6)
5962
+ or if the gtid_mode is ON or OFF.
5963
+ */
5964
+ my_snprintf (query , sizeof (query ), "SHOW VARIABLES LIKE %s" ,
5965
+ quote_for_like ("gtid_mode" , buf ));
5946
5966
5947
- if (mysql_query_with_error_report (mysql_con , & gtid_mode_res , query ))
5948
- return TRUE;
5967
+ if (mysql_query_with_error_report (mysql_con , & gtid_mode_res , query ))
5968
+ return TRUE;
5949
5969
5950
- gtid_mode_row = mysql_fetch_row (gtid_mode_res );
5970
+ gtid_mode_row = mysql_fetch_row (gtid_mode_res );
5951
5971
5952
- /*
5953
- gtid_mode_row is NULL for pre 5.6 versions. For versions >= 5.6,
5954
- get the gtid_mode value from the second column.
5955
- */
5956
- gtid_mode_val = gtid_mode_row ? (char * )gtid_mode_row [1 ] : NULL ;
5972
+ /*
5973
+ gtid_mode_row is NULL for pre 5.6 versions. For versions >= 5.6,
5974
+ get the gtid_mode value from the second column.
5975
+ */
5976
+ gtid_mode_val = gtid_mode_row ? (char * )gtid_mode_row [1 ] : NULL ;
5977
+ gtid_mode = (gtid_mode_val && strcmp (gtid_mode_val , "OFF" )) ? 1 : 0 ;
5978
+ mysql_free_result (gtid_mode_res );
5979
+ }
5957
5980
5958
- if (gtid_mode_val && strcmp ( gtid_mode_val , "OFF" ) )
5981
+ if (gtid_mode )
5959
5982
{
5960
5983
/*
5961
5984
For any gtid_mode !=OFF and irrespective of --set-gtid-purged
5962
5985
being AUTO or ON, add GTID_PURGED in the output.
5963
5986
*/
5964
- if (opt_databases || !opt_alldbs || !opt_dump_triggers
5965
- || !opt_routines || !opt_events )
5987
+ if (!flag )
5988
+ set_session_binlog ();
5989
+ else
5966
5990
{
5967
- fprintf (stderr ,"Warning: A partial dump from a server that has GTIDs will "
5968
- "by default include the GTIDs of all transactions, even "
5969
- "those that changed suppressed parts of the database. If "
5970
- "you don't want to restore GTIDs, pass "
5971
- "--set-gtid-purged=OFF. To make a complete dump, pass "
5972
- "--all-databases --triggers --routines --events. \n" );
5973
- }
5991
+ if (flag && (opt_databases || !opt_alldbs || !opt_dump_triggers
5992
+ || !opt_routines || !opt_events ))
5993
+ {
5994
+ fprintf (stderr ,"Warning: A partial dump from a server that has GTIDs will "
5995
+ "by default include the GTIDs of all transactions, even "
5996
+ "those that changed suppressed parts of the database. If "
5997
+ "you don't want to restore GTIDs, pass "
5998
+ "--set-gtid-purged=OFF. To make a complete dump, pass "
5999
+ "--all-databases --triggers --routines --events. \n" );
6000
+ }
5974
6001
5975
- set_session_binlog (FALSE);
5976
- if (add_set_gtid_purged (mysql_con ))
5977
- {
5978
- mysql_free_result (gtid_mode_res );
5979
- return TRUE;
6002
+ if (add_set_gtid_purged (mysql_con ))
6003
+ return TRUE;
5980
6004
}
5981
6005
}
5982
6006
else /* gtid_mode is off */
5983
6007
{
5984
- if (opt_set_gtid_purged_mode == SET_GTID_PURGED_ON )
6008
+ if (flag && opt_set_gtid_purged_mode == SET_GTID_PURGED_ON )
5985
6009
{
5986
6010
fprintf (stderr , "Error: Server has GTIDs disabled.\n" );
5987
- mysql_free_result (gtid_mode_res );
5988
6011
return TRUE;
5989
6012
}
5990
6013
}
5991
6014
5992
- mysql_free_result (gtid_mode_res );
5993
6015
return FALSE;
5994
6016
}
5995
6017
@@ -6323,12 +6345,10 @@ int main(int argc, char **argv)
6323
6345
if (opt_slave_apply && add_stop_slave ())
6324
6346
goto err ;
6325
6347
6326
-
6327
- /* Process opt_set_gtid_purged and add SET @@GLOBAL.GTID_PURGED if required. */
6328
- if (process_set_gtid_purged (mysql ))
6348
+ /* Process opt_set_gtid_purged and add SET disable binlog if required. */
6349
+ if (process_set_gtid_purged (mysql , FALSE))
6329
6350
goto err ;
6330
6351
6331
-
6332
6352
if (opt_master_data && do_show_master_status (mysql ))
6333
6353
goto err ;
6334
6354
if (opt_slave_data && do_show_slave_status (mysql ))
@@ -6381,11 +6401,9 @@ int main(int argc, char **argv)
6381
6401
if (opt_slave_data && do_start_slave_sql (mysql ))
6382
6402
goto err ;
6383
6403
6384
- /*
6385
- if --set-gtid-purged, restore binlog at the end of the session
6386
- if required.
6387
- */
6388
- set_session_binlog (TRUE);
6404
+ /* Process opt_set_gtid_purged and add SET @@GLOBAL.GTID_PURGED if required. */
6405
+ if (process_set_gtid_purged (mysql , TRUE))
6406
+ goto err ;
6389
6407
6390
6408
/* add 'START SLAVE' to end of dump */
6391
6409
if (opt_slave_apply && add_slave_statements ())
0 commit comments