mysqldump 備份單張表和恢復

huzhichengforce發表於2015-03-15
1、使用工具mysqldump 
步驟一:備份表
命令:mysqldump -uroot -p1qazxsw2 -S /var/lib/mysql/mysql.sock --tab=/soft/ huzhicheng user    ----huzhicheng為資料庫名稱,user 為我們備份的表名稱
在/soft 目錄下會有兩個檔案,一個字尾為.sql 一個為.txt .sql 為表結構.txt 儲存的是資料
步驟二:恢復表使用命令先恢復表結構
這裡我恢復到test庫裡面做測試,使用mysql 命令建立表使用的檔案就是我們剛才備份的.sql 檔案 命令列如下:
mysql -uroot -p1qazxsw2 -S /var/lib/mysql/mysql.sock test 完成後去test庫檢視錶是否建立
[root@mysql mysql]# mysql -uroot -p1qazxsw2
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 22
Server version: 5.6.22-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use test
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> desc user;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name  | varchar(10) | NO   |     | NULL    |       |
| sex   | char(1)     | YES  |     | NULL    |       |
| birth | date        | YES  |     | NULL    |       |
| addr  | varchar(50) | YES  |     | NULL    |       |
| phone | varchar(12) | YES  |     | NULL    |       |
| age   | varchar(3)  | YES  |     | NULL    |       |
| id    | varchar(10) | NO   | PRI |         |       |
| id2   | varchar(10) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
8 rows in set (0.00 sec)
表已經存在
下面恢復資料
登入資料庫:
[root@mysql mysql]# mysql -uroot -p1qazxsw2
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 22
Server version: 5.6.22-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use test
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql>load data infile '/soft/user.txt' into table user;
這樣就完成了資料的恢復。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26764973/viewspace-1460511/,如需轉載,請註明出處,否則將追究法律責任。

相關文章