MySQL数据备份之逻辑备份工具mysqldump
#前言:我们知道对数据进行备份很重要,出现非正常操作可以进行对数据进行恢复,下面我们就来使用一下mysql数据库自带的一个逻辑备份工具mysqldump
1.简单概述
#mysqldump:mysql数据自带的一个备份命令
#语法格式:
mysqldump -u用户名 -p 数据库名 > 备份的文件名
#mysqldump逻辑备份的工作原理
1、mysqldump命令备份数据的过程,实际是把数据从mysql库里以逻辑的sql语句形式直接输出或生成备份文件的过程 2、恢复的时候就是把sql语句在数据库里面执行一遍的过程
#mysqldump帮助文档
[root@ctos3 ~]# mysqldump --help > 1.txt [root@ctos3 ~]# cat 1.txt
mysqldump Ver 10.13 Distrib 5.7.29, for Linux (x86_64) Copyright (c) 2000, 2020, 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. Dumping structure and contents of MySQL databases and tables. Usage: mysqldump [OPTIONS] database [tables] OR mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...] OR mysqldump [OPTIONS] --all-databases [OPTIONS] Default options are read from the following files in the given order: /etc/my.cnf /etc/mysql/my.cnf /application/mysql/etc/my.cnf ~/.my.cnf The following groups are read: mysqldump client The following options may be given as the first argument: --print-defaults Print the program argument list and exit. --no-defaults Don't read default options from any option file, except for login file. --defaults-file=# Only read default options from the given file #. --defaults-extra-file=# Read this file after the global files are read. --defaults-group-suffix=# Also read groups with concat(group, suffix) --login-path=# Read this path from the login file. -A, --all-databases Dump all the databases. This will be same as --databases with all databases selected. -Y, --all-tablespaces Dump all the tablespaces. -y, --no-tablespaces Do not dump any tablespace information. --add-drop-database Add a DROP DATABASE before each create. --add-drop-table Add a DROP TABLE before each create. (Defaults to on; use --skip-add-drop-table to disable.) --add-drop-trigger Add a DROP TRIGGER before each create. --add-locks Add locks around INSERT statements. (Defaults to on; use --skip-add-locks to disable.) --allow-keywords Allow creation of column names that are keywords. --apply-slave-statements Adds 'STOP SLAVE' prior to 'CHANGE MASTER' and 'START SLAVE' to bottom of dump. --bind-address=name IP address to bind to. --character-sets-dir=name Directory for character set files. -i, --comments Write additional information. (Defaults to on; use --skip-comments to disable.) --compatible=name Change the dump to be compatible with a given mode. By default tables are dumped in a format optimized for MySQL. Legal modes are: ansi, mysql323, mysql40, postgresql, oracle, mssql, db2, maxdb, no_key_options, no_table_options, no_field_options. One can use several modes separated by commas. Note: Requires MySQL server version 4.1.0 or higher. This option is ignored with earlier server versions. --compact Give less verbose output (useful for debugging). Disables structure comments and header/footer constructs. Enables options --skip-add-drop-table --skip-add-locks --skip-comments --skip-disable-keys --skip-set-charset. -c, --complete-insert Use complete insert statements. -C, --compress Use compression in server/client protocol. -a, --create-options Include all MySQL specific create options. (Defaults to on; use --skip-create-options to disable.) -B, --databases Dump several databases. Note the difference in usage; in this case no tables are given. All name arguments are regarded as database names. 'USE db_name;' will be included in the output. -#, --debug[=#] This is a non-debug version. Catch this and exit. --debug-check This is a non-debug version. Catch this and exit. --debug-info This is a non-debug version. Catch this and exit. --default-character-set=name Set the default character set. --delete-master-logs Delete logs on master after backup. This automatically enables --master-data. -K, --disable-keys '/*!40000 ALTER TABLE tb_name DISABLE KEYS */; and '/*!40000 ALTER TABLE tb_name ENABLE KEYS */; will be put in the output. (Defaults to on; use --skip-disable-keys to disable.) --dump-slave[=#] This causes the binary log position and filename of the master to be appended to the dumped data output. Setting the value to 1, will printit as a CHANGE MASTER command in the dumped data output; if equal to 2, that command will be prefixed with a comment symbol. This option will turn --lock-all-tables on, unless --single-transaction is specified too (in which case a global read lock is only taken a short time at the beginning of the dump - don't forget to read about --single-transaction below). In all cases any action on logs will happen at the exact moment of the dump.Option automatically turns --lock-tables off. -E, --events Dump events. -e, --extended-insert Use multiple-row INSERT syntax that include several VALUES lists. (Defaults to on; use --skip-extended-insert to disable.) --fields-terminated-by=name Fields in the output file are terminated by the given string. --fields-enclosed-by=name Fields in the output file are enclosed by the given character. --fields-optionally-enclosed-by=name Fields in the output file are optionally enclosed by the given character. --fields-escaped-by=name Fields in the output file are escaped by the given character. -F, --flush-logs Flush logs file in server before starting dump. Note that if you dump many databases at once (using the option --databases= or --all-databases), the logs will be flushed for each database dumped. The exception is when using --lock-all-tables or --master-data: in this case the logs will be flushed only once, corresponding to the moment all tables are locked. So if you want your dump and the log flush to happen at the same exact moment you should use --lock-all-tables or --master-data with --flush-logs. --flush-privileges Emit a FLUSH PRIVILEGES statement after dumping the mysql database. This option should be used any time the dump contains the mysql database and any other database that depends on the data in the mysql database for proper restore. -f, --force Continue even if we get an SQL error. -?, --help Display this help message and exit. --hex-blob Dump binary strings (BINARY, VARBINARY, BLOB) in hexadecimal format. -h, --host=name Connect to host. --ignore-error=name A comma-separated list of error numbers to be ignored if encountered during dump. --ignore-table=name Do not dump the specified table. To specify more than one table to ignore, use the directive multiple times, once for each table. Each table must be specified with both database and table names, e.g., --ignore-table=database.table. --include-master-host-port Adds 'MASTER_HOST=<host>, MASTER_PORT=<port>' to 'CHANGE MASTER TO..' in dump produced with --dump-slave. --insert-ignore Insert rows with INSERT IGNORE. --lines-terminated-by=name Lines in the output file are terminated by the given string. -x, --lock-all-tables Locks all tables across all databases. This is achieved by taking a global read lock for the duration of the whole dump. Automatically turns --single-transaction and --lock-tables off. -l, --lock-tables Lock all tables for read. (Defaults to on; use --skip-lock-tables to disable.) --log-error=name Append warnings and errors to given file. --master-data[=#] This causes the binary log position and filename to be appended to the output. If equal to 1, will print it as a CHANGE MASTER command; if equal to 2, that command will be prefixed with a comment symbol. This option will turn --lock-all-tables on, unless --single-transaction is specified too (in which case a global read lock is only taken a short time at the beginning of the dump; don't forget to read about --single-transaction below). In all cases, any action on logs will happen at the exact moment of the dump. Option automatically turns --lock-tables off. --max-allowed-packet=# The maximum packet length to send to or receive from server. --net-buffer-length=# The buffer size for TCP/IP and socket communication. --no-autocommit Wrap tables with autocommit/commit statements. -n, --no-create-db Suppress the CREATE DATABASE ... IF EXISTS statement that normally is output for each dumped database if --all-databases or --databases is given. -t, --no-create-info Don't write table creation info. -d, --no-data No row information. -N, --no-set-names Same as --skip-set-charset. --opt Same as --add-drop-table, --add-locks, --create-options, --quick, --extended-insert, --lock-tables, --set-charset, and --disable-keys. Enabled by default, disable with --skip-opt. --order-by-primary Sorts each table's rows by primary key, or first unique key, if such a key exists. Useful when dumping a MyISAM table to be loaded into an InnoDB table, but will make the dump itself take considerably longer. -p, --password[=name] Password to use when connecting to server. If password is not given it's solicited on the tty. -P, --port=# Port number to use for connection. --protocol=name The protocol to use for connection (tcp, socket, pipe, memory). -q, --quick Don't buffer query, dump directly to stdout. (Defaults to on; use --skip-quick to disable.) -Q, --quote-names Quote table and column names with backticks (`). (Defaults to on; use --skip-quote-names to disable.) --replace Use REPLACE INTO instead of INSERT INTO. -r, --result-file=name Direct output to a given file. This option should be used in systems (e.g., DOS, Windows) that use carriage-return linefeed pairs (\r\n) to separate text lines. This option ensures that only a single newline is used. -R, --routines Dump stored routines (functions and procedures). --set-charset Add 'SET NAMES default_character_set' to the output. (Defaults to on; use --skip-set-charset to disable.) --set-gtid-purged[=name] Add 'SET @@GLOBAL.GTID_PURGED' to the output. Possible values for this option are ON, OFF and AUTO. If ON is used and GTIDs are not enabled on the server, an error is generated. If OFF is used, this option does nothing. If AUTO is used and GTIDs are enabled on the server, 'SET @@GLOBAL.GTID_PURGED' is added to the output. If GTIDs are disabled, AUTO does nothing. If no value is supplied then the default (AUTO) value will be considered. --single-transaction Creates a consistent snapshot by dumping all tables in a single transaction. Works ONLY for tables stored in storage engines which support multiversioning (currently only InnoDB does); the dump is NOT guaranteed to be consistent for other storage engines. While a --single-transaction dump is in process, to ensure a valid dump file (correct table contents and binary log position), no other connection should use the following statements: ALTER TABLE, DROP TABLE, RENAME TABLE, TRUNCATE TABLE, as consistent snapshot is not isolated from them. Option automatically turns off --lock-tables. --dump-date Put a dump date to the end of the output. (Defaults to on; use --skip-dump-date to disable.) --skip-opt Disable --opt. Disables --add-drop-table, --add-locks, --create-options, --quick, --extended-insert, --lock-tables, --set-charset, and --disable-keys. -S, --socket=name The socket file to use for connection. --secure-auth Refuse client connecting to server if it uses old (pre-4.1.1) protocol. Deprecated. Always TRUE --ssl-mode=name SSL connection mode. --ssl Deprecated. Use --ssl-mode instead. (Defaults to on; use --skip-ssl to disable.) --ssl-verify-server-cert Deprecated. Use --ssl-mode=VERIFY_IDENTITY instead. --ssl-ca=name CA file in PEM format. --ssl-capath=name CA directory. --ssl-cert=name X509 cert in PEM format. --ssl-cipher=name SSL cipher to use. --ssl-key=name X509 key in PEM format. --ssl-crl=name Certificate revocation list. --ssl-crlpath=name Certificate revocation list path. --tls-version=name TLS version to use, permitted values are: TLSv1, TLSv1.1, TLSv1.2 --server-public-key-path=name File path to the server public RSA key in PEM format. --get-server-public-key Get server public key -T, --tab=name Create tab-separated textfile for each table to given path. (Create .sql and .txt files.) NOTE: This only works if mysqldump is run on the same machine as the mysqld server. --tables Overrides option --databases (-B). --triggers Dump triggers for each dumped table. (Defaults to on; use --skip-triggers to disable.) --tz-utc SET TIME_ZONE='+00:00' at top of dump to allow dumping of TIMESTAMP data when a server has data in different time zones or data is being moved between servers with different time zones. (Defaults to on; use --skip-tz-utc to disable.) -u, --user=name User for login if not current user. -v, --verbose Print info about the various stages. -V, --version Output version information and exit. -w, --where=name Dump only selected records. Quotes are mandatory. -X, --xml Dump a database as well formed XML. --plugin-dir=name Directory for client-side plugins. --default-auth=name Default authentication client-side plugin to use. --enable-cleartext-plugin Enable/disable the clear text authentication plugin. Variables (--variable-name=value) and boolean options {FALSE|TRUE} Value (after reading options) --------------------------------- ---------------------------------------- all-databases FALSE all-tablespaces FALSE no-tablespaces FALSE add-drop-database FALSE add-drop-table TRUE add-drop-trigger FALSE add-locks TRUE allow-keywords FALSE apply-slave-statements FALSE bind-address (No default value) character-sets-dir (No default value) comments TRUE compatible (No default value) compact FALSE complete-insert FALSE compress FALSE create-options TRUE databases FALSE default-character-set utf8 delete-master-logs FALSE disable-keys TRUE dump-slave 0 events FALSE extended-insert TRUE fields-terminated-by (No default value) fields-enclosed-by (No default value) fields-optionally-enclosed-by (No default value) fields-escaped-by (No default value) flush-logs FALSE flush-privileges FALSE force FALSE hex-blob FALSE host (No default value) ignore-error (No default value) include-master-host-port FALSE insert-ignore FALSE lines-terminated-by (No default value) lock-all-tables FALSE lock-tables TRUE log-error (No default value) master-data 0 max-allowed-packet 25165824 net-buffer-length 1046528 no-autocommit FALSE no-create-db FALSE no-create-info FALSE no-data FALSE order-by-primary FALSE port 3306 quick TRUE quote-names TRUE replace FALSE routines FALSE set-charset TRUE single-transaction FALSE dump-date TRUE socket /application/mysql/tmp/mysql.sock secure-auth TRUE ssl TRUE ssl-verify-server-cert FALSE ssl-ca (No default value) ssl-capath (No default value) ssl-cert (No default value) ssl-cipher (No default value) ssl-key (No default value) ssl-crl (No default value) ssl-crlpath (No default value) tls-version (No default value) server-public-key-path (No default value) get-server-public-key FALSE tab (No default value) triggers TRUE tz-utc TRUE user (No default value) verbose FALSE where (No default value) plugin-dir (No default value) default-auth (No default value) enable-cleartext-plugin FALSE
#mysqldump备份使用场景
1、迁移或者升级数据库时 2、增加从库时候 3、因为硬件或特殊异常情况,主库或从库宕机 4、人为的DDL,DML语句,主从库没办法了,所有库都会执行 5、跨机房灾备,需要备份
2.mysqldump的一些关键参数
1、 -B --databases 指定多个库。增加建库语句和use语句 2、 --compact 去掉注释,适合调试输出 3、 -A --all-databases 备份所有库 4、 -F 刷新binlog 日志 5、--master-data 增加binlog日志文件名及对应的位置点 6、-x --lock-all-tables 全局的读锁,会阻止对所有表的写入操作 7、 -l 该锁不会阻止读,也不会阻止新的数据插入 8、 -d 只备份表结构 9、 -t 只备份数据 10、 --signle-transaction 适合innodb事务数据库备份
#在介绍相关参数的时候,我们先来了解一下-e参数
#作用:加上-e参数可以直接在外面交互式查看数据库里面的东西,而不用登录到数据库里面
#例1:查看数据库
[root@cots3 ~]# mysql -uroot -p -e "show databases;" Enter password: +--------------------+ | Database | +--------------------+ | information_schema | | data | | db | | mysql | | performance_schema | | sys | +--------------------+
#例二:可以执行多个操作,如进入某个数据库然后查看表
[root@cots3 ~]# mysql -uroot -p -e "use data;show tables;" Enter password: +----------------+ | Tables_in_data | +----------------+ | Student | +----------------+
#例三:查看数据库连接情况,也可以show full processlist查看完整的语句
[root@cots3 ~]# mysql -uroot -p -e "show processlist;" Enter password: +----+------+-----------+------+---------+------+----------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------+------+---------+------+----------+------------------+ | 31 | root | localhost | NULL | Query | 0 | starting | show processlist | +----+------+-----------+------+---------+------+----------+------------------+
#参数介绍:id:进程,user:用户,host:连接的主机,db:使用的哪个库,command:执行了什么命令,time:多长时间,state:状态,info:信息
#1.-B参数
#作用:如果删除了数据库,不加-B参数,备份回来是会报错的,除非将数据库创建回来,备份时候加了-B参数会有自动建库语句
#例子:我们来演示以下加-B和不加-B的效果
#1.先演示不加-B参数的效果 #提示:我这里创建的表的时候,库和表的字符集都是使用UTF8,以免导出中文的时候出现乱码 mysql> create database data character set=utf8; #表会随着库的字符集 Query OK, 1 row affected (0.00 sec) 使用root用户登录将data数据库备份到/opt目录下 [root@ctos3 ~]# mysqldump -uroot -p data > /opt/mysql.bak.sql Enter password: #查看备份的东西 [root@ctos3 ~]# grep -Ev "#|\*|--|^$" /opt/mysql.bak.sql DROP TABLE IF EXISTS `Student`; CREATE TABLE `Student` ( `id` int(4) NOT NULL AUTO_INCREMENT, `name` char(20) NOT NULL, `age` varchar(16) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; LOCK TABLES `Student` WRITE; INSERT INTO `Student` VALUES (1,'aa','19'),(2,'bb','20'),(3,'张冲','21'); UNLOCK TABLES; #恢复 #将表删除再恢复 [root@ctos3 ~]# mysql -uroot -p -e "use data;drop table Student;" #删除表 Enter password: [root@cots3 ~]# mysql -uroot -p -e "select * from data.Student;" #确认表有没有删除 Enter password: ERROR 1146 (42S02) at line 1: Table 'data.Student' doesn't exist [root@ctos3 ~]# mysql -uroot -p data < /opt/mysql.bak.sql #进行恢复,没问题 Enter password: [root@ctos3 ~]# mysql -uroot -p -e "select * from data.Student;" #查看 Enter password: +----+--------+-----+ | id | name | age | +----+--------+-----+ | 1 | aa | 19 | | 2 | bb | 20 | | 3 | 张冲 | 21 | +----+--------+-----+ #问题:如果删除了数据库,如果备份的时候不加-B参数,备份回来是会报错的,除非将数据库创建回来 mysql> drop database data; Query OK, 1 row affected (0.02 sec) [root@ctos3 ~]# mysql -uroot -p data < /opt/mysql.bak.sql #恢复的时候就会报错,备份的时候没有加-B参数,没有建库语句 Enter password: ERROR 1049 (42000): Unknown database 'data' [root@ctos3 ~]# mysql -uroot -p -e "create database data;" #将数据库创建回来 Enter password: [root@ctos3 ~]# mysql -uroot -p -e "show databases;" | grep -E "data" Enter password: data [root@ctos3 ~]# mysql -uroot -p data < /opt/mysql.bak.sql #恢复数据 Enter password: [root@ctos3 ~]# mysql -uroot -p -e "use data;select * from Student;" #查看 Enter password: +----+--------+-----+ | id | name | age | +----+--------+-----+ | 1 | aa | 19 | | 2 | bb | 20 | | 3 | 张冲 | 21 | +----+--------+-----+ #2.演示加-B参数备份 [root@cots3 ~]# mysqldump -uroot -p -B data > /opt/mysql.bak_B.sql #首先使用-B进行备份 Enter password: [root@cots3 ~]# cd /opt/ [root@cots3 opt]# ls mysql.bak_B.sql mysql.bak.sql [root@cots3 opt]# diff mysql.bak.sql mysql.bak_B.sql #对比一下加-B和不加的区别,加了-B会有创建数据库和连接数据库的命令 18a19,26 > -- Current Database: `data` > -- > > CREATE DATABASE /*!32312 IF NOT EXISTS*/ `data` /*!40100 DEFAULT CHARACTER SET latin1 */; > > USE `data`; > > -- 52c60 < -- Dump completed on 2020-03-12 5:46:28 --- > -- Dump completed on 2020-03-12 5:53:11 [root@cots3 ~]# mysql -uroot -p -e "drop database data;" #删除数据库 Enter password: [root@cots3 ~]# mysql -uroot -p < /opt/mysql.bak_B.sql #然后恢复 Enter password: [root@cots3 ~]# mysql -uroot -p -e "select * from data.Student;" Enter password: +----+--------+-----+ | id | name | age | +----+--------+-----+ | 1 | aa | 19 | | 2 | bb | 20 | | 3 | 张冲 | 21 | +----+--------+-----+ #提示:备份的时候加入-B参数好点,还原的时候不用加创建库
#2.-d参数:只备份表结构
#例子:只备份data库的所有表的结构
[root@cots3 ~]# mysqldump -uroot -p --compact -d data Enter password: /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `Student` ( `id` int(4) NOT NULL AUTO_INCREMENT, `name` char(20) NOT NULL, `age` varchar(16) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `test` ( `id` int(4) NOT NULL AUTO_INCREMENT, `name` char(20) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */;
#查看test表结构
[root@cots3 ~]# mysqldump -uroot -p --compact -d data test Enter password: /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `test` ( `id` int(4) NOT NULL AUTO_INCREMENT, `name` char(20) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */;
#提示:--compact参数会提供较少的输出(对调试很有用)
#3.-t参数:只输出数据
[root@cots3 ~]# mysqldump -uroot -p --compact -t data Enter password: INSERT INTO `Student` VALUES (1,'aa','19'),(2,'bb','20'),(3,'张冲','21');
#4.可以使用gzip对备份的数据进行压缩
#好处:使用压缩备份的话将会缩小文件大小,减少占用空间
[root@cots3 ~]# mysqldump -uroot -p -B data | gzip > /opt/mysql.bak_sql.gz Enter password: [root@cots3 ~]# ls -lh /opt/ total 8.0K -rw-r--r-- 1 root root 2.6K Mar 12 06:32 mysql.bak_B.sql -rw-r--r-- 1 root root 851 Mar 12 07:56 mysql.bak_sql.gz
3.备份库
#3.1.备份单个库
[root@cots3 ~]# mysqldump -uroot -p -B data > /opt/mysql.bak_data.sql
Enter password:
#3.2.备份多个库
[root@cots3 ~]# mysqldump -uroot -p -B data db | gzip > /opt/mysql.bak.sql.gz
Enter password:
#提示:-B参数是关键,在-B参数后指定库名,空格隔开备份多个库
#注意点:在备份多个数据库的时候最好使用root去备份,要不然如果使用别的用户取备份,在创建用户的时候如果没有指定那个库,那么就会相对应的备份不了
#演示效果
[root@cots3 ~]# mysql -uroot -p -e "grant all privileges on db.* to test@localhost identified by 'guoke123';" #创建一个test用户,指定管理db库 Enter password: [root@cots3 ~]# mysqldump -utest -p -B data db | gzip > /opt/test_bak1.sql.gz #备份到其他库就会报错,没有权限 Enter password: mysqldump: Got error: 1044: Access denied for user 'test'@'localhost' to database 'data' when selecting the database [root@cots3 ~]# mysqldump -utest -p -B db | gzip > /opt/test_bak1.sql.gz #备份自己管理的就可以 Enter password:
#3.3.分库备份
#简单理解:分库就是将数据库里面的库分开来进行备份
#注意点:每个库都可以用对应备份的库作为库名结尾加.sql
#分库备份的意义:有时一个企业的数据库里会有多个库,例如(www,guoke,blog),但是有时候出问题的可能只是某一个库,如果在备份时把所有的库都备份成一个数据文件的话,恢复某一个库的数据时就比较麻烦了
#备份方法
#方法1:使用一条命令完成
[root@cots3 ~]# mkdir /opt/back [root@cots3 ~]# mysql -uroot -p -e "show databases;" | grep -Evi "database|infor|per|sys" | sed -r 's#^([a-z].*$)#mysqldump -uroot -p -B \1|gzip >/opt/back/\1.sql.gz#g' | bash Enter password: Enter password: Enter password: Enter password: [root@cots3 ~]# ls -l /opt/back/ total 232 -rw-r--r-- 1 root root 851 Mar 12 08:14 data.sql.gz -rw-r--r-- 1 root root 1212 Mar 12 08:14 db.sql.gz -rw-r--r-- 1 root root 229070 Mar 12 08:14 mysql.sql.gz #提示:为什么是输入四次密码呢,因为第一次是登录的密码,其余3次是备份3个库的时候使用
#命令解释:首先使用grep过滤出想要备份的库,然后使用sed来进行替换,-r是支持扩展表达式
#方法二:如果库少的话就一个库对一个应该文件,一个一个来,如果库多久写脚本,用for循环
[root@cots3 ~]# mkdir /opt/bak [root@cots3 ~]# cat fenku.sh #!/bin/bash for dbname in `mysql -uroot -p"guoke123" -e "show databases;" | grep -Evi "database|infor|per|sys"` do mysqldump -uroot -p"guoke123" --events -B $dbname |gzip > /opt/bak/${dbname}.sql.gz done [root@cots3 ~]# ls -l /opt/bak/ #查看效果 total 232 -rw-r--r-- 1 root root 868 Mar 12 08:23 data.sql.gz -rw-r--r-- 1 root root 1230 Mar 12 08:23 db.sql.gz -rw-r--r-- 1 root root 229088 Mar 12 08:23 mysql.sql.gz
4.备份表
4.1.备份单个表
#语法格式:mysqldump -u 用户名 -p 数据库名 表名 > 备份的文件名
#例子:
[root@cots3 ~]# mysqldump -uroot -p db test > /opt/mysql_test.sql Enter password: [root@cots3 ~]# ls -l /opt/ | grep mysql_test -rw-r--r-- 1 root root 1934 Mar 12 08:26 mysql_test.sql
#提示:备份一个表的时候不要加-B,要不然所有表都会出来,就会报错
4.2.备份多个表
#语法格式:mysqldump -u 用于名 -p 数据库名 表名1 表名2 > 备份的文件名
#例子:
[root@cots3 ~]# mysqldump -uroot -p db guoke test > /opt/mysql_table.sql
#4.3使用脚本进行分库分表备份
[root@ctos3 ~]# cat bak.sh #!/bin/bash MYUSER="root" MYPASS="guoke123" MYLOG="mysql -u$MYUSER -p$MYPASS -e" MYDUMP="mysqldump -u$MYUSER -p$MYPASS -x -F" DBLIST=$($MYLOG "show databases;" | sed 1d | grep -Ev 'info|mysq|per|sys') DIR=/backup [ ! -d $DIR ] && mkdir $DIR cd $DIR for dbname in $DBLIST do TABLIST=$($MYLOG "show tables from $dbname;" | sed 1d) for tabname in $TABLIST do mkdir -p $DIR/$dbname $MYDUMP $dbname $tabname --events |gzip > $DIR/${dbname}/${tabname}_$(date +%F_%T).sql.gz done done
#备份的效果
[root@ctos3 ~]# ls -l /backup/ total 0 drwxr-xr-x. 2 root root 45 Mar 13 09:14 data drwxr-xr-x. 2 root root 48 Mar 13 09:14 db [root@ctos3 ~]# ls -l /backup/data/ total 4 -rw-r--r--. 1 root root 742 Mar 13 09:14 test_2020-03-13_09:14:26.sql.gz [root@ctos3 ~]# ls -l /backup/db/ total 4 -rw-r--r--. 1 root root 699 Mar 13 09:14 student_2020-03-13_09:14:27.sql.gz
5.恢复数据
#1.一般的恢复数据库命令
[root@cots3 ~]# mysql -uroot -p < /opt/mysql_test1.sql #备份时加-B参数恢复方法
[root@cots3 ~]# mysql -uroot -p test < /opt/mysql_test1.sql #备份时不加-B参数方法
#2.恢复分库分表备份的数据
#思路:先将所有库找出来,然后使用for循环将其恢复。
#提示:如果备份的时候是压缩了,可以先解压,或者可以使用sed过滤数据库名字出来,使用for循环恢复
#例子:
#1.首先备份所有数据库 [root@cots3 ~]# mysql -uroot -p -e "show databases;" | grep -Evi "database|infor|per|sys|mysql" | sed -r 's#^([a-z].*$)#mysqldump -uroot -p -B \1|gzip >/opt/back/\1.sql.gz#g' | bash Enter password: Enter password: Enter password: [root@cots3 ~]# ls -l /opt/back/ total 8 -rw-r--r-- 1 root root 851 Mar 12 08:48 data.sql.gz -rw-r--r-- 1 root root 1212 Mar 12 08:48 db.sql.gz #2.将数据库删除,进行解压缩再进行恢复 mysql> drop database data; Query OK, 2 rows affected (0.02 sec) mysql> drop database db; Query OK, 3 rows affected (0.02 sec) #3.将备份的文件解压缩 [root@cots3 ~]# cd /opt/back/ [root@cots3 back]# ls data.sql.gz db.sql.gz [root@cots3 back]# gunzip * [root@cots3 back]# ls data.sql db.sql #4.使用for循环恢复 [root@cots3 ~]# for dbname in `ls /opt/back/*.sql`;do mysql -uroot -p < ${dbname};done Enter password: Enter password:
#3.使用source恢复数据库
#方法:登录数据库mysql>source 备份文件
#例子:
[root@cots3 bak]# mysql -uroot -p -e "drop database data;" #将data库删除 mysql> system ls /opt/bak data.sql db.sql mysql.sql mysql> source /opt/bak/data.sql #进行恢复
6.全量备份与增量备份
#全量备份
全量备份就是把数据库中所有的数据进进行备份
#例子:
[root@cots3 ~]# mysqldump -uroot -p --events -F -A -B |gzip > /opt/back/mysqlback_$(date +%F).sql.gz #备份所有库 Enter password: [root@cots3 ~]# ls -l /opt/back/mysqlback_2020-03-12.sql.gz -rw-r--r-- 1 root root 186470 Mar 12 10:01 /opt/back/mysqlback_2020-03-12.sql.gz [root@cots3 ~]# mysqldump -uroot -p --events -F -B data | gzip > /opt/back/mysqlbak_$(date +%F).sql.gz #备份单个库 Enter password:
#增量备份
增量备份就是上一次全量备份之后,到下一次全备的中间的备份就是增量备份
#企业场景全量备份和增量备份部署
1.中小公司,全量一般是每天一次,业务流量低估执行全备,备份时会锁表 #按天备份的优缺点 优点:恢复时间短,维护成本低 缺点:占用空间多,占用系统资源多,经常锁表影响用户体验 2.大公司周备,每周六00点一次全量,下周日-下周六00点前都是增量 #按周备份的优缺点 优点:占用空间小,占用系统资源少,无需锁表,用户体验好点 缺点:维护成本高,恢复麻烦,时间长
#增量恢复
利用二进制日志binlog和全备进行的恢复过程,称为增量恢复
#增量恢复的应用场景
1.人为操作数据库SQL语句破坏主库 2.只有一个主库
#增量恢复的必要条件
#开启MySQL的log-bin日志功能
/etc/my.cnf [mysqld] log_bin=mysql_bin
server-id=1
7.使用mysqlbinlog来增量恢复
#增量恢复主要是靠binlog日志来进行恢复
#mysqlbinlog是用来解析mysql的binlog日志的
#mysqlbinlog的日志作用:用来记录MySQL内部增删改查等对MySQL数据库有更新的内容的记录
#提示:查看binlog日志需要使用mysqlbinlog打开
[root@ctos3 data]# mysqlbinlog mysql_bin.000001 /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/; /*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/; DELIMITER /*!*/; # at 4 #200313 8:31:31 server id 1 end_log_pos 123 CRC32 0x9ffc5808 Start: binlog v 4, server v 5.7.29-log created 200313 8:31:31 at startup # Warning: this binlog is either in use or was not closed properly. ROLLBACK/*!*/; BINLOG ' I31rXg8BAAAAdwAAAHsAAAABAAQANS43LjI5LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAjfWteEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA AQhY/J8= '/*!*/; # at 123 #200313 8:31:31 server id 1 end_log_pos 154 CRC32 0x2f9f0a63 Previous-GTIDs # [empty] # at 154 #200313 8:40:27 server id 1 end_log_pos 219 CRC32 0x49d233b5 Anonymous_GTID last_committed=0 sequence_number=1 rbr_only=no SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/; # at 219 #200313 8:40:27 server id 1 end_log_pos 313 CRC32 0x163c2e24 Query thread_id=3 exec_time=0 error_code=0 SET TIMESTAMP=1584103227/*!*/; SET @@session.pseudo_thread_id=3/*!*/; SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/; SET @@session.sql_mode=32/*!*/; SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/; /*!\C utf8 *//*!*/; SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=33/*!*/; SET @@session.lc_time_names=0/*!*/; SET @@session.collation_database=DEFAULT/*!*/; create database data /*!*/; # at 313 #200313 8:40:32 server id 1 end_log_pos 378 CRC32 0x404f0c1d Anonymous_GTID last_committed=1 sequence_number=2 rbr_only=no SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/; # at 378 #200313 8:40:32 server id 1 end_log_pos 466 CRC32 0xfcb07e1f Query thread_id=3 exec_time=0 error_code=0 SET TIMESTAMP=1584103232/*!*/; create database db /*!*/; # at 466 #200313 8:41:11 server id 1 end_log_pos 531 CRC32 0xb1d1de3b Anonymous_GTID last_committed=2 sequence_number=3 rbr_only=no SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/; # at 531 #200313 8:41:11 server id 1 end_log_pos 726 CRC32 0x9dbf88b2 Query thread_id=3 exec_time=0 error_code=0 use `data`/*!*/; SET TIMESTAMP=1584103271/*!*/; create table test ( id int(4) not null auto_increment, name char(20) not null, age varchar(16) not null, primary key(id)) /*!*/; # at 726 #200313 8:41:32 server id 1 end_log_pos 791 CRC32 0x44340287 Anonymous_GTID last_committed=3 sequence_number=4 rbr_only=yes /*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/; SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/; # at 791 #200313 8:41:32 server id 1 end_log_pos 863 CRC32 0xea8b7d1b Query thread_id=3 exec_time=0 error_code=0 SET TIMESTAMP=1584103292/*!*/; BEGIN /*!*/; # at 863 #200313 8:41:32 server id 1 end_log_pos 916 CRC32 0x8bc89c48 Table_map: `data`.`test` mapped to number 108 # at 916 #200313 8:41:32 server id 1 end_log_pos 981 CRC32 0xbccabc0a Write_rows: table id 108 flags: STMT_END_F BINLOG ' fH9rXhMBAAAANQAAAJQDAAAAAGwAAAAAAAEABGRhdGEABHRlc3QAAwP+DwT+PDAAAEicyIs= fH9rXh4BAAAAQQAAANUDAAAAAGwAAAAAAAEAAgAD//gBAAAACHpoYW5nc2FuAjE5+AIAAAAEbGlz aQIyMAq8yrw= '/*!*/; # at 981 #200313 8:41:32 server id 1 end_log_pos 1012 CRC32 0x24abc81e Xid = 13 COMMIT/*!*/; SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/; DELIMITER ; # End of log file /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/; /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
#恢复方法
#1.基于位置点的增量恢复
[root@ctos3 data]# pwd /application/mysql/data [root@ctos3 data]# ls -l | grep bin -rw-r-----. 1 mysql mysql 1035 Mar 13 08:44 mysql_bin.000001 -rw-r-----. 1 mysql mysql 154 Mar 13 08:44 mysql_bin.000002 -rw-r-----. 1 mysql mysql 38 Mar 13 08:44 mysql_bin.index #指定开始位置到结束位置,从pos466开始到531结束 [root@ctos3 data]# mysqlbinlog --no-defaults mysql_bin.000001 --start-position=466 --stop-position=531 -r pos.sql [root@ctos3 data]# ls -l | grep pos.sql -rw-r-----. 1 root root 936 Mar 13 08:47 pos.sql #指定开始位置到文件结束 [root@ctos3 data]# mysqlbinlog --no-defaults mysql_bin.000001 --start-position=531 -r pos531-end.sql [root@ctos3 data]# ls -l | grep 531 -rw-r-----. 1 root root 2450 Mar 13 08:49 pos531-end.sql #从文件开头到指定位置结束 #提示:关键参数为at,指定开始和结束的位置
#2.指定时间来恢复
[root@ctos3 data]# mysqlbinlog --no-defaults mysql-bin.000001 --start-datetime='2020-1-22 17:30:40' --stop-datetime='2020-3-22 17:30:58' -r time.sql
8.生产场景不同引擎备份命令
#企业生产场景不同引擎备份命令
#myisam
[root@ctos3 ~]# mysqldump -uroot -p -A -B --master-data=2 -x --events | gzip > /opt/all.sql.gz
#innodb
[root@ctos3 ~]# mysqldump -uroot -p -A -B --master-data=2 --events --single-transaction | gzip > /opt/all_1.sql.gz