日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql 5.7 数据库备份_MySQL5.7.20数据库备份与恢复

發(fā)布時(shí)間:2024/9/27 数据库 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql 5.7 数据库备份_MySQL5.7.20数据库备份与恢复 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

MySQL數(shù)據(jù)庫備份與恢復(fù)

·

####造成數(shù)據(jù)丟失的原因####

程序錯(cuò)誤

人為操作錯(cuò)誤

運(yùn)算錯(cuò)誤

磁盤故障

災(zāi)難(如火災(zāi)、地震)和盜竊

·

####物理與邏輯的角度####

1、物理備份:

冷備份:在數(shù)據(jù)關(guān)閉的狀態(tài)下運(yùn)行

熱備份:在數(shù)據(jù)庫運(yùn)行的狀態(tài)時(shí)進(jìn)行備份操作,該備份方法依賴數(shù)據(jù)庫日志文件。

溫備份:數(shù)據(jù)局鎖定表格(可以讀不可以寫)的狀態(tài)年備份數(shù)據(jù)庫

2、邏輯備份:

邏輯備份是對(duì)數(shù)據(jù)庫邏輯組件(如表等數(shù)據(jù)庫對(duì)象)的備份,表示為邏輯數(shù)據(jù)結(jié)構(gòu)(create database、create tabale)

和內(nèi)容(insert語句或分割文本文件)的信息

·

####數(shù)據(jù)庫的備份策略角度####

1、完全備份:

每次對(duì)數(shù)據(jù)庫進(jìn)行完整的備份,即對(duì)整個(gè)數(shù)據(jù)的備份,數(shù)據(jù)庫結(jié)構(gòu)和文件結(jié)構(gòu)備份。保存的是完整時(shí)刻的數(shù)據(jù)庫

2、差異備份

備份那些自從上次完整備份之后被修改過的所有文件,備份的時(shí)間節(jié)點(diǎn)是從上次完整備份起,備份的數(shù)據(jù)量會(huì)越來越大,

恢復(fù)數(shù)據(jù)只是恢復(fù)上次的完整備份和最近一次的差異備份。

3、增量備份

只有那些在上次完全備份或者增量備份后被修改的文件才會(huì)備份,以上次完成整備份,或者上次的增量備份的時(shí)間點(diǎn),

僅備份這之間的數(shù)據(jù)變化,因而備份的數(shù)據(jù)量小,占用空間下,備份數(shù)據(jù)速度快,但恢復(fù)時(shí)候,需要從上次的完整備份

開始到最后一次的增量備份之間所有的增量依次恢復(fù),如中間某次的恢復(fù)數(shù)據(jù)備份損壞,將導(dǎo)致數(shù)據(jù)的丟失。

·

#####常見的備份方法######

1、物理冷備份

tar命令

·

2、專用備份工具

mysqldump

Mysqlhotcopy

·

3、二進(jìn)制日志

·

4、第三方工具備份

Percona XtraBackup

Xtrabackup、innobackupex、xbstream

·

#############MySQL數(shù)據(jù)庫完全備份操作###############

·

1物理冷備份與恢復(fù)(tar命令直接打包數(shù)據(jù)庫文件夾)

備份數(shù)據(jù)庫:

·

systemctl stop mysqld ###關(guān)閉數(shù)據(jù)庫

·

mkdir /backup ###在根下新建個(gè)backup目錄

·

####/usr/local/mysql/data備份壓縮

tar zcf /backup/mysql_all-$(date +%F).tar.gz /usr/local/mysql/data/

·

##模擬故障

mkdir /bak ####在根下新建bak目錄

mv /usr/local/mysql/data/ /bak/ ####將目錄data數(shù)據(jù)移動(dòng)到bak 里面去

·

##數(shù)據(jù)庫恢復(fù)

mkdir /restore ####根目錄新建restore 目錄

tar xzvf /backup/mysql_all-2018-08-12.tar.gz -C /restore/ #####將備份數(shù)據(jù)庫解壓到/restore目錄下

·

mv /restore/usr/local/mysql/data/ /usr/local/mysql/ ####將備份數(shù)據(jù)移動(dòng)到/usr/local/mysql中

systemctl start mysqld ####啟動(dòng)mysql數(shù)據(jù)庫

