MySQL導(dǎo)入導(dǎo)出命令-mysqldump
一、mysqldump工具介紹
mysqldump 是個(gè)mysql數(shù)據(jù)庫(kù)自帶的命令行工具,單線程執(zhí)行,可以用來(lái)備份和還原數(shù)據(jù)。可以生成 CSV、TXT、XML格式的文件輸出。
查看幫助文檔
二、利用mysqldump進(jìn)行數(shù)據(jù)庫(kù)備份
1. 數(shù)據(jù)庫(kù)操作
(1) 備份所有數(shù)據(jù)庫(kù)
- mysqldump -h 主機(jī)IP -uroot -p --all-database > /data/dball.sql
(2) 備份多個(gè)數(shù)據(jù)庫(kù)
- mysqldump -h 主機(jī)IP -uroot -p db1 db2 db3 >/data/db123.sql
(3) 備份單數(shù)據(jù)庫(kù)
- mysqldump -h 主機(jī)IP -uroot -p db >/data/db.sql
2. 數(shù)據(jù)庫(kù)中表操作
(1) 備份數(shù)據(jù)庫(kù)中多張表
- mysqldump -h 主機(jī)IP -uroot -p db table1 table2 >/data/db_table12.sql
(2) 備份數(shù)據(jù)庫(kù)中一張表
- mysqldump -h 主機(jī)IP -uroot -p db table >/data/db_table.sql
(3) 根據(jù)where進(jìn)行備份
- mysqldump -h 主機(jī)IP -uroot -p db table --where " 查詢條件" >/data/db_table.sql
(4) 備份數(shù)據(jù)中,忽略某張表
- mysqldump -h 主機(jī)IP -uroot -p db --ignore-table=logtable --ignore-table=historytable >/data/db_table.sql
3. 數(shù)據(jù)庫(kù)只導(dǎo)出表結(jié)構(gòu)或數(shù)據(jù),正常情況下導(dǎo)出表結(jié)構(gòu)和數(shù)據(jù)都存在
(1) 只導(dǎo)出表結(jié)構(gòu),不導(dǎo)出數(shù)據(jù)
- mysqldump -h主機(jī)IP -d -uroot -p 數(shù)據(jù)庫(kù)名 > db.sql
(2) 只導(dǎo)出數(shù)據(jù),不導(dǎo)出表結(jié)構(gòu)
- mysqldump -h主機(jī)IP -t -uroot -p 數(shù)據(jù)庫(kù)名 > db.sql
4. 通用備份命令
- mysqldump -h -uroot -p --default-character-set=utf-8 --set-gtid-purged=OFF --lock-tables=false -R -E --databases db | gzip > /root/db.sql.gz
- --default-character-set=utf-8 指定字符集
- --set-gtid-purged=OFF 重新生產(chǎn)GTID,而不用原來(lái)的
- --lock-tables 不鎖表
- -R Dump stored routines (functions and procedures)
- -E Dump events
- gzip 對(duì)備份進(jìn)行壓縮
三、利用mysqldump進(jìn)行數(shù)據(jù)庫(kù)還原
第一種:
- mysqldump -h 主機(jī)IP -uroot -p db < /root/db.sql
第二種:source 命令
- [root@izbp10lvzs7171weegqj8xz ~]# mysql -uroot -p
- mysql: [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 62669
- Server version: 5.7.23-log MySQL Community Server (GPL)
- Copyright (c) 2000, 2018, 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.
- 21: root@localhost:[(none)]> use test;
- Database changed
- 21: root@localhost:[test]> source /root/db.sql