mysql进阶

mysql进阶


二进制格式mysql安装

[root@lnh ~]# cd /usr/src/
[root@lnh src]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
--2022-07-27 17:39:08--  https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
Resolving downloads.mysql.com (downloads.mysql.com)... 23.10.6.175, 2600:140b:2:a9b::2e31, 2600:140b:2:a93::2e31
Connecting to downloads.mysql.com (downloads.mysql.com)|23.10.6.175|:443... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz [following]
--2022-07-27 17:39:08--  https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
Resolving cdn.mysql.com (cdn.mysql.com)... 23.205.73.56
Connecting to cdn.mysql.com (cdn.mysql.com)|23.205.73.56|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 674830866 (644M) [application/x-tar-gz]
Saving to: ‘mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz’

mysql-5.7.38-linux-gl 100%[=======================>] 643.57M  3.85MB/s    in 3m 13s  

2022-07-27 17:42:22 (3.34 MB/s) - ‘mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz’ saved [674830866/674830866]
//在www.mysql.com网站里面复制下载地址下载mysql包
[root@lnh src]# useradd -Mrs /sbin/nologin  mysql 
//创建用户和组,使其所属主和组都是mysql
[root@lnh src]# ls
debug  kernels  mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
[root@lnh src]# tar -xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local/      //将这个包解压到/usr/local这个目录
[root@lnh src]# ls /usr/local/
apache  apr-util  etc    include  lib64    mysql-5.7.38-linux-glibc2.12-x86_64  share
apr     bin       games  lib      libexec  sbin                                 src
//查看解压出来的东西
[root@lnh src]# cd /usr/local/
[root@lnh local]# ln -sv mysql-5.7.38-linux-glibc2.12-x86_64/ mysql
'mysql' -> 'mysql-5.7.38-linux-glibc2.12-x86_64/'    /v是显示过程
[root@lnh local]# ll
total 0
drwxr-xr-x. 14 root root 164 Jul 21 21:32 apache
drwxr-xr-x.  6 root root  58 Jul 21 21:12 apr
drwxr-xr-x.  5 root root  43 Jul 21 21:17 apr-util
drwxr-xr-x.  2 root root   6 May 19  2020 bin
drwxr-xr-x.  2 root root   6 May 19  2020 etc
drwxr-xr-x.  2 root root   6 May 19  2020 games
drwxr-xr-x.  2 root root   6 May 19  2020 include
drwxr-xr-x.  2 root root   6 May 19  2020 lib
drwxr-xr-x.  3 root root  17 Jul 19 16:13 lib64
drwxr-xr-x.  2 root root   6 May 19  2020 libexec
lrwxrwxrwx.  1 root root  36 Jul 27 18:01 mysql -> mysql-5.7.38-linux-glibc2.12-x86_64/
drwxr-xr-x.  9 root root 129 Jul 27 17:54 mysql-5.7.38-linux-glibc2.12-x86_64
drwxr-xr-x.  2 root root   6 May 19  2020 sbin
drwxr-xr-x.  5 root root  49 Jul 19 16:13 share
drwxr-xr-x.  5 root root 145 Jul 21 21:19 src
//映射头文件并查看
[root@lnh ~]# chown -R mysql.mysql /usr/local/mysql
[root@lnh ~]# ll -d  /usr/local/mysql
lrwxrwxrwx. 1 mysql mysql 36 Jul 27 18:01 /usr/local/mysql -> mysql-5.7.38-linux-glibc2.12-x86_64/
//修改目录/usr/local/mysql的属主属组
[root@lnh ~]# cd /usr/local/mysql   //进目录查看配置
[root@lnh mysql]# ls
bin  docs  include  lib  LICENSE  man  README  share  support-files
[root@lnh mysql]# cd bin/
[root@lnh bin]# pwd     //提前打印路径
/usr/local/mysql/bin
[root@lnh bin]# echo 'export PATH=$PATH:/usr/local/mysql/bin' > /etc/profile.d/mysql.sh //打印配置
[root@lnh bin]# source /etc/profile.d/mysql.sh //生效
[root@lnh bin]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin
[root@lnh bin]# which mysql
/usr/local/mysql/bin/mysql
[root@lnh bin]# cd ..
[root@lnh mysql]# ls
bin  docs  include  lib  LICENSE  man  README  share  support-files
[root@lnh mysql]# ln -sv /usr/local/mysql/include /usr/include/mysql
'/usr/include/mysql' -> '/usr/local/mysql/include/'   //做头文件的软链接
[root@lnh ~]# ll -d /usr/include/mysql     //查看
lrwxrwxrwx. 1 root root 25 Aug  1 22:47 /usr/include/mysql -> /usr/local/mysql/include/
[root@lnh ~]# cd -     //返回上一个正在运行的目录并打印出来
/usr/local/mysql
[root@lnh mysql]# vim /etc/ld.so.conf.d/mysql.conf    //将lib的路径写到这里面
[root@lnh mysql]# cat /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib/
[root@lnh mysql]# ldconfig    //使其生效
[root@lnh mysql]# vim /etc/man_db.conf    
#
MANDATORY_MANPATH                       /usr/man
MANDATORY_MANPATH                       /usr/share/man
MANDATORY_MANPATH                       /usr/local/share/man
MANDATORY_MANPATH                       /usr/local/mysql/man //添加这一行
//添加环境变量
[root@lnh ~]# mkdir /opt/xbz
[root@lnh ~]# chown -R mysql.mysql /opt/xbz/
[root@lnh ~]# ll /opt/
total 0
drwxr-xr-x. 2 mysql mysql 6 Jul 27 18:11 xbz
//建立数据存放目录
[root@lnh ~]# mysqld --initialize --user mysql --datadir /opt/xbz/
2022-08-01T15:20:41.188094Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-08-01T15:20:41.412531Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-08-01T15:20:41.451348Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-08-01T15:20:41.457615Z 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: 814b7dc2-11ad-11ed-9e82-000c2981084f.
2022-08-01T15:20:41.458288Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-08-01T15:20:41.770520Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-08-01T15:20:41.770596Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-08-01T15:20:41.772873Z 0 [Warning] CA certificate ca.pem is self signed.
2022-08-01T15:20:41.843494Z 1 [Note] A temporary password is generated for root@localhost: kcJQPhqoF5<f
//初始化数据库
//请注意,这个命令的最后会生成一个临时密码,此处密码是kcJQPhqoF5<f
//再次注意,这个密码是随机的,你的不会跟我一样,一定要记住这个密码,因为一会登录时会用到
[root@lnh ~]# echo 'kcJQPhqoF5<f' >111    //可以将临时密码进行复制导入111这个文件里面
[root@lnh ~]# cat 111 
kcJQPhqoF5<f
[root@lnh ~]# vim /etc/my.cnf
[root@lnh ~]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/xbz
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/xbz/mysql.pid
user = mysql
skip-name-resolve
sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
//生成配置文件
[root@lnh ~]# cd /usr/local/mysql
[root@lnh mysql]# cd support-files/
[root@lnh support-files]# ls
magic  mysqld_multi.server  mysql-log-rotate  mysql.server
[root@lnh support-files]# file mysql.server 
mysql.server: POSIX shell script, ASCII text executable   //POSIX shell脚本,ASCII文本可执行文件,可以通过这里路径启动mysql
[root@lnh support-files]# cd
方法1:
[root@lnh ~]# /usr/local/mysql/support-files/mysql.server start
Starting MySQL.Logging to '/opt/xbz/slave.err'.
 SUCCESS! 
