mysql进阶备份与恢复配置

1.二进制格式mysql安装

进官网下载二进制格式的mysql软件包到当前目录并解压到/usr/local/

[root@wyt1 ~]# ls
anaconda-ks.cfg  mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz
[root@wyt1 ~]# tar xf mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@wyt1 ~]# cd /usr/local
[root@wyt1 local]# ls
bin  games    lib    libexec                              sbin   src
etc  include  lib64  mysql-5.7.30-linux-glibc2.12-x86_64  share

创建用户和组

[root@wyt1 ~]# groupadd -r mysql
[root@wyt1 ~]# useradd -r -M -s /sbin/nologin -g mysql mysql

创建链接

[root@wyt1 local]# ln -s mysql-5.7.30-linux-glibc2.12-x86_64 mysql
[root@wyt1 local]# ll  /usr/local
total 0
drwxr-xr-x. 2 root root   6 Nov  5  2016 bin
drwxr-xr-x. 2 root root   6 Nov  5  2016 etc
drwxr-xr-x. 2 root root   6 Nov  5  2016 games
drwxr-xr-x. 2 root root   6 Nov  5  2016 include
drwxr-xr-x. 2 root root   6 Nov  5  2016 lib
drwxr-xr-x. 2 root root   6 Nov  5  2016 lib64
drwxr-xr-x. 2 root root   6 Nov  5  2016 libexec
lrwxrwxrwx. 1 root root  35 Jun 10 17:07 mysql -> mysql-5.7.30-linux-glibc2.12-x86_64
drwxr-xr-x. 9 root root 129 Jun 10 17:05 mysql-5.7.30-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root   6 Nov  5  2016 sbin
drwxr-xr-x. 5 root root  49 Mar 28 05:47 share
drwxr-xr-x. 2 root root   6 Nov  5  2016 src

修改目录/usr/local/mysql的属主属组

[root@wyt1 local]# chown -R mysql.mysql mysql*
[root@wyt1 local]# ll
total 0
drwxr-xr-x. 2 root  root    6 Nov  5  2016 bin
drwxr-xr-x. 2 root  root    6 Nov  5  2016 etc
drwxr-xr-x. 2 root  root    6 Nov  5  2016 games
drwxr-xr-x. 2 root  root    6 Nov  5  2016 include
drwxr-xr-x. 2 root  root    6 Nov  5  2016 lib
drwxr-xr-x. 2 root  root    6 Nov  5  2016 lib64
drwxr-xr-x. 2 root  root    6 Nov  5  2016 libexec
lrwxrwxrwx. 1 mysql mysql  35 Jun 10 17:07 mysql -> mysql-5.7.30-linux-glibc2.12-x86_64
drwxr-xr-x. 9 mysql mysql 129 Jun 10 17:05 mysql-5.7.30-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root  root    6 Nov  5  2016 sbin
drwxr-xr-x. 5 root  root   49 Mar 28 05:47 share
drwxr-xr-x. 2 root  root    6 Nov  5  2016 src

添加环境变量

[root@wyt1 local]# vim /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH
[root@wyt1 local]# source /etc/profile.d/mysql.sh
[root@wyt1 local]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@wyt1 local]# which mysql
/usr/local/mysql/bin/mysql

建立数据存放目录

[root@wyt1 ~]# mkdir /opt/mysqldata
[root@wyt1 ~]# chown -R mysql.mysql /opt/mysqldata //修改属主属组
[root@wyt1 ~]# ll /opt
total 0
drwxr-xr-x. 2 mysql mysql 6 Jun 10 22:56 mysqldata

初始化数据库

[root@wyt1 ~]# mysqld --initialize --user=mysql --datadir=/opt/mysqldata/
2020-06-10T15:03:08.714695Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-06-10T15:03:09.471739Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-06-10T15:03:09.580498Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-06-10T15:03:09.721273Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 7f60c20c-ab2b-11ea-ad3c-000c29a54fe1.
2020-06-10T15:03:09.752943Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-06-10T15:03:10.876563Z 0 [Warning] CA certificate ca.pem is self signed.
2020-06-10T15:03:11.048856Z 1 [Note] A temporary password is generated for root@localhost: k<;kQw0xmk1) //生成的临时随机密码
[root@wyt1 ~]# echo 'k<;kQw0xmk1)' > pass //临时密码写入文件
[root@wyt1 ~]# ls
pass
[root@wyt1 ~]# cat pass
k<;kQw0xmk1)

