mysqldude

Copying a database, users and privileges between two servers

Version compatibility

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.8
mysqldump -r file db, restore into a new database (original)
original post
✓✓✓✓✗ 3546✓✓✓✓
gtid_mode of the server (decides whether mysqldump writes SET @@GLOBAL.GTID_PURGED)
version check
✓✓✓✓≠✗ 1193✗ 1193✗ 1193✗ 1193
Same copy with --set-gtid-purged=OFF (MySQL; avoids 3546 on GTID servers)
modern rewrite
✓✓✓✓✓✗ exit 7✗ exit 7✗ exit 7✗ exit 7
mysqldump --flush-privileges mysql (users as raw grant tables, original)
original post
✓✓✓✓✓✗ exit 1✗ exit 1✗ exit 1✗ exit 1
SHOW CREATE USER + SHOW GRANTS for 5 users, drop them, replay (MySQL 8.0+ hashes contain random binary bytes)
modern rewrite
✗ 1064✓✗ 1827✗ 1827✗ exit 1✓✓✓✓
Same replay with print_identified_with_as_hex = ON (MySQL 8.0.17+)
modern rewrite
✗ 1193✗ 1193✓✓✓✗ 1193✗ 1193✗ 1193✗ 1193
mariadb-dump --system=users (MariaDB 10.5+ logical user export)
modern rewrite
✗ exit 7✗ exit 7✗ exit 7✗ exit 7✗ exit 7✓✓✓✓
mysqldump -r file db, restore into a new database (original) original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✗ 3546
MariaDB 10.6✓10.11✓11.4✓11.8✓
gtid_mode of the server (decides whether mysqldump writes SET @@GLOBAL.GTID_PURGED) version check
MySQL 5.6✓5.7✓8.0✓8.4✓9≠
MariaDB 10.6✗ 119310.11✗ 119311.4✗ 119311.8✗ 1193
Same copy with --set-gtid-purged=OFF (MySQL; avoids 3546 on GTID servers) modern rewrite
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✗ exit 710.11✗ exit 711.4✗ exit 711.8✗ exit 7
mysqldump --flush-privileges mysql (users as raw grant tables, original) original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✗ exit 110.11✗ exit 111.4✗ exit 111.8✗ exit 1
SHOW CREATE USER + SHOW GRANTS for 5 users, drop them, replay (MySQL 8.0+ hashes contain random binary bytes) modern rewrite
MySQL 5.6✗ 10645.7✓8.0✗ 18278.4✗ 18279✗ exit 1
MariaDB 10.6✓10.11✓11.4✓11.8✓
Same replay with print_identified_with_as_hex = ON (MySQL 8.0.17+) modern rewrite
MySQL 5.6✗ 11935.7✗ 11938.0✓8.4✓9✓
MariaDB 10.6✗ 119310.11✗ 119311.4✗ 119311.8✗ 1193
mariadb-dump --system=users (MariaDB 10.5+ logical user export) modern rewrite
MySQL 5.6✗ exit 75.7✗ exit 78.0✗ exit 78.4✗ exit 79✗ exit 7
MariaDB 10.6✓10.11✓11.4✓11.8✓
✓works ✓ 1062fails with the error the step demonstrates ⚠works with a warning ≠runs, result differs ✗fails Tested 2026-11-25 on 5.6.51, 5.7.44, 8.0.46, 8.4.13, 9.7.4, 10.6.28, 10.11.19, 11.4.13, 11.8.9
What the warnings and failures say (13)
  • mysqldump -r file db, restore into a new database (original) (MySQL 9): error 3546: ERROR 3546 (HY000) at line 24: @@GLOBAL.GTID_PURGED cannot be changed: the added gtid set must not overlap with @@GLOBAL.GTID_EXECUTED
  • gtid_mode of the server (decides whether mysqldump writes SET @@GLOBAL.GTID_PURGED) (MySQL 9): rows differ
  • gtid_mode of the server (decides whether mysqldump writes SET @@GLOBAL.GTID_PURGED) (MariaDB 10.6, MariaDB 10.11, MariaDB 11.4, MariaDB 11.8): error 1193: Unknown system variable 'gtid_mode'
  • Same copy with --set-gtid-purged=OFF (MySQL; avoids 3546 on GTID servers) (MariaDB 10.6, MariaDB 10.11): error exit 7: mysqldump: unknown variable 'set-gtid-purged=OFF'
  • Same copy with --set-gtid-purged=OFF (MySQL; avoids 3546 on GTID servers) (MariaDB 11.4, MariaDB 11.8): error exit 7: mariadb-dump: unknown variable 'set-gtid-purged=OFF'
  • mysqldump --flush-privileges mysql (users as raw grant tables, original) (MariaDB 10.6, MariaDB 10.11, MariaDB 11.4, MariaDB 11.8): error exit 1:
  • SHOW CREATE USER + SHOW GRANTS for 5 users, drop them, replay (MySQL 8.0+ hashes contain random binary bytes) (MySQL 5.6): error 1064: ERROR 1045 (28000): Access denied for user 'copyme1'@'127.0.0.1' (using password: YES)
  • SHOW CREATE USER + SHOW GRANTS for 5 users, drop them, replay (MySQL 8.0+ hashes contain random binary bytes) (MySQL 8.0, MySQL 8.4): error 1827: ERROR 1827 (HY000) at line 1: The password hash doesn't have the expected format.
  • SHOW CREATE USER + SHOW GRANTS for 5 users, drop them, replay (MySQL 8.0+ hashes contain random binary bytes) (MySQL 9): error exit 1: ERROR at line 1: Unknown command '\t'.
  • Same replay with print_identified_with_as_hex = ON (MySQL 8.0.17+) (MySQL 5.6, MySQL 5.7, MariaDB 10.6, MariaDB 10.11, MariaDB 11.4, MariaDB 11.8): error 1193: ERROR 1045 (28000): Access denied for user 'copyme1'@'127.0.0.1' (using password: YES)
  • mariadb-dump --system=users (MariaDB 10.5+ logical user export) (MySQL 5.6): error exit 7: mysqldump: unknown variable 'system=users'
  • mariadb-dump --system=users (MariaDB 10.5+ logical user export) (MySQL 5.7): error exit 7: mysqldump: [ERROR] unknown variable 'system=users'
  • mariadb-dump --system=users (MariaDB 10.5+ logical user export) (MySQL 8.0, MySQL 8.4, MySQL 9): error exit 7: mysqldump: [ERROR] unknown variable 'system=users'.