[root@lnh ~]# ss -antl
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                 [::]:22                [::]:*                
LISTEN   0        80                     *:3306                 *:*                
[root@lnh ~]# /usr/local/mysql/support-files/mysql.server stop
Shutting down MySQL.. SUCCESS! 
[root@lnh ~]# ss -antl
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                 [::]:22                [::]:*    
方法2:            
[root@lnh ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@lnh ~]# vim /etc/init.d/mysqld    //配置这个启动方式
basedir=/usr/local/mysql/
datadir=/opt/xbz/
[root@lnh ~]# service mysqld start
Starting MySQL. SUCCESS! 
[root@lnh ~]# ss -antl
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                 [::]:22                [::]:*                
LISTEN   0        80                     *:3306                 *:*                
[root@lnh ~]# service mysqld stop
Shutting down MySQL.. SUCCESS! 
[root@lnh ~]# ss -antl
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                 [::]:22                [::]:*                
[root@lnh ~]# rm -rf /etc/init.d/mysqld  //删除这个,那么将无法通过这个方式进行启动
[root@lnh ~]# cd /usr/lib/systemd/system/
[root@lnh system]# cp sshd.service mysqld.service
[root@lnh system]# vim mysqld.service 
[root@lnh system]# cat mysqld.service 
[Unit]
Description=mysqld server daemon
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.target
Wants=sshd-keygen.target

[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.server stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
[root@lnh system]# systemctl daemon-reload 
[root@lnh system]# systemctl restart mysqld.service 
[root@lnh system]# ss -anlt | grep 3306
LISTEN 0      80                 *:3306            *:*          
//将mysqld添加到system里面
[root@lnh ~]# 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.38

Copyright (c) 2000, 2022, 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> set password = password('xbz123');
Query OK, 0 rows affected, 1 warning (0.00 sec)
//修改密码
mysql> quit
Bye
[root@lnh ~]# mysql -uroot -pxbz123
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 6
Server version: 5.7.38 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, 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>
//密码修改成功

错误解决

报错:
[root@lnh ~]# mysqld --initialize --user mysql --datadir /opt/xbz/
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
解决:
[root@lnh ~]# dnf provides libncurses.so.5
Last metadata expiration check: 0:00:08 ago on Mon 01 Aug 2022 11:12:24 PM CST.
ncurses-compat-libs-6.1-9.20180224.el8.i686 : Ncurses compatibility libraries
Repo        : base
Matched from:
Provide    : libncurses.so.5
[root@lnh ~]# dnf -y install ncurses-compat-libs-6.1-9.20180224.el8.

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将无法正常处理连接请求
破解密码:
[root@lnh ~]# vim /etc/my.cnf   //修改/etc/my.cnf配置文件
[root@lnh ~]# cat /etc/my.cnf 
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/xbz
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/xbz/mysql.pid
user = mysql
skip-name-resolve
skip-grant-tables //添加这一行
sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
[root@lnh ~]# systemctl restart mysqld.service 
//重启服务
[root@lnh ~]# mysql   //直接免密登录
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.38 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, 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 |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)
//查看默认数据库
mysql> use mysql;    //进入数据库
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> update user set authentication_string=password('lnh123') where user='root';     //修改密码
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
//刷新修改
[root@lnh ~]# vim /etc/my.cnf 
[root@lnh ~]# cat /etc/my.cnf 
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/xbz
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/xbz/mysql.pid
user = mysql
skip-name-resolve      //删除skip-grant-tables
sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
[root@lnh ~]# systemctl restart mysqld.service   //重启服务
[root@lnh ~]# mysql -uroot -plnh123   //进行登录
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.38 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, 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> quit
Bye

多表联合查询

什么是多表联合查询

前面所讲的查询语句都是针对一个表的,但是在关系型数据库中,表与表之间是有联系的,所以在实际应用中,经常使用多表查询。多表查询就是同时查询两个或两个以上的表。在 MySQL 中,多表查询主要有交叉连接、内连接、外连接、分组查询与子查询等5种。

笛卡尔积

交叉连接(CROSS JOIN):有两种,显式的和隐式的2种,一般用来返回连接表的笛卡尔积。
笛卡尔积(Cartesian product)是指两个集合 X 和 Y 的乘积。
eg:
A:1,2
B: 3,4,5
A X B={(1,3), (1,4), (1,5), (2,3), (2,4), (2,5) };
B X A={(3,1), (3,2), (4,1), (4,2), (5,1), (5,2) };
就是取两个范围内的交集

交叉连接

交叉连接的语法格式如下:

SELECT <字段名> FROM <表1> CROSS JOIN <表2> [WHERE子句];

SELECT <字段名> FROM <表1>, <表2> [WHERE子句];
1)查询 tb_students_info 表中的数据,SQL 语句和运行结果如下:

mysql> SELECT * FROM tb_students_info;
+----+--------+------+------+--------+-----------+
| id | name | age | sex | height | course_id |
+----+--------+------+------+--------+-----------+
| 1 | Dany | 25 | 男 | 160 | 1 |
| 2 | Green | 23 | 男 | 158 | 2 |
| 3 | Henry | 23 | 女 | 185 | 1 |
| 4 | Jane | 22 | 男 | 162 | 3 |
| 5 | Jim | 24 | 女 | 175 | 2 |
| 6 | John | 21 | 女 | 172 | 4 |
| 7 | Lily | 22 | 男 | 165 | 4 |
| 8 | Susan | 23 | 男 | 170 | 5 |
| 9 | Thomas | 22 | 女 | 178 | 5 |
| 10 | Tom | 23 | 女 | 165 | 5 |
+----+--------+------+------+--------+-----------+
10 rows in set (0.00 sec)

[root@lnh ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.38 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, 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> create database lnh character set utf8;   //创建数据库
Query OK, 1 row affected (0.00 sec)

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

mysql> use lnh;    //进入数据库
Database changed
mysql> create table tb_students_info(id int primary key auto_increment,name varchar(20),age tinyint,sex varchar(6),height int,course_id tinyint) charset utf8;
Query OK, 0 rows affected (0.01 sec)

mysql>  insert into   tb_students_info(name,age,sex,height,course_id) values('Dany',25,'男',160,1),('Green',23,'男',158,2),('Henry',23,'女',185,1),('Jane',22,'男',162,3),('Jin',21,'女',172,4),('John',21,'女',172,4),('Lily',22,'男',165,4),('Susan',23,'男',170,5),('Thomas',22,'女',178,5),('Tom',23,'女',165,5);
Query OK, 10 rows affected (0.00 sec)
Records: 10  Duplicates: 0  Warnings: 0
//插入表
mysql> select * from tb_students_info;    //查看这个表的数据
+----+--------+------+------+--------+-----------+
| id | name   | age  | sex  | height | course_id |
+----+--------+------+------+--------+-----------+
|  1 | Dany   |   25 | 男   |    160 |         1 |
|  2 | Green  |   23 | 男   |    158 |         2 |
|  3 | Henry  |   23 | 女   |    185 |         1 |
|  4 | Jane   |   22 | 男   |    162 |         3 |
|  5 | Jin    |   21 | 女   |    172 |         4 |
|  6 | John   |   21 | 女   |    172 |         4 |
|  7 | Lily   |   22 | 男   |    165 |         4 |
|  8 | Susan  |   23 | 男   |    170 |         5 |
|  9 | Thomas |   22 | 女   |    178 |         5 |
| 10 | Tom    |   23 | 女   |    165 |         5 |
+----+--------+------+------+--------+-----------+
10 rows in set (0.00 sec)

2)查询 tb_course 表中的数据,SQL 语句和运行结果如下:

mysql> SELECT * FROM tb_course;
+----+-------------+
| id | course_name |
+----+-------------+
| 1 | Java |
| 2 | MySQL |
| 3 | Python |
| 4 | Go |
| 5 | C++ |
+----+-------------+
5 rows in set (0.00 sec)

mysql> create table tb_course(id int primary key auto_increment,course_name varchar(20)) charset utf8;
Query OK, 0 rows affected (0.02 sec)

mysql> insert into tb_course(id,course_name) values(1,'java'),(2,'mysql'),(3,'python')),(4,'Go'),(5,'c++');
Query OK, 5 rows affected (0.01 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> select * from tb_course;
+----+-------------+
| id | course_name |
+----+-------------+
|  1 | java        |
|  2 | mysql       |
|  3 | python      |
|  4 | Go          |
|  5 | c++         |
+----+-------------+
5 rows in set (0.00 sec)

3)使用 CROSS JOIN 查询出两张表中的笛卡尔积,SQL 语句和运行结果如下:

mysql> select * from tb_course cross join tb_students_info;
+----+-------------+----+--------+------+------+--------+-----------+
| id | course_name | id | name   | age  | sex  | height | course_id |
+----+-------------+----+--------+------+------+--------+-----------+
|  1 | java        |  1 | Dany   |   25 | 男   |    160 |         1 |
|  2 | mysql       |  1 | Dany   |   25 | 男   |    160 |         1 |
|  3 | python      |  1 | Dany   |   25 | 男   |    160 |         1 |
|  4 | Go          |  1 | Dany   |   25 | 男   |    160 |         1 |
|  5 | c++         |  1 | Dany   |   25 | 男   |    160 |         1 |
|  1 | java        |  2 | Green  |   23 | 男   |    158 |         2 |
|  2 | mysql       |  2 | Green  |   23 | 男   |    158 |         2 |
|  3 | python      |  2 | Green  |   23 | 男   |    158 |         2 |
|  4 | Go          |  2 | Green  |   23 | 男   |    158 |         2 |
|  5 | c++         |  2 | Green  |   23 | 男   |    158 |         2 |
|  1 | java        |  3 | Henry  |   23 | 女   |    185 |         1 |
|  2 | mysql       |  3 | Henry  |   23 | 女   |    185 |         1 |
|  3 | python      |  3 | Henry  |   23 | 女   |    185 |         1 |
|  4 | Go          |  3 | Henry  |   23 | 女   |    185 |         1 |
|  5 | c++         |  3 | Henry  |   23 | 女   |    185 |         1 |
|  1 | java        |  4 | Jane   |   22 | 男   |    162 |         3 |
|  2 | mysql       |  4 | Jane   |   22 | 男   |    162 |         3 |
|  3 | python      |  4 | Jane   |   22 | 男   |    162 |         3 |
|  4 | Go          |  4 | Jane   |   22 | 男   |    162 |         3 |
|  5 | c++         |  4 | Jane   |   22 | 男   |    162 |         3 |
|  1 | java        |  5 | Jin    |   21 | 女   |    172 |         4 |
|  2 | mysql       |  5 | Jin    |   21 | 女   |    172 |         4 |
|  3 | python      |  5 | Jin    |   21 | 女   |    172 |         4 |
|  4 | Go          |  5 | Jin    |   21 | 女   |    172 |         4 |
|  5 | c++         |  5 | Jin    |   21 | 女   |    172 |         4 |
|  1 | java        |  6 | John   |   21 | 女   |    172 |         4 |
|  2 | mysql       |  6 | John   |   21 | 女   |    172 |         4 |
|  3 | python      |  6 | John   |   21 | 女   |    172 |         4 |
|  4 | Go          |  6 | John   |   21 | 女   |    172 |         4 |
|  5 | c++         |  6 | John   |   21 | 女   |    172 |         4 |
|  1 | java        |  7 | Lily   |   22 | 男   |    165 |         4 |
|  2 | mysql       |  7 | Lily   |   22 | 男   |    165 |         4 |
|  3 | python      |  7 | Lily   |   22 | 男   |    165 |         4 |
|  4 | Go          |  7 | Lily   |   22 | 男   |    165 |         4 |
|  5 | c++         |  7 | Lily   |   22 | 男   |    165 |         4 |
|  1 | java        |  8 | Susan  |   23 | 男   |    170 |         5 |
|  2 | mysql       |  8 | Susan  |   23 | 男   |    170 |         5 |
|  3 | python      |  8 | Susan  |   23 | 男   |    170 |         5 |
|  4 | Go          |  8 | Susan  |   23 | 男   |    170 |         5 |
|  5 | c++         |  8 | Susan  |   23 | 男   |    170 |         5 |
|  1 | java        |  9 | Thomas |   22 | 女   |    178 |         5 |
|  2 | mysql       |  9 | Thomas |   22 | 女   |    178 |         5 |
|  3 | python      |  9 | Thomas |   22 | 女   |    178 |         5 |
|  4 | Go          |  9 | Thomas |   22 | 女   |    178 |         5 |
|  5 | c++         |  9 | Thomas |   22 | 女   |    178 |         5 |
|  1 | java        | 10 | Tom    |   23 | 女   |    165 |         5 |
|  2 | mysql       | 10 | Tom    |   23 | 女   |    165 |         5 |
|  3 | python      | 10 | Tom    |   23 | 女   |    165 |         5 |
|  4 | Go          | 10 | Tom    |   23 | 女   |    165 |         5 |
|  5 | c++         | 10 | Tom    |   23 | 女   |    165 |         5 |
+----+-------------+----+--------+------+------+--------+-----------+
50 rows in set (0.00 sec)

由运行结果可以看出,tb_course 和 tb_students_info 表交叉连接查询后,返回了 50 条记录。可以想象,当表中的数据较多时,得到的运行结果会非常长,而且得到的运行结果也没太大的意义。所以,通过交叉连接的方式进行多表查询的这种方法并不常用,我们应该尽量避免这种查询。
例 2
查询 tb_course 表中的 id 字段和 tb_students_info 表中的 course_id 字段相等的内容, SQL 语句和运行结果如下:

mysql> select * from tb_course cross join tb_students_info where tb_students_info.course_id =tb_course.id;
+----+-------------+----+--------+------+------+--------+-----------+
| id | course_name | id | name   | age  | sex  | height | course_id |
+----+-------------+----+--------+------+------+--------+-----------+
|  1 | java        |  1 | Dany   |   25 | 男   |    160 |         1 |
|  2 | mysql       |  2 | Green  |   23 | 男   |    158 |         2 |
|  1 | java        |  3 | Henry  |   23 | 女   |    185 |         1 |
|  3 | python      |  4 | Jane   |   22 | 男   |    162 |         3 |
|  4 | Go          |  5 | Jin    |   21 | 女   |    172 |         4 |
|  4 | Go          |  6 | John   |   21 | 女   |    172 |         4 |
|  4 | Go          |  7 | Lily   |   22 | 男   |    165 |         4 |
|  5 | c++         |  8 | Susan  |   23 | 男   |    170 |         5 |
|  5 | c++         |  9 | Thomas |   22 | 女   |    178 |         5 |
|  5 | c++         | 10 | Tom    |   23 | 女   |    165 |         5 |
+----+-------------+----+--------+------+------+--------+-----------+
10 rows in set (0.00 sec)