编辑配置文件

[root@wyt1 ~]# vim /etc/my.cnf

[mysqld]
basedir = /usr/local/mysql
datadir = /opt/mysqldata
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/mysqldata/mysql.pid
user = mysql
skip-name-resolve

配置服务启动脚本启动服务

[root@wyt1 ~]# cd /usr/local/mysql
[root@wyt1 mysql]# ls
bin  docs  include  lib  LICENSE  man  README  share  support-files
[root@wyt1 mysql]# cd support-files/
[root@wyt1 support-files]# ls
magic  mysqld_multi.server  mysql-log-rotate  mysql.server
[root@wyt1 support-files]# cp mysql.server /etc/init.d/mysqld
[root@wyt1 support-files]# cd
[root@wyt1 ~]# ll /etc/init.d/mysqld
-rwxr-xr-x. 1 root root 10576 Jun 10 23:13 /etc/init.d/mysqld
[root@wyt1 ~]# vim /etc/init.d/mysqld
basedir=/usr/local/mysql
datadir=/opt/mysqldata
[root@wyt1 ~]# service mysqld start //启动服务
Starting MySQL. SUCCESS! 
[root@wyt1 ~]# service mysqld status //查看启动状态
 SUCCESS! MySQL running (22771)
[root@wyt1 ~]# ss -antl|grep 3306
LISTEN     0      80          :::3306                    :::*                  
[root@wyt1 ~]# chkonfig mysqld on //开机自动启动

修改密码

[root@wyt1 ~]# cat pass //查看临时密码
k<;kQw0xmk1)
[root@wyt1 ~]# mysql -uroot -p'k<;kQw0xmk1)' //临时密码登录
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 2
Server version: 5.7.30

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.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> set password = password('123456'); //修改密码
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> quit
Bye
[root@wyt1 ~]# mysql -uroot -p'123456' //使用新密码登录
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 3
Server version: 5.7.30 MySQL Community Server (GPL)

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.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

mysql脚本部署

#!/bin/bash

