mysql通過(guò)binlog定時(shí)備份數(shù)據(jù)庫(kù)與恢復(fù)的方法
更新時(shí)間:2024年12月24日 09:20:03 作者:另類程序員132
這篇文章主要介紹了mysql通過(guò)binlog定時(shí)備份數(shù)據(jù)庫(kù)與恢復(fù)的方法,本文給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
1.配置binlog
vim /etc/my.cnf server_id=1 log-bin=mysql-bin
查看指定binlog文件中的事件
-- 查看指定binlog文件中的所有事件 show binlog events in 'binlog.000001' -- 查看指定binlog文件中從指定位置(position)開始的所有事件 show binlog events in 'binlog.000001' from 32556; -- 分頁(yè)查詢 show binlog events in 'binlog.000001' from 32556 limit 10;
2.編寫shell腳本
1.正常安裝 mysqldump -uroot -proot --databases test --master-data=2 --flush-logs > /backup/`date +%F-%H`-mysql-vending-machine.sql 2.通過(guò)docker部署 docker exec e2c326627ed5 sh -c 'exec mysqldump --databases test -u root -p"root" --master-data=2 --flush-logs' > /opt/backup/`date +%F-%H`-mysql-vending-machine.sql 注: --databases后面是數(shù)據(jù)庫(kù)名,master-data=2 注釋掉日志記錄
1.mkdir /opt/backup 2.vim backup.sh 3.#!/bin/bash #mysqldump -uroot -proot --databases test --master-data=2 --flush-logs > /backup/`date +%F-%H`-mysql-test.sql
3.查看備份好的數(shù)據(jù)庫(kù).sql腳本
CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=154;
4.創(chuàng)建定時(shí)任務(wù)
crontab -e // 創(chuàng)建定時(shí)任務(wù),從凌晨2點(diǎn)開始執(zhí)行 0 2 * * * /opt/backup/backup.sh crontab -l // 查看定時(shí)任務(wù)
5.恢復(fù)數(shù)據(jù)
1.刪除數(shù)據(jù)執(zhí)行備份好的sql腳本: /backup/date-mysql-test.sql 2.查看date-mysql-test.sql里的CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=154;記錄mysql-bin.000001與154,執(zhí)行binlog從mysql-bin.000001文件的154開始恢復(fù) 3.開始通過(guò)binlog恢復(fù)數(shù)據(jù) mysqlbinlog mysql-bin.000001 --start-position=154 --stop-position=71012 | mysql -uroot -p'root' 注: --start-position:開始恢復(fù)的位置 --stop-position:結(jié)束的位置,如果需要執(zhí)行到最后這個(gè)參數(shù)可以不寫
到此這篇關(guān)于mysql通過(guò)binlog定時(shí)備份數(shù)據(jù)庫(kù)與恢復(fù)的文章就介紹到這了,更多相關(guān)mysql binlog定時(shí)備份數(shù)據(jù)庫(kù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
安裝MySQL 5.7出現(xiàn)報(bào)錯(cuò):unknown variable ‘mysqlx_port
這篇文章主要介紹了安裝MySQL 5.7出現(xiàn)報(bào)錯(cuò):unknown variable ‘mysqlx_port=0.0‘的解決方法,文中通過(guò)圖文結(jié)合的方式介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-06-06CentOS7環(huán)境下安裝MySQL5.5數(shù)據(jù)庫(kù)
大家好,本篇文章主要講的是CentOS7環(huán)境下安裝MySQL5.5數(shù)據(jù)庫(kù),感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12MySQL優(yōu)化之如何了解SQL的執(zhí)行頻率
MySQL 客戶端連接成功后,通過(guò) show [session|global]status 命令 可以提供服務(wù)器狀態(tài)信息,也可以在操作系統(tǒng)上使用 mysqladmin extended-status 命令獲得這些消息2014-05-05