MySQL版本信息:
[root@db02 data]# mysql --version
mysql Ver 14.14 Distrib 5.6.36, for Linux (x86_64) using EditLine wrapper
选项: -A , --all-databases 全库备份 -B , --databases 增加建库(create)及“use库”的语句 可以直接接多个库名,同时备份多个库 -B 库1 库2 -R , --routines 备份存储过程和函数数据 --triggers 备份触发器数据 --master-data={1|2} 告诉备份后时刻的binlog位置 2 注释 1 非注释,要执行(主从复制)对恢复没什么用 --single-transaction 对innodb引擎进行热备 -F, --flush-logs 刷新binlog日志
全备
[root@db02 ~]# mysqldump -uroot -p123 -A >/backup/full.sql
单库备份 使用-B的区别
[root@db02 ~]# mysqldump -uroot -p123 test > ./test.sql
Warning: Using a password on the command line interface can be insecure.
[root@db02 ~]# mysqldump -uroot -p123 -B test > ./test_B.sql
Warning: Using a password on the command line interface can be insecure.
[root@db02 ~]# vimdiff test.sql test_B.sql
多库备份 -- -B 数据1 数据库2
[root@db02 ~]# mysqldump -uroot -p123 -B test mysql > ./test_mysql.sql
Warning: Using a password on the command line interface can be insecure.
多表备份 -- 数据库名 表名1 表名2
[root@db02 data]# mysqldump -uroot -p123 mysql user proc > ./mysql_user_proc.sql
Warning: Using a password on the command line interface can be insecure.
--master-data=2
[root@db02 data]# mysqldump -uroot -p123 --master-data=2 test > ./test.sql
Warning: Using a password on the command line interface can be insecure.
[root@db02 data]# vim ./test.sql
-- CHANGE MASTER TO MASTER_LOG_FILE='mysql_bin.000002', MASTER_LOG_POS=262;
--single-transaction
[root@db02 ~]# mysqldump -uroot -p123 --master-data=2 --single-transaction test > ./test2.sql
Warning: Using a password on the command line interface can be insecure.
-F
[root@db02 ~]# mysqldump -uroot -p123 --master-data=2 --single-transaction -R --triggers -B test -F > ./test3.sql
Warning: Using a password on the command line interface can be insecure.
压缩备份
[root@db02 ~]# mysqldump -uroot -p123 --master-data=2 --single-transaction -R --triggers -B test -F | gzip > ./test3.sql.gz
Warning: Using a password on the command line interface can be insecure.
[root@db02 ~]# ll ./test3.sql.gz
-rw-r--r-- 1 root root 818 Apr 11 17:12 ./test3.sql.gz
[root@db02 ~]# file ./test3.sql.gz
./test3.sql.tar.gz: gzip compressed data, from Unix, last modified: Wed Apr 11 17:12:48 2018
解压
[root@db02 ~]# mysqldump -uroot -p123 --master-data=2 --single-transaction -R --triggers -B test -F | gzip > ./test3.sql.gz
Warning: Using a password on the command line interface can be insecure.
[root@db02 ~]# gunzip test3.sql.gz
或者
[root@db02 ~]# gzip -d test3.sql.gz
或者
[root@db02 ~]# zcat test3.sql.gz > test3.sql
恢复 mysql> set sql_log_bin=0 # 恢复操作,不写入binlog日志中,因为写入也是无用的 mysql> source /root/test.sql
注:本博客仅供参考!