欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Mysql二进制安装与备份的全过程记录

程序员文章站 2022-03-01 17:55:02
目录mysql的二进制安装mysql的配置文件备份与恢复总结mysql的二进制安装下载安装包[root@localhost mysql]# wget https://downloads.mysql.c...

mysql的二进制安装

下载安装包

[root@localhost mysql]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
--2021-05-04 20:34:21--  https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
resolving downloads.mysql.com (downloads.mysql.com)... 137.254.60.14
connecting to downloads.mysql.com (downloads.mysql.com)|137.254.60.14|:443... connected.
http request sent, awaiting response... 302 found
location: https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz [following]
--2021-05-04 20:34:23--  https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
resolving cdn.mysql.com (cdn.mysql.com)... 223.119.236.209
connecting to cdn.mysql.com (cdn.mysql.com)|223.119.236.209|:443... connected.
http request sent, awaiting response... 200 ok
length: 661718255 (631m) [application/x-tar-gz]
saving to: ‘mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz'
mysql-5.7.33-linux- 100%[================>] 631.06m  11.3mb/s    in 57s     
2021-05-04 20:35:21 (11.0 mb/s) - ‘mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz' saved [661718255/661718255]

创建用户

[root@localhost mysql]# useradd -r -m -s /sbin/nologin mysql

解压至指定目录

[root@localhost mysql]# tar xf mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz -c /usr/local

创建软链接或者修改目录名

[root@localhost local]# mv mysql-5.7.33-linux-glibc2.12-x86_64/ mysql
[root@localhost local]# ls
apache  apr-util  etc    include  lib64    mysql  share
apr     bin       games  lib      libexec  sbin   src

修改属主和属组

[root@localhost local]# chown -r mysql.mysql /usr/local/mysql/
[root@localhost local]# ll
drwxr-xr-x.  9 mysql mysql 129 五月    4 20:40 mysql

设置环境变量(因为不是用yum装的,找不到mysql程序)

[root@localhost mysql]# vim /etc/profile.d/mysql.sh
export path=/usr/local/mysql/bin:$path
[root@localhost mysql]# . /etc/profile.d/mysql.sh
[root@localhost mysql]# which mysql
/usr/local/mysql/bin/mysql

创建存放数据的目录并修改属主属组

需要一个空间大的目录,或者将目录创建后将硬盘设备挂载在此目录上

[root@localhost mysql]# mkdir /opt/mysql_data
[root@localhost mysql]# chown -r mysql.mysql /opt/mysql_data/
[root@localhost mysql]# ll /opt
drwxr-xr-x. 2 mysql mysql 6 五月    4 20:58 mysql_data

初始化并保存密码

[root@localhost mysql]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/mysql_data/        //初始化    控制mysql的用户        数据存放目录
2021-05-04t13:01:07.403961z 0 [warning] timestamp with implicit default value is deprecated. please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-05-04t13:01:07.683107z 0 [warning] innodb: new log files created, lsn=45790
2021-05-04t13:01:07.739366z 0 [warning] innodb: creating foreign key constraint system tables.
2021-05-04t13:01:07.746720z 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: caa21b8a-acd8-11eb-b7ab-000c294bb269.
2021-05-04t13:01:07.747895z 0 [warning] gtid table is not ready to be used. table 'mysql.gtid_executed' cannot be opened.
2021-05-04t13:01:09.096727z 0 [warning] ca certificate ca.pem is self signed.
2021-05-04t13:01:09.485357z 1 [note] a temporary password is generated for root@localhost: q_ug8?3sa/l%
[root@localhost mysql]# vim password
q_ug8?3sa/l%

写配置文件

[root@localhost mysql]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql            //程序位置
datadir = /opt/mysql_data            //数据存放位置
socket = /tmp/mysql.sock            //文件套接字位置
port = 3306                                    //端口
pid-file = /opt/mysql_data/mysql.pid        //进程文件位置
user = mysql                                //用户
skip-name-resolve                        //跳过域名解析,即直接在内网使用ip连接数据库

配置启动脚本和开机自启

[root@localhost ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@localhost ~]# sed -ri 's#^(datadir=).*#\1/opt/mysql_data#g' /etc/init.d/mysqld
[root@localhost mysql]# head -47 /etc/init.d/mysqld |tail -2
basedir=/usr/local/mysql
datadir=/opt/mysql_data
[root@localhost mysql]# service mysqld start
starting mysql.logging to '/opt/data/localhost.localdomain.err'.
 success! 