如果在交叉连接时使用 WHERE 子句,MySQL 会先生成两个表的笛卡尔积,然后再选择满足 WHERE 条件的记录。因此,表的数量较多时,交叉连接会非常非常慢。一般情况下不建议使用交叉连接。

在 MySQL 中,多表查询一般使用内连接和外连接,它们的效率要高于交叉连接。

内连接

内连接(inner join)通过改变连接的方式,来将查询的结果中交叉连接的数据进行消除
内连接使用 inner join关键字连接两张表,并使用 on 子句来设置连接条件。如果没有连接条件,inner join 和 cross join 在语法上是等同的,两者可以互换。
内连接的语法格式如下:
SELECT <字段名> FROM <表1> INNER JOIN <表2> [ON子句];

mysql> select s.name,c.course_name from tb_students_info s inner join tb_course c on s.course_id =c.id;
+--------+-------------+
| name   | course_name |
+--------+-------------+
| Dany   | java        |
| Green  | mysql       |
| Henry  | java        |
| Jane   | python      |
| Jin    | Go          |
| John   | Go          |
| Lily   | Go          |
| Susan  | c++         |
| Thomas | c++         |
| Tom    | c++         |
+--------+-------------+
10 rows in set (0.01 sec)
//在 tb_students_info 表和 tb_course 表之间,使用内连接查询学生姓名和相对应的课程名称
mysql> select s.name,c.course_name from tb_students_info s inner join tb_course c
on s.course_id =c.id and c.course_name='c++';
+--------+-------------+
| name   | course_name |
+--------+-------------+
| Susan  | c++         |
| Thomas | c++         |
| Tom    | c++         |
+--------+-------------+
3 rows in set (0.01 sec)
//查询学习c++的学生
mysql> select count(s.name),c.course_name from tb_students_info s inner join tb_cou
urse c on s.course_id =c.id and c.course_name='c++';
+---------------+-------------+
| count(s.name) | course_name |
+---------------+-------------+
|             3 | c++         |
+---------------+-------------+
1 row in set (0.00 sec)
//统计学习c++的人数
mysql> select (s.name),c.course_name from tb_students_info s inner join tb_coursec on s.course_id =c.id and (c.course_name='c++' or c.course_name='GO');
+--------+-------------+
| name   | course_name |
+--------+-------------+
| Jin    | Go          |
| John   | Go          |
| Lily   | Go          |
| Susan  | c++         |
| Thomas | c++         |
| Tom    | c++         |
+--------+-------------+
6 rows in set (0.00 sec)
//查看学习c++或者GO语言的学生
mysql> select count(s.name),c.course_name from tb_students_info s inner join tb_course c on s.course_id =c.id and (c.course_name='c++' or c.course_name='GO') group
by c.course_name;
+---------------+-------------+
| count(s.name) | course_name |
+---------------+-------------+
|             3 | c++         |
|             3 | Go          |
+---------------+-------------+
2 rows in set (0.00 sec)
//查看学习c++或者Go语言的各有多少人
mysql> select group_concat(s.name),c.course_name from tb_students_info s inner joinn tb_course c on s.course_id =c.id and (c.course_name='c++' or c.course_name='GO') group by c.course_name;
+----------------------+-------------+
| group_concat(s.name) | course_name |
+----------------------+-------------+
| Susan,Tom,Thomas     | c++         |
| John,Jin,Lily        | Go          |
+----------------------+-------------+
2 rows in set (0.00 sec)
//列出学习这两种语言学习的学生

在这里的查询语句中,两个表之间的关系通过 inner join指定,连接的条件使用on子句给出
注意:当对多个表进行查询时,要在 select语句后面指定字段是来源于哪一张表。因此,在多表查询时,select 语句后面的写法是表名.列名。另外,如果表名非常长的话,也可以给表设置别名,这样就可以直接在 select 语句后面写上表的别名.列名。

外连接

内连接的查询结果都是符合连接条件的数据,而外连接会先将连接的表分为基表和参考表,再以基表为依据返回满足和不满足条件的记录。
外连接可以分为左外连接和右外连接2种。

左连接

左外连接又称为左连接,使用 left outer join 关键字连接两个表,并使用 on子句来设置连接条件。
左连接的语法格式如下:

SELECT <字段名> FROM <表1> LEFT OUTER JOIN <表2> <ON子句>;

mysql> insert into tb_course(course_name) values('HTML');
Query OK, 1 row affected (0.01 sec)  //插入数据
mysql> insert into tb_students_info(name,age,sex,height,course_id) values('LiMing'2,22,'男',180,7);
Query OK, 1 row affected (0.00 sec) //插入数据
mysql> select * from tb_course;  //查看表的数据
+----+-------------+
| id | course_name |
+----+-------------+
|  1 | java        |
|  2 | mysql       |
|  3 | python      |
|  4 | Go          |
|  5 | c++         |
|  6 | HTML        |
+----+-------------+
6 rows in set (0.00 sec)

mysql> select * from tb_students_info; //查看表的数据
+----+--------+------+------+--------+-----------+
| id | name   | age  | sex  | height | course_id |
+----+--------+------+------+--------+-----------+
|  1 | Dany   |   25 | 男   |    160 |         1 |
|  2 | Green  |   23 | 男   |    158 |         2 |
|  3 | Henry  |   23 | 女   |    185 |         1 |
|  4 | Jane   |   22 | 男   |    162 |         3 |
|  5 | Jin    |   21 | 女   |    172 |         4 |
|  6 | John   |   21 | 女   |    172 |         4 |
|  7 | Lily   |   22 | 男   |    165 |         4 |
|  8 | Susan  |   23 | 男   |    170 |         5 |
|  9 | Thomas |   22 | 女   |    178 |         5 |
| 10 | Tom    |   23 | 女   |    165 |         5 |
| 11 | LiMing |   22 | 男   |    180 |         7 |
+----+--------+------+------+--------+-----------+
11 rows in set (0.00 sec)
mysql> select s.name,c.course_name from tb_students_info s left outer join tb_course c on s.course_id=c.id;
+--------+-------------+
| name   | course_name |
+--------+-------------+
| Dany   | java        |
| Green  | mysql       |
| Henry  | java        |
| Jane   | python      |
| Jin    | Go          |
| John   | Go          |
| Lily   | Go          |
| Susan  | c++         |
| Thomas | c++         |
| Tom    | c++         |
| LiMing | NULL        |
+--------+-------------+
11 rows in set (0.00 sec)

可以看到,运行结果显示了 11 条记录,name 为 LiMing 的学生目前没有课程,因为对应的 tb_course 表中没有该学生的课程信息,所以该条记录只取出了 tb_students_info 表中相应的值,而从 tb_course 表中取出的值为 NULL。

右连接

右外连接又称为右连接,右连接是左连接的反向连接。使用 right outer join 关键字连接两个表,并使用 on 子句来设置连接条件。
右连接的语法格式如下:

SELECT <字段名> FROM <表1> RIGHT OUTER JOIN <表2> <ON子句>;

