mysql进阶(备份与恢复)

mysql进阶

二进制格式mysql安装

//二进制格式的mysql软件包
[root@lwq-client ~]# cd /usr/src/
[root@lwq-client src]# ls
debug  kernels
[root@lwq-client src]# ls  
debug  kernels  mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

//创建用户和组
[root@lwq-client src]# groupadd -r mysql
[root@lwq-client src]# useradd -M -s /sbin/nologin -g mysql mysql

//解压软件至/usr/local
[root@lwq-client src]# ls
debug  kernels  mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
[root@lwq-client src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@lwq-client src]# ls
debug  kernels  mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
[root@lwq-client src]# ls /usr/local/
bin  etc  games  include  lib  lib64  libexec  mysql-5.7.22-linux-glibc2.12-x86_64  sbin  share  src
[root@lwq-client src]# cd /usr/local/
[root@lwq-client local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql
"mysql" -> "mysql-5.7.22-linux-glibc2.12-x86_64/"
[root@lwq-client local]# ll
总用量 0
drwxr-xr-x. 2 root root   6 3月  10 2016 bin
drwxr-xr-x. 2 root root   6 3月  10 2016 etc
drwxr-xr-x. 2 root root   6 3月  10 2016 games
drwxr-xr-x. 2 root root   6 3月  10 2016 include
drwxr-xr-x. 2 root root   6 3月  10 2016 lib
drwxr-xr-x. 2 root root   6 3月  10 2016 lib64
drwxr-xr-x. 2 root root   6 3月  10 2016 libexec
lrwxrwxrwx. 1 root root  36 8月  16 14:37 mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 root root 129 8月  16 14:36 mysql-5.7.22-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root   6 3月  10 2016 sbin
drwxr-xr-x. 5 root root  49 7月  20 01:10 share
drwxr-xr-x. 2 root root   6 3月  10 2016 src

//修改目录/usr/local/mysql的属主属组
[root@lwq-client ~]# chown -R mysql.mysql /usr/local/mysql
[root@lwq-client ~]# ll /usr/local/mysql -d
lrwxrwxrwx. 1 mysql mysql 36 8月  16 14:37 /usr/local/mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/

//添加环境变量
[root@lwq-client ~]# ls /usr/local/mysql
bin  COPYING  docs  include  lib  man  README  share  support-files
[root@lwq-client ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@lwq-client ~]# . /etc/profile.d/mysql.sh
[root@lwq-client ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

//建立数据存放目录
[root@lwq-client ~]# mkdir /opt/data
[root@lwq-client ~]# chown -R mysql.mysql /opt/data/
[root@lwq-client ~]# ll /opt/
总用量 0
drwxr-xr-x. 2 mysql mysql 6 8月  16 14:41 data
drwxr-xr-x. 2 root  root  6 3月   9 2015 rh

//初始化数据库
[root@lwq-client ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2019-08-16T06:42:19.728361Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-08-16T06:42:20.065155Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-08-16T06:42:20.123829Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-08-16T06:42:20.202434Z 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: fef4d122-bff0-11e9-a474-000c29b10852.
2019-08-16T06:42:20.203107Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-08-16T06:42:20.204275Z 1 [Note] A temporary password is generated for root@localhost: >TWNn>4VfvRr


//生成配置文件
[root@lwq-client ~]# 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
> EOF

//配置服务启动脚本
//cp -a 保留原文件属性的前提下复制文件
[root@lwq-client ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@lwq-client ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@lwq-client ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld

//启动mysql
[root@lwq-client ~]# etc/init.d/mysqld start
-bash: etc/init.d/mysqld: 没有那个文件或目录
[root@lwq-client ~]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/opt/data/lwq-client.err'.
. SUCCESS! 
[root@lwq-client ~]# ps -ef|grep mysql
root       4350      1  0 14:45 pts/2    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
mysql      4528   4350 11 14:45 pts/2    00:00:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=lwq-client.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root       4560   3867  0 14:45 pts/2    00:00:00 grep --color=auto mysql
[root@lwq-client ~]# ss -antl
State       Recv-Q Send-Q      Local Address:Port                     Peer Address:Port              
LISTEN      0      128                     *:111                                 *:*                  
LISTEN      0      5           192.168.122.1:53                                  *:*                  
LISTEN      0      128                     *:22                                  *:*                  
LISTEN      0      128             127.0.0.1:631                                 *:*                  
LISTEN      0      100             127.0.0.1:25                                  *:*                  
LISTEN      0      128             127.0.0.1:6010                                *:*                  
LISTEN      0      128                    :::111                                :::*                  
LISTEN      0      128                    :::22                                 :::*                  
LISTEN      0      128                   ::1:631                                :::*                  
LISTEN      0      100                   ::1:25                                 :::*                  
LISTEN      0      128                   ::1:6010                               :::*                  
LISTEN      0      80                     :::3306                               :::*                  

//修改密码
//使用临时密码登陆
[root@lwq-client ~]# /usr/local/mysql/bin/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.22

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> set password = password('lwq');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> exit
Bye
mysql -uroot -p'wangqing123!' -e 'set password=password ("wangqing1234!");' --connect-expired-password

mysql配置文件

mysql的配置文件为 /etc/my.cnf
~/.my.cnf
[client]
user =
password =

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

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

mysql数据库备份与恢复

数据库常用备份方案

数据库备份方案:

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

mysql备份工具mysqldump

//语法:
mysqldump [options] database [tables ...]
mysqldump [options] --all-databases [options]
mysqldump [options] --databases [options] DB1 [DB2 DB3...]

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

//备份整个数据库(全备)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| liuwanqian         |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> use liuwanqian;
Database changed
mysql> show tables;
+----------------------+
| Tables_in_liuwanqian |
+----------------------+
| student              |
| student1             |
+----------------------+
2 rows in set (0.00 sec)

mysql> exit
Bye
[root@lwq-client ~]# ls
anaconda-ks.cfg        
[root@lwq-client ~]# mysqldump -uroot -pqqq -h127.0.0.1 --all-databases > all-11111.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@lwq-client ~]# ls
all-11111.sql   anaconda-ks.cfg 

//备份liuwanqian库的student和student1表
[root@lwq-client ~]# mysqldump -uroot -pqqq liuwanqian student student1 > table-11111.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@lwq-client ~]# ls
all-11111.sql    table-11111.sql  anaconda-ks.cfg 

//备份liuwanqian库
[root@lwq-client ~]# mysqldump -uroot -pqqq --databases liuwanqian > wq.11111.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@lwq-client ~]# ls
all-11111.sql     table-11111.sql  anaconda-ks.cfg  wq.11111.sql   

//模拟误删liuwanqian数据库
mysql> drop database liuwanqian;
Query OK, 2 rows affected (0.00 sec)

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

mysql数据恢复

//恢复liuwanqian数据库
[root@lwq-client ~]# ls
all-11111.sql    table-11111.sql   anaconda-ks.cfg    wq.11111.sql    
[root@lwq-client ~]# mysql -uroot -pqqq < all-11111.sql 
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@lwq-client ~]# mysql -uroot -pqqq -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| liuwanqian         |
| mysql              |
| performance_schema |
| sys                |
+--------------------+


//恢复liuwanqian数据库student表和student1表
mysql> use liuwanqian;
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 table-11111.sql
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.00 sec)
mysql> show tables;
+----------------------+
| Tables_in_liuwanqian |
+----------------------+
| student              |
| student1             |
+----------------------+
2 rows in set (0.00 sec)


//模拟删除整个数据库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| liuwanqian         |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

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

//恢复整个数据库
[root@lwq-client ~]# ls
all-11111.sql   table-11111.sql  anaconda-ks.cfg  wq.11111.sql     
[root@lwq-client ~]# mysql -uroot -pqqq < all-11111.sql 
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@lwq-client ~]# mysql -uroot -pqqq -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| liuwanqian         |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

差异备份与恢复

mysql差异备份

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

[root@lwq-client ~]# vim /etc/my.cnf
[root@lwq-client ~]# service mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 
[root@lwq-client ~]# 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
(skip-grant-tables)

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

对数据库进行完全备份

[root@lwq-client ~]# mysql -uroot -pqqq
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.22-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 |
| liuwanqian         |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> show tables from liuwanqian;
+----------------------+
| Tables_in_liuwanqian |
+----------------------+
| student              |
| student1             |
+----------------------+
2 rows in set (0.00 sec)

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

//完全备份
[root@lwq-client ~]# mysqldump -uroot -pqqq --single-transaction --flush-logs --master-data=2 --all-databases --delete-master-logs > all-11111.sql 
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@lwq-client ~]# ll
总用量 812
-rw-r--r--. 1 root root 803497 8月  18 19:59 all-11111.sql
-rw-------. 1 root root   1874 7月  20 01:20 anaconda-ks.cfg
-rw-r--r--. 1 root root   2735 8月  18 19:20 table-11111.sql
-rw-r--r--. 1 root root   2892 8月  18 19:29 wq.11111.sql

