diff --git a/client/mysqldump.cc b/client/mysqldump.cc index d86eda1a43e66..03720901f91a3 100644 --- a/client/mysqldump.cc +++ b/client/mysqldump.cc @@ -195,6 +195,7 @@ static uint opt_protocol= 0; static char *opt_plugin_dir= 0, *opt_default_auth= 0; static uint opt_parallel= 0; static char *opt_dir; +static char *opt_init_command= 0; /** A flag to indicate that backup uses multiple files for output. @@ -461,6 +462,10 @@ static struct my_option my_long_options[] = "be specified with both database and table names, e.g., " "--ignore-table=database.table.", 0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0}, + {"init-command", 0, + "SQL command to execute when connecting to the server. " + "Useful for setting session variables like SQL_MODE before the dump.", + &opt_init_command, &opt_init_command, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0}, {"include-master-host-port", 0, "Adds 'MASTER_HOST=, MASTER_PORT=' to 'CHANGE MASTER TO..' " "in dump produced with --dump-slave.", &opt_include_master_host_port, @@ -2106,6 +2111,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/mysql-test/main/mysqldump-init-command.result b/mysql-test/main/mysqldump-init-command.result new file mode 100644 index 0000000000000..a34a521fab751 --- /dev/null +++ b/mysql-test/main/mysqldump-init-command.result @@ -0,0 +1,22 @@ +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; diff --git a/mysql-test/main/mysqldump-init-command.test b/mysql-test/main/mysqldump-init-command.test new file mode 100644 index 0000000000000..ad6ad041dc383 --- /dev/null +++ b/mysql-test/main/mysqldump-init-command.test @@ -0,0 +1,16 @@ +--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;