systemctl status mysqld ####查看mysql數(shù)據(jù)庫啟動(dòng)狀態(tài) ,日志文件顯示正常。

● mysqld.service - MySQL Server

Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)

Active: active (running) since 日 2018-08-12 22:48:20 CST; 7s ago

Docs: man:mysqld(8)

http//dev.mysql.com/doc/refman/en/using-systemd.html

Process: 2036 ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/usr/local/mysql/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)

Process: 2016 ExecStartPre=/usr/local/mysql/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)

Main PID: 2039 (mysqld)

CGroup: /system.slice/mysqld.service

└─2039 /usr/local/mysql/bin/mysqld --daemonize --pid-file=/usr/local/mys...

·

8月 12 22:48:20 localhost.localdomain mysqld[2036]: 2018-08-12T14:48:20.795875Z 0....

8月 12 22:48:20 localhost.localdomain mysqld[2036]: 2018-08-12T14:48:20.795889Z 0...;

8月 12 22:48:20 localhost.localdomain mysqld[2036]: 2018-08-12T14:48:20.795911Z 0....

8月 12 22:48:20 localhost.localdomain mysqld[2036]: 2018-08-12T14:48:20.812257Z 0...s

8月 12 22:48:20 localhost.localdomain mysqld[2036]: 2018-08-12T14:48:20.812564Z 0....

8月 12 22:48:20 localhost.localdomain mysqld[2036]: Version: '5.7.20' socket: '/...n

8月 12 22:48:20 localhost.localdomain mysqld[2036]: 2018-08-12T14:48:20.812584Z 0....

8月 12 22:48:20 localhost.localdomain mysqld[2036]: 2018-08-12T14:48:20.812591Z 0...s

8月 12 22:48:20 localhost.localdomain mysqld[2036]: 2018-08-12T14:48:20.826263Z 0...s

8月 12 22:48:20 localhost.localdomain systemd[1]: Started MySQL Server.

Hint: Some lines were ellipsized, use -l to show in full.

·

####################mysqldump備份與恢復(fù)####################

將指定的庫、表、或全部的庫導(dǎo)出為SQL腳本

·

#####備份操作#####

mysqldump備份需要和mysql進(jìn)行數(shù)據(jù)交互,如果關(guān)閉mysql 則無法備份和恢復(fù)

mysqldump [選項(xiàng)] 庫名 [表名1] [表名2] … > /備份路徑/備份文件名

mysqldump [選項(xiàng)] --databases 庫名1 [庫名2] … > /備份路徑/備份文件名

mysqldump [選項(xiàng)] --all-databases > /備份路徑/備份文件名

·

####備份數(shù)據(jù)路mysql中的user表,文件保存在當(dāng)前操作目錄下面

[root@localhost /]# mysqldump -u root -p mysql user >mysql-user.sql

Enter password: ###輸入密碼abc123

·

####備份auth數(shù)據(jù)庫,文件保存在當(dāng)前操作目錄下面

[root@localhost /]# mysqldump -u root -p --databases auth >auth.sql

Enter password: ####輸入密碼abc123

··

·

####恢復(fù)操作

mysql [選項(xiàng)] [庫名] [表名] < /備份路徑/備份文件名

·

####表恢復(fù)####

[root@localhost /]# mysql -u root -p

Enter password:

mysql> show databases; ###查看數(shù)據(jù)庫####

+--------------------+

| Database |

+--------------------+

| information_schema |

| auth |

| bdqn |

| myadm |

| mysql |

| performance_schema |

| sys |

·

mysql> create database text ; ####新建數(shù)據(jù)庫text ,導(dǎo)表用###

Query OK, 1 row affected (0.00 sec)

mysql> exit

·

[root@localhost opt]# mysql -u root -p text < mysql-user.sql ###將表導(dǎo)入text

Enter password:

·

[root@localhost /]# mysql -u root -p -e 'show tables from text' #######驗(yàn)證輸出結(jié)果####

Enter password:

+----------------+

| Tables_in_text |

+----------------+

| user | ####以被導(dǎo)入

+----------------+

·

####數(shù)據(jù)庫恢復(fù)#####

[root@localhost /]# mysql -u root -p -e 'drop database auth' ######模擬故障刪除auth數(shù)據(jù)庫