//增加新内容
[root@lwq-client ~]# mysql -uroot -pqqq
mysql> insert into student1(name,age) values("xixi",20),("hehe",50);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from student1;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | tom   |    9 |
|  2 | jerry |    8 |
|  3 | xixi  |   20 |
|  4 | hehe  |   50 |
+----+-------+------+
4 rows in set (0.00 sec)
mysql> update student1 set age = 25 where id = 3;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from student1;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | tom   |    9 |
|  2 | jerry |    8 |
|  3 | xixi  |   25 |
|  4 | hehe  |   50 |
+----+-------+------+
4 rows in set (0.00 sec)
mysql差异备份恢复

模拟误删数据

[root@lwq-client ~]# mysql -uroot -pqqq -e 'drop database liuwanqian;'
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@lwq-client ~]# mysql -uroot -pqqq -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
//由上可以看到liuwanqian这个数据库已被删除

刷新创建新的二进制日志

[root@lwq-client ~]# ll /opt/data/
总用量 122976
-rw-r-----. 1 mysql mysql       56 8月  18 16:10 auto.cnf
-rw-r-----. 1 mysql mysql      617 8月  18 19:47 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 8月  18 20:10 ibdata1
-rw-r-----. 1 mysql mysql 50331648 8月  18 20:10 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 8月  18 16:10 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 8月  18 20:02 ibtmp1
-rw-r-----. 1 mysql mysql    45670 8月  18 19:47 lwq-client.err
drwxr-x---. 2 mysql mysql     4096 8月  18 19:36 mysql
-rw-r-----. 1 mysql mysql      914 8月  18 20:10 mysql_bin.000003
-rw-r-----. 1 mysql mysql       19 8月  18 19:59 mysql_bin.index
-rw-r-----. 1 mysql mysql        5 8月  18 19:47 mysql.pid
drwxr-x---. 2 mysql mysql     8192 8月  18 16:10 performance_schema
drwxr-x---. 2 mysql mysql     8192 8月  18 16:10 sys

