利用mysqldump:
適用情況:本質上是把表結構和數據還原成sql語句,所以可以備份表結構,來進行mysql備份恢復
備份:
- 單表備份:mysqldump -u用戶名 -p 數據庫名 表名 > 備份文件路徑
- 多表備份:mysqldump -u用戶名 -p 數據庫名 表名1 [其他表名] > 備份文件路徑
- 單個數據庫備份:mysqldump -u用戶名 -p 數據庫名 > 備份文件路徑
- 多個數據庫備份:mysqldump -u用戶名 -p --databases 數據庫名1 數據庫名2 [其他數據庫] > 備份文件路徑
- 備份所有數據庫:mysqldump -u用戶名 -p --all --databases > 備份文件路徑
還原:
- mysqldump -u用戶名 -p密碼 [數據庫名] < 備份文件路徑 【指定數據庫時代表只恢復指定數據庫的備份,不指定為恢復備份文件中所有內容】
補充:
- mysqldump還可以僅僅導出表數據【不過考慮到有select into outfile了,這里就不介紹了】
利用select into outfile:
適用情況:單表數據備份,只能復制表數據,不能復制表結構。
備份:
- 語法:select *\字段列表 from 數據源 into outfile 備份文件目標路徑 [fields 字段處理] [lines 行處理]; 【備份文件目標路徑中文件必須不存在】
- fields 字段處理是可選的,是每個字段的數據如何存儲,有以下即可小選項
- enclosed by:以什么字符來包裹數據,默認是空字符
- terminated by :以什么字符作為字段數據結尾,默認是\t,退格符
- escaped by: 特殊符號用什么方法處理,默認是"\\"
- optionally enclosed by:以什么字符來包裹數據類型為字符串的數據。
- 語法:
select * from class into outfile "d:\c1.txt" fields enclosed by '-'; select * from class into outfile "d:\c1.txt" fields enclosed by '-' optionally enclosed by "'";
- lines 行處理是可選的
- starting by :每行以什么開始,默認是空字符串
- terminated by :每行記錄以什么結束,默認是“\n”
- 語法:
select * from class into outfile "d:\c.txt" lines starting by 'start:'; select * from class into outfile "d:\c1.txt" lines starting by 'start:' terminated by ' \n';
注意:在5.7新增了一個特性,secure_file_priv,它會限制數據的導出,secure-file-priv參數是用來限制LOAD DATA, SELECT ... OUTFILE, and LOAD_FILE()傳到哪個指定目錄的。
還原:
- 語法:load date infile 備份文件路徑 into table 表名 [fields 字段處理] [lines 行處理];【要把之前的字段處理和行處理都寫下來,不然無法正確區分數據】mysql安裝教程
其它
- 備份:
- 使用mysql命令備份
- 利用xtraback備份
- 利用backup table
- 還原:
- 使用mysqlimport導入數據文件。
- 使用restore還原
- 使用source還原