目錄
- 資料備份
- 概述
- 使用MySQLdump命令備份
- 資料恢復
- 概述
- MySQL命令恢復
資料備份
概述
資料備份是非常重要的,由于系統崩潰等一系列的原因都可能會導致資料庫的資料丟失,因此應該定期地備份資料庫,使得在意外情況發生時,盡可能減少損失,
使用MySQLdump命令備份
MySQLdump是MySQL提供的一個非常有用的資料庫備份工具,在使用該工具時可以生成一個文本檔案,該檔案會包含多個CREATE和INSERT陳述句,使用該陳述句可以重新創建表和插入資料,
語法格式:mysqldump -u root -p 資料庫名 >路徑/yingmo.sql
這里先創建一個名為yingmo的資料庫了三張表,

1.MySQLdump備份單個資料庫中所有表
mysqldump -u root -p yingmo >F:/yingmo.sql
Enter password: ****
這時在本地檔案夾下會生成一個名為yingmo.sql的檔案

打開看一下

2.MySQLdump備份資料庫某個表
備份yingmo資料庫中的student表
mysqldump -u root -p yingmo student>F:/yingmo.sql
Enter password: ****

3.MySQLdump備份多個資料庫
要備份多個資料庫,需要使用–databases引數 多個資料庫名稱之間用空格隔開
語法格式 mysqldump -u root -p --databases 資料庫1 資料庫2>F:/yingmo.sql
再創建一個名為wb的資料庫且創建一張表
mysqldump -u root -p --databases yingmo wb >F:/yingmo.sql
Enter password: ****

4.MySQLdump備份所有資料庫
語法格式:mysqldump -u root -p --all-databases >F:/yingmo.sql
無需指定資料庫名
資料恢復
概述
在上述已經備份好資料的基礎上,通過恢復資料來盡量減少資料的丟失,
在上面備份資料時我們就已經知道,在備份的時候就包含了CREATE和INSERT陳述句,所以可以使用MySQL陳述句進行恢復,
MySQL命令恢復
這里先把之前建立的資料庫給洗掉,包括:yingmo wb
mysql> drop database yingmo;
Query OK, 7 rows affected (0.05 sec)
mysql> drop database wb;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| kefu |
| mysql |
+--------------------+
3 rows in set (0.00 sec)
以下開始進行恢復
語法格式:mysql -u root -p < F:/yingmo.sql無需指定資料庫名
C:\Users\acer>mysql -u root -p < F:/yingmo.sql
Enter password: ****
C:\Users\acer>mysql -u root -p
Enter password: ****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4 to server version: 5.1.60-community-log
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| kefu |
| mysql |
| wb |
| yingmo |
+--------------------+
5 rows in set (0.00 sec)
mysql> use yingmo;
Database changed
mysql> show tables;
+------------------+
| Tables_in_yingmo |
+------------------+
| course |
| sc |
| student |
| view_003 |
| view_c001 |
| view_male |
| view_sg |
+------------------+
7 rows in set (0.00 sec)
OK已經恢復完成,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/210889.html
標籤:其他
