Mysql的備份與恢復(fù)全過程(使用XtraBackup)
xtrabackup是MySQL的一種物理備份工具,相對于mysqldump,備份和還原速度更快。
一、安裝 XtraBackup
MySQL 8.0版本
# CentOS 7 wget https://repo.percona.com/yum/percona-release-latest.noarch.rpm sudo rpm -ivh percona-release-latest.noarch.rpm sudo percona-release enable-only tools release yum install -y percona-xtrabackup-80 # CentOS 8 dnf install -y https://repo.percona.com/yum/percona-release-latest.noarch.rpm percona-release setup -y ps80 dnf install -y percona-xtrabackup-80 # Ubuntu wget https://repo.percona.com/apt/percona-release_latest.$(lsb_release -sc)_all.deb sudo dpkg -i percona-release_latest.$(lsb_release -sc)_all.deb sudo percona-release enable-only tools release sudo apt update sudo apt install -y percona-xtrabackup-80
MySQL 5.7版本
# CentOS 7 wget https://repo.percona.com/yum/percona-release-latest.noarch.rpm sudo rpm -ivh percona-release-latest.noarch.rpm sudo percona-release enable tools release sudo yum install percona-xtrabackup-24 # CentOS 8 sudo dnf install -y https://repo.percona.com/yum/percona-release-latest.noarch.rpm sudo percona-release setup tools sudo dnf install percona-xtrabackup-24 # Ubuntu wget https://repo.percona.com/apt/percona-release_latest.$(lsb_release -sc)_all.deb sudo dpkg -i percona-release_latest.$(lsb_release -sc)_all.deb sudo percona-release enable tools release sudo apt update sudo apt install percona-xtrabackup-24
注意:
- Percona XtraBackup 8.0 版本適合 MySQL 8.0
- Percona XtraBackup 2.4 版本適合 MySQL 5.7
二、備份 MySQL 數(shù)據(jù)庫
1.創(chuàng)建備份目錄
sudo mkdir -p /data/backups/mysql sudo chown -R mysql:mysql /data/backups
2.執(zhí)行備份
(1)全量備份
# MySQL 5.7 xtrabackup --user=backupuser --password=password \ --host=localhost --backup --target-dir=/data/backups/mysql/full_$(date +%F) # MySQL 8.0(需添加 --no-timestamp 參數(shù)) xtrabackup --user=backupuser --password=password \ --host=localhost --backup --target-dir=/data/backups/mysql/full_$(date +%F) \ --no-timestamp
(2)增量備份(可選)
# 首次全量備份 xtrabackup --backup --target-dir=/data/backups/full # 后續(xù)增量備份 xtrabackup --backup --target-dir=/data/backups/incremental_1 \ --incremental-basedir=/data/backups/full xtrabackup --backup --target-dir=/data/backups/incremental_2 \ --incremental-basedir=/data/backups/incremental_1
三、恢復(fù) MySQL 數(shù)據(jù)庫
1.停止 MySQL 服務(wù)
# CentOS 7 sudo systemctl stop mysqld # CentOS 8/Ubuntu sudo systemctl stop mysql
2.執(zhí)行恢復(fù)操作
(1)清空數(shù)據(jù)目錄
sudo rm -rf /var/lib/mysql/*
(2)全量備份還原
# 注意:恢復(fù)時需使用 --copy-back 參數(shù) xtrabackup --prepare --target-dir=/data/backups/mysql/full_$(date +%F) xtrabackup --copy-back --target-dir=/data/backups/mysql/full_$(date +%F)
(3)增量備份還原(可選)
# 有增量備份的還原,最后一次還原不需要加選項--apply-log-only,假設(shè)存在兩個增量備份:incremental_1 和 incremental_2,按順序應(yīng)用增量備份 # 準(zhǔn)備全量備份(不生成回滾點) xtrabackup --prepare --apply-log-only --target-dir=/data/backups/full # 應(yīng)用第一個增量備份 xtrabackup --prepare --apply-log-only \ --target-dir=/data/backups/full \ --incremental-dir=/data/backups/incremental_1 # 應(yīng)用第二個增量備份 xtrabackup --prepare --apply-log-only \ --target-dir=/data/backups/full \ --incremental-dir=/data/backups/incremental_2 # 最終準(zhǔn)備(生成回滾點) xtrabackup --prepare --target-dir=/data/backups/full # 還原最終回滾點 xtrabackup --copy-back --target-dir=/data/backups/mysql/full
關(guān)鍵參數(shù)說明
- 1.--apply-log-only
- 防止在應(yīng)用增量日志后自動生成回滾點,確??梢岳^續(xù)應(yīng)用后續(xù)增量備份
- 2.--incremental-dir
- 指定增量備份文件所在目錄
- 3.最終 prepare 不使用 --apply-log-only
- 生成最終回滾點,使備份數(shù)據(jù)處于一致性狀態(tài)
3.修復(fù)權(quán)限
sudo chown -R mysql:mysql /var/lib/mysql
4.啟動 MySQL 服務(wù)
# CentOS 7 sudo systemctl start mysqld # CentOS 8/Ubuntu sudo systemctl start mysql
注意:
1.MySQL 8.0 特殊配置
- 需添加 --no-timestamp 參數(shù)避免創(chuàng)建時間戳目錄
- 可能需要調(diào)整 validate_password 插件配置
2.備份用戶需具備 RELOAD, LOCK TABLES, REPLICATION CLIENT 權(quán)限
CREATE USER 'backupuser'@'localhost' IDENTIFIED BY 'password'; GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT, PROCESS ON *.* TO 'backupuser'@'localhost'; FLUSH PRIVILEGES;
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
計算機管理服務(wù)中找不到mysql的服務(wù)的解決辦法
MySQL是一種流行的開源關(guān)系型數(shù)據(jù)庫管理系統(tǒng),用于存儲和管理大量數(shù)據(jù),在計算機管理中,啟動MySQL服務(wù)是一項重要的任務(wù),因為它可以確保數(shù)據(jù)庫系統(tǒng)的順利運行,這篇文章主要給大家介紹了關(guān)于計算機管理服務(wù)中找不到mysql的服務(wù)的解決辦法,需要的朋友可以參考下2023-05-05
Lost connection to MySQL server at ''reading authorization p
這篇文章主要介紹了Lost connection to MySQL server at 'reading authorization packet', system error: 0錯誤解決方法,需要的朋友可以參考下2014-08-08
詳解mysql中字符串轉(zhuǎn)為數(shù)字的三種方法
這篇文章主要為大家詳細(xì)介紹了mysql中字符串轉(zhuǎn)為數(shù)字的三種常用方法,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11
MySQL中可為空的字段設(shè)置為NULL還是NOT NULL
今天小編就為大家分享一篇關(guān)于MySQL中可為空的字段設(shè)置為NULL還是NOT NULL,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03
MySQL重復(fù)數(shù)據(jù)提取最新一條技術(shù)方法詳解
在MySQL數(shù)據(jù)庫中清除重復(fù)數(shù)據(jù)是一項常見的任務(wù),下面這篇文章主要給大家介紹了關(guān)于MySQL重復(fù)數(shù)據(jù)提取最新一條的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07
MySQL使用IF語句及用case語句對條件并結(jié)果進(jìn)行判斷?
這篇文章主要介紹了MySQL使用IF語句及用case語句對條件并結(jié)果進(jìn)行判斷,文章通過圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09