mysql> select s.name,c.course_name from tb_students_info s right outer join tb_courrse c on s.course_id=c.id;
+--------+-------------+
| name   | course_name |
+--------+-------------+
| Dany   | java        |
| Green  | mysql       |
| Henry  | java        |
| Jane   | python      |
| Jin    | Go          |
| John   | Go          |
| Lily   | Go          |
| Susan  | c++         |
| Thomas | c++         |
| Tom    | c++         |
| NULL   | HTML        |
+--------+-------------+
11 rows in set (0.00 sec)

可以看到,结果显示了 11 条记录,名称为 HTML 的课程目前没有学生,因为对应的tb_students_info 表中并没有该学生的信息,所以该条记录只取出了 tb_course 表中相应的值,而从 tb_students_info 表中取出的值为 NULL。
多个表左/右连接时,在 ON 子句后连续使用 left/right outer join 或 left/right join 即可。

使用外连接查询时,一定要分清需要查询的结果,是需要显示左表的全部记录还是右表的全部记录,然后选择相应的左连接和右连接。

分组查询

在 MySQL 中,group by 关键字可以根据一个或多个字段对查询结果进行分组。
使用 group by 关键字的语法格式如下:
GROUP BY <字段名>

group by单独使用

mysql> select name,sex from tb_students_info group by sex;
+-------+------+
| name  | sex  |
+-------+------+
| Henry | 女   |
| Dany  | 男   |
+-------+------+
2 rows in set (0.00 sec)
//只显示一个分组里面sex字段的内容
mysql> select name,sex from tb_students_info ;
+--------+------+
| name   | sex  |
+--------+------+
| Dany   | 男   |
| Green  | 男   |
| Henry  | 女   |
| Jane   | 男   |
| Jin    | 女   |
| John   | 女   |
| Lily   | 男   |
| Susan  | 男   |
| Thomas | 女   |
| Tom    | 女   |
| LiMing | 男   |
+--------+------+
11 rows in set (0.00 sec)
//显示全部组的

group by 与 group_concat()

group by 关键字可以和 group_concat()函数一起使用。group_concat() 函数会把每个分组的字段值都显示出来。

mysql> select sex,group_concat(name) from tb_students_info group by sex;
+------+-----------------------------------+
| sex  | group_concat(name)                |
+------+-----------------------------------+
| 女   | Henry,Jin,John,Thomas,Tom         |
| 男   | Dany,Green,Jane,Lily,Susan,LiMing |
+------+-----------------------------------+
2 rows in set (0.00 sec)
//由结果可以看到,查询结果分为两组,sex 字段值为"女"的是一组,值为"男"的是一组,且每组的学生姓名都显示出来了。
mysql> select age,sex,group_concat(name) from tb_students_info group by age,sex;
+------+------+--------------------+
| age  | sex  | group_concat(name) |
+------+------+--------------------+
|   21 | 女   | Jin,John           |
|   22 | 女   | Thomas             |
|   22 | 男   | Jane,Lily,LiMing   |
|   23 | 女   | Henry,Tom          |
|   23 | 男   | Green,Susan        |
|   25 | 男   | Dany               |
+------+------+--------------------+
6 rows in set (0.00 sec)
//在分组过程中,先按照 age 字段进行分组,当 age 字段值相等时,再把 age 字段值相等的记录按照 sex 字段进行分组。

多个字段分组查询时,会先按照第一个字段进行分组。如果第一个字段中有相同的值,MySQL 才会按照第二个字段进行分组。如果第一个字段中的数据都是唯一的,那么 MySQL 将不再对第二个字段进行分组。

group by 与聚合函数

在数据统计时,group by 关键字经常和聚合函数一起使用
聚合函数包括 : count()用来统计记录的条数,sum()用来计算字段值的总和,avg()用来计算字段值的平均值,max()用来查询字段的最大值和min()用来查询字段的最小值

mysql> select sex,count(sex) from tb_students_info group by sex;
+------+------------+
| sex  | count(sex) |
+------+------------+
| 女   |          5 |
| 男   |          6 |
+------+------------+
2 rows in set (0.01 sec)
//sex字段中女是一组有5条记录
男是一组有6条记录

group by 与 with rollup

with rollup 关键字用来在所有记录的最后加上一条记录,这条记录是上面所有记录的总和,即统计记录数量

mysql> select sex,group_concat(name) from tb_students_info group by sex with rollup;
+------+-------------------------------------------------------------+
| sex  | group_concat(name)                                          |
+------+-------------------------------------------------------------+
| 女   | Henry,Jin,John,Thomas,Tom                                   |
| 男   | Dany,Green,Jane,Lily,Susan,LiMing                           |
| NULL | Henry,Jin,John,Thomas,Tom,Dany,Green,Jane,Lily,Susan,LiMing |
+------+-------------------------------------------------------------+
3 rows in set (0.00 sec)
//group concat(name)显示了每个分组的name字段,最后一条记录的group_concat(name)字段的值是分组name字段的总和

子查询

子查询是 MySQL 中比较常用的查询方法,通过子查询可以实现多表查询。子查询指将一个查询语句嵌套在另一个查询语句中。子查询可以在 select、update 和 delete语句中使用,而且可以进行多层嵌套。在实际开发时,子查询经常出现在 where子句中。
子查询在where中的语法格式如下:
WHERE <表达式> <操作符> (子查询)
操作符:in,not in,exists,not exists
in ,not in
当表达式与子查询返回的结果集中的某个值相等时,返回 true,否则返回 false;;若使用关键字 not,则返回值正好相反.
exists,not exists
用于判断子查询的结果集是否为空,若子查询的结果集不为空,返回 true,否则返回 false;若使用关键字 not,则返回的值正好相反。

使用子查询在 tb_students_info 表和 tb_course 表中查询学习 c++课程的学生姓名
mysql> select name from tb_students_info where course_id in (select id from tb_courrse where course_name='c++');
+--------+
| name   |
+--------+
| Susan  |
| Thomas |
| Tom    |
+--------+
3 rows in set (0.00 sec)
//学习c++课程的只有Sunsan,Thomas,Tom,
上述查询过程也可以分为以下 2 步执行,实现效果是相同的。
mysql> select id from tb_course where course_name='c++';
+----+
| id |
+----+
|  5 |
+----+
1 row in set (0.00 sec)
首先单独执行内查询,查询出 tb_course 表中课程为c++的id
mysql> select name from tb_students_info where course_id in (5);
+--------+
| name   |
+--------+
| Susan  |
| Thomas |
| Tom    |
+--------+
3 rows in set (0.00 sec)
然后执行外层查询,在 tb_students_info 表中查询 course_id 等于 5的学生姓名

习惯上,外层的 select查询称为父查询,圆括号中嵌入的查询称为子查询(子查询必须放在圆括号内)。MySQL 在处理上例的 select语句时,执行流程为:先执行子查询,再执行父查询。