#配置YUM源
mv /etc/yum.repos.d/* /tmp/
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
sed -i 's/$releasever/7/g' /etc/yum.repos.d/CentOS-Base.repo
sed -i -e '/mirrors.cloud.alinyuncs.com/d' -e 'mirrors.alinyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo

#清楚缓存安装libaio-deve包 
yum clean all
yum -y install libaio-devel

#设置变量
install_dir=/usr/local
soft=mysql-5.7.30-linux-glibc2.12-x86_64
datadir=/opt/data
read -p "请输入你要设置的数据库密码" password

#创建用户和组
id mysql &>/dev/null
if [ $? -ne 0 ];then
    useradd -r -M -s /sbin/nologin -u 306 mysql
fi

#解压软件包到指定目录
if [ ! -d $install_dir/$soft ];then
    tar xf source/${soft}.tar.gz -C $install_dir
fi

#进入目录创建软链接并修改属组和属主
cd $install_dir
ln -s $soft mysql &>/dev/null
chown -R mysql.mysql $install_dir/mysql*

#添加环境变量
echo "export PATH=$install_dir/mysql/bin:\$PATH" > /etcprofile.d/mysql.sh

#建立数据库存放目录
if [ ! -d $datadir ];then
    mkdir -p $datadir
fi

#修改目录属主属组
chown -R mysql.mysql $datadir

#初始化数据库并提取临时密码到指定目录
datastatus=$(ls $datadir|wc -l)
if [ $datastatus -eq 0 ];then
    $datastatus/mysql/bin/mysqld --initialize --user=mysql --dttadir=$datadir &> /tmp/temppass
    temp_passwd=$(grep 'password' /tmp/temppass|awk '{print $NF}')

#生成配置文件
cat > /etc/my.cnf </dev/null

#登录数据库修改密码
$install_dir/mysql/bin/mysql -uroot -p$temp_passwd --connect-expired-password -e "set password=password('$password';)"

2. mysql配置文件

mysql的配置文件为/etc/my.cnf

配置文件查找次序:若在多个配置文件中均有设定,则最后找到的最终生效

/etc/my.cnf --> /etc/mysql/my.cnf --> --default-extra-file=/PATH/TO/CONF_FILE --> ~/.my.cnf

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将无法正常处理连接请求

3.mysql数据库备份与恢复

3.1 数据库常用备份方案

数据库备份方案:

  • 全量备份
  • 增量备份
  • 差异备份
备份方案 特点
全量备份 对所有数据完全拷贝,完全复制,数据都可以恢复,备份时间长
增量备份 需要在全量备份之后增加上次丢失或者修改过的数据,备份时间短 ,要是其中有一天丢失了数据就需要先恢复全量备份再接着恢复后面每一天的增量备份,但必须按顺序恢复,比较麻烦
差异备份 备份全量备份之后丢失或者修改过的有差异的数据,恢复时间短,不会很麻烦

3.2 mysql备份工具mysqldump与恢复

语法:mysqldump [OPTIONS] database [tables …] //备份数据库其中某些表,需要恢复数据库

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

mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| student          |
| teacher          |
+------------------+
2 rows in set (0.00 sec)

[root@wyt1 ~]# mysqldump -uroot -p123456 school teacher > teacher-2020061302.sql //备份数据库中的teacher表
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@wyt1 ~]# ls
all-2020061301.sql   teacher-2020061302.sql

mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| student          |
| teacher          |
+------------------+
2 rows in set (0.00 sec)

mysql> drop table teacher; //删除表
Query OK, 0 rows affected (0.03 sec)

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

[root@wyt1 ~]# mysql -uroot -p123456 //登录数据库
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> source teacher-2020061302.sql; //恢复表
Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)
.......

mysql> show tables; //查看表
+------------------+
| Tables_in_school |
+------------------+
| student          |
| teacher          |
+------------------+
2 rows in set (0.00 sec)

mysql> drop database school; //删除数据库
Query OK, 2 rows affected (0.04 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

[root@wyt1 ~]# mysql -uroot -p123456 school <  teacher-2020061302.sql //恢复表报错没有数据库
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1049 (42000): Unknown database 'school'

mysql> create database school; //创建数据库
Query OK, 1 row affected (0.00 sec)

[root@wyt1 ~]# mysql -uroot -p123456 school <  teacher-2020061302.sql //恢复表
mysql: [Warning] Using a password on the command line interface can be insecure.

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> show tables; //只恢复备份的表
+------------------+
| Tables_in_school |
+------------------+
| teacher          |
+------------------+
1 row in set (0.01 sec)

[root@wyt1 ~]# mysqldump -uroot -p123456 school > school-2020061303.sql //备份数据库
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@wyt1 ~]# ls
all-2020061301.sql       school-2020061303.sql
teacher-2020061302.sql

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| sys                |
+--------------------+
5 rows in set (0.00 sec)
mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| student          |
| teacher          |
+------------------+
2 rows in set (0.00 sec)
mysql> drop table student;//删除表
Query OK, 0 rows affected (0.04 sec)

mysql> drop table teacher; //删除表
Query OK, 0 rows affected (0.03 sec)

mysql> show tables; //查看表
Empty set (0.01 sec)
[root@wyt1 ~]# mysql -uroot -p123456 school < school-2020061303.sql //恢复数据库中的表
mysql: [Warning] Using a password on the command line interface can be insecure.

mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| student          |
| teacher          |
+------------------+
2 rows in set (0.00 sec)

语法:mysqldump [OPTIONS] --all-databases [OPTIONS] //备份所有的数据库

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

mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| student          |
| teacher          |
+------------------+
2 rows in set (0.00 sec)

[root@wyt1 ~]# mysqldump -uroot -p123456 --all-databases > all-2020061301.sql  //备份数据库
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@wyt1 ~]# ls
all-2020061301.sql 

mysql> drop database school; //删除数据库
Query OK, 2 rows affected (0.09 sec)

mysql> show databases; //查看数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

[root@wyt1 ~]# mysql -uroot -p123456 < all-2020061301.sql //恢复数据库
mysql: [Warning] Using a password on the command line interface can be insecure.

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

语法:mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3…] //备份数据库里面的所有表,不需要恢复数据库

[root@wyt1 ~]# mysqldump -uroot -p123456 --databases school > school-2020061304.sql//备份数据库
mysqldump: [Warning] Using a password on the command line interface can be insecure.

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

mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| student          |
| teacher          |
+------------------+
2 rows in set (0.00 sec)

mysql> drop table teacher; //删除表
Query OK, 0 rows affected (0.04 sec)

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

[root@wyt1 ~]# mysql -uroot -p123456 < school-2020061304.sql //恢复表
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@wyt1 ~]# ls
all-2020061301.sql                      school-2020061303.sql
teacher-2020061302.sql                  school-2020061304.sql 

mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| student          |
| teacher          |
+------------------+
2 rows in set (0.00 sec)

3.3差异备份与恢复

3.3.1. mysql差异备份

开启MySQL服务器的二进制日志功能

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

server-id = 1  //设置服务器标识符
log-bin = mysql.bin  //开启二进制日志功能
[root@wyt1 ~]# service mysqld restart //重启服务

对数据库进行完全备份

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

mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| student          |
| teacher          |
+------------------+
2 rows in set (0.00 sec)

mysql> select * from student;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | wangqing    |   25 |
|  4 | sean        |   28 |
|  5 | zhangshan   |   26 |
|  6 | zhangshan   |   20 |
|  7 | lisi        | NULL |
|  8 | chenshuo    |   10 |
|  9 | wangwu      |    3 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
+----+-------------+------+
11 rows in set (0.00 sec)

mysql> select * from teacher;
+----+----------+------+--------+
| id | name     | age  | salary |
+----+----------+------+--------+
|  1 | lisha    |   25 |   8000 |
|  2 | xiaozhao |   23 |  10000 |
|  3 | dawang   |   26 |  12000 |
|  4 | xiaoli   |   28 |  15000 |
|  5 | xiaxia   |   27 |   9000 |
+----+----------+------+--------+
5 rows in set (0.01 sec)

[root@wyt1 ~]#  mysqldump -uroot -p123456 --single-transaction --flush-logs --master-data=2 --all-databases --delete-master-logs > all-2020061300.sql //完全备份
mysqldump: [Warning] Using a password on the command line interface can be insecure.

增加新内容

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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| student          |
| teacher          |
+------------------+
2 rows in set (0.00 sec)

mysql> select * from student;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | wangqing    |   25 |
|  4 | sean        |   28 |
|  5 | zhangshan   |   26 |
|  6 | zhangshan   |   20 |
|  7 | lisi        | NULL |
|  8 | chenshuo    |   10 |
|  9 | wangwu      |    3 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
+----+-------------+------+
11 rows in set (0.00 sec)
mysql> insert student(name,age) value('gaoxing',18);
Query OK, 1 row affected (0.02 sec)

mysql> select * from student;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | wangqing    |   25 |
|  4 | sean        |   28 |
|  5 | zhangshan   |   26 |
|  6 | zhangshan   |   20 |
|  7 | lisi        | NULL |
|  8 | chenshuo    |   10 |
|  9 | wangwu      |    3 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
| 12 | gaoxing     |   18 |
+----+-------------+------+
12 rows in set (0.00 sec)

mysql> update student set age = 22 where name = 'tom';
Query OK, 1 row affected (0.02 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from student;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   22 |
|  2 | jerry       |   23 |
|  3 | wangqing    |   25 |
|  4 | sean        |   28 |
|  5 | zhangshan   |   26 |
|  6 | zhangshan   |   20 |
|  7 | lisi        | NULL |
|  8 | chenshuo    |   10 |
|  9 | wangwu      |    3 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
| 12 | gaoxing     |   18 |
+----+-------------+------+
12 rows in set (0.01 sec)

3.3.2. mysql差异备份恢复

删除数据库

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| sys                |
+--------------------+
5 rows in set (0.00 sec)
mysql> drop database school;
Query OK, 2 rows affected (0.05 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

刷新创建新的二进制日志

[root@wyt1 ~]# ll /opt/mysqldata/
total 122996
-rw-r-----. 1 mysql mysql       56 Jun 10 23:03 auto.cnf
-rw-------. 1 mysql mysql     1676 Jun 10 23:03 ca-key.pem
-rw-r--r--. 1 mysql mysql     1112 Jun 10 23:03 ca.pem
-rw-r--r--. 1 mysql mysql     1112 Jun 10 23:03 client-cert.pem
-rw-------. 1 mysql mysql     1680 Jun 10 23:03 client-key.pem
-rw-r-----. 1 mysql mysql     1083 Jun 14 14:36 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 Jun 14 15:09 ibdata1
-rw-r-----. 1 mysql mysql 50331648 Jun 14 15:09 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 Jun 10 23:03 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 Jun 14 15:00 ibtmp1
drwxr-x---. 2 mysql mysql     4096 Jun 14 00:10 mysql
-rw-r-----. 1 mysql mysql      874 Jun 14 15:09 mysql.000002
-rw-r-----. 1 mysql mysql       15 Jun 14 15:00 mysql.index
-rw-r-----. 1 mysql mysql        6 Jun 14 14:46 mysql.pid
drwxr-x---. 2 mysql mysql     8192 Jun 10 23:03 performance_schema
-rw-------. 1 mysql mysql     1676 Jun 10 23:03 private_key.pem
-rw-r--r--. 1 mysql mysql      452 Jun 10 23:03 public_key.pem
-rw-r--r--. 1 mysql mysql     1112 Jun 10 23:03 server-cert.pem
-rw-------. 1 mysql mysql     1676 Jun 10 23:03 server-key.pem
drwxr-x---. 2 mysql mysql     8192 Jun 10 23:03 sys
-rw-r-----. 1 mysql mysql    34047 Jun 14 14:46 wyt1.err
[root@wyt1 ~]#  mysqladmin -uroot -p123456 flush-logs
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
[root@wyt1 ~]# ll /opt/mysqldata/
total 123000
-rw-r-----. 1 mysql mysql       56 Jun 10 23:03 auto.cnf
-rw-------. 1 mysql mysql     1676 Jun 10 23:03 ca-key.pem
-rw-r--r--. 1 mysql mysql     1112 Jun 10 23:03 ca.pem
-rw-r--r--. 1 mysql mysql     1112 Jun 10 23:03 client-cert.pem
-rw-------. 1 mysql mysql     1680 Jun 10 23:03 client-key.pem
-rw-r-----. 1 mysql mysql     1083 Jun 14 14:36 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 Jun 14 15:09 ibdata1
-rw-r-----. 1 mysql mysql 50331648 Jun 14 15:09 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 Jun 10 23:03 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 Jun 14 15:00 ibtmp1
drwxr-x---. 2 mysql mysql     4096 Jun 14 00:10 mysql
-rw-r-----. 1 mysql mysql      917 Jun 14 15:14 mysql.000002
-rw-r-----. 1 mysql mysql      154 Jun 14 15:14 mysql.000003
-rw-r-----. 1 mysql mysql       30 Jun 14 15:14 mysql.index
-rw-r-----. 1 mysql mysql        6 Jun 14 14:46 mysql.pid
drwxr-x---. 2 mysql mysql     8192 Jun 10 23:03 performance_schema
-rw-------. 1 mysql mysql     1676 Jun 10 23:03 private_key.pem
-rw-r--r--. 1 mysql mysql      452 Jun 10 23:03 public_key.pem
-rw-r--r--. 1 mysql mysql     1112 Jun 10 23:03 server-cert.pem
-rw-------. 1 mysql mysql     1676 Jun 10 23:03 server-key.pem
drwxr-x---. 2 mysql mysql     8192 Jun 10 23:03 sys
-rw-r-----. 1 mysql mysql    34047 Jun 14 14:46 wyt1.err

恢复完全备份

[root@wyt1 ~]#  mysql -uroot -p123456 < all-2020061300.sql
mysql: [Warning] Using a password on the command line interface can be insecure.

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

mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| student          |
| teacher          |
+------------------+
2 rows in set (0.00 sec)

mysql> select * from student;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | wangqing    |   25 |
|  4 | sean        |   28 |
|  5 | zhangshan   |   26 |
|  6 | zhangshan   |   20 |
|  7 | lisi        | NULL |
|  8 | chenshuo    |   10 |
|  9 | wangwu      |    3 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
+----+-------------+------+
11 rows in set (0.00 sec)

恢复差异备份

[root@wyt1 ~]# ll /opt/mysqldata/
total 123000
-rw-r-----. 1 mysql mysql       56 Jun 10 23:03 auto.cnf
-rw-------. 1 mysql mysql     1676 Jun 10 23:03 ca-key.pem
-rw-r--r--. 1 mysql mysql     1112 Jun 10 23:03 ca.pem
-rw-r--r--. 1 mysql mysql     1112 Jun 10 23:03 client-cert.pem
-rw-------. 1 mysql mysql     1680 Jun 10 23:03 client-key.pem
-rw-r-----. 1 mysql mysql     1083 Jun 14 14:36 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 Jun 14 15:09 ibdata1
-rw-r-----. 1 mysql mysql 50331648 Jun 14 15:09 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 Jun 10 23:03 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 Jun 14 15:00 ibtmp1
drwxr-x---. 2 mysql mysql     4096 Jun 14 00:10 mysql
-rw-r-----. 1 mysql mysql      917 Jun 14 15:14 mysql.000002
-rw-r-----. 1 mysql mysql      154 Jun 14 15:14 mysql.000003
-rw-r-----. 1 mysql mysql       30 Jun 14 15:14 mysql.index
-rw-r-----. 1 mysql mysql        6 Jun 14 14:46 mysql.pid
drwxr-x---. 2 mysql mysql     8192 Jun 10 23:03 performance_schema
-rw-------. 1 mysql mysql     1676 Jun 10 23:03 private_key.pem
-rw-r--r--. 1 mysql mysql      452 Jun 10 23:03 public_key.pem
-rw-r--r--. 1 mysql mysql     1112 Jun 10 23:03 server-cert.pem
-rw-------. 1 mysql mysql     1676 Jun 10 23:03 server-key.pem
drwxr-x---. 2 mysql mysql     8192 Jun 10 23:03 sys
-rw-r-----. 1 mysql mysql    34047 Jun 14 14:46 wyt1.err

检查误删数据库的位置在什么地方

[root@wyt1 ~]#  mysql -uroot -p123456 
mysql> show binlog events in 'mysql.000002'; 
+--------------+-----+----------------+-----------+-------------+---------------------------------------+
| Log_name     | Pos | Event_type     | Server_id | End_log_pos | Info                                  |
+--------------+-----+----------------+-----------+-------------+---------------------------------------+
| mysql.000002 |   4 | Format_desc    |         1 |         123 | Server ver: 5.7.30-log, Binlog ver: 4 |
| mysql.000002 | 123 | Previous_gtids |         1 |         154 |                                       |
| mysql.000002 | 154 | Anonymous_Gtid |         1 |         219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'  |
| mysql.000002 | 219 | Query          |         1 |         293 | BEGIN                                 |
| mysql.000002 | 293 | Table_map      |         1 |         349 | table_id: 140 (school.student)        |
| mysql.000002 | 349 | Write_rows     |         1 |         398 | table_id: 140 flags: STMT_END_F       |
| mysql.000002 | 398 | Xid            |         1 |         429 | COMMIT /* xid=485 */                  |
| mysql.000002 | 429 | Anonymous_Gtid |         1 |         494 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'  |
| mysql.000002 | 494 | Query          |         1 |         568 | BEGIN                                 |
| mysql.000002 | 568 | Table_map      |         1 |         624 | table_id: 140 (school.student)        |
| mysql.000002 | 624 | Update_rows    |         1 |         680 | table_id: 140 flags: STMT_END_F       |
| mysql.000002 | 680 | Xid            |         1 |         711 | COMMIT /* xid=487 */                  |
| mysql.000002 | 711 | Anonymous_Gtid |         1 |         776 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'  |
| mysql.000002 | 776 | Query          |         1 |         874 | drop database school                  |
| mysql.000002 | 874 | Rotate         |         1 |         917 | mysql.000003;pos=4                    |
+--------------+-----+----------------+-----------+-------------+---------------------------------------+
15 rows in set (0.00 sec)

使用mysqlbinlog恢复差异备份

[root@wyt1 ~]#  mysql -uroot -p123456 
[root@wyt1 ~]#  mysqlbinlog --stop-position=776 /opt/mysqldata/mysql.000002 |mysql -uroot -p123456;
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql> select * from student;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   22 |
|  2 | jerry       |   23 |
|  3 | wangqing    |   25 |
|  4 | sean        |   28 |
|  5 | zhangshan   |   26 |
|  6 | zhangshan   |   20 |
|  7 | lisi        | NULL |
|  8 | chenshuo    |   10 |
|  9 | wangwu      |    3 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
| 12 | gaoxing     |   18 |
+----+-------------+------+
12 rows in set (0.00 sec)

你可能感兴趣的:(mysql进阶备份与恢复配置)