In this blog, we’ll look at the
mysqlpump utility.
mysqlpump is a utility that performs logical backups (which means backing up your data as SQL statements instead of a raw copy of data files). It was added in MySQL Server version 5.7.8, and can be used to dump a database or a set of databases to a file and then loaded on another SQL server (not necessarily a MySQL server).
Its usage is similar to mysqldump, but it includes a new set of features. Many of the options are the same, but it was written from scratch to avoid being limited to mysqldump compatibility.
This feature provides more control over customizing your dumps, and filter the data that you need. Using this feature, you can be more selective with the data you want to dump (databases, tables, triggers, events, routines, users) and save file size, process time and transferring time while copying/moving the file to another host.
Keep in mind that there are some options that are mutually exclusive: e.g., if you use the --all-databases option, the --exclude-databases parameter won’t take effect. By default, mysqlpump will not dump the following databases unless you specify them using the --include-databases option: INFORMATION_SCHEMA, performance_schema, ndbinfo and sys.
Values for these options need to be declared by comma-separated listing. Using a “%” as a value for any of the exclude/include options acts as a wildcard. For example, you can dump all databases starting with “t” and “p” by adding the option --include-databases=t%,p% to the command line.
For users, routines, triggers and events, mysqlpump has --include-* and --exclude-* options with similar usage. Some specific notes:
This feature allows you to process several databases, and tables within the databases, in parallel. By default, mysqlpump uses one processing queue with two threads. You can increase the number of threads for this default queue with --default-parallelism. Unless you create additional queues, all the databases and/or tables you elect to dump go through the default queue.
To create additional queues you can use the --parallel-schemas option, which takes two parameters: the number of threads for the queue and the sub-set of databases this queue processes. As an example, you could run:
|
1 |
mysqlpump --include-databases=a,b,c,d,e,f,g,h --default-parallelism=3 --parallel-schemas=4:a,b<br> |
so that schemas c, d, e, f, g and h are processed by the default queue (which uses three threads), and then tables from schemas a and b are processed by a separate queue (that uses four threads). Database names should be included as a comma-separated list:
|
1 |
$ mysqlpump --parallel-schemas=4:example1,example2,example3 --parallel-schemas=3:example4,example5 > examples.sql<br>Dump progress: 0/1 tables, 250/261184 rows<br>Dump progress: 24/30 tables, 1204891/17893833 rows<br>Dump progress: 29/30 tables, 1755611/17893833 rows<br>Dump progress: 29/30 tables, 2309111/17893833 rows<br>...<br>Dump completed in 42424 milliseconds |
User accounts can be dumped using this tool. Here’s a comparison of our Percona Tool pt-show-grants versus mysqlpump to check their differences.
By default, mysqlpump doesn’t dump user account definitions (even while dumping the MySQL database). To include user accounts on the dump, you must specify the --users option.
Here’s an example on how use mysqlpump to get only user accounts dumped to a file:
|
1 |
$ mysqlpump --exclude-databases=% --exclude-triggers=% --users<br>-- Dump created by MySQL dump utility, version: 5.7.8-rc, linux-glibc2.5 (x86_64)<br>-- Dump start time: Thu Aug 27 17:10:10 2015<br>-- Server version: 5.7.8<br><br>SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;<br>SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;<br>SET @OLD_TIME_ZONE=@@TIME_ZONE;<br>SET TIME_ZONE='+00:00';<br>SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT;<br>SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS;<br>SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION;<br>SET NAMES utf8;<br>CREATE USER 'msandbox'@'127.%' IDENTIFIED WITH 'mysql_native_password' AS '*6C387FC3893DBA1E3BA155E74754DA6682D04747' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK;<br>GRANT ALL PRIVILEGES ON *.* TO 'msandbox'@'127.%';<br>CREATE USER 'msandbox_ro'@'127.%' IDENTIFIED WITH 'mysql_native_password' AS '*6C387FC3893DBA1E3BA155E74754DA6682D04747' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK;<br>GRANT SELECT, EXECUTE ON *.* TO 'msandbox_ro'@'127.%';<br>CREATE USER 'msandbox_rw'@'127.%' IDENTIFIED WITH 'mysql_native_password' AS '*6C387FC3893DBA1E3BA155E74754DA6682D04747' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK;<br>GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, SHOW DATABASES, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE ON *.* TO 'msandbox_rw'@'127.%';<br>CREATE USER 'rsandbox'@'127.%' IDENTIFIED WITH 'mysql_native_password' AS '*B07EB15A2E7BD9620DAE47B194D5B9DBA14377AD' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK;<br>GRANT REPLICATION SLAVE ON *.* TO 'rsandbox'@'127.%';<br>CREATE USER 'furrywall'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*AB8D50A9E3B8D1F3ACE85C54736B5BF472B44539' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT LOCK;<br>GRANT USAGE ON *.* TO 'furrywall'@'localhost';<br>CREATE USER 'msandbox'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*6C387FC3893DBA1E3BA155E74754DA6682D04747' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK;<br>GRANT ALL PRIVILEGES ON *.* TO 'msandbox'@'localhost';<br>CREATE USER 'msandbox_ro'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*6C387FC3893DBA1E3BA155E74754DA6682D04747' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK;<br>GRANT SELECT, EXECUTE ON *.* TO 'msandbox_ro'@'localhost';<br>CREATE USER 'msandbox_rw'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*6C387FC3893DBA1E3BA155E74754DA6682D04747' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK;<br>GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, SHOW DATABASES, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE ON *.* TO 'msandbox_rw'@'localhost';<br>CREATE USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*6C387FC3893DBA1E3BA155E74754DA6682D04747' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK;<br>GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;<br>CREATE USER 'testuser'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*6E543F385210D9BD42A4FDB4BB23FD2C31C95462' REQUIRE NONE PASSWORD EXPIRE INTERVAL 30 DAY ACCOUNT UNLOCK;<br>GRANT USAGE ON *.* TO 'testuser'@'localhost';<br>SET TIME_ZONE=@OLD_TIME_ZONE;<br>SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT;<br>SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS;<br>SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION;<br>SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;<br>SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;<br>-- Dump end time: Thu Aug 27 17:10:10 2015<br>Dump completed in 823 milliseconds |
As you can see, above the tool makes sure the session uses known values for timezone and character sets. This won’t affect users, it’s part of the dump process to ensure correctness while restoring on the destination.
Comparing it with pt-show-grants from Percona Toolkit, we can see that mysqlpump dumps the CREATE USER information as well. The statements produced by mysqlpump are the right thing to run to recreate users (and should be the preferred method), especially because of the sql_mode NO_AUTO_CREATE_USERS. If enabled, it renders pt-show-grants useless.
Here’s an example of pt-show-grants usage:
|
1 |
$ pt-show-grants --host 127.0.0.1 --port 5708 --user msandbox --ask-pass<br>Enter password: <br>-- Grants dumped by pt-show-grants<br>-- Dumped from server 127.0.0.1 via TCP/IP, MySQL 5.7.8-rc at 2015-08-27 17:06:52<br>-- Grants for 'furrywall'@'localhost'<br>GRANT USAGE ON *.* TO 'furrywall'@'localhost';<br>-- Grants for 'msandbox'@'127.%'<br>GRANT ALL PRIVILEGES ON *.* TO 'msandbox'@'127.%';<br>-- Grants for 'msandbox'@'localhost'<br>GRANT ALL PRIVILEGES ON *.* TO 'msandbox'@'localhost';<br>-- Grants for 'msandbox_ro'@'127.%'<br>GRANT EXECUTE, SELECT ON *.* TO 'msandbox_ro'@'127.%';<br>-- Grants for 'msandbox_ro'@'localhost'<br>GRANT EXECUTE, SELECT ON *.* TO 'msandbox_ro'@'localhost';<br>-- Grants for 'msandbox_rw'@'127.%'<br>GRANT ALTER, CREATE, CREATE TEMPORARY TABLES, DELETE, DROP, EXECUTE, INDEX, INSERT, LOCK TABLES, SELECT, SHOW DATABASES, UPDATE ON *.* TO 'msandbox_rw'@'127.%';<br>-- Grants for 'msandbox_rw'@'localhost'<br>GRANT ALTER, CREATE, CREATE TEMPORARY TABLES, DELETE, DROP, EXECUTE, INDEX, INSERT, LOCK TABLES, SELECT, SHOW DATABASES, UPDATE ON *.* TO 'msandbox_rw'@'localhost';<br>-- Grants for 'root'@'localhost'<br>GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;<br>GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION;<br>-- Grants for 'rsandbox'@'127.%'<br>GRANT REPLICATION SLAVE ON *.* TO 'rsandbox'@'127.%';<br>-- Grants for 'testuser'@'localhost'<br>GRANT USAGE ON *.* TO 'testuser'@'localhost'; |
Resources
RELATED POSTS