The original article

First published 2011-03-02. The text below is the original; the table above shows how it behaves on current versions.

Recently I have seen this question raised numerous times. This is particularly important when databases need to be migrated off of one server and on to another. Sometimes a copy of all the database files (while the MySQL server is down) can work. However, it is a better way using MySQL available tools.

It should be noted that the commands and responses are on a MySQL 5.0 server. Different version of the MySQL server may give more or different information but the ideas and concepts remain valid in this document.

Before starting down the path of explaining how this should work we should have a look at any MySQL server. The server contains numerous databases including an internal database used to store internal information such as users, privileges, functions, stored procedures etc. This information is stored in the “mysql” database. If you issue the “show databases” command as the root or admin user you will see the existence of this database:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+

The “mysql” database contains numerous tables that provide information about the tables in a particular database, stored procedures that are created for that database:

mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| func                      |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| host                      |
| proc                      |
| procs_priv                |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
17 rows in set (0.00 sec)

The first step is to actually take a copy of the database. To take a backup of a database in MySQL we will use the mysqldump command. This can take numerous parameters but the most important ones are:

$ mysqldump -?
mysqldump  Ver 10.13 Distrib 5.5.9, for Linux (i686)
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Dumping structure and contents of MySQL databases and tables.
Usage: mysqldump [OPTIONS] database [tables]
OR     mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
OR     mysqldump [OPTIONS] --all-databases [OPTIONS]
...
--flush-privileges  Emit a FLUSH PRIVILEGES statement after dumping the mysql
                      database.  This option should be used any time the dump
                      contains the mysql database and any other database that
                      depends on the data in the mysql database for proper
                      restore.
-h, --host=name     Connect to host.
-p, --password[=name]
                      Password to use when connecting to server. If password is
                      not given it's solicited on the tty.
-P, --port=#        Port number to use for connection.
-u, --user=name     User for login if not current user.
-r, --result-file=name
                      Direct output to a given file. This option should be used
                      in systems (e.g., DOS, Windows) that use carriage-return
                      linefeed pairs (\r\n) to separate text lines. This option
                      ensures that only a single newline is used.
...

The “-r” and “—flush-privileges” are particularly important. The “-r” flag will generate the dump file in a consistent way across all platforms. So if you wish to dump a database on Windows and import it into a Linux MySQL server this will always work. The “—flush-privileges” appends the MySQL command “FLUSH PRIVILEGES” to the end of the dump file. This will be needed when we dump the users and privilege information into the database.

Now we are ready to start. We are assuming copying a database called “test” from one server to another:

$ mysqldump -u root -r test.sql -p test
Enter password:

The result from this command is a file called test.sql which contains every SQL statement for creating a tables, integrity constraints, indexes as well as adding the data into their respective tables.

We also need to take a copy of the “mysql” database which contains all the users accounts and privileges.

$ mysqldump -u root --flush-privileges -r mysql.sql -p mysql
Enter password:

Of course the mysql.sql command contains all users and all privileges. This file may be edited to strip out the users you do not wish to copy across and their respective privileges.

To restore these databases on the new server simply issue the following commands in the following order:

$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 432
Server version: 5.0.45 SUSE MySQL RPM

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> create database test;
Query OK, 1 row affected (0.00 sec)

mysql> use test;
Database changed

mysql> source test.sql
... starting loading the database and creating the various tables, indexes etc.

mysql> use mysql;
Database changed

mysql> source mysql.sql
... starting loading the users and privileges

If previously you had not dumped the “mysql” database with the “—flush-privileges” option you can still manually issue the “FLUSH PRIVILEGES” command which makes the privileges immediately recognized by the MySQL server.

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.02 sec)