MysqL 的逻辑备份
1. 什么是逻辑备份
2. 常用的逻辑备份场景
- 备份所有数据库
shell> MysqLdump [options] --all-databases
实际案例:备份所有数据库
[MysqL@localhost ~]$ MysqLdump -uroot -p --all-databases > /tmp/all_databases.sql
Enter password:
[MysqL@localhost ~]$ ls -lrt all_databases.sql
-rw-r--r-- 1 MysqL MysqL 136106866 Jul 23 17:02 all_databases.sql
shell> MysqLdump [options] --databases db_name ...
实际案例:备份数据库 tempdb
[MysqL@localhost ~]$ MysqLdump -uroot -p --databases tempdb > /tmp/db_tempdb.sql
Enter password:
[MysqL@localhost ~]$ ls -lrt db_tempdb.sql
-rw-r--r-- 1 MysqL MysqL 19602842 Jul 23 17:17 db_tempdb.sql
实际案例:备份数据库 tempdb 和 test111
[MysqL@localhost ~]$ MysqLdump -uroot -p --databases tempdb test111 > /tmp/db_tempdb_test111.sql
Enter password:
[MysqL@localhost ~]$ ls -lrt db_tempdb_test111.sql
-rw-r--r-- 1 MysqL MysqL 19604085 Jul 23 17:23 db_tempdb_test111.sql
- 备份一个或多个表
shell> MysqLdump [options] db_name [tbl_name ...]
实际案例:备份数据库tempdb的表customer
[MysqL@localhost ~]$ MysqLdump -uroot -p tempdb customer > /tmp/table_customer.sql
Enter password:
[MysqL@localhost ~]$ ls -lrt table_customer.sql
-rw-r--r-- 1 MysqL MysqL 2512 Jul 23 17:35 table_customer.sql
实际案例:备份数据库 tempdb 的表 customer 和 t1
[MysqL@localhost ~]$ MysqLdump -uroot -p tempdb customer t1 > /tmp/table_customer_t1.sql
Enter password:
[MysqL@localhost ~]$ ls -lrt table_customer_t1.sql
-rw-r--r-- 1 MysqL MysqL 3141 Jul 23 17:37 table_customer_t1.sql
- 备份表结构-不包含数据
实际案例:备份数据库 tempdbd 的表结构:
[MysqL@localhost ~]$ MysqLdump -uroot -p --databases tempdb -d > /tmp/structure_tempdb.sql
Enter password:
[MysqL@localhost ~]$ ls -lrt structure_db_tempdb.sql
-rw-r--r-- 1 MysqL MysqL 9987 Jul 23 17:40 structure_db_tempdb.sql
实际案例:备份数据库tempdb表customer的表结构
[MysqL@localhost ~]$ MysqLdump -uroot -p tempdb customer -d > /tmp/structure_table_customer.sql
Enter password:
[MysqL@localhost ~]$ ls -lrt structure_table_customer.sql
-rw-r--r-- 1 MysqL MysqL 2230 Jul 23 17:48 structure_table_customer.sql
3. 相关参数说明
MysqLdump 的选项很多,可以通过 --help 查看帮助:
以下为常用的参数(从 MysqL 官方文档摘录)
Option Name | Description | Introduced | Deprecated |
---|---|---|---|
–all-databases | Dump all tables in all databases | ||
–databases | Interpret all name arguments as database names | ||
–default-character-set | Specify default character set | ||
–events | Dump events from dumped databases | ||
–force | Continue even if an sql error occurs during a table dump | ||
–host | Host on which MysqL server is located | ||
–ignore-table | Do not dump given table | ||
–master-data | Write the binary log file name and position to the output | ||
–no-create-db | Do not write CREATE DATABASE statements | ||
–no-create-info | Do not write CREATE TABLE statements that re-create each dumped table | ||
–no-data | Do not dump table contents | ||
–order-by-primary | Dump each table’s rows sorted by its primary key, or by its first unique index | ||
–password | Password to use when connecting to server | ||
–port | TCP/IP port number for connection | ||
–quick | Retrieve rows for a table from the server a row at a time | ||
–routines | Dump stored routines (procedures and functions) from dumped databases | ||
–set-gtid-purged | Whether to add SET @@GLOBAL.GTID_PURGED to output | ||
–single-transaction | Issue a BEGIN sql statement before dumping data from server | ||
–socket | Unix socket file or Windows named pipe to use | ||
–tables | Override --databases or -B option |