MySQL導入導出命令-mysqldump
作者:Tom-時光
mysqldump 是個mysql數據庫自帶的命令行工具,單線程執行,可以用來備份和還原數據。可以生成 CSV、TXT、XML格式的文件輸出。
一、mysqldump工具介紹
mysqldump 是個mysql數據庫自帶的命令行工具,單線程執行,可以用來備份和還原數據。可以生成 CSV、TXT、XML格式的文件輸出。
查看幫助文檔
二、利用mysqldump進行數據庫備份
1. 數據庫操作
(1) 備份所有數據庫
- mysqldump -h 主機IP -uroot -p --all-database > /data/dball.sql
(2) 備份多個數據庫
- mysqldump -h 主機IP -uroot -p db1 db2 db3 >/data/db123.sql
(3) 備份單數據庫
- mysqldump -h 主機IP -uroot -p db >/data/db.sql
2. 數據庫中表操作
(1) 備份數據庫中多張表
- mysqldump -h 主機IP -uroot -p db table1 table2 >/data/db_table12.sql
(2) 備份數據庫中一張表
- mysqldump -h 主機IP -uroot -p db table >/data/db_table.sql
(3) 根據where進行備份
- mysqldump -h 主機IP -uroot -p db table --where " 查詢條件" >/data/db_table.sql
(4) 備份數據中,忽略某張表
- mysqldump -h 主機IP -uroot -p db --ignore-table=logtable --ignore-table=historytable >/data/db_table.sql
3. 數據庫只導出表結構或數據,正常情況下導出表結構和數據都存在
(1) 只導出表結構,不導出數據
- mysqldump -h主機IP -d -uroot -p 數據庫名 > db.sql
(2) 只導出數據,不導出表結構
- mysqldump -h主機IP -t -uroot -p 數據庫名 > 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 重新生產GTID,而不用原來的
- --lock-tables 不鎖表
- -R Dump stored routines (functions and procedures)
- -E Dump events
- gzip 對備份進行壓縮
三、利用mysqldump進行數據庫還原
第一種:
- mysqldump -h 主機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
責任編輯:趙寧寧
來源:
Linux運維技術之路