1. 二進制格式mysql安裝
//下載二進制格式的mysql軟件包
[root@cl ~]# cd /usr/src/
[root@cl 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@cl src]# groupadd -r mysql
[root@cl src]# useradd -M -s /sbin/nologin -g mysql mysql
//解壓軟件至/usr/local/
[root@clcl src]# ls
debug kernels mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
[root@clcl src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@clcl ~]# ls /usr/local/
bin games lib libexec sbin src
etc include lib64 mysql-5.7.22-linux-glibc2.12-x86_64 share
[root@clcl ~]# cd /usr/local/
[root@clcl local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql
‘mysql’ -> ‘mysql-5.7.22-linux-glibc2.12-x86_64/’
[root@clcl 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@clcl ~]# chown -R mysql.mysql /usr/local/mysql
[root@clcl ~]# 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@clcl ~]# ls /usr/local/mysql
bin COPYING docs include lib man README share support-files
[root@clcl ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@clcl ~]# . /etc/profile.d/mysql.sh
[root@clcl ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
//建立數據存放目錄
[root@cl mysql]# mkdir /opt/data
[root@cl mysql]# chown -R mysql.mysql /opt/data/
[root@cl 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: /Tw,fZyuq4db
//請注意,這個命令的最后會生成一個臨時密碼,此處密碼是/Tw,fZyuq4db
//再次注意,這個密碼是隨機的,你的不會跟我一樣,一定要記住這個密碼,因為一會登錄時會用到
//生成配置文件
[root@cl ~]# 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@cl ~]# 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@cl ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@cl ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@cl ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld
//啟動mysql
[root@cl ~]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/opt/data/localhost.localdomain.err'.
SUCCESS!
[root@cl ~]# 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@cl ~]# 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@cl ~]# /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('cljhfy');
Query OK, 0 rows affected, 1 warning (0.00 sec)
1.1破解密碼以及無密碼登錄
1.1.1破解密碼
[root@cl ~]# vim /etc/my.cnf
[mysqld]
skip-grant-tables //增加這一行,然后重啟服務
[root@cl ~]# mysql
mysql> use mysql;
mysql> select * from user \G
*************************** 1. row ***************************
-----//密碼
authentication_string: *376C8403A2C3233B69F4763C3E428E4ED7314D42
-----
mysql> UPDATE user SET password(或authentication_string)=password("123456") WHERE user='root';
mysql> flush privileges;
mysql> exit;
1.1.2無密碼登錄
[root@cl ~]# vim .my.cnf
[client]
user=root //你要無秘登錄的用戶
password=cl //用戶的密碼
1.1.3定義不同的客戶端
[mysql] //給/usr/local/mysql/bin/mysql使用
user="root"
password="123456"
[mysqladmin] //給/usr/local/mysql/bin/mysqladmin使用
user="root"
password="123456"
//在家目錄下的隱藏文件.my.cnf
2. mysql配置文件
mysql
的配置文件為/etc/my.cnf
配置文件查找次序:若在多個配置文件中均有設定,則最后找到的最終生效
/etc/my.cnf --> /etc/mysql/my.cnf --> --default-extra-file=/PATH/TO/CONF_FILE --> ~/.my.cnf
mysql常用配置文件參數:
參數 | 說明 |
---|---|
port = 3306 | 設置監聽端口 |
socket = /tmp/mysql.sock | 指定套接字文件位置 |
basedir = /usr/local/mysql | 指定MySQL的安裝路徑 |
datadir = /data/mysql | 指定MySQL的數據存放路徑 |
pid-file = /data/mysql/mysql.pid | 指定進程ID文件存放路徑 |
user = mysql | 指定MySQL以什么用戶的身份提供服務 |
skip-name-resolve | - 禁止MySQL對外部連接進行DNS解析 - 使用這一選項可以消除MySQL進行DNS解析的時間。 - 若開啟該選項,則所有遠程主機連接授權都要使用IP地址 方式否則MySQL將無法正常處理連接請求 |
3. mysql數據庫備份與恢復
為什么要備份
災難恢復:硬件故障、軟件故障、自然災害、黑客攻擊、誤操作測試等數據丟失場景
備份注意要點
能容忍最多丟失多少數據
恢復數據需要在多長時間內完成
需要恢復哪些數據
還原要點
做還原測試,用於測試備份的可用性
還原演練
3.1備份類型
備份類型:
- 完全備份,部分備份
- 完全備份:整個數據集
- 部分備份:只備份數據子集,如部分庫或表
- 完全備份、增量備份、差異備份
- 增量備份:僅備份最近一次完全備份或增量備份(如果存在增量)以來變化的數據,備份較快,還原復雜
- 差異備份:僅備份最近一次完全備份以來變化的數據,備份較慢,還原簡單
//注意:二進制日志文件不應該與數據文件放在同一磁盤
3.2備份種類
冷、溫、熱備份
- 冷備:讀寫操作均不可進行
- 溫備:讀操作可執行;但寫操作不可執行
- 熱備:讀寫操作均可執行
MyISAM引擎:溫備,不支持熱備
InnoDB引擎: 都支持
- 物理和邏輯備份
- 物理備份:直接復制數據文件進行備份,與存儲引擎有關,占用較多的空間,速度快
- 邏輯備份:從數據庫中“導出”數據另存而進行的備份,與存儲引擎無關,占用空間少,速度慢,可能丟失精度
3.3mysql備份工具
mysqldump+復制binlog:
- mysqldump:完全備份復制binlog中指定時間范圍的event:增量備份
LVM快照+復制binlog:
- LVM快照:使用cp或tar等做物理備份;完全備份復制binlog中指定時間范圍的event:增量備份
其他工具 - xtrabackup:由Percona提供支持對InnoDB做熱備(物理備份)的工具,支持完全備份、增量備份
- MariaDB Backup: 從MariaDB 10.1.26開始集成,基於
Percona XtraBackup 2.3.8實現 - mysqlbackup:熱備份, MySQL Enterprise Edition組件
3.3.1 mysqldump備份命令
//語法:
mysqldump [OPTIONS] database [tables ...]
mysqldump [OPTIONS] --all-databases [OPTIONS]
mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
//常用的OPTIONS:
-uUSERNAME //指定數據庫用戶名
-hHOST //指定服務器主機,請使用ip地址
-pPASSWORD //指定數據庫用戶的密碼
-P# //指定數據庫監聽的端口,這里的#需用實際的端口號代替,如-P3307
-A, --all-databases 備份所有數據庫,含create database
-B , --databases db_name… 指定備份的數據庫,包括
create database語句
-E, --events:備份相關的所有event scheduler
-R, --routines:備份所有存儲過程和存儲函數
--triggers:備份表相關的觸發器,默認啟用,用--skip-triggers,不備份觸發器
--master-data[=#]: 此選項須啟用二進制日志
1:所備份的數據之前加一條記錄為CHANGE MASTER TO語句,非注釋,不指定#,默認為1
2:記錄為注釋的CHANGE MASTER TO語句
此選項會自動關閉--lock-tables功能,自動打開--lock-all-tables功能(除非開啟--single-transaction)
-F, --flush-logs :備份前滾動日志,鎖定表完成后,執行flush logs命令,生成新的二進制日志文件,配合-A時,會導致刷新多次數據
庫,在同一時刻執行轉儲和日志刷新,則應同時使用--flush-logs和-x,--master-data或-single-transaction,此時只刷新一次
建議:和-x,--master-data或 --single-transaction一起使用
--compact 去掉注釋,適合調試,生產不使用
-d, --no-data 只備份表結構
-t, --no-create-info 只備份數據,不備份create table
-n,--no-create-db 不備份create database,可被-A或-B覆蓋
--flush-privileges 備份mysql或相關時需要使用
-f, --force 忽略SQL錯誤,繼續執行
--hex-blob 使用十六進制符號轉儲二進制列(例如,“abc”變為0x616263),受影響的數據類型包括BINARY, VARBINARY,BLOB,BIT
-q, --quick 不緩存查詢,直接輸出,加快備份速度
//備份整個數據庫(全備)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| cljhfy |
+--------------------+
5 rows in set (0.00 sec)
mysql> use cljhfy;
Database changed
mysql> show tables;
+----------------------+
| Tables_in_wangqingge |
+----------------------+
| teacher |
| student |
+----------------------+
3 rows in set (0.00 sec)
[root@cl ~]# ls
anaconda-ks.cfg
[root@cl ~]# mysqldump -uroot -p -h127.0.0.1 --all-databases > all-201904301500.sql
Enter password:
[root@cl ~]# ls
all-201904301500.sql anaconda-ks.cfg
//備份cljhfy庫的student表和teacher表
[root@cl ~]# mysqldump -uroot -p -h127.0.0.1 cljhfy student teacher > table-201904301500.sql
Enter password:
[root@cl ~]# ls
all-201904301500.sql anaconda-ks.cfg table-201904301500.sql
//備份wangqingge庫
[root@cl ~]# mysqldump -uroot -p -h127.0.0.1 --databases cljhfy > cl-201904301500.sql
Enter password:
[root@cl ~]# ls
all-201904301500.sql table-201904301500.sql
anaconda-ks.cfg cl-201904301500.sql
// mysql數據恢復
//模擬誤刪cljhfy數據庫
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)
//恢復cljhfy數據庫
[root@cl ~]# ls
all-201904301500.sql table-201904301500.sql
anaconda-ks.cfg cl-201904301500.sql
[root@cl ~]# mysql -uroot -p -h127.0.0.1 < all-201904301500.sql
Enter password:
[root@cl ~]# mysql -uroot -p -h127.0.0.1 -e 'show databases;'
Enter password:
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| wangqingge |
+--------------------+
//恢復的方式都一樣
3.3.2 xtrabackup備份命令
Xtrabackup
- percona提供的mysql數據庫備份工具,惟一開源的能夠對innodb和xtradb數據庫進行熱備的工具
特點:
- 備份還原過程快速、可靠
- 備份過程不會打斷正在執行的事務
- 能夠基於壓縮等功能節約磁盤空間和流量
- 自動實現備份檢驗
- 開源,免費
Xtrabackup2.2版之前包括4個可執行文件:
- Innobackupex: perl 腳本
- Xtrabackup: C/C++ 編譯的二進制
- Xbcrypt:加解密
- Xbstream:支持並發寫的流文件格式
xtrabackup 是用來備份 InnoDB 表的,不能備份非 InnoDB 表,和 mysqld server 沒有交互;innobackupex 腳本用來備份非InnoDB 表,同時會調用 xtrabackup 命令來備份 InnoDB 表,還會和 mysqld server 發送命令進行交互,如加讀鎖(FTWRL)、獲取位點(SHOW SLAVESTATUS)等。即innobackupex是在xtrabackup 之上做了一層封裝實現的。
雖然目前一般不用 MyISAM 表,只是mysql 庫下的系統表是MyISAM 的,因此備份基本都通過 innobackupex 命令進行
xtrabackup的特性:
使用innobakupex備份時,其會調用xtrabackup備份所有的InnoDB表,復制所有關於表結構定義的相關文件(.frm)、以及MyISAM、MERGE、CSV和ARCHIVE表的相關文件,同時還會備份觸發器和數據庫配置信息相關的文件。這些文件會被保存至一個以時間命名的目錄中,在備份時,innobackupex還會在備份目錄中創建如下文件:
- xtrabackup_checkpoints:備份類型(如完全或增量)、備份狀態(如是否已經為prepared狀態)和LSN(日志序列號)范圍信息,每個InnoDB頁(通常為16k大小)都會包含一個日志序列號,即LSN。LSN是整個數據庫系統的系統版本號,每個頁面相關的LSN能夠表明此頁面最近是如何發生改變的
- xtrabackup_binlog_info:mysql服務器當前正在使用的二進制日志文件及至備份這一刻為止二進制日志事件的位置
- xtrabackup_binlog_pos_innodb:二進制日志文件及用於InnoDB或XtraDB表的二進制日志文件的當position
- xtrabackup_binary:備份中用到的xtrabackup的可執行文件
- backup-my.cnf:備份命令用到的配置選項信息在使用innobackupex進行備份時,還可以使用--no-timestamp選項來阻止命令自動創建一個以時間命名的目錄;innobackupex命令將會創建一個BACKUP-DIR目錄來存儲備份數據
xtrabackup用法
備份:innobackupex [option] BACKUP-ROOT-DIR
選項說明:
--user:該選項表示備份賬號
--password:該選項表示備份的密碼
--host:該選項表示備份數據庫的地址
--databases:該選項接受的參數為數據名,如果要指定多個數據庫,彼此間需要以空格隔開;如:"xtra_test dba_test",同時,在指定某數據庫時,也可以只指定其中的某張表。如:"mydatabase.mytable"。該選項對innodb引擎表無效,還是會備份所有innodb表
--defaults-file:該選項指定了從哪個文件讀取MySQL配置,必須放在命令行第一個選項的位置
--incremental:該選項表示創建一個增量備份,需要指定--incremental-
basedir
--incremental-basedir:該選項表示接受了一個字符串參數指定含有full backup的目錄為增量備份的base目錄,與--incremental同時使用
--incremental-dir:該選項表示增量備份的目錄
--include=name:指定表名,格式:databasename.tablename
Prepare:innobackupex --apply-log [option] BACKUP-DIR
選項說明:
--apply-log:一般情況下,在備份完成后,數據尚且不能用於恢復操作,因為備份的數據中可能會包含尚未提交的事務或已經提交但尚未同步至數據文件中的事務。因此,此時數據文件仍處理不一致狀態。此選項作用是通過回滾未提交的事務及同步已經提交的事務至數據文件使數據文件處於一致性狀態
--use-memory:該選項表示和--apply-log選項一起使用,prepare 備份的時候,xtrabackup做crash recovery分配的內存大小,單位字節。也可(1MB,1M,1G,1GB),推薦1G
--defaults-file:該選項指定了從哪個文件讀取MySQL配置,必須放在命令行第一個選項的位置
-export:表示開啟可導出單獨的表之后再導入其他Mysql中
--redo-only:這個選項在prepare base full backup,往其中merge增量備份時候使用
還原:innobackupex --copy-back [選項] BACKUP-DIR
innobackupex --move-back [選項] [--defaults-group=GROUP-NAME] BACKUP-DIR
選項說明:
--copy-back:做數據恢復時將備份數據文件拷貝到MySQL服務器的datadir
--move-back:這個選項與--copy-back相似,唯一的區別是它不拷貝文件,而是移動文件到目的地。這個選項移除backup文件,用時候必須小心。使用場景:沒有足夠的磁盤空間同事保留數據文件和Backup副本
注意事項
1.datadir目錄必須為空。除非指定innobackupex --force-non-empty-directorires選項指定,否則--copy-backup選項不會覆蓋
2.在restore之前,必須shutdown MySQL實例,你不能將一個運行中的實例restore到datadir目錄中
3.由於文件屬性會被保留,大部分情況下你需要在啟動實例之前將文件的屬主改為mysql,這些文件將屬於創建備份的用戶chown -R mysql:mysql /data/mysql以上需要在用戶調用innobackupex之前完成--force-non-empty-directories:指定該參數時候,使得innobackupex --copy-back或--move-back選項轉移文件到非空目錄,已存在的文件不會被覆蓋。如果--copy-back和--move-back文件需要從備份目錄拷貝一個在datadir已經存在的文件,會報錯失敗!
4.知識補充:差異備份
4.1開啟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=16 //設置服務器標識符
log-bin=cl_bin //開啟二進制日志功能
[root@localhost ~]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@cl ~]# cd /opt/data/
[root@cl data]# ls
auto.cnf cl.err ib_logfile0 mysql sys
cl_bin.000001 ib_buffer_pool ib_logfile1 mysql.pid
cl_bin.index ibdata1 ibtmp1 performance_schema
4.2對數據庫進行完全備份
[root@cl ~]# mysql -uroot -pcljhfy
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 |
| chenliang |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> show tables;
+---------------------+
| Tables_in_chenliang |
+---------------------+
| student |
| teacher |
+---------------------+
2 rows in set (0.00 sec)
mysql> select * from student;
+----+---------+
| id | name |
+----+---------+
| 1 | leo |
| 2 | natasha |
| 3 | tonny |
+----+---------+
3 rows in set (0.01 sec)
mysql> select * from teacher;
+----+---------+
| id | name |
+----+---------+
| 1 | leo |
| 2 | natasha |
| 3 | tor |
+----+---------+
3 rows in set (0.00 sec)
//完全備份
[root@cl ~]# mysqldump -uroot -pcljhfy --single-transaction --flush-logs --master-data=2 --all-databases --delete-master-logs > chen-201904301600.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@cl ~]# ls
anaconda-ks.cfg mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
chen-201904301600.sql
//增加新內容
mysql> use chenliang;
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 teacher values(1,'jerry'),(2,'kiso');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from teacher;
+----+---------+
| id | name |
+----+---------+
| 1 | leo |
| 2 | natasha |
| 3 | tor |
| 1 | jerry |
| 2 | kiso |
+----+---------+
5 rows in set (0.00 sec)
4.3mysql差異備份恢復
模擬誤刪數據
[root@cl ~]# mysql -uroot -pcljhfy -e 'drop database chenliang'
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@cl ~]# mysql -uroot -pcljhfy -e 'show databases'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
刷新創建新的二進制日志
[root@cl ~]# ll /opt/data/
total 122944
-rw-r-----. 1 mysql mysql 56 May 2 11:44 auto.cnf
-rw-r-----. 1 mysql mysql 613 May 2 12:07 cl_bin.000002
-rw-r-----. 1 mysql mysql 16 May 2 12:03 cl_bin.index
-rw-r-----. 1 mysql mysql 12587 May 2 11:54 cl.err
-rw-r-----. 1 mysql mysql 349 May 2 11:54 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 May 2 12:07 ibdata1
-rw-r-----. 1 mysql mysql 50331648 May 2 12:07 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 May 2 11:44 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 May 2 12:03 ibtmp1
drwxr-x---. 2 mysql mysql 4096 May 2 11:44 mysql
-rw-r-----. 1 mysql mysql 5 May 2 11:54 mysql.pid
drwxr-x---. 2 mysql mysql 8192 May 2 11:44 performance_schema
drwxr-x---. 2 mysql mysql 8192 May 2 11:44 sys
[root@cl ~]# mysqladmin -uroot -pcljhfy flush-logs //刷新創建新的二進制日志
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
[root@cl ~]# ll /opt/data/
total 122948
-rw-r-----. 1 mysql mysql 56 May 2 11:44 auto.cnf
-rw-r-----. 1 mysql mysql 657 May 2 12:08 cl_bin.000002
-rw-r-----. 1 mysql mysql 154 May 2 12:08 cl_bin.000003
-rw-r-----. 1 mysql mysql 32 May 2 12:08 cl_bin.index
-rw-r-----. 1 mysql mysql 12587 May 2 11:54 cl.err
-rw-r-----. 1 mysql mysql 349 May 2 11:54 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 May 2 12:07 ibdata1
-rw-r-----. 1 mysql mysql 50331648 May 2 12:07 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 May 2 11:44 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 May 2 12:03 ibtmp1
drwxr-x---. 2 mysql mysql 4096 May 2 11:44 mysql
-rw-r-----. 1 mysql mysql 5 May 2 11:54 mysql.pid
drwxr-x---. 2 mysql mysql 8192 May 2 11:44 performance_schema
drwxr-x---. 2 mysql mysql 8192 May 2 11:44 sys
4.4恢復完全備份
[root@cl ~]# mysql -uroot -pcljhfy < chen-201904301600.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@cl ~]# mysql -uroot -pcljhfy -e 'show databases'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| chenliang |
| mysql |
| performance_schema |
| sys |
+--------------------+
[root@cl ~]# mysql -uroot -pcljhfy -e 'select * from chenliang.teacher'
mysql: [Warning] Using a password on the command line interface can be insecure.
+----+---------+
| id | name |
+----+---------+
| 1 | leo |
| 2 | natasha |
| 3 | tor |
+----+---------+
4.5恢復差異備份
//檢查誤刪數據庫的位置在什么地方
mysql> show binlog events in 'cl_bin.000002';
+---------------+-----+----------------+-----------+-------------+---------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+---------------+-----+----------------+-----------+-------------+---------------------------------------+
| cl_bin.000002 | 4 | Format_desc | 16 | 123 | Server ver: 5.7.22-log, Binlog ver: 4 |
| cl_bin.000002 | 123 | Previous_gtids | 16 | 154 | |
| cl_bin.000002 | 154 | Anonymous_Gtid | 16 | 219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| cl_bin.000002 | 219 | Query | 16 | 296 | BEGIN |
| cl_bin.000002 | 296 | Table_map | 16 | 354 | table_id: 113 (chenliang.teacher) |
| cl_bin.000002 | 354 | Write_rows | 16 | 410 | table_id: 113 flags: STMT_END_F |
| cl_bin.000002 | 410 | Xid | 16 | 441 | COMMIT /* xid=485 */ |
| cl_bin.000002 | 441 | Anonymous_Gtid | 16 | 506 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| cl_bin.000002 | 506 | Query | 16 | 613 | drop database chenliang |
| cl_bin.000002 | 613 | Rotate | 16 | 657 | cl_bin.000003;pos=4 |
+---------------+-----+----------------+-----------+-------------+---------------------------------------+
10 rows in set (0.00 sec)
//使用mysqlbinlog恢復差異備份
[root@cl ~]# mysqlbinlog --stop-position=506 /opt/data/cl_bin.000002 |mysql -uroot -pcljhfy
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@cl ~]# mysql -uroot -pcljhfy -e 'select * from chenliang.teacher'
mysql: [Warning] Using a password on the command line interface can be insecure.
+----+---------+
| id | name |
+----+---------+
| 1 | leo |
| 2 | natasha |
| 3 | tor |
| 1 | jerry |
| 2 | kiso |
+----+---------+