Enter password:

[root@localhost /]# mysql -u root -p -e 'show databases' ######查看所有數(shù)據(jù)庫確認(rèn)auth被刪除

Enter password:

+--------------------+

| Database |

+--------------------+

| information_schema |

| bdqn |

| myadm |

| mysql |

| performance_schema |

| sys |

| text |

·

[root@localhost /]# mysql -u root -p < ./auth.sql ####導(dǎo)入數(shù)據(jù)庫auth

Enter password:

[root@localhost /]# mysql -u root -p -e 'show databases' ######查看所有數(shù)據(jù)庫確認(rèn)auth被導(dǎo)入

Enter password:

+--------------------+

| Database |

+--------------------+

| information_schema |

| auth |

| bdqn |

| myadm |

| mysql |

| performance_schema |

| sys |

| text |

+--------------------+

·

·

·

·

##########MySQL數(shù)據(jù)庫增量備份恢復(fù)##############

1、增量備份的特點(diǎn)

與完全備份完全不同,增量備份沒有重復(fù)數(shù)據(jù),備份量不大,時(shí)間短;但其恢復(fù)麻煩,需要上次完成完全備份及備份之后的所有增量備份才能恢復(fù)。

·

2、MySQL數(shù)據(jù)庫二進(jìn)制日志對(duì)備份的意義

二進(jìn)制日志保存了所有更新或者可能更新數(shù)據(jù)的操作。二進(jìn)制日志在啟動(dòng)mysql服務(wù)器后開始記錄,并在文件達(dá)到二進(jìn)制日志所設(shè)置的最大值

或者接受到flush logs命令后重新創(chuàng)建新的日志文件,生成二進(jìn)制的文件序列,并及時(shí)把這些日志文件保存到安全的存儲(chǔ)位置,即可完成一個(gè)時(shí)間段

的增量備份

·

[root@localhost mysql]# vim /etc/my.cnf #####編輯my.cnf配置文件

[client]

port = 3306

default-character-set=utf8

socket = /usr/local/mysql/mysql.sock

·

[mysql]

port = 3306

default-character-set=utf8

socket = /usr/local/mysql/mysql.sock

·

[mysqld]

user = mysql

basedir = /usr/local/mysql

datadir = /usr/local/mysql/data

port = 3306

character_set_server=utf8

pid-file = /usr/local/mysql/mysqld.pid

socket = /usr/local/mysql/mysql.sock

server-id = 1

log-bin=/usr/local/mysql/data/mysql-bin ####這個(gè)地方開啟二進(jìn)制日志功能

·

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES

·

[root@localhost mysql]# systemctl restart mysqld ####重啟數(shù)據(jù)庫

·

[root@localhost ~]# ll /usr/local/mysql/data/

總用量 122924

-rw-r----- 1 mysql mysql 56 8月 9 16:16 auto.cnf

-rw-r----- 1 mysql mysql 308 8月 15 05:50 ib_buffer_pool

-rw-r----- 1 mysql mysql 12582912 8月 15 05:50 ibdata1

-rw-r----- 1 mysql mysql 50331648 8月 15 05:50 ib_logfile0

-rw-r----- 1 mysql mysql 50331648 8月 9 16:16 ib_logfile1

-rw-r----- 1 mysql mysql 12582912 8月 15 05:50 ibtmp1

drwxr-x--- 2 mysql mysql 4096 8月 9 16:16 mysql

-rw-r----- 1 mysql mysql 154 8月 15 05:50 mysql-bin.000001 ####二進(jìn)制日志文件

-rw-r----- 1 mysql mysql 39 8月 15 05:50 mysql-bin.index

drwxr-x--- 2 mysql mysql 8192 8月 9 16:16 performance_schema

drwxr-x--- 2 mysql mysql 8192 8月 9 16:16 sys

·

·

·

####################增量恢復(fù)的方法######################

#####一般恢復(fù)####

mysqlbinlog [--no-defaults] 增量備份文件 | mysql -u 用戶名 -p

·

####基于位置的恢復(fù)###

1、恢復(fù)數(shù)據(jù)到指定位置

mysqlbinlog --stop-position=’操作 id’ 二進(jìn)制日志 |mysql -u 用戶名 -p 密碼