mysql> select name from tb_students_info where course_id not in (select id from tb__course where course_name='c++');
+--------+
| name   |
+--------+
| Dany   |
| Green  |
| Henry  |
| Jane   |
| Jin    |
| John   |
| Lily   |
| LiMing |
+--------+
8 rows in set (0.00 sec)
//查询出来的都不是学习c++的
mysql> select name from tb_students_info where course_id <> (select id from tb_courrse where course_name='c++');
+--------+
| name   |
+--------+
| Dany   |
| Green  |
| Henry  |
| Jane   |
| Jin    |
| John   |
| Lily   |
| LiMing |
+--------+
8 rows in set (0.00 sec)
//使用运算符<>,在 tb_course 表和 tb_students_info 表中查询出没有学习c++课程的学生姓名
mysql> select name from tb_students_info where course_id = (select id from tb_coursse where course_name='c++');
+--------+
| name   |
+--------+
| Susan  |
| Thomas |
| Tom    |
+--------+
3 rows in set (0.00 sec)
//使用运算符=,在 tb_course 表和 tb_students_info 表中查询出学习c++课程的学生姓名
mysql> select * from tb_students_info where exists(select course_name from tb_course where id=5);
+----+--------+------+------+--------+-----------+
| id | name   | age  | sex  | height | course_id |
+----+--------+------+------+--------+-----------+
|  1 | Dany   |   25 | 男   |    160 |         1 |
|  2 | Green  |   23 | 男   |    158 |         2 |
|  3 | Henry  |   23 | 女   |    185 |         1 |
|  4 | Jane   |   22 | 男   |    162 |         3 |
|  5 | Jin    |   21 | 女   |    172 |         4 |
|  6 | John   |   21 | 女   |    172 |         4 |
|  7 | Lily   |   22 | 男   |    165 |         4 |
|  8 | Susan  |   23 | 男   |    170 |         5 |
|  9 | Thomas |   22 | 女   |    178 |         5 |
| 10 | Tom    |   23 | 女   |    165 |         5 |
| 11 | LiMing |   22 | 男   |    180 |         7 |
+----+--------+------+------+--------+-----------+
11 rows in set (0.00 sec)
//查询 tb_course 表中是否存在 id=5的课程,如果存在,就查询出 tb_students_info 表中的记录

由结果可以看到,tb_course 表中存在 id=5的记录,因此 EXISTS 表达式返回 TRUE,外层查询语句接收 TRUE 之后对表 tb_students_info 进行查询,返回所有的记录。

mysql> select * from tb_students_info where  age>24 and exists(select course_name
from tb_course where id=1);
+----+------+------+------+--------+-----------+
| id | name | age  | sex  | height | course_id |
+----+------+------+------+--------+-----------+
|  1 | Dany |   25 | 男   |    160 |         1 |
+----+------+------+------+--------+-----------+
1 row in set (0.00 sec)
//查询 tb_course 表中是否存在 id=1的课程,如果存在,就查询出 tb_students_info 表中 age 字段大于 24 的记录

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 |
| lnh                |
| mysql              |
| performance_schema |
| sys                |
| tushanbu           |
+--------------------+
6 rows in set (0.01 sec)

mysql> use lnh;
Database changed
mysql> show tables;
+------------------+
| Tables_in_lnh    |
+------------------+
| tb_course        |
| tb_students_info |
+------------------+
2 rows in set (0.00 sec)

mysql> quit
Bye
[root@lnh ~]# ls
anaconda-ks.cfg  mysql57-community-release-el7-11.noarch.rpm
[root@lnh ~]# mysqldump -uroot -plnh123 lnh tb_course > tb_course.sql  //备份表
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@lnh ~]# ls
anaconda-ks.cfg  mysql57-community-release-el7-11.noarch.rpm  tb_course.sql
[root@lnh ~]# mysqldump -uroot -plnh123 --databases lnh > lnh.sql  //备份数据库
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@lnh ~]# mysqldump -uroot -plnh123  --all-databases lnh > all-$(date '+%Y%m%d%H%M%S').sql //整个数据库备份
mysqldump: [Warning] Using a password on the command line interface can be insecure. 
//这里还有上面的都不是报错只是说密码写在命令行上面不安全,我们可以进行设置免密登录,
[root@lnh ~]# ls
all-20220728222444.sql  lnh.sql
all-.sql                mysql57-community-release-el7-11.noarch.rpm
anaconda-ks.cfg         tb_course.sql
[root@lnh ~]# vim .my.cnf
[root@lnh ~]# cat .my.cnf 
[mysqldump]    //免密登入,这里在登录的时候就不会出现是输入不安全
user=root
password=lnh123
[root@lnh ~]# mysqldump --all-databases lnh > all-$(date '+%Y%m%d%H%M%S').sql
[root@lnh ~]# ls
all-20220728222444.sql  lnh.sql
all-20220728222853.sql  mysql57-community-release-el7-11.noarch.rpm
all-.sql                tb_course.sql
anaconda-ks.cfg

mysql数据恢复

[root@lnh ~]# mysql -uroot -plnh123
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 17
Server version: 5.7.38 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, 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 |
| lnh                |
| mysql              |
| performance_schema |
| sys                |
| tushanbu           |
+--------------------+
6 rows in set (0.00 sec)
mysql> drop database lnh;
Query OK, 2 rows affected (0.01 sec)

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

mysql> quit
Bye
[root@lnh ~]# mysql -uroot -p < lnh.sql       
Enter password: 
[root@lnh ~]# mysql -uroot -p -e 'show databases;'
Enter password: 
+--------------------+
| Database           |
+--------------------+
| information_schema |
| lnh                |
| mysql              |
| performance_schema |
| sys                |
| tushanbu           |
+--------------------+
[root@lnh ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 23
Server version: 5.7.38 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, 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> drop database lnh;
Query OK, 2 rows affected (0.01 sec)

mysql> source lnh.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)

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)

Query OK, 0 rows affected, 1 warning (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Database changed
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.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 6 rows affected (0.01 sec)
Records: 6  Duplicates: 0  Warnings: 0

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)

Query OK, 0 rows affected (0.00 sec)

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, 11 rows affected (0.00 sec)
Records: 11  Duplicates: 0  Warnings: 0

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, 1 warning (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)

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 databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| lnh                |
| mysql              |
| performance_schema |
| sys                |
| tushanbu           |
+--------------------+
6 rows in set (0.00 sec)
 //这里也可以不在命令行里面恢复,可以在数据库里面进行恢复

冷备份

[root@lnh ~]# ss -antl
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                    *:80                   *:*                
LISTEN   0        128                 [::]:22                [::]:*                
LISTEN   0        80                     *:3306                 *:*                
[root@lnh ~]# cd /opt/xbz/
[root@lnh xbz]# ls
auto.cnf         ib_buffer_pool  lnh                 private_key.pem  tushanbu
ca-key.pem       ibdata1         lnh.err             public_key.pem
ca.pem           ib_logfile0     mysql               server-cert.pem
client-cert.pem  ib_logfile1     mysql.pid           server-key.pem
client-key.pem   ibtmp1          performance_schema  sys
[root@lnh xbz]# mkdir /tmp/mysql_bak
[root@lnh xbz]# mv * /tmp/mysql_bak/
[root@lnh xbz]# ls
[root@lnh xbz]# ss -antl
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                    *:80                   *:*                
LISTEN   0        128                 [::]:22                [::]:*                
LISTEN   0        80                     *:3306                 *:*                
[root@lnh xbz]# mysql -uroot -plnh123
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 15
Server version: 5.7.38 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, 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 database;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'database' at line 1
mysql> quit
Bye
[root@lnh xbz]# mv /tmp/mysql_bak/* .
[root@lnh xbz]# ls
auto.cnf         ib_buffer_pool  lnh                 private_key.pem  tushanbu
ca-key.pem       ibdata1         lnh.err             public_key.pem
ca.pem           ib_logfile0     mysql               server-cert.pem
client-cert.pem  ib_logfile1     mysql.pid           server-key.pem
client-key.pem   ibtmp1          performance_schema  sys
[root@lnh xbz]# mysql -uroot -plnh123
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 16
Server version: 5.7.38 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, 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 |
| lnh                |
| mysql              |
| performance_schema |
| sys                |
| tushanbu           |
+--------------------+
6 rows in set (0.00 sec)

