在使用MySQL數(shù)據(jù)庫時(shí),經(jīng)常需要備份和恢復(fù)數(shù)據(jù)庫,通過導(dǎo)出sql文件備份和直接執(zhí)行sql文件恢復(fù)是非常便捷的方式。本篇博客主要介紹如何對(duì)MySQL數(shù)據(jù)庫通過cmd命令導(dǎo)出和導(dǎo)入sql文件。
1.導(dǎo)出sql文件
導(dǎo)出sql文件可以使用mysqldump。主要有如下幾種操作:
①導(dǎo)出整個(gè)數(shù)據(jù)庫(包括數(shù)據(jù)庫中的數(shù)據(jù)):
mysqldump -hip -Pport -u username -ppassword dbname > dbname.sql;
mysqldump --single-transaction --host $ip -P $ip_port -u$dbUser $dbName --skip-lock-tables --tables $table > test.sql
mysql --default-character-set=utf8 -h$IP -P$PORT -u$USER -p$PASSWORD $DBNAME --skip-column-names -e "select * from db.table " | sed -e 's/\<NULL\>/\\N/g' >> down.txt
②導(dǎo)出數(shù)據(jù)庫中的數(shù)據(jù)表(包括數(shù)據(jù)表中的數(shù)據(jù)):
mysqldump -u username -ppassword dbname tablename > tablename.sql;
③導(dǎo)出數(shù)據(jù)庫結(jié)構(gòu)(不包括數(shù)據(jù),只有創(chuàng)建數(shù)據(jù)表語句):
mysqldump -u username -ppassword -d dbname > dbname.sql;
④導(dǎo)出數(shù)據(jù)庫中數(shù)據(jù)表的表結(jié)構(gòu)(不包括數(shù)據(jù),只有創(chuàng)建數(shù)據(jù)表語句):
mysqldump -u username -ppassword -d dbname tablename > tablename.sql
2.導(dǎo)入sql文件
①在命令窗口輸入mysql -hip -Pport -uroot -proot(兩個(gè)root依次表示登陸MySQL數(shù)據(jù)庫的用戶名和密碼,根據(jù)自己的情況更改),接著輸入show databases;(注意一定要輸入分號(hào);否則需要關(guān)閉命令窗口重新操作)
假設(shè)MySQL數(shù)據(jù)庫中已經(jīng)存在名為db_jxc的空數(shù)據(jù)庫。同上1中的①②步打開命令窗口。
②接著輸入use db_jxc(數(shù)據(jù)庫中已經(jīng)存在名為db_jxc的空數(shù)據(jù)庫,先前已創(chuàng)建的),然后執(zhí)行source /var/lib/mysql-files/aa.sql(sql文件路徑根據(jù)自己的實(shí)際情況而定在mysql客戶端所在機(jī)器的目錄中)即可成功導(dǎo)入sql文件(進(jìn)入MySQL數(shù)據(jù)可以看到數(shù)據(jù)庫下已經(jīng)創(chuàng)建了多張數(shù)據(jù)表)
show tables;可以查看當(dāng)前數(shù)據(jù)庫內(nèi)所有表。
mysql -uroot -p123456aA -Djettomanagerdev -e "source /var/lib/mysql-files/jettomanagerdev.sql";
mysql --default-character-set=utf8 -h$IP -P$PORT -u$USER -p$PASSWORD $DBNAME -e "load data local infile 'down.txt' into table db.table FIELDS ESCAPED BY '\/' LINES TERMINATED BY '\n' (col1, col2)"
??如果你指定關(guān)鍵詞low_priority,?那么MySQL將會(huì)等到?jīng)]有其他人讀這個(gè)表的時(shí)候
? 如果指定local關(guān)鍵詞,則表明從客戶主機(jī)讀文件
?分隔符的使用
?fields關(guān)鍵字指定了文件記段的分割格式,如果用到這個(gè)關(guān)鍵字,MySQL剖析器希望
看到至少有下面的一個(gè)選項(xiàng):
terminated by 分隔符:意思是以什么字符作為分隔符
enclosed by字段括起字符
escaped by轉(zhuǎn)義字符
terminated by 描述字段的分隔符,默認(rèn) 情況下是tab字符(\t)
enclosed by描述的是字段的括起字符。
escaped by描述的轉(zhuǎn)義字符。默認(rèn)的是反斜杠 (backslash:\ )?
?文章來源:http://www.zghlxwxcb.cn/news/detail-409731.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-409731.html
到了這里,關(guān)于mysql,mysqldump數(shù)據(jù)庫導(dǎo)入導(dǎo)出sql文件的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!