·

·

2、從指定的位置開始恢復(fù)數(shù)據(jù)

mysqlbinlog --start-position=’操作 id’ 二進(jìn)制日志 |mysql -u 用戶名 -p 密碼

·

#####基于時(shí)間點(diǎn)的恢復(fù)#####

1、從日志開頭截止到某個(gè)時(shí)間點(diǎn)的恢復(fù)

mysqlbinlog [--no-defaults] --stop-datetime=’年-月-日 小時(shí):分鐘:秒’ 二進(jìn)制日志 | mysql -u 用戶名 -p 密碼

·

2、從某個(gè)時(shí)間點(diǎn)到日志結(jié)尾的恢復(fù)

mysqlbinlog [--no-defaults] --start-datetime=’年-月-日 小時(shí):分鐘:秒’ 二進(jìn)制日志 | mysql -u 用戶名 -p 密碼

·

3、從某個(gè)時(shí)間點(diǎn)到某個(gè)時(shí)間點(diǎn)的恢復(fù)

mysqlbinlog [--no-defaults] --start-datetime=’年-月-日 小時(shí):分鐘:秒’ --stop-datetime=’年-月-日小時(shí):分鐘:秒’ 二進(jìn)制日志 | mysql -u 用戶名 -p 密碼

·

·

#########mysql企業(yè)備份案例######

1、一般恢復(fù)(丟什么數(shù)據(jù),補(bǔ)什么數(shù)據(jù))

[root@localhost opt]# mysql -u root -p

Enter password:

mysql> create database client;

Query OK, 1 row affected (0.01 sec)

mysql> use client;

mysql> create table user_info(××× char(20) not null,姓名 char(20) not null,姓別 char(4),用戶ID號(hào) char(10) not null,資費(fèi) int(10));

mysql> insert into user_info values('000006','張三','男','016','10');

mysql> insert into user_info values('000007','李四','女','017','91');

mysql> insert into user_info values('000008','王五','女','018','23');

mysql> select * from user_info;

·

mysql> show master logs; ####查看當(dāng)前數(shù)據(jù)庫binlog文件

+------------------+-----------+

| Log_name | File_size |

+------------------+-----------+

| mysql-bin.000001 | 1492 |

+------------------+-----------+

1 row in set (0.00 sec)

·

·

·

·

2、進(jìn)行一次完全備份

mysql> flush tables with read lock; #####備份前需要將數(shù)據(jù)庫加讀鎖,防止數(shù)據(jù)在備份時(shí)寫入。

Query OK, 0 rows affected (0.01 sec)

mysql> exit

Bye

·

[root@localhost ~]# mkdir /mysql_bak ###創(chuàng)建/mysql_bak 目錄

·

[root@localhost opt]# mysqldump -u root -p client user_info >/mysql_bak/client_userinfo-$(date +%F).sql

Enter password:

[root@localhost ~]# ll /mysql_bak/

總用量 4

-rw-r--r-- 1 root root 2055 8月 15 05:55 client_userinfo-2018-08-15.sql

·

[root@localhost mysql_bak]# mysqladmin -u root -p flush-logs

[root@localhost ~]# ll /usr/local/mysql/data/

總用量 122928

-rw-r----- 1 mysql mysql 56 8月 9 16:16 auto.cnf

drwxr-x--- 2 mysql mysql 62 8月 15 06:04 client

-rw-r----- 1 mysql mysql 308 8月 15 06:03 ib_buffer_pool

-rw-r----- 1 mysql mysql 12582912 8月 15 06:04 ibdata1

-rw-r----- 1 mysql mysql 50331648 8月 15 06:04 ib_logfile0

-rw-r----- 1 mysql mysql 50331648 8月 9 16:16 ib_logfile1

-rw-r----- 1 mysql mysql 12582912 8月 15 06:05 ibtmp1

drwxr-x--- 2 mysql mysql 4096 8月 9 16:16 mysql

-rw-r----- 1 mysql mysql 1539 8月 15 06:05 mysql-bin.000001

-rw-r----- 1 mysql mysql 154 8月 15 06:05 mysql-bin.000002

-rw-r----- 1 mysql mysql 78 8月 15 06:05 mysql-bin.index