差异备份与恢复

mysql差异备份

[root@lnh ~]# vim /etc/my.cnf 
[root@lnh ~]# cat /etc/my.cnf 
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/xbz
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/xbz/mysql.pid
user = mysql
skip-name-resolve
sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
server-id=1          //设置服务器标识符
log-bin=mysql_bin     //开启二进制日志功能

[root@lnh ~]# service mysqld restart 
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS!      

对数据库进行完全备份

[root@lnh ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.38-log MySQL Community Server (GPL)

Copyright (c) 2000, 2022, 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 |
| lnh                |
| mysql              |
| performance_schema |
| sys                |
| tushanbu           |
+--------------------+
6 rows in set (0.01 sec)

mysql> show tables from lnh;
+------------------+
| Tables_in_lnh    |
+------------------+
| tb_course        |
| tb_students_info |
+------------------+
2 rows in set (0.00 sec)

mysql> select * from lnh.tb_course;
+----+-------------+
| id | course_name |
+----+-------------+
|  1 | java        |
|  2 | mysql       |
|  3 | python      |
|  4 | Go          |
|  5 | c++         |
+----+-------------+
5 rows in set (0.00 sec)

mysql> select * from lnh.tb_students_info;
+----+--------+------+------+--------+-----------+
| id | name   | age  | sex  | height | course_id |
+----+--------+------+------+--------+-----------+
|  1 | Dany   |   25 | 男   |    160 |         1 |
|  2 | Green  |   23 | 男   |    158 |         2 |
|  3 | Henry  |   23 | 女   |    185 |         1 |
|  4 | Jane   |   22 | 男   |    162 |         3 |
|  5 | Jin    |   21 | 女   |    172 |         4 |
|  6 | John   |   21 | 女   |    172 |         4 |
|  7 | Lily   |   22 | 男   |    165 |         4 |
|  8 | Susan  |   23 | 男   |    170 |         5 |
|  9 | Thomas |   22 | 女   |    178 |         5 |
| 10 | Tom    |   23 | 女   |    165 |         5 |
+----+--------+------+------+--------+-----------+
10 rows in set (0.00 sec)
//查看数据库里面表的内容,准备对其进行备份
[root@lnh ~]# mysqldump -uroot -plnh123 --single-transaction --flush-logs  --master-data=2 --all-databases --delete-master-logs >all-$(date '+%Y%m%d%H%M%S').sql
mysqldump: [Warning] Using a password on the command line interface can be insecure. //这里不是报错只是说密码写在命令行不安全
[root@lnh ~]# ll
total 892
-rw-r--r--. 1 root root 878821 Aug  1 10:07 all-20220801100730.sql
-rw-------. 1 root root   1081 Jul 19 16:17 anaconda-ks.cfg
-rw-r--r--. 1 root root  25680 Apr 27  2017 mysql57-community-release-el7-11.noarch.rpm
[root@lnh ~]# vim .my.cnf    //设置免密登录
[root@lnh ~]# cat .my.cnf    
[mysqldump]
user=root
password=lnh123
[root@lnh ~]# mysqldump  --single-transaction --flush-logs  --master-data=2 --all-databases --delete-master-logs >all-$(date '+%Y%m%d%H%M%S').sql
[root@lnh ~]# ll
total 1752
-rw-r--r--. 1 root root 878821 Aug  1 10:07 all-20220801100730.sql
-rw-r--r--. 1 root root 878821 Aug  1 10:15 all-20220801101523.sql
-rw-------. 1 root root   1081 Jul 19 16:17 anaconda-ks.cfg
-rw-r--r--. 1 root root  25680 Apr 27  2017 mysql57-community-release-el7-11.noarch.rpm
//完全备份
[root@lnh ~]# mysql -uroot -plnh123
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 11
Server version: 5.7.38-log MySQL Community Server (GPL)

Copyright (c) 2000, 2022, 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> use lnh;
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_lnh    |
+------------------+
| tb_course        |
| tb_students_info |
+------------------+
2 rows in set (0.00 sec)

mysql> select * from tb_course;
+----+-------------+
| id | course_name |
+----+-------------+
|  1 | java        |
|  2 | mysql       |
|  3 | python      |
|  4 | Go          |
|  5 | c++         |
+----+-------------+
5 rows in set (0.00 sec)
mysql> insert into tb_course values(6,'sjk'),(7,'docker');
Query OK, 2 rows affected (0.00 sec) //插入两条数据
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from tb_course;
+----+-------------+
| id | course_name |
+----+-------------+
|  1 | java        |
|  2 | mysql       |
|  3 | python      |
|  4 | Go          |
|  5 | c++         |
|  6 | sjk         |
|  7 | docker      |
+----+-------------+
7 rows in set (0.00 sec)
mysql> update tb_course set course_name ='wysj' where id =4;
Query OK, 1 row affected (0.00 sec) //修改第四条数据
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from tb_course;
+----+-------------+
| id | course_name |
+----+-------------+
|  1 | java        |
|  2 | mysql       |
|  3 | python      |
|  4 | wysj        |
|  5 | c++         |
|  6 | sjk         |
|  7 | docker      |
+----+-------------+
7 rows in set (0.00 sec)

mysql差异备份恢复

模拟误删数据

[root@lnh ~]# mysql -uroot -plnh123 -e 'drop database lnh;'
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@lnh ~]# mysql -uroot -plnh123 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| tushanbu           |
+--------------------+
//lnh这个数据库已经删除

刷新创建新的二进制日志

