標籤:備份
F.1 mysqldump的工作原理
利用mysqldump命令備份資料的過程,實際上就是把資料從mysql庫裡以邏輯的sql語句的形式直接輸出或者產生備份檔案的過程。
F.2 備份F.2.1備份單個資料庫,聯絡多種參數使用
mysql資料庫內建的備份命令mysqldump
文法:mysqldump -h資料庫地址 -u使用者名稱 -p資料庫 >備份的目錄和檔案
範例1:備份名字為test123的庫
a、查看備份前的資料
[[email protected] ~]# mysql -h 127.0.0.1-u root -poracle -e "show databases;use test123;show tables;select * fromtest;"
Warning: Using a password on thecommand line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| 3306 |
| mysql |
| performance_schema |
| test |
| test123 |
| testtest |
+--------------------+
+-------------------+
| Tables_in_test123 |
+-------------------+
| student |
| test |
| test01 |
| test02 |
+-------------------+
+----+--------+------+----------+------+
| id | name | age | password | sex |
+----+--------+------+----------+------+
| 1 | test01 | NULL | test01 |NULL |
| 2 | test02 | NULL | test02 |NULL |
| 3 | test03 | NULL | test03 |NULL |
| 4 | test04 | NULL | test04 |NULL |
+----+--------+------+----------+------+
b、執行備份的命令(推薦使用-B參數)
[[email protected] ~]# mysqldump-h127.0.0.1 -uroot -p test123 >/opt/test123_bak.sql
Enter password:
c、檢查備份的結果
egrep -v “#|\*|--|^$”/opt/mysql_bak.sql
650) this.width=650;" src="/e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" height="152" width="407" alt="spacer.gif" />
注意:其中在恢複的時候,首先做的操作是刪除表,之後建立表的過程
d、使用-B進行備份
(1、在備份檔案中會產生使用備份的資料庫(test123),不然,在恢複的時候需要指定恢複的資料庫;2、在備份檔案中會有create database db資訊;)
[[email protected] ~]# mysqldump-h127.0.0.1 -uroot -poracle -B test123 >/opt/test123_B_bak.sql
f、檢查備份的結果
egrep -v “#|\*|--|^$”/opt/test123_B_bak.sql
650) this.width=650;" src="/e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" height="162" width="554" alt="spacer.gif" />
注意:添加的-B參數就相當於在恢複的時候指定了資料庫。
g、恢複的時候不需要指定資料庫和字元集
mysql> drop table test;
Query OK, 0 rows affected (0.64 sec)
[[email protected] ~]# mysql -h127.0.0.1-uroot -p </opt/test123_B_bak.sql
Enter password:
[[email protected] ~]# mysql -h127.0.0.1-uroot -poracle -e "use test123;select * from test;"
Warning: Using a password on the commandline interface can be insecure.
+----+--------+------+----------+------+
| id | name | age | password | sex |
+----+--------+------+----------+------+
| 1 | test01 | NULL | test01 |NULL |
| 2 | test02 | NULL | test02 |NULL |
| 3 | test03 | NULL | test03 |NULL |
| 4 | test04 | NULL | test04 |NULL |
+----+--------+------+----------+------+
h、備份的時候使用gzip進行壓縮
(使用壓縮可以減少使用的空間)
[[email protected] ~]# mysqldump-h127.0.0.1 -uroot -poracle -B test123 |gzip >/opt/test123_B.gz
[[email protected] ~]# ll /opt
total 24
-rw-r--r--. 1 root mysql 4435 May23 23:52 test123_bak.sql
-rw-r--r--. 1 root mysql 4583 May24 00:17 test123_B_bak.sql
-rw-r--r--. 1 root mysql 1057 May24 00:46 test123_B.gz
由上列資訊可以看到,其中沒有通過-B參數的比通過-B參數的備份要小,通過-B參數和gzip壓縮的是最小的,比例大概為4:1(不能當做通用值使用)
總結:在通過mysqldump備份的時候參數要使用-B(省略需要指定資料庫use database和create database db資訊),使用gzip(減少備份所佔用的空間)
例如:
mysqldump -h127.0.0.1 -uroot -poracle -B 需要備份的資料庫 |gzip >需要備份到的目錄及備份檔案的名字(注意:在不使用gzip時,尾碼是sql;使用gzip尾碼名是gz)
A.2.2備份多個資料庫
[[email protected] ~]# mysqldump-h127.0.0.1 -uroot -poracle -B test123 testtest |gzip >/opt/test123_testtest_bak.gz
[[email protected] ~]# ll /opt
total 32
-rw-r-----. 1 root mysql 345 May 23 06:09 mysqlbin_test.000001.bak
-rw-r--r--. 1 root mysql 4435 May 2323:52 test123_bak.sql
-rw-r--r--. 1 root mysql 4583 May 2400:17 test123_B_bak.sql
-rw-r--r--. 1 root mysql 1057 May 2400:46 test123_B.gzip
-rw-r--r--. 1 root mysql 1131 May 2401:52 test123_testtest_bak.gz
本文出自 “技術博” 部落格,謝絕轉載!
MySQL系列之F------MySQL備份