MySQL备份

1.MySQL的配置文件

MySQL的配置文件在/etc/my.cnf

若是想要不输入用户和密码登录,则修改配置文件,或者在当前用户的家目录下创建一个隐藏文件,文件名是.my.cnf,那么MySQL服务的配置文件就有两种,这里就会涉及到MySQL的配置文件的查找顺序:

/etc/my.cnf------> ~/.my.cnf
//MySQL最后查找到的文件最终生效

mysql常用配置文件参数:

参数说明
port = 3306设置监听端口
socket = /tmp/mysql.sock指定套接字文件位置
basedir = /usr/local/mysql指定MySQL的安装路径
datadir = /data/mysql指定MySQL的数据存放路径
pid-file = /data/mysql/mysql.pid指定进程ID文件存放路径
user = mysql指定MySQL以什么用户的身份提供服务
skip-name-resolve禁止MySQL对外部连接进行DNS解析
使用这一选项可以消除MySQL进行DNS解析的时间。
若开启该选项,则所有远程主机连接授权都要使用IP地址方式否则MySQL将无法正常处理连接请求

2. mysql数据库备份

2.1 MySQL的三种备份方式
  • 完全备份
    每次对数据进行完整的备份。可以备份整个数据库,包含用户表、系统表、索引、视图和存储过程等所有数据库对象。但它需要花费更多的时间和空间,所以,做一次完全备份的周期要长些。
  • 增量备份
    只有那些在上次完全备份或者增量备份后被修改的文件才会被备份。
  • 差异备份
    备份那些自从上次完全备份之后被修改过的文件,只备份数据库部分的内容。它比最初的完全备份小,因为只包含自上次完全备份以来所改变的数据库。它的优点是存储和恢复速度快。

额外知识
备份的类型可以根据是否需要数据库离线来分类:(取决于业务的需求,而不是备份工具)

  • 冷备(脱机备份)
    在关闭数据库时进行的备份操作,能够较好地保证数据库的完整性,读写请求均不允许状态下进行。
  • 热备(联机备份)
    在数据库运行状态中进行操作,这种备份方法依赖于数据库的日志文件,备份的同时,业务不受影响。
  • 温备
    服务在线,但仅支持读请求,不允许写请求;

2.2mysql备份工具mysqldump

//语法:
mysqldump [OPTIONS] database [tables …]
mysqldump [OPTIONS] --all-databases [OPTIONS]
mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3…]

//常用的OPTIONS:
-uUSERNAME //指定数据库用户名
-hHOST //指定服务器主机,请使用ip地址
-pPASSWORD //指定数据库用户的密码
-P# //指定数据库监听的端口,这里的#需用实际的端口号代替

  • 备份整个数据库
//查看数据库
[root@localhost ~]# mysql -e 'show databases'
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| sys                |
+--------------------+
//用mysqldump备份整个数据库(全量备份)
[root@localhost ~]# mysqldump --all-databases > all-databases201902211431
[root@localhost ~]# ls
all-databases201902211431  anaconda-ks.cfg  
  • 备份数据库中的表
//查看数据库school中的student表
[root@localhost ~]# mysql -e 'select * from school.student'
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | tom   |   12 |
|  2 | jerry |   13 |
|  3 | jack  |   11 |
|  4 | xxy   |   10 |
|  5 | harry |   12 |
+----+-------+------+
//备份数据库school中的student表
[root@localhost ~]# mysqldump school student > table_from_student201902211435
[root@localhost ~]# ls
all-databases201902211431  anaconda-ks.cfg table_from_student201902211435
  • 只备份某个数据库
//备份school数据库
[root@localhost ~]# mysqldump --databases school > database_school201920211737
[root@localhost ~]# ls
all-databases201902211431  anaconda-ks.cfg  database_school201920211737   table_from_student201902211435
  • 模拟误删其中一个数据库
