《MySQL備份與恢復(fù)Mysql學(xué)習(xí)》要點(diǎn):
本文介紹了MySQL備份與恢復(fù)Mysql學(xué)習(xí),希望對(duì)您有用。如果有疑問(wèn),可以聯(lián)系我們。
- [root@localhost?~]#?mysql?
- mysql>?create?database?auth;?
- Query?OK,?1?row?affected?(0.00?sec)?
- mysql>?use?auth;?
- Database?changed?
- mysql>?create?table?user(name?char(10)?not?null,ID?int(48));?
- Query?OK,?0?rows?affected?(0.04?sec)?
- mysql>?insert?into?user?values('crushlinux','123');?
- Query?OK,?1?row?affected?(0.01?sec)?
- mysql>?select?*?from?user;?
- +------------+------+?
- |?name?|?ID?|?
- +------------+------+?
- |?crushlinux?|?123?|?
- +------------+------+?
- 1?row?in?set?(0.00?sec)?
- mysql>?exit?
- Bye?
- [root@localhost?~]#?service?mysqld?stop?
- Shutting?down?MySQL..?[?OK?]?
- [root@localhost?~]#?yum?-y?install?xz?
- [root@localhost?~]#?tar?Jcf?mysql_all-$(date?+%F).tar.xz?/usr/local/mysql/data/?
- tar:?Removing?leading?`/'?from?member?names?
MYSQL教程模擬數(shù)據(jù)丟失!
?
- [root@localhost?~]#?mkdir?bak?
- [root@localhost?~]#?mv?/usr/local/mysql/data/*?bak/?
MYSQL教程恢復(fù)數(shù)據(jù):
?
- [root@localhost?~]#?mkdir?restore?
- [root@localhost?~]#?tar?xf?mysql_all-2016-12-08.tar.xz?-C?restore/?
- [root@localhost?~]#?mv?restore/usr/local/mysql/data/*?/usr/local/mysql/data/?
- [root@localhost?~]#?service?mysqld?start?
- Starting?MySQL..?[?OK?]?
- [root@localhost?~]#?mysql?
- mysql>?select?*?from?auth.user;?
- +------------+------+?
- |?name?|?ID?|?
- +------------+------+?
- |?crushlinux?|?123?|?
- +------------+------+?
- 1?row?in?set?(0.00?sec)?
MYSQL教程2、使用專(zhuān)用備份工具 mysqldump
MySQL 自帶的備份工具,相當(dāng)方便對(duì) MySQL 進(jìn)行備份.通過(guò)該命令工具可以將數(shù)據(jù)庫(kù)、
數(shù)據(jù)表或全部的庫(kù)導(dǎo)出為 SQL 腳本,在需要恢復(fù)時(shí)可進(jìn)行數(shù)據(jù)恢復(fù).
(1)對(duì)單個(gè)庫(kù)進(jìn)行完全備份
格式:mysqldump -u 用戶(hù)名 -p[密碼] [選項(xiàng)] [數(shù)據(jù)庫(kù)名] > /備份路徑/備份文件名
示例:
?
- [root@localhost?~]#?mkdir?/backup?
- [root@localhost?~]#?mysqldump?-uroot?-p123123?auth?>?/backup/auth-$(date?+%Y%m%d).sql?
- [root@localhost?~]#?echo?$??
- 0?
- [root@localhost?~]#?cat?/backup/auth-20161208.sql?
MYSQL教程(2)對(duì)多個(gè)庫(kù)進(jìn)行完全備份
格式:mysqldump -u 用戶(hù)名 -p [密碼] [選項(xiàng)] --databases 庫(kù)名 1 [庫(kù)名 2]… > /備份路徑/備份
文件名
示例:
- [root@localhost?~]#?mysqldump?-uroot?-p123123?--databases?mysql?auth?>?
- /backup/mysql+auth-$(date?+%Y%m%d).sql?
- [root@localhost?~]#?cat?/backup/mysql+auth-20161208.sql?
MYSQL教程(3)對(duì)所有庫(kù)進(jìn)行完全備份
格式:mysqldump -u 用戶(hù)名 -p [密碼] [選項(xiàng)] --all-databases > /備份路徑/備份文件名
示例:
?
- [root@localhost?~]#?mysqldump?-uroot?-p123123?--opt?--all-?
- databases?>/backup/mysql_all.$(date?+%Y%m%d).sql?
- ??
- [root@localhost?~]#?cat?/backup/mysql_all.20161208.sql?
- //--opt?加快備份速度,當(dāng)備份數(shù)據(jù)量大時(shí)使用<br>[root@localhost?~]#?cat?/backup/mysql_all.20160505.sql?
MYSQL教程(4)對(duì)表進(jìn)行完全備份
格式:mysqldump -u 用戶(hù)名 -p [密碼] [選項(xiàng)] 數(shù)據(jù)庫(kù)名 表名 > /備份路徑/備份文件名
示例:
?
- [root@localhost?~]#?mysqldump?-uroot?-p123123?auth?user?>/backup/auth_user-$(date?
- +%Y%m%d).sql?
- [root@localhost?~]#?cat?/backup/auth_user-20161208.sql?
MYSQL教程(5)對(duì)表結(jié)構(gòu)的備份
格式:mysqldump -u 用戶(hù)名 -p [密碼] -d 數(shù)據(jù)庫(kù)名 表名 > /備份路徑/備份文件名
示例:
?
- [root@localhost?~]#?mysqldump?-uroot?-p123123?-d?mysql?user?>/backup/desc_mysql_user-?
- $(date?+%Y%m%d).sql?
- [root@localhost?~]#?cat?/backup/desc_mysql_user-20161208.sql?
MYSQL教程4、使用? mysqldump? 備份后,恢復(fù)數(shù)據(jù)庫(kù)
1、source 命令
登錄到 MySQL 數(shù)據(jù)庫(kù),執(zhí)行 source 備份 sql 腳本路徑
示例
?
- [root@localhost?~]#?mysql?
- mysql>?show?databases;?
- +--------------------+?
- |?Database?|?
- +--------------------+?
- |?information_schema?|?
- |?auth?|?
- |?mysql?|?
- |?performance_schema?|?
- |?test?|?
- |?usr?|?
- +--------------------+?
- 6?rows?in?set?(0.00?sec)?
- mysql>?drop?database?auth;?
- Query?OK,?1?row?affected?(0.12?sec)?
- mysql>?source?/backup/mysql_all.20161208.sql?
- mysql>?show?databases;?
- ??
- +--------------------+?
- |?Database?|?
- +--------------------+?
- |?information_schema?|?
- |?auth?|?
- |?mysql?|?
- |?performance_schema?|?
- |?test?|?
- |?usr?|?
- +--------------------+?
- 6?rows?in?set?(0.00?sec)?
MYSQL教程2、mysql 命令
格式:mysql -u 用戶(hù)名 -p [密碼] < 庫(kù)備份腳本的路徑
mysql -u 用戶(hù)名 -p [密碼] 庫(kù)名 < 表備份腳本的路徑
示例:
?
- [root@localhost?~]#?mysql?-uroot?-p123123?-e?'show?databases;'?
- +--------------------+?
- |?Database?|?
- +--------------------+?
- |?information_schema?|?
- |?auth?|?
- |?mysql?|?
- |?performance_schema?|?
- |?test?|?
- |?usr?|?
- +--------------------+?
- [root@localhost?~]#?mysql?-uroot?-p123123?-e?'drop?database?auth;'?
- [root@localhost?~]#?mysql?-uroot?-p123123?<?/backup/mysql_all.20161208.sql?
- [root@localhost?~]#?mysql?-uroot?-p123123?-e?'show?databases;'?
- +--------------------+?
- |?Database?|?
- +--------------------+?
- |?information_schema?|?
- |?auth?|?
- |?mysql?|?
- |?performance_schema?|?
- |?test?|?
- |?usr?|?
- +--------------------+?
- ?
- [root@localhost?~]#?mysql?-uroot?-p123123?-e?'drop?table?auth.user;'?
- [root@localhost?~]#?mysql?-uroot?-p123123?auth<?/backup/auth_user-20161208.sql?
- [root@localhost?~]#?mysql?-uroot?-p123123?-e?'select?*?from?auth.user;'?
MYSQL教程+------------+------+
| name | ID |
+------------+------+
| crushlinux | 123 |
+------------+------+
?
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.snjht.com/jiaocheng/5593.html