//刷新创建新的二进制日志
[root@lwq-client ~]# mysqladmin -uroot -pqqq flush-logs
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
[root@lwq-client ~]# ll /opt/data/
总用量 122980
-rw-r-----. 1 mysql mysql       56 8月  18 16:10 auto.cnf
-rw-r-----. 1 mysql mysql      617 8月  18 19:47 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 8月  18 20:10 ibdata1
-rw-r-----. 1 mysql mysql 50331648 8月  18 20:10 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 8月  18 16:10 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 8月  18 20:02 ibtmp1
-rw-r-----. 1 mysql mysql    45670 8月  18 19:47 lwq-client.err
drwxr-x---. 2 mysql mysql     4096 8月  18 19:36 mysql
-rw-r-----. 1 mysql mysql      961 8月  18 20:11 mysql_bin.000003
-rw-r-----. 1 mysql mysql      154 8月  18 20:11 mysql_bin.000004
-rw-r-----. 1 mysql mysql       38 8月  18 20:11 mysql_bin.index
-rw-r-----. 1 mysql mysql        5 8月  18 19:47 mysql.pid
drwxr-x---. 2 mysql mysql     8192 8月  18 16:10 performance_schema
drwxr-x---. 2 mysql mysql     8192 8月  18 16:10 sys

恢复完全备份

[root@lwq-client ~]# mysql -uroot -pqqq < all-11111.sql 
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@lwq-client ~]# mysql -uroot -pqqq -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| liuwanqian         |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
[root@lwq-client ~]# mysql -uroot -pqqq -e 'show tables from liuwanqian;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+----------------------+
| Tables_in_liuwanqian |
+----------------------+
| student              |
| student1             |
+----------------------+
[root@lwq-client ~]# mysql -uroot -pqqq -e 'select * from liuwanqian.studnet1;'
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1146 (42S02) at line 1: Table 'liuwanqian.studnet1' doesn't exist
[root@lwq-client ~]# mysql -uroot -pqqq -e 'select * from liuwanqian.student1;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | tom   |    9 |
|  2 | jerry |    8 |
+----+-------+------+
[root@lwq-client ~]# mysql -uroot -pqqq -e 'select * from liuwanqian.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        |   50 |
|  8 | chenshuo    |   10 |
|  9 | wangwu      |    3 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
+----+-------------+------+