//模拟删除school数据库
[root@localhost ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 5.7.23 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.

mysql> drop database school;
Query OK, 1 row affected (0.03 sec)

mysql> exit
Bye
[root@localhost ~]# mysql -e 'show databases'
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

2.3 mysql数据恢复

  • 恢复上面刚刚误删的某个数据库
[root@localhost ~]# mysql < database_school201920211737 
[root@localhost ~]# mysql -e 'show databases'
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| sys                |
+--------------------+
[root@localhost ~]# mysql -e 'select * from school.student'
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | tom   |   12 |
|  2 | jerry |   13 |
|  3 | jack  |   11 |
|  4 | xxy   |   10 |
|  5 | harry |   12 |
+----+-------+------+
  • 模拟删除整个数据库
[root@localhost ~]# mysql -e 'show databases'
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
  • 恢复整个数据库
[root@localhost ~]# mysql < all-databases201902211431 
[root@localhost ~]# mysql -e 'show databases'
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| sys                |
+--------------------+
[root@localhost ~]# mysql -e 'select * from school.student'
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | tom   |   12 |
|  2 | jerry |   13 |
|  3 | jack  |   11 |
|  4 | xxy   |   10 |
|  5 | harry |   12 |
+----+-------+------+

3.差异备份与恢复

差异备份用到的工具:

  • mysqladmin ----用来备份数据库
  • mysqlbinlog ----用来恢复数据库

3.1差异备份

修改MySQL的配置文件,开启MySQL服务器的二进制(binlog)的日志功能

[root@localhost ~]# cd /opt/data/
[root@localhost data]# ll
总用量 110684
-rw-r-----. 1 mysql mysql       56 2月  20 16:23 auto.cnf
-rw-r-----. 1 mysql mysql     1406 2月  21 14:49 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 2月  21 14:49 ibdata1
-rw-r-----. 1 mysql mysql 50331648 2月  21 14:49 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 2月  20 16:23 ib_logfile1
-rw-r-----. 1 mysql mysql    54161 2月  21 14:49 localhost.localdomain.err
drwxr-x---. 2 mysql mysql     4096 2月  21 14:45 mysql
drwxr-x---. 2 mysql mysql     8192 2月  20 16:23 performance_schema
drwxr-x---. 2 mysql mysql       58 2月  21 14:45 school
drwxr-x---. 2 mysql mysql     8192 2月  20 16:23 sys
[root@localhost ~]# ss -antl
State      Recv-Q Send-Q                       Local Address:Port                                      Peer Address:Port              
LISTEN     0      128                                      *:22                                                   *:*                  
LISTEN     0      100                              127.0.0.1:25                                                   *:*                  
LISTEN     0      128                                     :::22                                                  :::*                  
LISTEN     0      100                                    ::1:25                                                  :::*                  
LISTEN     0      80                                      :::3306                                                :::*                  

[root@localhost ~]# vim /etc/my.cnf
[root@localhost ~]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve

server-id = 1			//设置服务器标识符
log-bin = mysql_log			//开启二进制日志功能

[root@localhost ~]# cd /opt/data/
[root@localhost data]# ll
总用量 123056
-rw-r-----. 1 mysql mysql       56 2月  20 16:23 auto.cnf
-rw-r-----. 1 mysql mysql      561 2月  22 14:08 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 2月  22 14:08 ibdata1
-rw-r-----. 1 mysql mysql 50331648 2月  22 14:08 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 2月  20 16:23 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 2月  22 14:08 ibtmp1
-rw-r-----. 1 mysql mysql    75021 2月  22 14:08 localhost.localdomain.err
drwxr-x---. 2 mysql mysql     4096 2月  21 14:45 mysql
-rw-r-----. 1 mysql mysql      154 2月  22 14:08 mysql_log.000001
-rw-r-----. 1 mysql mysql       19 2月  22 14:08 mysql_log.index
-rw-r-----. 1 mysql mysql        5 2月  22 14:08 mysql.pid
drwxr-x---. 2 mysql mysql     8192 2月  20 16:23 performance_schema
drwxr-x---. 2 mysql mysql       58 2月  21 14:45 school
drwxr-x---. 2 mysql mysql     8192 2月  20 16:23 sys

对数据库进行全量备份

[root@localhost ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
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.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| sys                |
+--------------------+
5 rows in set (0.05 sec)

mysql> show tables from school;
+------------------+
| Tables_in_school |
+------------------+
| student          |
+------------------+
1 row in set (0.00 sec)

mysql> select * from school.student;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | tom   |   12 |
|  2 | jerry |   13 |
|  3 | jack  |   11 |
|  4 | xxy   |   10 |
|  5 | harry |   12 |
+----+-------+------+
5 rows in set (0.07 sec)

mysql> exit
Bye
[root@localhost ~]# mysqldump -uroot -pzml123! --single-transaction --flush-logs --master-data=2 --all-databases --delete-master-logs > all-scholl201902221415
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# ls
all-scholl201902221415  anaconda-ks.cfg  

在数据库中添加新内容

[root@localhost ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
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.

mysql> use school
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> insert into student values (6,'dsz',9);
Query OK, 1 row affected (0.01 sec)

mysql> select * from student;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | tom   |   12 |
|  2 | jerry |   13 |
|  3 | jack  |   11 |
|  4 | xxy   |   10 |
|  5 | harry |   12 |
|  6 | dsz   |    9 |
+----+-------+------+
6 rows in set (0.00 sec)

mysql> update student set age = 19 where id = 6;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from student;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | tom   |   12 |
|  2 | jerry |   13 |
|  3 | jack  |   11 |
|  4 | xxy   |   10 |
|  5 | harry |   12 |
|  6 | dsz   |   19 |
+----+-------+------+
6 rows in set (0.00 sec)

mysql> exit
Bye

3.2 恢复差异备份的数据库

模拟误删数据库

[root@localhost ~]# mysql -e 'drop database school;'
[root@localhost ~]# mysql -e 'show databases;'
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
[root@localhost ~]# mysql -e 'show databases'
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

刷新创建新的二进制日志

[root@localhost ~]# cd /opt/data/
[root@localhost data]# ll
总用量 123004
-rw-r-----. 1 mysql mysql       56 2月  20 16:23 auto.cnf
-rw-r-----. 1 mysql mysql      561 2月  22 14:08 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 2月  22 14:25 ibdata1
-rw-r-----. 1 mysql mysql 50331648 2月  22 14:25 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 2月  20 16:23 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 2月  22 14:15 ibtmp1
-rw-r-----. 1 mysql mysql    75021 2月  22 14:08 localhost.localdomain.err
drwxr-x---. 2 mysql mysql     4096 2月  21 14:45 mysql
-rw-r-----. 1 mysql mysql      870 2月  22 14:25 mysql_log.000002
-rw-r-----. 1 mysql mysql       19 2月  22 14:15 mysql_log.index
-rw-r-----. 1 mysql mysql        5 2月  22 14:08 mysql.pid
drwxr-x---. 2 mysql mysql     8192 2月  20 16:23 performance_schema
drwxr-x---. 2 mysql mysql     8192 2月  20 16:23 sys

[root@localhost data]# mysqladmin flush-logs
[root@localhost data]# ll
总用量 123008
-rw-r-----. 1 mysql mysql       56 2月  20 16:23 auto.cnf
-rw-r-----. 1 mysql mysql      561 2月  22 14:08 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 2月  22 14:25 ibdata1
-rw-r-----. 1 mysql mysql 50331648 2月  22 14:25 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 2月  20 16:23 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 2月  22 14:15 ibtmp1
-rw-r-----. 1 mysql mysql    75021 2月  22 14:08 localhost.localdomain.err
drwxr-x---. 2 mysql mysql     4096 2月  21 14:45 mysql
-rw-r-----. 1 mysql mysql      917 2月  22 14:26 mysql_log.000002
-rw-r-----. 1 mysql mysql      154 2月  22 14:26 mysql_log.000003
-rw-r-----. 1 mysql mysql       38 2月  22 14:26 mysql_log.index
-rw-r-----. 1 mysql mysql        5 2月  22 14:08 mysql.pid
drwxr-x---. 2 mysql mysql     8192 2月  20 16:23 performance_schema
drwxr-x---. 2 mysql mysql     8192 2月  20 16:23 sys

恢复完全备份

[root@localhost ~]# mysql < all-scholl201902221415 
[root@localhost ~]# mysql -e 'show databases'
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| sys                |
+--------------------+
[root@localhost ~]# mysql -e 'select * from school.student'
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | tom   |   12 |
|  2 | jerry |   13 |
|  3 | jack  |   11 |
|  4 | xxy   |   10 |
|  5 | harry |   12 |
+----+-------+------+

恢复差异备份

[root@localhost ~]# ll /opt/data/
总用量 124028
-rw-r-----. 1 mysql mysql       56 2月  20 16:23 auto.cnf
-rw-r-----. 1 mysql mysql      561 2月  22 14:08 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 2月  22 14:30 ibdata1
-rw-r-----. 1 mysql mysql 50331648 2月  22 14:30 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 2月  20 16:23 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 2月  22 14:15 ibtmp1
-rw-r-----. 1 mysql mysql    75021 2月  22 14:08 localhost.localdomain.err
drwxr-x---. 2 mysql mysql     4096 2月  22 14:30 mysql
-rw-r-----. 1 mysql mysql      917 2月  22 14:26 mysql_log.000002
-rw-r-----. 1 mysql mysql   784883 2月  22 14:30 mysql_log.000003
-rw-r-----. 1 mysql mysql       38 2月  22 14:26 mysql_log.index
-rw-r-----. 1 mysql mysql        5 2月  22 14:08 mysql.pid
drwxr-x---. 2 mysql mysql     8192 2月  20 16:23 performance_schema
drwxr-x---. 2 mysql mysql       58 2月  22 14:30 school
drwxr-x---. 2 mysql mysql     8192 2月  20 16:23 sys

//检查误删数据库的位置在什么地方
[root@localhost ~]# mysql 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 17
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.

mysql> show binlog events in 'mysql_log.000002';
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| Log_name         | Pos | Event_type     | Server_id | End_log_pos | Info                                  |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| mysql_log.000002 |   4 | Format_desc    |         1 |         123 | Server ver: 5.7.23-log, Binlog ver: 4 |
| mysql_log.000002 | 123 | Previous_gtids |         1 |         154 |                                       |
| mysql_log.000002 | 154 | Anonymous_Gtid |         1 |         219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'  |
| mysql_log.000002 | 219 | Query          |         1 |         293 | BEGIN                                 |
| mysql_log.000002 | 293 | Table_map      |         1 |         349 | table_id: 141 (school.student)        |
| mysql_log.000002 | 349 | Write_rows     |         1 |         394 | table_id: 141 flags: STMT_END_F       |
| mysql_log.000002 | 394 | Xid            |         1 |         425 | COMMIT /* xid=472 */                  |
| mysql_log.000002 | 425 | Anonymous_Gtid |         1 |         490 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'  |
| mysql_log.000002 | 490 | Query          |         1 |         564 | BEGIN                                 |
| mysql_log.000002 | 564 | Table_map      |         1 |         620 | table_id: 141 (school.student)        |
| mysql_log.000002 | 620 | Update_rows    |         1 |         676 | table_id: 141 flags: STMT_END_F       |
| mysql_log.000002 | 676 | Xid            |         1 |         707 | COMMIT /* xid=474 */                  |
| mysql_log.000002 | 707 | Anonymous_Gtid |         1 |         772 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'  |
| mysql_log.000002 | 772 | Query          |         1 |         870 | drop database school                  |
| mysql_log.000002 | 870 | Rotate         |         1 |         917 | mysql_log.000003;pos=4                |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
15 rows in set (0.00 sec)

mysql> exit
Bye

//使用mysqlbinlog恢复差异备份
[root@localhost ~]# mysqlbinlog --stop-position=772 /opt/data/mysql_log.000002 | mysql 
[root@localhost ~]# mysql -e 'select * from school.student'
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | tom   |   12 |
|  2 | jerry |   13 |
|  3 | jack  |   11 |
|  4 | xxy   |   10 |
|  5 | harry |   12 |
|  6 | dsz   |   19 |
+----+-------+------+
[root@localhost ~]#
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值