[root@localhost mysql]# chkconfig  mysqld on
[root@localhost mysql]# chkconfig --list
note: this output shows sysv services only and does not include native
      systemd services. sysv configuration data might be overridden by native
      systemd configuration.
      if you want to list systemd services use 'systemctl list-unit-files'.
      to see services enabled on particular target use
      'systemctl list-dependencies [target]'.
mysqld             0:off    1:off    2:on    3:on    4:on    5:on    6:off
[root@localhost mysql]# ss -anlt
state   recv-q  send-q    local address:port     peer address:port  process  
listen  0       128             0.0.0.0:22            0.0.0.0:*              
listen  0       128                   *:443                 *:*              
listen  0       80                    *:3306                *:*              
listen  0       128                   *:80                  *:*              
listen  0       128                [::]:22               [::]:*              

头文件和库文件配置

[root@localhost mysql]# ln -s /usr/local/mysql/include /usr/include/mysql                        [root@localhost mysql]# vim /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
[root@localhost mysql]# ldconfig            //重新读取配置文件

启动并设置密码

[root@localhost local]# mysql -uroot -p
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: no such file or directory        //缺少包
[root@localhost local]# dnf provides libncurses.so.5                        //查看包所依赖的类库
warning: failed loading '/etc/yum.repos.d/mssql-server.repo', skipping.
ncurses-compat-libs-6.1-7.20180224.el8.i686 : ncurses compatibility libraries
repo        : baseos
matched from:
provide    : libncurses.so.5
[root@localhost local]# dnf -y install ncurses-compat-libs
[root@localhost local]# cat password
/sdjtcedy7f7
[root@localhost local]# mysql -uroot -p
enter password: 
welcome to the mysql monitor.  commands end with ; or \g.
your mysql connection id is 2
server version: 5.7.33
copyright (c) 2000, 2021, oracle and/or its affiliates.
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的配置文件

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

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

/etc/my.cnf --> /etc/mysql/my.cnf --> --default-extra-file=/path/to/conf_file --> ~/.my.cnf

 

参数 说明
port = 3306 设置监听端口,默认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将无法正常处理连接请求

备份与恢复

数据库常用备份方案

数据库备份方案:

  • 全量备份
  • 增量备份
  • 差异备份
备份方案 特点
全量备份 全量备份就是指对某一个时间点上的所有数据或应用进行的一个完全拷贝
数据恢复快,备份时间长
增量备份 增量备份是指在一次全备份或上一次增量备份后,以后每次的备份只需备份
与前一次相比增加和者被修改的文件。这就意味着,第一次增量备份的对象
是进行全备后所产生的增加和修改的文件
第二次增量备份的对象是进行第一次增量
备份后所产生的增加和修改的文件,如此类推
没有重复的备份数据
备份时间短
恢复数据时必须按一定的顺序进行
差异备份 备份上一次的完全备份后发生变化的所有文件
差异备份是指在一次全备份后到进行差异备份的这段时间内
对那些增加或者修改文件的备份。在进行恢复时
我们只需对第一次全量备份和最后一次差异备份进行恢复

mysql备份工具mysqldump

mysqldump 语法:

     mysqldump [options] database [tables ...]                                                //备份数据表
    mysqldump [options] --all-databases [options]                                    //备份全部数据库
    mysqldump [options] --databases [options] db1 [db2 db3...]            //备份某个数据库
    -uusername      //指定数据库用户名
    -hhost          //指定服务器主机,请使用ip地址
    -ppassword      //指定数据库用户的密码
    -p#             //指定数据库监听的端口,这里的#需用实际的端口号代替

备份所有数据库

[root@localhost ~]# mysqldump -uroot -p  --all-databases > /mysql_dump/all_20210504.sql
enter password: 
[root@localhost ~]# ls /mysql_dump/
all_20210504.sql

备份某个数据库

[root@localhost ~]# mysqldump -uroot -p  --databases hanao > /mysql_dump/hanao_db_20210504.sql
enter password: 
[root@localhost ~]# ls /mysql_dump/
all_20210504.sql  hanao_db_20210504.sql

备份某个数据表

[root@localhost ~]# mysqldump -uroot -p hanao student > /mysql_dump/student_table_20210504.sql
enter password: 
[root@localhost ~]# ls /mysql_dump/
all_20210504.sql  hanao_db_20210504.sql  student_table_20210504.sql

