mysql 备份_MySQL数据库备份实操
本文基于mysql(8.0.20)及xtrabackup(8.0.13)最新版本,實(shí)現(xiàn)了完整的mysqldump邏輯備份、binlog增量備份、xtrabackup物理備份恢復(fù),幫你快速掌握操作要點(diǎn)
主要內(nèi)容:
基礎(chǔ)環(huán)境搭建
mysqldump邏輯備份及恢復(fù)
數(shù)據(jù)準(zhǔn)備
全量備份
全量恢復(fù)
其他備份
Xtrabackup物理備份及恢復(fù)
程序安裝
全量備份及恢復(fù)
增量備份及恢復(fù)
binlog增量備份及恢復(fù)
更多說明
mysql時(shí)區(qū)配置
參考資料
基礎(chǔ)環(huán)境搭建
本文相關(guān)環(huán)境均基于docker實(shí)現(xiàn),下面是啟動一個最基本的mysql數(shù)據(jù)庫:
docker?run?--name?mysql-dump-test?-p?3306:3306?-e?MYSQL_ROOT_PASSWORD=admin?-e?MYSQL_DATABASE=user?-d?mysqlmysqldump邏輯備份及恢復(fù)
在開展備份之前,需要先準(zhǔn)備如下的測試數(shù)據(jù):
數(shù)據(jù)準(zhǔn)備
create?table?if?not?exists?t_user1(
????id?????????bigint(20)?auto_increment?primary?key?comment?'主鍵',
????name???????varchar(64)?comment?'用戶名稱',
????birth_date?timestamp?comment?'生日',
????assert?????decimal(10,?2)?comment?'資產(chǎn)'
)?engine?=?InnoDB
??charset?utf8mb4?comment?'用戶表';
insert?into?t_user1(name,?birth_date,?assert)
values?('zhangsan',?now(),?1123000.99);
insert?into?t_user1(name,?birth_date,?assert)
values?('lisi',?now(),?1159000.99);
insert?into?t_user1(name,?birth_date,?assert)
values?('wangwu',?now(),?12341234.00);
如上,創(chuàng)建一張測試表,并插入3條測試數(shù)據(jù)。
全量備份
全量備份基本模式:
- 進(jìn)入到mysql容器中
- 創(chuàng)建備份目錄
- 通過mysqldump命令,執(zhí)行數(shù)據(jù)庫邏輯備份操作,將結(jié)果輸出到 sql文件中。
主要命令如下:
#?級聯(lián)創(chuàng)建數(shù)據(jù)備份目錄mkdir?-p?/data/backups/dmp
#?實(shí)現(xiàn)所有數(shù)據(jù)庫備份
mysqldump?--opt?--single-transaction?--master-data=2?--host=localhost?--user=root?--password=admin?--all-databases?>?/data/backups/dmp/dmp1.sql
mysqldump相關(guān)參數(shù)說明:
- --opt 適用于備份大表,同時(shí)激活了-add-drop-table --add-locks --create-options --disable-keys --extended-insert --lock-tables --quick --set-charset 命令
- --single-transaction 開啟一個事務(wù),并設(shè)置備份事務(wù)為可重復(fù)讀,保持備份數(shù)據(jù)一致性
- --master-data=2 表示在備份過程中記錄主庫的binlog和pos點(diǎn),并且在dump文件中注釋改行
- --all-databases 導(dǎo)出所有數(shù)據(jù)庫,包括mysql庫
全量恢復(fù)
通過在上述庫中,執(zhí)行drop table t_user1,刪除該表后,開展恢復(fù):
- 通過mysql命令,即可將 dump sql文件執(zhí)行到對應(yīng)的數(shù)據(jù)庫中。
其他備份
1、導(dǎo)出指定數(shù)據(jù)庫的指定表:
- --databases 指定備份的數(shù)據(jù)庫
- --tables 指定備份的具體數(shù)據(jù)庫表
2、只導(dǎo)出建表語句:
- --no-data 申明不導(dǎo)出數(shù)據(jù),只導(dǎo)出表結(jié)構(gòu)
3、條件備份:
- --where 來指定具體的查詢條件
- --no-create-db 申明不導(dǎo)出數(shù)據(jù)庫創(chuàng)建等信息
- --no-create-info 申明不導(dǎo)出創(chuàng)建表等信息,這樣就可以避免數(shù)據(jù)表被刪除
Xtrabackup物理備份及恢復(fù)
當(dāng)前xtrabackup的8.0.13已經(jīng)支持 mysql 8.0.20版本(8.0.20版本對innodb的數(shù)據(jù)文件模式進(jìn)行了修改)
程序安裝
與mysql環(huán)境一樣,需要將xtrabackup安裝到mysql容器中:
- 在 https://www.percona.com/downloads/Percona-XtraBackup-LATEST/ 中下載二進(jìn)制包,例如當(dāng)前docker-mysql容器是 debian的buster系統(tǒng),則下載對應(yīng)的文件percona-xtrabackup-80_8.0.13-1.buster_amd64.deb
- 因?yàn)樯鲜鑫募惭b會還會依賴其他庫,因此要將 /etc/apt/sources.list內(nèi)容替換為國內(nèi)鏡像,例如阿里云的。
- 更多安裝說明,參考https://www.percona.com/doc/percona-xtrabackup/8.0/installation/apt_repo.html
原/etc/apt/sources.list文件內(nèi)容如下:
#?deb?http://snapshot.debian.org/archive/debian/20200422T000000Z?buster?maindeb?http://deb.debian.org/debian?buster?main
#?deb?http://snapshot.debian.org/archive/debian-security/20200422T000000Z?buster/updates?main
deb?http://security.debian.org/debian-security?buster/updates?main
#?deb?http://snapshot.debian.org/archive/debian/20200422T000000Z?buster-updates?main
deb?http://deb.debian.org/debian?buster-updates?main
將內(nèi)容全部替換為:
deb?http://mirrors.aliyun.com/debian/?buster?main?non-free?contribdeb?http://mirrors.aliyun.com/debian-security?buster/updates?main
deb?http://mirrors.aliyun.com/debian/?buster-updates?main?non-free?contrib
deb?http://mirrors.aliyun.com/debian/?buster-backports?main?non-free?contribb
具體命令操作:
#?將容器中的文件拷貝出來docker?cp?mysql-dump-test:/etc/apt/sources.list?D:\dev2\test\mysqldump
#?將修改后的文件覆蓋回容器中
docker?cp?D:\dev2\test\mysqldump\sources.list?mysql-dump-test:/etc/apt/
#?進(jìn)入容器以后,執(zhí)行下面命令更新apt信息
apt-get?update
完成上述準(zhǔn)備工作以后,即可開始安裝xtrabackup:
- 將下載好的文件percona-xtrabackup-80_8.0.13-1.buster_amd64.deb,拷貝到容器中
- 通過dpkg來安裝,第一次執(zhí)行會報(bào)錯,根據(jù)錯誤提示信息,發(fā)現(xiàn)最底部依賴libev4
- 執(zhí)行apt install libev4,此時(shí)依然會報(bào)錯,根據(jù)提示信息,執(zhí)行apt --fix-broken install,此時(shí)會下載和安裝所有依賴包。
- 上一步執(zhí)行完成后,再次執(zhí)行dpkg名稱,完成安裝
具體命令操作如下:
#?將下載文件拷貝到容器中docker?cp?D:\dev2\test\mysqldump\percona-xtrabackup-80_8.0.13-1.buster_amd64.deb?mysql-dump-test:/data
#?執(zhí)行第一次安裝,此時(shí)會出現(xiàn)錯誤提示
dpkg?-i?percona-xtrabackup-80_8.0.13-1.buster_amd64.deb
#?執(zhí)行l(wèi)ibev4?安裝,也會出錯
apt?install?libev4
#?執(zhí)行相關(guān)依賴安裝
apt?--fix-broken?install
#?再次執(zhí)行,完成安裝
dpkg?-i?percona-xtrabackup-80_8.0.13-1.buster_amd64.deb
全量備份及恢復(fù)
安裝成功后,即可測試全量備份:
- 提前建立好 /data/backups/ 目錄
- 通過查看用戶手冊,整個備份及恢復(fù)主要是三個過程:backup、prepa、copy-back
具體命令操作如下:
#?啟動全量備份xtrabackup?--backup?--target-dir=/data/backups/base1?--user=root?--password=admin
#?通過執(zhí)行drop?table?t_user1來模擬誤操作
#?準(zhǔn)備全量恢復(fù)
xtrabackup?--prepare?--target-dir=/data/backups/base1
#?將備份文件同步到mysql數(shù)據(jù)文件目錄中
rsync?-avrP?/data/backups/base1/?/var/lib/mysql/
#?退出容器后,執(zhí)行容器重啟,完成恢復(fù)
docker?restart?mysql-dump-test
同時(shí),上述rsync也可以用如下命令替代,但需要保證datadir(也即/var/lib/mysql/)是空的:
xtrabackup?--copy-back?--target-dir=/data/backups/base1增量備份及恢復(fù)
增量備份及恢復(fù)過程如下:
- 先創(chuàng)建全量備份
- 再在全量備份基礎(chǔ)上,執(zhí)行增量備份
- 恢復(fù)時(shí),先執(zhí)行全量路徑的prepare,再執(zhí)行增量路徑的prepare
備份具體操作:
#?全量備份xtrabackup?--backup?--target-dir=/data/backups/base2?--user=root?--password=admin
#?第一次增量備份
xtrabackup?--backup?--target-dir=/data/backups/inc1?--incremental-basedir=/data/backups/base2?--user=root?--password=admin
#?第二次增量備份
xtrabackup?--backup?--target-dir=/data/backups/inc2?--incremental-basedir=/data/backups/inc1?--user=root?--password=admin
恢復(fù)具體操作:
#?先恢復(fù)全量備份xtrabackup?--prepare?--apply-log-only?--target-dir=/data/backups/base2
#?逐項(xiàng)恢復(fù)增量備份
xtrabackup?--prepare?--apply-log-only?--target-dir=/data/backups/base2?--incremental-dir=/data/backups/inc1
xtrabackup?--prepare?--apply-log-only?--target-dir=/data/backups/base2?--incremental-dir=/data/backups/inc2
#?將恢復(fù)文件進(jìn)行同步
rsync?-avrP?/data/backups/base2/?/var/lib/mysql/
#?退出容器后,執(zhí)行容器重啟,完成恢復(fù)
docker?restart?mysql-dump-test
binlog增量備份及恢復(fù)
binlog增量備份及恢復(fù)主要過程:
- 在全量邏輯備份時(shí),增加--flush-logs,目的是將緩存寫入到binlog,并開始一個新的binlog
- 執(zhí)行誤操作以后,通過定位到對應(yīng)binlog文件位置,通過mysqlbinlog實(shí)現(xiàn)增量恢復(fù)
先執(zhí)行全量備份:
#?執(zhí)行全量備份mysqldump?--opt?--single-transaction?--master-data=2?--flush-logs?--host=localhost?--user=root?--password=admin?--databases?user?>?/data/backups/dmp/dmp3.sql
下面是模擬誤操作(新增一條記錄后,刪除數(shù)據(jù)表):
insert?into?t_user1(name,?birth_date,?assert)values?('maliu',?now(),?12341234.00);
drop?table?t_user1;
接下來是具體binlog定位及分析過程:
1、通過show variables like 'log_bin%'命令,查看binlog路徑,具體輸出如下:
Variable_name,Valuelog_bin,ON
log_bin_basename,/var/lib/mysql/binlog
log_bin_index,/var/lib/mysql/binlog.index
log_bin_trust_function_creators,OFF
log_bin_use_v1_row_events,OFF
2、通過show master logs,查看當(dāng)前binlog明細(xì)
Log_name,File_size,Encryptedbinlog.000001,3101453,No
binlog.000002,179,No
binlog.000003,3382334,No
binlog.000004,684,No
3、通過show master status查看最新binlog狀態(tài)
File,Position,Binlog_Do_DB,Binlog_Ignore_DB,Executed_Gtid_Setbinlog.000004,684,"","",""
4、將誤操作時(shí)binlog備份到執(zhí)行目錄,然后通過全量備份恢復(fù)數(shù)據(jù)
#?備份binlogmkdir?-p?/data/backups/binlog/
cp?/var/lib/mysql/binlog.000004?/data/backups/binlog/
#?先通過全量備份還原數(shù)據(jù)
mysql?-h?localhost?-u?root?-p?
5、分析binlog,確定具體誤操作位置
mysqlbinlog?--base64-output=DECODE-ROWS?/data/backups/binlog/binlog.000004?|?more例如,通過上述命令,可查看binlog詳情(也可以通過show binlog events in 'binlog.000004'來分析),輸出如下:
#?at?552#200719?14:48:19?server?id?1??end_log_pos?684?CRC32?0x2e00f1bb??Query???thread_id=8?????exec_time=0?????error_code=0????Xid?=?1534
use?`user`/*!*/;
SET?TIMESTAMP=1595170099/*!*/;
DROP?TABLE?`t_user1`?/*?generated?by?server?*/
分析后,即可定位到552位置即drop table的時(shí)刻
6、通過mysqlbinlog及定位到的位置,實(shí)現(xiàn)增量恢復(fù)
#?基于備份的binlog,將數(shù)據(jù)庫恢復(fù)到552位置mysqlbinlog?--stop-position=552?/data/backups/binlog/binlog.000004?|?mysql?-u?root?-p
至此,binlog增量備份及恢復(fù)就完成了。
更多說明
mysql時(shí)區(qū)配置
補(bǔ)充一個配置,在mysql8中,默認(rèn)的binlog_format是row,可通過show variables like “binlog_format”查看,因此就不需要單獨(dú),但因?yàn)閐ocker默認(rèn)是標(biāo)準(zhǔn)時(shí)區(qū),為了便于后續(xù)開發(fā),還是需要配置為+8時(shí)區(qū):
- 將/etc/mysql/my.cnf文件拷貝出來
- 增加default_time_zone='+8:00'相關(guān)配置
- 將修改后的文件復(fù)制回去之后,重啟服務(wù)即可
修訂后的my.cnf文件:
[mysqld]pid-file????????=?/var/run/mysqld/mysqld.pid
socket??????????=?/var/run/mysqld/mysqld.sock
datadir?????????=?/var/lib/mysql
secure-file-priv=?NULL
default_time_zone='+8:00'
主要操作命令:
#?將配置文件拷貝出來docker?cp?mysql-dump-test:/etc/mysql/my.cnf?D:\dev2\test\mysqldump
#?將修改后的配置文件覆蓋回去
docker?cp?D:\dev2\test\mysqldump\my.cnf?mysql-dump-test:/etc/mysql
#?重啟mysql數(shù)據(jù)庫
docker?restart?mysql-dump-test
#?查看日志情況
docker?logs?mysql-dump-test
參考資料
歡迎關(guān)注我的公眾號【技術(shù)路漫漫】,反饋相關(guān)問題和意見:
更多參考資料:
https://dev.mysql.com/doc/refman/8.0/en/mysqldump.html
https://www.percona.com/doc/percona-xtrabackup/8.0/index.html
總結(jié)
以上是生活随笔為你收集整理的mysql 备份_MySQL数据库备份实操的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言数组宏定义标识符,C语言学习笔记-
- 下一篇: 判断resultset是否遍历到最后一条