drwxr-x--- 2 mysql mysql 8192 8月 9 16:16 performance_schema

drwxr-x--- 2 mysql mysql 8192 8月 9 16:16 sys

·

·

·

3、繼續(xù)錄入新的內(nèi)容并進(jìn)行增量備份

[root@localhost mysql_bak]# mysql -u root -p

Enter password:

mysql> unlock tables; #######解除表鎖。

Query OK, 0 rows affected (0.00 sec)

mysql> use client;

mysql> insert into user_info values('000009','趙六','男','019','37');

mysql> insert into user_info values('000010','孫七','男','020','36');

mysql> select from user_info;

+-----------+--------+--------+-------------+--------+

| ××× | 姓名 | 姓別 | 用戶ID號(hào) | 資費(fèi) |

+-----------+--------+--------+-------------+--------+

| 000006 | 張三 | 男 | 016 | 10 |

| 000007 | 李四 | 女 | 017 | 91 |

| 000008 | 王五 | 女 | 018 | 23 |

| 000009 | 趙六 | 男 | 019 | 37 |

| 000010 | 孫七 | 男 | 020 | 36 |

+-----------+--------+--------+-------------+--------+

5 rows in set (0.00 sec)

·

mysql> exit

Bye

[root@localhost mysql_bak]# mysqladmin -u root -p flush-logs ###刷新日志文件

Enter password:

·

[root@localhost ~]# ll /usr/local/mysql/data

總用量 122932

-rw-r----- 1 mysql mysql 56 8月 9 16:16 auto.cnf

drwxr-x--- 2 mysql mysql 62 8月 15 06:04 client

-rw-r----- 1 mysql mysql 308 8月 15 06:03 ib_buffer_pool

-rw-r----- 1 mysql mysql 12582912 8月 15 06:07 ibdata1

-rw-r----- 1 mysql mysql 50331648 8月 15 06:07 ib_logfile0

-rw-r----- 1 mysql mysql 50331648 8月 9 16:16 ib_logfile1

-rw-r----- 1 mysql mysql 12582912 8月 15 06:05 ibtmp1

drwxr-x--- 2 mysql mysql 4096 8月 9 16:16 mysql

-rw-r----- 1 mysql mysql 1539 8月 15 06:05 mysql-bin.000001

-rw-r----- 1 mysql mysql 797 8月 15 06:07 mysql-bin.000002 ###增量備份所需要的數(shù)據(jù)在000002里面

-rw-r----- 1 mysql mysql 154 8月 15 06:07 mysql-bin.000003

-rw-r----- 1 mysql mysql 117 8月 15 06:07 mysql-bin.index

drwxr-x--- 2 mysql mysql 8192 8月 9 16:16 performance_schema

drwxr-x--- 2 mysql mysql 8192 8月 9 16:16 sys

·

·

[root@localhost ~]# cp /usr/local/mysql/data/mysql-bin.000002 /mysql_bak/

·

4、模擬誤操作刪除 user_info表

[root@localhost mysql_bak]# mysql -u root -p -e 'drop table client.user_info'

Enter password:

[root@localhost mysql_bak]# mysql -u root -p -e 'select from client.user_info'

Enter password:

ERROR 1146 (42S02) at line 1: Table 'client.user_info' doesn't exist

·

5、恢復(fù)操作

[root@localhost mysql_bak]# mysql -u root -p client < /mysql_bak/client_userinfo-2018-08-14.sql

Enter password:

[root@localhost mysql_bak]# mysql -u root -p -e'select from client.user_info'

Enter password:

+-----------+--------+--------+-------------+--------+

| ××× | 姓名 | 姓別 | 用戶ID號(hào) | 資費(fèi) |

+-----------+--------+--------+-------------+--------+

·

| 000006 | 張三 | 男 | 016 | 10 |

| 000007 | 李四 | 女 | 017 | 91 |

| 000008 | 王五 | 女 | 018 | 23 |

+-----------+--------+--------+-------------+--------+

·

·

[root@localhost ~]# mysqlbinlog --no-defaults /mysql_bak/mysql-bin.000002 |mysql -u root -p

Enter password:

[root@localhost ~]# mysql -u root -p -e'select from client.user_info'

Enter password:

+-----------+--------+--------+-------------+--------+