恢复差异备份

[root@lwq-client ~]# ll /opt/data/
总用量 124000
-rw-r-----. 1 mysql mysql       56 8月  18 16:10 auto.cnf
-rw-r-----. 1 mysql mysql      617 8月  18 19:47 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 8月  18 20:11 ibdata1
-rw-r-----. 1 mysql mysql 50331648 8月  18 20:11 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 8月  18 16:10 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 8月  18 20:02 ibtmp1
drwxr-x---. 2 mysql mysql       98 8月  18 20:11 liuwanqian
-rw-r-----. 1 mysql mysql    45670 8月  18 19:47 lwq-client.err
drwxr-x---. 2 mysql mysql     4096 8月  18 20:11 mysql
-rw-r-----. 1 mysql mysql      961 8月  18 20:11 mysql_bin.000003
-rw-r-----. 1 mysql mysql   786268 8月  18 20:11 mysql_bin.000004
-rw-r-----. 1 mysql mysql       38 8月  18 20:11 mysql_bin.index
-rw-r-----. 1 mysql mysql        5 8月  18 19:47 mysql.pid
drwxr-x---. 2 mysql mysql     8192 8月  18 16:10 performance_schema
drwxr-x---. 2 mysql mysql     8192 8月  18 16:10 sys

//检查误数据库的位置在什么地方
[root@lwq-client ~]# mysql -uroot -pqqq
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 14
Server version: 5.7.22-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_bin.000003';
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| Log_name         | Pos | Event_type     | Server_id | End_log_pos | Info                                  |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| mysql_bin.000003 |   4 | Format_desc    |         1 |         123 | Server ver: 5.7.22-log, Binlog ver: 4 |
| mysql_bin.000003 | 123 | Previous_gtids |         1 |         154 |                                       |
| mysql_bin.000003 | 154 | Anonymous_Gtid |         1 |         219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'  |
| mysql_bin.000003 | 219 | Query          |         1 |         297 | BEGIN                                 |
| mysql_bin.000003 | 297 | Table_map      |         1 |         358 | table_id: 103 (liuwanqian.student1)   |
| mysql_bin.000003 | 358 | Write_rows     |         1 |         415 | table_id: 103 flags: STMT_END_F       |
| mysql_bin.000003 | 415 | Xid            |         1 |         446 | COMMIT /* xid=507 */                  |
| mysql_bin.000003 | 446 | Anonymous_Gtid |         1 |         511 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'  |
| mysql_bin.000003 | 511 | Query          |         1 |         589 | BEGIN                                 |
| mysql_bin.000003 | 589 | Table_map      |         1 |         650 | table_id: 103 (liuwanqian.student1)   |
| mysql_bin.000003 | 650 | Update_rows    |         1 |         708 | table_id: 103 flags: STMT_END_F       |
| mysql_bin.000003 | 708 | Xid            |         1 |         739 | COMMIT /* xid=509 */                  |
| mysql_bin.000003 | 739 | Anonymous_Gtid |         1 |         804 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'  |
| mysql_bin.000003 | 804 | Query          |         1 |         914 | drop database liuwanqian              |
| mysql_bin.000003 | 914 | Rotate         |         1 |         961 | mysql_bin.000004;pos=4                |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
15 rows in set (0.00 sec)

mysql> exit
Bye

//使用mysqlbinlog恢复差异备份
[root@lwq-client ~]# mysqlbinlog --stop-position=804 /opt/data/mysql_bin.000003 | mysql -uroot -pqqq  //后面一定要跟mysql -uroot -pqqq 不然会报错
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@lwq-client ~]# mysql -uroot -pqqq -e 'select * from liuwanqian.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        |   50 |
|  8 | chenshuo    |   10 |
|  9 | wangwu      |    3 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
+----+-------------+------+
[root@lwq-client ~]# mysql -uroot -pqqq -e 'select * from liuwanqian.student1;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | tom   |    9 |
|  2 | jerry |    8 |
|  3 | xixi  |   25 |
|  4 | hehe  |   50 |
+----+-------+------+

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