mysql全备数据恢复

恢复数据表

mysql> use hanao
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> drop table student;
query ok, 0 rows affected (0.01 sec)

mysql> show tables;
empty set (0.00 sec)

[root@localhost ~]# cd /mysql_dump/
[root@localhost mysql_dump]# mysql -uroot -pha153624.
mysql> use hanao;
database changed
mysql> source student_table_20210504.sql
query ok, 0 rows affected (0.00 sec)
query ok, 0 rows affected (0.00 sec)
mysql> show tables;
+-----------------+
| tables_in_hanao |
+-----------------+
| student         |
+-----------------+
1 row in set (0.00 sec)

恢复数据库

mysql> drop database hanao;
query ok, 1 row affected (0.00 sec)

[root@localhost mysql_dump]# mysql -uroot -pha153624. < hanao_db_20210504.sql 
mysql: [warning] using a password on the command line interface can be insecure.


[root@localhost mysql_dump]# mysql -uroot -pha153624. -e 'show databases;'
mysql: [warning] using a password on the command line interface can be insecure.
+--------------------+
| database           |
+--------------------+
| information_schema |
| ha                 |
| hanao              |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

恢复所有数据库

mysql> drop database ha;
query ok, 1 row affected (0.01 sec)

[root@localhost mysql_dump]# mysql -uroot -pha153624. < all_20210504.sql 
mysql: [warning] using a password on the command line interface can be insecure.
[root@localhost mysql_dump]# mysql -uroot -pha153624. -e 'show databases;'
mysql: [warning] using a password on the command line interface can be insecure.
+--------------------+
| database           |
+--------------------+
| information_schema |
| ha                 |
| hanao              |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

mysql差备(事务日志备份/binlog备份)

备份

开启mysql服务二进制日志功能

root@localhost mysql_data]# vim /etc/my.cnf 
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/mysql_data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/mysql_data/mysql.pid
user = mysql
skip-name-resolve
server-id=1						//设置服务器标识符
log-bin=mysql_bin			//开启二进制日志功能

重启服务

[root@localhost mysql_data]# service mysqld restart
shutting down mysql.. success! 
starting mysql. success! 

先对数据库进行全备

查看数据库中的表和数据

[root@localhost /]# mysql -uroot -pha153624...
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 4
server version: 5.7.33-log mysql community server (gpl)

copyright (c) 2000, 2021, oracle and/or its affiliates.

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 |
| hanao              |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> use hanao;
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_hanao |
+-----------------+
| student         |
+-----------------+
1 row in set (0.00 sec)

mysql> desc student;
+-------+--------------+------+-----+---------+----------------+
| field | type         | null | key | default | extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | no   | pri | null    | auto_increment |
| name  | varchar(100) | no   |     | null    |                |
| age   | tinyint(4)   | yes  |     | null    |                |
+-------+--------------+------+-----+---------+----------------+
3 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 | chengshuo   |   10 |
|  9 | wangwu      |    3 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
+----+-------------+------+
11 rows in set (0.00 sec)

对数据进行全备

[root@localhost mysql_dump]# mysqldump -uroot -pha153624... --single-transaction --flush-logs --master-data=2 --all-databases --delete-master-logs > all-20210506.sql
mysqldump: [warning] using a password on the command line interface can be insecure.
[root@localhost mysql_dump]# ll
total 1720
-rw-r--r--. 1 root root 874762 五月    4 22:48 all_20210504.sql
-rw-r--r--. 1 root root 873608 五月    6 18:19 all-20210506.sql
-rw-r--r--. 1 root root   2250 五月    4 22:51 hanao_db_20210504.sql
-rw-r--r--. 1 root root   2108 五月    4 22:52 student_table_20210504.sql

插入新数据

mysql> use hanao;
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(name,age) value('hanao',22);
query ok, 1 row affected (0.00 sec)

mysql> insert into student(name,age) value('hanao',21);
query ok, 1 row affected (0.00 sec)