| ××× | 姓名 | 姓別 | 用戶ID號(hào) | 資費(fèi) |

+-----------+--------+--------+-------------+--------+

| 000006 | 張三 | 男 | 016 | 10 |

| 000007 | 李四 | 女 | 017 | 91 |

| 000008 | 王五 | 女 | 018 | 23 |

| 000009 | 趙六 | 男 | 019 | 37 |

| 000010 | 孫七 | 男 | 020 | 36 |

+-----------+--------+--------+-------------+--------+

·

·

·

######基于位置恢復(fù)#####

[root@localhost mysql_bak]# mysql -uroot -p -e 'drop table client.user_info' ### 刪除表user_info

Enter password:

·

[root@localhost mysql_bak]# mysql -uroot -p -e 'select from client.user_info'

Enter password:

ERROR 1146 (42S02) at line 1: Table 'client.user_info' doesn't exist

·

·

·

[root@localhost mysql_bak]# mysql -u root -p client < /mysql_bak/client_userinfo-2018-08-14.sql

Enter password:

·

[root@localhost ~]# mysql -u root -p -e'select from client.user_info'

Enter password:

+-----------+--------+--------+-------------+--------+

| ××× | 姓名 | 姓別 | 用戶ID號(hào) | 資費(fèi) |

+-----------+--------+--------+-------------+--------+

| 000006 | 張三 | 男 | 016 | 10 |

| 000007 | 李四 | 女 | 017 | 91 |

| 000008 | 王五 | 女 | 018 | 23 |

+-----------+--------+--------+-------------+--------+

·

·

·

·

[root@localhost ~]# mysqlbinlog --no-defaults --base64-output=decode-rows -v /mysql_bak/mysql-bin.000002 ####--base64-output=decode-rows 64解碼 ### -v 換行顯示

·

·###########################################################################

·# at 293

#180815 6:07:34 server id 1 end_log_pos 359 CRC32 0x3ccdc62e Table_map: client.user_info mapped to number 221

·# at 359

#180815 6:07:34 server id 1 end_log_pos 421 CRC32 0x55f72453 Write_rows: table id 221 flags: STMT_END_F

·### INSERT INTO client.user_info

·### SET

·### @1='000009'

·### @2='趙六'

·### @3='男'

·### @4='019'

·### @5=37

·# at 421

#180815 6:07:34 server id 1 end_log_pos 452 CRC32 0x45fd2af6 Xid = 50

COMMIT/!/;

·# at 452

#180815 6:07:39 server id 1 end_log_pos 517 CRC32 0xa553503b Anonymous_GTID last_committed=1 sequence_number=2 rbr_only=yes

/!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED//!/;

SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/!/;

·# at 517

#180815 6:07:39 server id 1 end_log_pos 591 CRC32 0x02ad5ee9 Query thread_id=7 exec_time=0 error_code=0

SET TIMESTAMP=1534284459/!/;

BEGIN

/!/;

·# at 591

#180815 6:07:39 server id 1 end_log_pos 657 CRC32 0x3f478909 Table_map: client.user_info mapped to number 221

·# at 657

#180815 6:07:39 server id 1 end_log_pos 719 CRC32 0xb478e1c5 Write_rows: table id 221 flags: STMT_END_F

·### INSERT INTO client.user_info

·### SET

·### @1='000010'

·### @2='孫七'

·### @3='男'

·### @4='020'

·### @5=36

·# at 719

#180815 6:07:39 server id 1 end_log_pos 750 CRC32 0xe681426b Xid = 51

COMMIT/!/;

·# at 750

#180815 6:07:59 server id 1 end_log_pos 797 CRC32 0xbc922301 Rotate to mysql-bin.000003 pos: 4

SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /added by mysqlbinlog/ /!/;

DELIMITER ;

·# End of log file

/!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE/;

/!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0/;

########################################################################################·

·

[root@localhost ~]# mysqlbinlog --no-defaults --stop-position='657' /mysql_bak/mysql-bin.000002 |mysql -u root -p ####操作的節(jié)點(diǎn)附近即可

[root@localhost ~]# mysql -u root -p -e'select from client.user_info'

Enter password:

+-----------+--------+--------+-------------+--------+

