mysql進階
一. 二進制格式mysql安裝
//下載二進制格式的mysql軟件包
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
--2018-08-13 23:56:27-- https://downloads.mysql.com/archives/get/file/mysql-5.7.22-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.22-linux-glibc2.12-x86_64.tar.gz [following]
......
Saving to: ‘mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz’
100%[=====================================>] 643,790,848 2.46MB/s in 4m 20s
2018-08-14 00:00:50 (2.36 MB/s) - ‘mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz’saved [643790848/643790848]
//創建用戶和組
[root@localhost src]# groupadd -r mysql
[root@localhost src]# useradd -M -s /sbin/nologin -g mysql mysql
//解壓軟件至/usr/local/
[root@localhost src]# ls
debug kernels mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
[root@localhost src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost ~]# ls /usr/local/
bin games lib libexec sbin src
etc include lib64 mysql-5.7.22-linux-glibc2.12-x86_64 share
[root@localhost ~]# cd /usr/local/
[root@localhost local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql
‘mysql’ -> ‘mysql-5.7.22-linux-glibc2.12-x86_64/’
[root@localhost local]# ll
total 0
drwxr-xr-x. 2 root root 6 Mar 10 2016 bin
drwxr-xr-x. 2 root root 6 Mar 10 2016 etc
drwxr-xr-x. 2 root root 6 Mar 10 2016 games
drwxr-xr-x. 2 root root 6 Mar 10 2016 include
drwxr-xr-x. 2 root root 6 Mar 10 2016 lib
drwxr-xr-x. 2 root root 6 Mar 10 2016 lib64
drwxr-xr-x. 2 root root 6 Mar 10 2016 libexec
lrwxrwxrwx 1 root root 36 Aug 14 16:00 mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/
drwxr-xr-x 9 root root 129 Aug 14 00:16 mysql-5.7.22-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 Mar 10 2016 sbin
drwxr-xr-x. 5 root root 49 Jun 13 19:03 share
drwxr-xr-x. 2 root root 6 Mar 10 2016 src
//修改目錄/usr/local/mysql的屬主屬組
[root@localhost ~]# chown -R mysql.mysql /usr/local/mysql
[root@localhost ~]# ll /usr/local/mysql -d
lrwxrwxrwx 1 mysql mysql 36 Aug 14 16:00 /usr/local/mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/
//添加環境變量
[root@localhost ~]# ls /usr/local/mysql
bin COPYING docs include lib man README share support-files
[root@localhost ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost ~]# . /etc/profile.d/mysql.sh
[root@localhost ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
//建立數據存放目錄
[root@localhost mysql]# mkdir /opt/data
[root@localhost mysql]# chown -R mysql.mysql /opt/data/
[root@localhost mysql]# ll /opt/
total 0
drwxr-xr-x 2 mysql mysql 6 Aug 14 16:54 data
//初始化數據庫
[root@localhost ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2018-08-15T07:57:46.168380Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-08-15T07:57:50.542516Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-08-15T07:57:50.927286Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-08-15T07:57:51.071260Z 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: e8600890-a060-11e8-b1a2-000c294c50b4.
2018-08-15T07:57:51.074566Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-08-15T07:57:51.078089Z 1 [Note] A temporary password is generatedfor root@localhost: jtBzkkb=r5ik
//請注意,這個命令的最後會生成一個臨時密碼,此處密碼是jtBzkkb=r5ik
//再次注意,這個密碼是隨機的,你的不會跟我一樣,一定要記住這個密碼,因爲一會登錄時會用到
//生成配置文件
[root@localhost ~]# cat > /etc/my.cnf <<EOF
> [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
[root@localhost ~]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
//配置服務啓動腳本
[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/data#g' /etc/init.d/mysqld
//啓動mysql
[root@localhost ~]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/opt/data/localhost.localdomain.err'.
SUCCESS!
[root@localhost ~]# ps -ef|grep mysql
root 1521 1 0 01:58 pts/0 00:00:00 /bin/sh /usr/local/mysql/binmysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
mysql 1699 1521 0 01:58 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=localhost.localdomain.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root 1734 1301 0 01:59 pts/0 00:00:00 grep --color=auto mysql
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 80 :::3306 :::*
//修改密碼
//使用臨時密碼登錄
[root@localhost ~]# /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>
//設置新密碼
mysql> set password = password('wangqing123!');
Query OK, 0 rows affected, 1 warning (0.00 sec)
二. 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將無法正常處理連接請求 |
三. mysql數據庫備份與恢復
3.1 數據庫常用備份方案
數據庫備份方案:
- 全量備份
- 增量備份
- 差異備份
備份方案 | 特點 |
---|---|
全量備份 | 全量備份就是指對某一個時間點上的所有數據或應用進行的一個完全拷貝。數據恢復快。備份時間長 |
增量備份 | 增量備份是指在一次全備份或上一次增量備份後,以後每次的備份只需備份與前一次相比增加和者被修改的文件。這就意味着,第一次增量備份的對象是進行全備後所產生的增加和修改的文件;第二次增量備份的對象是進行第一次增量備份後所產生的增加和修改的文件,如此類推。沒有重複的備份數據備份時間短恢復數據時必須按一定的順序進行 |
差異備份 | 備份上一次的完全備份後發生變化的所有文件。差異備份是指在一次全備份後到進行差異備份的這段時間內對那些增加或者修改文件的備份。在進行恢復時,我們只需對第一次全量備份和最後一次差異備份進行恢復。 |
3.2 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 |
| mysql |
| performance_schema |
| sys |
| wangqingge |
+--------------------+
5 rows in set (0.00 sec)
mysql> use wangqingge;
Database changed
mysql> show tables;
+----------------------+
| Tables_in_wangqingge |
+----------------------+
| chenshuo |
| guohui |
| wangqing |
+----------------------+
3 rows in set (0.00 sec)
[root@localhost ~]# ls
anaconda-ks.cfg
[root@localhost ~]# mysqldump -uroot -p -h127.0.0.1 --all-databases > all-201808131500.sql
Enter password:
[root@localhost ~]# ls
all-201808131500.sql anaconda-ks.cfg
//備份wangqingge庫的wangqing表和guohui表
[root@localhost ~]# mysqldump -uroot -p -h127.0.0.1 wangqingge wangqing guohui > table-201808131500.sql
Enter password:
[root@localhost ~]# ls
all-201808131500.sql anaconda-ks.cfg table-201808131500.sql
//備份wangqingge庫
[root@localhost ~]# mysqldump -uroot -p -h127.0.0.1 --databases wangqingge > wq-201808131500.sql
Enter password:
[root@localhost ~]# ls
all-201808131500.sql table-201808131500.sql
anaconda-ks.cfg wq-201808131500.sql
//模擬誤刪wangqingge數據庫
mysql> drop database wangqingge;
Query OK, 3 rows affected (0.02 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
3.3 mysql數據恢復
//恢復wangqingge數據庫
[root@localhost ~]# ls
all-201808131500.sql table-201808131500.sql
anaconda-ks.cfg wq-201808131500.sql
[root@localhost ~]# mysql -uroot -p -h127.0.0.1 < all-201808131500.sql
Enter password:
[root@localhost ~]# mysql -uroot -p -h127.0.0.1 -e 'show databases;'
Enter password:
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| wangqingge |
+--------------------+
//恢復wangqingge數據庫的wangqing表和guohui表
mysql> use wangqingge;
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-201808131500.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)
mysql> show tables;
+----------------------+
| Tables_in_wangqingge |
+----------------------+
| chenshuo |
| guohui |
| wangqing |
+----------------------+
3 rows in set (0.00 sec)
//模擬刪除整個數據庫
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| wangqingge |
+--------------------+
5 rows in set (0.00 sec)
mysql> drop database wangqingge;
Query OK, 3 rows affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
//恢復整個數據庫
[root@localhost ~]# ls
all-201808131500.sql table-201808131500.sql
anaconda-ks.cfg wq-201808131500.sql
[root@localhost ~]# mysql -uroot -p -h127.0.0.1 < all-201808131500.sql
Enter password:
[root@localhost ~]# mysql -uroot -p -h127.0.0.1 -e 'show databases;'
Enter password:
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| wangqingge |
+--------------------+
3.4 差異備份與恢復
3.4.1. mysql差異備份
開啓MySQL服務器的二進制日誌功能
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
user = mysql
pid-file = /tmp/mysql.pid
skip-name-resolve
server-id=1 //設置服務器標識符
log-bin=mysql_bin //開啓二進制日誌功能
[root@localhost ~]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
對數據庫進行完全備份
[root@localhost ~]# mysql -uroot -pwangqing123!
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 |
| mysql |
| performance_schema |
| sys |
| wangqing |
+--------------------+
5 rows in set (0.00 sec)
mysql> show tables from wangqing;
+--------------------+
| Tables_in_wangqing |
+--------------------+
| runtime |
| student |
+--------------------+
2 rows in set (0.01 sec)
mysql> select * from wangqing.runtime;
+------+-------+------+
| id | name | age |
+------+-------+------+
| 1 | tom | 10 |
| 2 | jerry | 30 |
+------+-------+------+
2 rows in set (0.01 sec)
mysql> select * from wangqing.student;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhangshan | 20 |
| 7 | lisi | NULL |
| 8 | chenshuo | 10 |
| 9 | wangwu | 3 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
11 rows in set (0.00 sec)
//完全備份
[root@localhost ~]# mysqldump -uroot -pwangqing123! --single-transaction --flush-logs --master-data=2 --all-databases --delete-master-logs > all-201902211406.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# ll
總用量 792
-rw-r--r--. 1 root root 803946 2月 21 13:47 all-201902211406.sql
-rw-------. 1 root root 1259 1月 7 16:39 anaconda-ks.cfg
//增加新內容
[root@localhost ~]# mysql -uroot -pwangqing123!
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.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> use wangqing;
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 runtime values(3,'hehe',20),(4,'xixi',50);
Query OK, 2 rows affected (0.02 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from runtime;
+------+-------+------+
| id | name | age |
+------+-------+------+
| 1 | tom | 10 |
| 2 | jerry | 30 |
| 3 | hehe | 20 |
| 4 | xixi | 50 |
+------+-------+------+
4 rows in set (0.00 sec)
mysql> update runtime set age = 40 where id = 3;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from runtime;
+------+-------+------+
| id | name | age |
+------+-------+------+
| 1 | tom | 10 |
| 2 | jerry | 30 |
| 3 | hehe | 40 |
| 4 | xixi | 50 |
+------+-------+------+
4 rows in set (0.00 sec)
3.4.2. mysql差異備份恢復
模擬誤刪數據
[root@localhost ~]# mysql -uroot -pwangqing123! -e 'drop database wangqing;'
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# mysql -uroot -pwangqing123! -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
//由上可以看到wangqing這個數據庫已被刪除
刷新創建新的二進制日誌
[root@localhost ~]# ll /opt/data/
總用量 188548
-rw-r-----. 1 mysql mysql 56 2月 20 10:11 auto.cnf
-rw-r-----. 1 mysql mysql 996 2月 21 14:54 ib_buffer_pool
-rw-r-----. 1 mysql mysql 79691776 2月 21 16:09 ibdata1
-rw-r-----. 1 mysql mysql 50331648 2月 21 16:09 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 2月 20 10:11 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 2月 21 14:54 ibtmp1
-rw-r-----. 1 mysql mysql 8304 2月 21 08:45 linux-node1.com.err
-rw-r-----. 1 mysql mysql 74620 2月 21 14:54 localhost.err
drwxr-x---. 2 mysql mysql 4096 2月 21 16:03 mysql
-rw-r-----. 1 mysql mysql 154 2月 21 16:09 mysql_bin.000001
-rw-r-----. 1 mysql mysql 19 2月 21 16:09 mysql_bin.index
drwxr-x---. 2 mysql mysql 8192 2月 20 10:11 performance_schema
drwxr-x---. 2 mysql mysql 8192 2月 20 10:11 sys
//刷新創建新的二進制日誌
[root@localhost ~]# mysqladmin -uroot -pwangqing123! flush-logs
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# ll /opt/data/
總用量 188552
-rw-r-----. 1 mysql mysql 56 2月 20 10:11 auto.cnf
-rw-r-----. 1 mysql mysql 996 2月 21 14:54 ib_buffer_pool
-rw-r-----. 1 mysql mysql 79691776 2月 21 16:17 ibdata1
-rw-r-----. 1 mysql mysql 50331648 2月 21 16:18 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 2月 20 10:11 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 2月 21 14:54 ibtmp1
-rw-r-----. 1 mysql mysql 8304 2月 21 08:45 linux-node1.com.err
-rw-r-----. 1 mysql mysql 74620 2月 21 14:54 localhost.err
drwxr-x---. 2 mysql mysql 4096 2月 21 16:16 mysql
-rw-r-----. 1 mysql mysql 945 2月 21 16:18 mysql_bin.000001
-rw-r-----. 1 mysql mysql 154 2月 21 16:18 mysql_bin.000002
-rw-r-----. 1 mysql mysql 38 2月 21 16:18 mysql_bin.index
drwxr-x---. 2 mysql mysql 8192 2月 20 10:11 performance_schema
drwxr-x---. 2 mysql mysql 8192 2月 20 10:11 sys
恢復完全備份
[root@localhost ~]# mysql -uroot -pwangqing123! < all-201902211406.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# mysql -uroot -pwangqing123! -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| wangqing |
+--------------------+
[root@localhost ~]# mysql -uroot -pwangqing123! -e 'show tables from wangqing;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Tables_in_wangqing |
+--------------------+
| runtime |
| student |
+--------------------+
[root@localhost ~]# mysql -uroot -pwangqing123! -e 'select * from wangqing.runtime;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+-------+------+
| id | name | age |
+------+-------+------+
| 1 | tom | 10 |
| 2 | jerry | 30 |
+------+-------+------+
[root@localhost ~]# mysql -uroot -pwangqing123! -e 'select * from wangqing.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 | chenshuo | 10 |
| 9 | wangwu | 3 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
恢復差異備份
[root@localhost ~]# ll /opt/data/
總用量 189572
-rw-r-----. 1 mysql mysql 56 2月 20 10:11 auto.cnf
-rw-r-----. 1 mysql mysql 996 2月 21 14:54 ib_buffer_pool
-rw-r-----. 1 mysql mysql 79691776 2月 21 16:20 ibdata1
-rw-r-----. 1 mysql mysql 50331648 2月 21 16:20 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 2月 20 10:11 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 2月 21 14:54 ibtmp1
-rw-r-----. 1 mysql mysql 8304 2月 21 08:45 linux-node1.com.err
-rw-r-----. 1 mysql mysql 74620 2月 21 14:54 localhost.err
drwxr-x---. 2 mysql mysql 4096 2月 21 16:20 mysql
-rw-r-----. 1 mysql mysql 945 2月 21 16:18 mysql_bin.000001
-rw-r-----. 1 mysql mysql 786443 2月 21 16:20 mysql_bin.000002
-rw-r-----. 1 mysql mysql 38 2月 21 16:18 mysql_bin.index
drwxr-x---. 2 mysql mysql 8192 2月 20 10:11 performance_schema
drwxr-x---. 2 mysql mysql 8192 2月 20 10:11 sys
drwxr-x---. 2 mysql mysql 96 2月 21 16:20 wangqing
//檢查誤刪數據庫的位置在什麼地方
[root@localhost ~]# mysql -uroot -pwangqing123!
mysql> show binlog events in 'mysql_bin.000001';
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| mysql_bin.000001 | 4 | Format_desc | 1 | 123 | Server ver: 5.7.22-log, Binlog ver: 4 |
| mysql_bin.000001 | 123 | Previous_gtids | 1 | 154 | |
| mysql_bin.000001 | 154 | Anonymous_Gtid | 1 | 219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql_bin.000001 | 219 | Query | 1 | 295 | BEGIN |
| mysql_bin.000001 | 295 | Table_map | 1 | 353 | table_id: 330 (wangqing.runtime) |
| mysql_bin.000001 | 353 | Write_rows | 1 | 410 | table_id: 330 flags: STMT_END_F |
| mysql_bin.000001 | 410 | Xid | 1 | 441 | COMMIT /* xid=2628 */ |
| mysql_bin.000001 | 441 | Anonymous_Gtid | 1 | 506 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql_bin.000001 | 506 | Query | 1 | 582 | BEGIN |
| mysql_bin.000001 | 582 | Table_map | 1 | 640 | table_id: 330 (wangqing.runtime) |
| mysql_bin.000001 | 640 | Update_rows | 1 | 698 | table_id: 330 flags: STMT_END_F |
| mysql_bin.000001 | 698 | Xid | 1 | 729 | COMMIT /* xid=2630 */ |
| mysql_bin.000001 | 729 | Anonymous_Gtid | 1 | 794 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql_bin.000001 | 794 | Query | 1 | 898 | drop database wangqing | //此處就是刪除數據庫的位置,對應的pos位置是794
| mysql_bin.000001 | 898 | Rotate | 1 | 945 | mysql_bin.000002;pos=4 |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
15 rows in set (0.00 sec)
//使用mysqlbinlog恢復差異備份
[root@localhost ~]# mysqlbinlog --stop-position=794 /opt/data/mysql_bin.000001 |mysql -uroot -pwangqing123!
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# mysql -uroot -pwangqing123! -e 'select * from wangqing.runtime;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+-------+------+
| id | name | age |
+------+-------+------+
| 1 | tom | 10 |
| 2 | jerry | 30 |
| 3 | hehe | 40 |
| 4 | xixi | 50 |
+------+-------+------+