mysql> insert into student(name,age) value('tengjia',21);
query ok, 1 row affected (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 | chengshuo   |   10 |
|  9 | wangwu      |    3 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
| 12 | hanao       |   22 |
| 13 | hanao       |   21 |
| 14 | tengjia     |   21 |
+----+-------------+------+
14 rows in set (0.00 sec)

恢复备份

模拟误删数据

mysql> drop database hanao;
query ok, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

刷新二进制日志文件

[root@localhost mysql_dump]# ll /opt/mysql_data/
total 123020
-rw-r-----. 1 mysql mysql       56 五月    4 22:01 auto.cnf
-rw-------. 1 mysql mysql     1680 五月    4 22:01 ca-key.pem
-rw-r--r--. 1 mysql mysql     1112 五月    4 22:01 ca.pem
-rw-r--r--. 1 mysql mysql     1112 五月    4 22:01 client-cert.pem
-rw-------. 1 mysql mysql     1680 五月    4 22:01 client-key.pem
-rw-r-----. 1 mysql mysql      562 五月    6 18:06 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 五月    6 18:24 ibdata1
-rw-r-----. 1 mysql mysql 50331648 五月    6 18:24 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 五月    4 22:01 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 五月    6 18:19 ibtmp1
-rw-r-----. 1 mysql mysql    58417 五月    6 18:06 localhost.localdomain.err
drwxr-x---. 2 mysql mysql     4096 五月    4 23:03 mysql
-rw-r-----. 1 mysql mysql     1129 五月    6 18:23 mysql_bin.000010
-rw-r-----. 1 mysql mysql       19 五月    6 18:19 mysql_bin.index
-rw-r-----. 1 mysql mysql        6 五月    6 18:06 mysql.pid
drwxr-x---. 2 mysql mysql     8192 五月    4 22:01 performance_schema
-rw-------. 1 mysql mysql     1680 五月    4 22:01 private_key.pem
-rw-r--r--. 1 mysql mysql      452 五月    4 22:01 public_key.pem
-rw-r--r--. 1 mysql mysql     1112 五月    4 22:01 server-cert.pem
-rw-------. 1 mysql mysql     1676 五月    4 22:01 server-key.pem
drwxr-x---. 2 mysql mysql     8192 五月    4 22:01 sys
[root@localhost mysql_dump]# mysqladmin -uroot -pha153624... flush-logs
mysqladmin: [warning] using a password on the command line interface can be insecure.
[root@localhost mysql_dump]# ll /opt/mysql_data/
total 123024
-rw-r-----. 1 mysql mysql       56 五月    4 22:01 auto.cnf
-rw-------. 1 mysql mysql     1680 五月    4 22:01 ca-key.pem
-rw-r--r--. 1 mysql mysql     1112 五月    4 22:01 ca.pem
-rw-r--r--. 1 mysql mysql     1112 五月    4 22:01 client-cert.pem
-rw-------. 1 mysql mysql     1680 五月    4 22:01 client-key.pem
-rw-r-----. 1 mysql mysql      562 五月    6 18:06 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 五月    6 18:24 ibdata1
-rw-r-----. 1 mysql mysql 50331648 五月    6 18:24 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 五月    4 22:01 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 五月    6 18:19 ibtmp1
-rw-r-----. 1 mysql mysql    58417 五月    6 18:06 localhost.localdomain.err
drwxr-x---. 2 mysql mysql     4096 五月    4 23:03 mysql
-rw-r-----. 1 mysql mysql     1176 五月    6 18:24 mysql_bin.000010
-rw-r-----. 1 mysql mysql      154 五月    6 18:24 mysql_bin.000011
-rw-r-----. 1 mysql mysql       38 五月    6 18:24 mysql_bin.index
-rw-r-----. 1 mysql mysql        6 五月    6 18:06 mysql.pid
drwxr-x---. 2 mysql mysql     8192 五月    4 22:01 performance_schema
-rw-------. 1 mysql mysql     1680 五月    4 22:01 private_key.pem
-rw-r--r--. 1 mysql mysql      452 五月    4 22:01 public_key.pem
-rw-r--r--. 1 mysql mysql     1112 五月    4 22:01 server-cert.pem
-rw-------. 1 mysql mysql     1676 五月    4 22:01 server-key.pem
drwxr-x---. 2 mysql mysql     8192 五月    4 22:01 sys
[root@localhost mysql_dump]# cat /opt/mysql_data/mysql_bin.index
./mysql_bin.000010
./mysql_bin.000011

查看恢复的全备的数据

数据中没有后面新增的数据

[root@localhost mysql_dump]# mysql -uroot -pha153624... < all-20210506.sql 
mysql: [warning] using a password on the command line interface can be insecure.
[root@localhost mysql_dump]# mysql -uroot -pha153624... -e 'show databases;'
mysql: [warning] using a password on the command line interface can be insecure.
+--------------------+
| database           |
+--------------------+
| information_schema |
| hanao              |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
[root@localhost mysql_dump]# mysql -uroot -pha153624... -e 'select * from hanao.student;'
mysql: [warning] using a password on the command line interface can be insecure.
+----+-------------+------+
| 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 | chengshuo   |   10 |
|  9 | wangwu      |    3 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
+----+-------------+------+

恢复差异备份

查看误删数据库的操作在什么地方

可以看到drop命令开始之前的pos是1034,所以我们用1034恢复到数据删除之前

[root@localhost mysql_dump]# mysql -uroot -pha153624...
mysql> show binlog events in 'mysql_bin.000010';
+------------------+------+----------------+-----------+-------------+---------------------------------------+
| log_name         | pos  | event_type     | server_id | end_log_pos | info                                  |
+------------------+------+----------------+-----------+-------------+---------------------------------------+
| mysql_bin.000010 |    4 | format_desc    |         1 |         123 | server ver: 5.7.33-log, binlog ver: 4 |
| mysql_bin.000010 |  123 | previous_gtids |         1 |         154 |                                       |
| mysql_bin.000010 |  154 | anonymous_gtid |         1 |         219 | set @@session.gtid_next= 'anonymous'  |
| mysql_bin.000010 |  219 | query          |         1 |         292 | begin                                 |
| mysql_bin.000010 |  292 | table_map      |         1 |         347 | table_id: 133 (hanao.student)         |
| mysql_bin.000010 |  347 | write_rows     |         1 |         394 | table_id: 133 flags: stmt_end_f       |
| mysql_bin.000010 |  394 | xid            |         1 |         425 | commit /* xid=518 */                  |
| mysql_bin.000010 |  425 | anonymous_gtid |         1 |         490 | set @@session.gtid_next= 'anonymous'  |
| mysql_bin.000010 |  490 | query          |         1 |         563 | begin                                 |
| mysql_bin.000010 |  563 | table_map      |         1 |         618 | table_id: 133 (hanao.student)         |
| mysql_bin.000010 |  618 | write_rows     |         1 |         665 | table_id: 133 flags: stmt_end_f       |
| mysql_bin.000010 |  665 | xid            |         1 |         696 | commit /* xid=519 */                  |
| mysql_bin.000010 |  696 | anonymous_gtid |         1 |         761 | set @@session.gtid_next= 'anonymous'  |
| mysql_bin.000010 |  761 | query          |         1 |         834 | begin                                 |
| mysql_bin.000010 |  834 | table_map      |         1 |         889 | table_id: 133 (hanao.student)         |
| mysql_bin.000010 |  889 | write_rows     |         1 |         938 | table_id: 133 flags: stmt_end_f       |
| mysql_bin.000010 |  938 | xid            |         1 |         969 | commit /* xid=520 */                  |
| mysql_bin.000010 |  969 | anonymous_gtid |         1 |        1034 | set @@session.gtid_next= 'anonymous'  |
| mysql_bin.000010 | 1034 | query          |         1 |        1129 | drop database hanao                   |
| mysql_bin.000010 | 1129 | rotate         |         1 |        1176 | mysql_bin.000011;pos=4                |
+------------------+------+----------------+-----------+-------------+---------------------------------------+
20 rows in set (0.00 sec)

使用mysqlbinlog恢复差异备份

可以看到之前新增的数据也恢复回来了

[root@localhost mysql_dump]# mysqlbinlog --stop-position=1034 /opt/mysql_data/mysql_bin.000010 |mysql -uroot -pha153624...
mysql: [warning] using a password on the command line interface can be insecure.
[root@localhost mysql_dump]# mysql -uroot -pha153624... -e 'select * from hanao.student;'
mysql: [warning] using a password on the command line interface can be insecure.
+----+-------------+------+
| 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 | chengshuo   |   10 |
|  9 | wangwu      |    3 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
| 12 | hanao       |   22 |
| 13 | hanao       |   21 |
| 14 | tengjia     |   21 |
+----+-------------+------+

总结

到此这篇关于mysql二进制安装与备份的文章就介绍到这了,更多相关mysql二进制安装与备份内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!