Skip to content
/ server Public
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions client/mysqldump.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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=<host>, MASTER_PORT=<port>' to 'CHANGE MASTER TO..' "
"in dump produced with --dump-slave.", &opt_include_master_host_port,
Expand Down Expand Up @@ -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");
Expand Down
22 changes: 22 additions & 0 deletions mysql-test/main/mysqldump-init-command.result
Original file line number Diff line number Diff line change
@@ -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;
16 changes: 16 additions & 0 deletions mysql-test/main/mysqldump-init-command.test
Original file line number Diff line number Diff line change
@@ -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;