diff --git a/client/mysql.cc b/client/mysql.cc index 3158d270ae7d5..e242193ea4c5c 100644 --- a/client/mysql.cc +++ b/client/mysql.cc @@ -270,8 +270,7 @@ static int sigint_received= 0; #endif static char *current_host,*current_db,*current_user=0,*opt_password=0, *current_prompt=0, *delimiter_str= 0, - *default_charset= (char*) MYSQL_AUTODETECT_CHARSET_NAME, - *opt_init_command= 0; + *default_charset= (char*) MYSQL_AUTODETECT_CHARSET_NAME; static char *histfile; static char *histfile_tmp; static String glob_buffer,old_buffer; @@ -308,6 +307,7 @@ static uint opt_protocol=0; static const char *opt_protocol_type= ""; #include "sslopt-vars.h" +#include "common-cli-vars.h" const char *default_dbug_option="d:t:o,/tmp/mariadb.trace"; @@ -1833,10 +1833,6 @@ static struct my_option my_long_options[] = 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0}, {"ignore-spaces", 'i', "Ignore space after function names.", &ignore_spaces, &ignore_spaces, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0}, - {"init-command", 0, - "SQL Command to execute when connecting to MariaDB server. Will " - "automatically be re-executed when reconnecting.", &opt_init_command, - &opt_init_command, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0}, {"line-numbers", 0, "Write line numbers for errors.", &line_numbers, &line_numbers, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0, 0}, {"skip-line-numbers", 'L', "Don't write line number for errors.", 0, 0, 0, @@ -1948,6 +1944,7 @@ static struct my_option my_long_options[] = &opt_mysql_unix_port, &opt_mysql_unix_port, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0}, #include "sslopt-longopts.h" +#include "common-cli-longopts.h" {"table", 't', "Output in table format.", &output_tables, &output_tables, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0}, {"tee", OPT_TEE, diff --git a/client/mysqldump.cc b/client/mysqldump.cc index d86eda1a43e66..94923468a6e70 100644 --- a/client/mysqldump.cc +++ b/client/mysqldump.cc @@ -188,6 +188,7 @@ static MYSQL_RES *routine_res, *routine_list_res, *slave_status_res= NULL; #include +#include FILE *md_result_file= 0; FILE *stderror_file=0; @@ -592,6 +593,7 @@ static struct my_option my_long_options[] = &opt_mysql_unix_port, &opt_mysql_unix_port, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0}, #include +#include {"system", 0, "Dump system tables as portable SQL", &opt_system, &opt_system, &opt_system_types, GET_SET, REQUIRED_ARG, 0, 0, 0, 0, 0, 0}, {"tab",'T', @@ -2106,6 +2108,9 @@ static MYSQL* connect_to_db(char *host, char *user,char *passwd) if (opt_default_auth && *opt_default_auth) mysql_options(con, MYSQL_DEFAULT_AUTH, opt_default_auth); + if (opt_init_command && *opt_init_command) + mysql_options(con, MYSQL_INIT_COMMAND, opt_init_command); + mysql_options(con, MYSQL_OPT_CONNECT_ATTR_RESET, 0); mysql_options4(con, MYSQL_OPT_CONNECT_ATTR_ADD, "program_name", "mysqldump"); diff --git a/client/mysqlslap.c b/client/mysqlslap.c index a90c059c65a01..e69641560673e 100644 --- a/client/mysqlslap.c +++ b/client/mysqlslap.c @@ -86,6 +86,7 @@ #include #include #include +#include #ifndef _WIN32 #include #endif @@ -118,8 +119,7 @@ static char *host= NULL, *opt_password= NULL, *user= NULL, *default_engine= NULL, *pre_system= NULL, *post_system= NULL, - *opt_mysql_unix_port= NULL, - *opt_init_command= NULL; + *opt_mysql_unix_port= NULL; static char *opt_plugin_dir= 0, *opt_default_auth= 0; const char *delimiter= "\n"; @@ -617,11 +617,6 @@ static struct my_option my_long_options[] = {"host", 'h', "Connect to host. Defaults in the following order: " "$MARIADB_HOST, and then localhost", &host, &host, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0}, - {"init-command", 0, - "SQL Command to execute when connecting to MariaDB server. Will " - "automatically be re-executed when reconnecting.", - &opt_init_command, &opt_init_command, 0, - GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0}, {"iterations", 'i', "Number of times to run the tests.", &iterations, &iterations, 0, GET_UINT, REQUIRED_ARG, 1, 0, 0, 0, 0, 0}, {"no-drop", 0, "Do not drop the schema after the test.", @@ -685,6 +680,7 @@ static struct my_option my_long_options[] = &opt_mysql_unix_port, &opt_mysql_unix_port, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0}, #include +#include #ifndef DONT_ALLOW_USER_CHANGE {"user", 'u', "User for login if not current user.", &user, &user, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0}, diff --git a/include/common-cli-longopts.h b/include/common-cli-longopts.h new file mode 100644 index 0000000000000..fc572f758173b --- /dev/null +++ b/include/common-cli-longopts.h @@ -0,0 +1,10 @@ +#ifndef COMMON_CLI_LONGOPTS_INCLUDED +#define COMMON_CLI_LONGOPTS_INCLUDED + + {"init-command", 0, + "SQL Command to execute when connecting to MariaDB server. Will " + "automatically be re-executed when reconnecting.", + &opt_init_command, &opt_init_command, 0, + GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0}, + +#endif /* COMMON_CLI_LONGOPTS_INCLUDED */ diff --git a/include/common-cli-vars.h b/include/common-cli-vars.h new file mode 100644 index 0000000000000..2d726394b1a51 --- /dev/null +++ b/include/common-cli-vars.h @@ -0,0 +1,6 @@ +#ifndef COMMON_CLI_VARS_INCLUDED +#define COMMON_CLI_VARS_INCLUDED + +static char *opt_init_command= 0; + +#endif /* COMMON_CLI_VARS_INCLUDED */ diff --git a/mysql-test/main/mysqldump-init-command.result b/mysql-test/main/mysqldump-init-command.result new file mode 100644 index 0000000000000..2c91a417f5ecd --- /dev/null +++ b/mysql-test/main/mysqldump-init-command.result @@ -0,0 +1,23 @@ +CREATE DATABASE 28911_mysqldump; +USE 28911_mysqldump; +CREATE TABLE t1(id INT, ts TIMESTAMP); +SET time_zone='+00:00'; +INSERT INTO t1 VALUES (1, '2026-01-01 12:00:00'); + +# dump with --init-command="SET SESSION time_zone='+05:00'" (INSERT shows 17:00:00) +/*M!999999\- enable the sandbox mode */ +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8mb4 */; +CREATE TABLE `t1` ( + `id` int(11) DEFAULT NULL, + `ts` timestamp NULL DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci; +/*!40101 SET character_set_client = @saved_cs_client */; +SET @OLD_AUTOCOMMIT=@@AUTOCOMMIT, @@AUTOCOMMIT=0; +INSERT INTO `t1` VALUES +(1,'2026-01-01 17:00:00'); +COMMIT; +SET AUTOCOMMIT=@OLD_AUTOCOMMIT; +DROP TABLE t1; +DROP DATABASE 28911_mysqldump; +# End of MDEV-28911 diff --git a/mysql-test/main/mysqldump-init-command.test b/mysql-test/main/mysqldump-init-command.test new file mode 100644 index 0000000000000..4e2554d2a262b --- /dev/null +++ b/mysql-test/main/mysqldump-init-command.test @@ -0,0 +1,18 @@ +--source include/not_embedded.inc + +# MDEV-28911: Add --init-command option to mariadb-dump / mysqldump + +CREATE DATABASE 28911_mysqldump; +USE 28911_mysqldump; +CREATE TABLE t1(id INT, ts TIMESTAMP); +SET time_zone='+00:00'; +INSERT INTO t1 VALUES (1, '2026-01-01 12:00:00'); + +--echo +--echo # dump with --init-command="SET SESSION time_zone='+05:00'" (INSERT shows 17:00:00) +--exec $MYSQL_DUMP --compact --skip-comments --skip-tz-utc --init-command="SET SESSION time_zone='+05:00'" 28911_mysqldump + +DROP TABLE t1; +DROP DATABASE 28911_mysqldump; + +--echo # End of MDEV-28911