[root@lnh ~]# ll /opt/xbz/
total 123076
-rw-r-----. 1 mysql mysql       56 Jul 27 18:22 auto.cnf
-rw-------. 1 mysql mysql     1676 Jul 27 18:22 ca-key.pem
-rw-r--r--. 1 mysql mysql     1112 Jul 27 18:22 ca.pem
-rw-r--r--. 1 mysql mysql     1112 Jul 27 18:22 client-cert.pem
-rw-------. 1 mysql mysql     1680 Jul 27 18:22 client-key.pem
-rw-r-----. 1 mysql mysql      310 Aug  1 09:24 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 Aug  1 10:30 ibdata1
-rw-r-----. 1 mysql mysql 50331648 Aug  1 10:30 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 Jul 27 18:22 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 Aug  1 10:15 ibtmp1
-rw-r-----. 1 mysql mysql   115647 Aug  1 10:12 lnh.err
drwxr-x---. 2 mysql mysql     4096 Jul 27 18:22 mysql
-rw-r-----. 1 mysql mysql      860 Aug  1 10:30 mysql_bin.000007//注意这个
-rw-r-----. 1 mysql mysql       19 Aug  1 10:15 mysql_bin.index//数据库当前操作的存放日志
-rw-r-----. 1 mysql mysql        6 Aug  1 09:24 mysql.pid
drwxr-x---. 2 mysql mysql     8192 Jul 27 18:22 performance_schema
-rw-------. 1 mysql mysql     1676 Jul 27 18:22 private_key.pem
-rw-r--r--. 1 mysql mysql      452 Jul 27 18:22 public_key.pem
-rw-r--r--. 1 mysql mysql     1112 Jul 27 18:22 server-cert.pem
-rw-------. 1 mysql mysql     1676 Jul 27 18:22 server-key.pem
drwxr-x---. 2 mysql mysql     8192 Jul 27 18:22 sys
drwxr-x---. 2 mysql mysql       50 Jul 27 21:39 tushanbu
[root@lnh ~]# cat /opt/xbz/mysql_bin.index 
./mysql_bin.000007
//数据库当前操作的存放日志是指东西会传到这里面来
[root@lnh ~]# mysqladmin -uroot -plnh123 flush-logs 
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
//刷新创建新的二进制日志
[root@lnh ~]# ll /opt/xbz/
total 123080
-rw-r-----. 1 mysql mysql       56 Jul 27 18:22 auto.cnf
-rw-------. 1 mysql mysql     1676 Jul 27 18:22 ca-key.pem
-rw-r--r--. 1 mysql mysql     1112 Jul 27 18:22 ca.pem
-rw-r--r--. 1 mysql mysql     1112 Jul 27 18:22 client-cert.pem
-rw-------. 1 mysql mysql     1680 Jul 27 18:22 client-key.pem
-rw-r-----. 1 mysql mysql      310 Aug  1 09:24 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 Aug  1 10:32 ibdata1
-rw-r-----. 1 mysql mysql 50331648 Aug  1 10:32 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 Jul 27 18:22 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 Aug  1 10:15 ibtmp1
-rw-r-----. 1 mysql mysql   115647 Aug  1 10:12 lnh.err
drwxr-x---. 2 mysql mysql     4096 Jul 27 18:22 mysql
-rw-r-----. 1 mysql mysql      907 Aug  1 10:33 mysql_bin.000007
-rw-r-----. 1 mysql mysql      154 Aug  1 10:33 mysql_bin.000008//空文件
-rw-r-----. 1 mysql mysql       38 Aug  1 10:33 mysql_bin.index
-rw-r-----. 1 mysql mysql        6 Aug  1 09:24 mysql.pid
drwxr-x---. 2 mysql mysql     8192 Jul 27 18:22 performance_schema
-rw-------. 1 mysql mysql     1676 Jul 27 18:22 private_key.pem
-rw-r--r--. 1 mysql mysql      452 Jul 27 18:22 public_key.pem
-rw-r--r--. 1 mysql mysql     1112 Jul 27 18:22 server-cert.pem
-rw-------. 1 mysql mysql     1676 Jul 27 18:22 server-key.pem
drwxr-x---. 2 mysql mysql     8192 Jul 27 18:22 sys
drwxr-x---. 2 mysql mysql       50 Jul 27 21:39 tushanbu
[root@lnh ~]# cat /opt/xbz/mysql_bin.index 
./mysql_bin.000007
./mysql_bin.000008
//可以看见多出来了一个,目前这个里面是空的

恢复完全备份

[root@lnh ~]# mysql -uroot -plnh123 <all-20220801101523.sql 
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@lnh ~]# mysql -uroot -plnh123 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| lnh                |
| mysql              |
| performance_schema |
| sys                |
| tushanbu           |
+--------------------+
[root@lnh ~]# mysql -uroot -plnh123 -e 'show tables from lnh;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------------+
| Tables_in_lnh    |
+------------------+
| tb_course        |
| tb_students_info |
+------------------+
[root@lnh ~]# mysql -uroot -plnh123 -e 'select * from lnh.tb_course;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+----+-------------+
| id | course_name |
+----+-------------+
|  1 | java        |
|  2 | mysql       |
|  3 | python      |
|  4 | Go          |
|  5 | c++         |
+----+-------------+
[root@lnh ~]# mysql -uroot -plnh123 -e 'select * from lnh.tb_students_info;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+----+--------+------+------+--------+-----------+
| id | name   | age  | sex  | height | course_id |
+----+--------+------+------+--------+-----------+
|  1 | Dany   |   25 | 男   |    160 |         1 |
|  2 | Green  |   23 | 男   |    158 |         2 |
|  3 | Henry  |   23 | 女   |    185 |         1 |
|  4 | Jane   |   22 | 男   |    162 |         3 |
|  5 | Jin    |   21 | 女   |    172 |         4 |
|  6 | John   |   21 | 女   |    172 |         4 |
|  7 | Lily   |   22 | 男   |    165 |         4 |
|  8 | Susan  |   23 | 男   |    170 |         5 |
|  9 | Thomas |   22 | 女   |    178 |         5 |
| 10 | Tom    |   23 | 女   |    165 |         5 |
+----+--------+------+------+--------+-----------+
//可以看见里面原来的东西又恢复了,但是新添加的东西是没有恢复的

恢复差异备份

[root@lnh ~]# mysql -uroot -plnh123;
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 20
Server version: 5.7.38-log MySQL Community Server (GPL)

Copyright (c) 2000, 2022, 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 binlog events in 'mysql_bin.000007';//检查误删数据库的位置在什么地方
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| Log_name         | Pos | Event_type     | Server_id | End_log_pos | Info                                  |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| mysql_bin.000007 |   4 | Format_desc    |         1 |         123 | Server ver: 5.7.38-log, Binlog ver: 4 |
| mysql_bin.000007 | 123 | Previous_gtids |         1 |         154 |                                       |
| mysql_bin.000007 | 154 | Anonymous_Gtid |         1 |         219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'  |
| mysql_bin.000007 | 219 | Query          |         1 |         290 | BEGIN                                 |
| mysql_bin.000007 | 290 | Table_map      |         1 |         344 | table_id: 212 (lnh.tb_course)         |
| mysql_bin.000007 | 344 | Write_rows     |         1 |         400 | table_id: 212 flags: STMT_END_F       |
| mysql_bin.000007 | 400 | Xid            |         1 |         431 | COMMIT /* xid=1491 */                 |
| mysql_bin.000007 | 431 | Anonymous_Gtid |         1 |         496 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'  |
| mysql_bin.000007 | 496 | Query          |         1 |         567 | BEGIN                                 |
| mysql_bin.000007 | 567 | Table_map      |         1 |         621 | table_id: 212 (lnh.tb_course)         |
| mysql_bin.000007 | 621 | Update_rows    |         1 |         675 | table_id: 212 flags: STMT_END_F       |
| mysql_bin.000007 | 675 | Xid            |         1 |         706 | COMMIT /* xid=1494 */                 |
| mysql_bin.000007 | 706 | Anonymous_Gtid |         1 |         771 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'  |
| mysql_bin.000007 | 771 | Query          |         1 |         860 | drop database lnh                     |//此处771就是之前删除数据库的地方
| mysql_bin.000007 | 860 | Rotate         |         1 |         907 | mysql_bin.000008;pos=4                |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
15 rows in set (0.00 sec)
[root@lnh ~]# mysqlbinlog  --stop-position=771 /opt/xbz/mysql_bin.000007 |mysql -uroot -plnh123 //使用mysqlbinlog恢复差异备份
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@lnh ~]# mysql -uroot -plnh123 -e 'select * from lnh.tb_course;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+----+-------------+
| id | course_name |
+----+-------------+
|  1 | java        |
|  2 | mysql       |
|  3 | python      |
|  4 | wysj        |
|  5 | c++         |
|  6 | sjk         |
|  7 | docker      |
+----+-------------+
//可以看见之前新添加的数据恢复了
posted @ 2022-07-27 22:54  涂山小布  阅读(140)  评论(0编辑  收藏  举报