| ××× | 姓名 | 姓別 | 用戶ID號(hào) | 資費(fèi) |

+-----------+--------+--------+-------------+--------+

| 000006 | 張三 | 男 | 016 | 10 |

| 000007 | 李四 | 女 | 017 | 91 |

| 000008 | 王五 | 女 | 018 | 23 |

| 000009 | 趙六 | 男 | 019 | 37 |

+-----------+--------+--------+-------------+--------+

·

#######

[root@localhost ~]# mysql -u root -p -e 'drop table client.user_info'

Enter password:

[root@localhost ~]# mysql -u root -p client < /mysql_bak/client_userinfo-2018-08-15.sql

Enter password:

[root@localhost ~]# mysqlbinlog --no-defaults --start-position='657' /mysql_bak/mysql-bin.000002 |mysql -u root -p ####操作的節(jié)點(diǎn)附近即可

Enter password:

[root@localhost ~]# mysql -u root -p -e'select from client.user_info'

Enter password:

+-----------+--------+--------+-------------+--------

| ××× | 姓名 | 姓別 | 用戶ID號(hào) | 資費(fèi) |

+-----------+--------+--------+-------------+--------+

| 000006 | 張三 | 男 | 016 | 10 |

| 000007 | 李四 | 女 | 017 | 91 |

| 000008 | 王五 | 女 | 018 | 23 |

| 000010 | 孫七 | 男 | 020 | 36 |

+-----------+--------+--------+-------------+--------+

####提醒

基于位置恢復(fù)的操作,節(jié)點(diǎn)不要選擇太靠近操作的節(jié)點(diǎn),否則容易操作錯(cuò)誤。

·

·

·

·

######基于時(shí)間恢復(fù)#####

mysqlbinlog --no-defaults --stop-datetime='2018-8-15 6:07:39' /mysql_bak/mysql-bin.000002 |mysql -u root -p

mysqlbinlog --no-defaults --start-datetime='2018-8-15 6:07:39' /mysql_bak/mysql-bin.000002 |mysql -u root -p

####列子###

[root@localhost ~]# mysql -u root -p -e 'drop table client.user_info'

Enter password:

[root@localhost ~]# mysql -u root -p client < /mysql_bak/client_userinfo-2018-08-15.sql

Enter password:

[root@localhost ~]# mysqlbinlog --no-defaults --stop-datetime='2018-8-15 6:07:39' /mysql_bak/mysql-bin.000002 |mysql -u root -p ######時(shí)間格式不能弄錯(cuò)

Enter password:

[root@localhost ~]# mysql -u root -p -e'select from client.user_info'

Enter password:

+-----------+--------+--------+-------------+--------+

| ××× | 姓名 | 姓別 | 用戶ID號(hào) | 資費(fèi) |

+-----------+--------+--------+-------------+--------+

| 000006 | 張三 | 男 | 016 | 10 |

| 000007 | 李四 | 女 | 017 | 91 |

| 000008 | 王五 | 女 | 018 | 23 |

| 000009 | 趙六 | 男 | 019 | 37 |

+-----------+--------+--------+-------------+--------+

·

·

####列子###

[root@localhost ~]# mysql -u root -p -e 'drop table client.user_info'

Enter password:

[root@localhost ~]# mysql -u root -p client < /mysql_bak/client_userinfo-2018-08-15.sql

Enter password:

[root@localhost ~]# mysqlbinlog --no-defaults --start-datetime='2018-8-15 15:33:48' /mysql_bak/mysql-bin.000002 |mysql -u root -p ######時(shí)間格式不能弄錯(cuò)

Enter password:

[root@localhost ~]# mysql -u root -p -e'select from client.user_info'

Enter password:

+-----------+--------+--------+-------------+--------+

| ××× | 姓名 | 姓別 | 用戶ID號(hào) | 資費(fèi) |

+-----------+--------+--------+-------------+--------+

| 000006 | 張三 | 男 | 016 | 10 |

| 000007 | 李四 | 女 | 017 | 91 |

| 000008 | 王五 | 女 | 018 | 23 |

| 000010 | 孫七 | 男 | 020 | 36 |

+-----------+--------+--------+-------------+--------+

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的mysql 5.7 数据库备份_MySQL5.7.20数据库备份与恢复的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。