1. 程式人生 > >MySQL資料備份之邏輯備份工具mysqldump

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
View Code

 

#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*/;
mysqlbinlog

 

#恢復方法

#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

&n