MySQL 的邏輯備份
邏輯備份的最大優(yōu)點(diǎn)是對于所有存儲引擎都可以用同樣的方法來備份,是目前中小型系統(tǒng)最常使用最簡單的備份方式。本小節(jié)將主要介紹 MySQL 的邏輯備份。
1. 什么是邏輯備份
簡單的說,邏輯備份就是將數(shù)據(jù)庫中的數(shù)據(jù)備份為文本文件,備份文件可以查看和編輯。針對中小型系統(tǒng),邏輯備份要來的簡單高效。
2. 常用的邏輯備份場景
MySQL 中,mysqldump 是常用的邏輯備份工具。以下是常用的備份場景:
- 備份所有數(shù)據(jù)庫
shell> mysqldump [options] --all-databases
實際案例:備份所有數(shù)據(jù)庫
[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
- 備份一個或多個數(shù)據(jù)庫
shell> mysqldump [options] --databases db_name ...
實際案例:備份數(shù)據(jù)庫 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
實際案例:備份數(shù)據(jù)庫 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 ...]
實際案例:備份數(shù)據(jù)庫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
實際案例:備份數(shù)據(jù)庫 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
- 備份表結(jié)構(gòu)-不包含數(shù)據(jù)
實際案例:備份數(shù)據(jù)庫 tempdbd 的表結(jié)構(gòu):
[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
實際案例:備份數(shù)據(jù)庫tempdb表customer的表結(jié)構(gòu)
[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. 相關(guān)參數(shù)說明
mysqldump 的選項很多,可以通過 --help 查看幫助:
[mysql@localhost ~]$ mysqldump --help
以下為常用的參數(shù)(從 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 |
4. 小結(jié)
本小節(jié)通過常用的邏輯備份案例,介紹了 MySQL 邏輯備份工具 mysqldump 的具體使用方法。邏輯備份適合中小型系統(tǒng),大型系統(tǒng)請考慮物理備份。