百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

腾讯云-RDS-MySQL8数据恢复

nanshan 2025-05-21 15:22 4 浏览 0 评论

简介

这里采用的是MySQL 8.0版本,腾讯云云上数据库备份默认是qpress结合 Percona Xtrabackup 备份的。新找台空闲服务器来操作,这里演示的服务器系统为Ubuntu 22.04 LTS版本。需要提前安装好MySQL 8.0以上的数据库版本、Percona Xtrabackup和qpress

数据库安装

#!/bin/bash
#
#********************************************************************
#Author:        mr.luo            
#Date:          2025-02-26
#FileName:     install_mysql_for_ubuntu.sh
#URL:           https://www.dklwj.com
#Description:      The test script
#Copyright (C):     2025 All rights reserved
#********************************************************************

color () {
    RES_COL=60
    MOVE_TO_COL="echo -en \\033[${RES_COL}G"
    SETCOLOR_SUCCESS="echo -en \\033[1;32m"
    SETCOLOR_FAILURE="echo -en \\033[1;31m"
    SETCOLOR_WARNING="echo -en \\033[1;33m"
    SETCOLOR_NORMAL="echo -en \E[0m"
    echo -n "$1" && $MOVE_TO_COL
    echo -n "["
    if [ $2 = "success" -o $2 = "0" ] ;then
        ${SETCOLOR_SUCCESS}
        echo -n #34;  OK  "    
    elif [ $2 = "failure" -o $2 = "1"  ] ;then 
        ${SETCOLOR_FAILURE}
        echo -n #34;FAILED"
    else
        ${SETCOLOR_WARNING}
        echo -n #34;WARNING"
    fi
    ${SETCOLOR_NORMAL}
    echo -n "]"
    echo 
}

install() {
	softpath="/app/soft"

    mysqlversion="mysql-8.0.39-linux-glibc2.28-x86_64.tar.xz"

# 解压MySQL 二进制包到/app/soft下
tar xf ${softpath}/${mysqlversion} -C ${softpath}

if id mysql &> /dev/null ;then 
    color "mysql 用户已存在" 1 
else
    useradd -r -s /sbin/nologin mysql
    color "MySql  用户创建成功" 0
fi

mkdir /app/mydata/{logs,data} -p  && chown -R mysql.mysql /app/mydata

ln -s ${softpath}/mysql-8.0.39-linux-glibc2.28-x86_64/ /usr/local/mysql && chown -R mysql.mysql /usr/local/mysql/

cat >> /etc/my.cnf <<EOF
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
server_id=1
port = 3306
user = mysql
character-set-server = utf8
default_storage_engine = innodb
log_timestamps = SYSTEM
socket = /tmp/mysql.sock
basedir = /usr/local/mysql
datadir = /app/mydata/data
pid-file = /app/mydata/data/mysql.pid
max_connections = 10000
max_connect_errors = 1000
table_open_cache = 1024
max_allowed_packet = 128M
open_files_limit = 65535
log_bin = mysql-bin
binlog_format  = MIXED
sort_buffer_size = 16M
join_buffer_size = 16M
skip_ssl 
#####====================================[innodb]==============================
innodb_buffer_pool_size = 1024M
innodb_file_per_table = 1
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_purge_threads = 2
innodb_flush_log_at_trx_commit = 1
innodb_log_file_size = 512M
innodb_log_files_in_group = 2
innodb_log_buffer_size = 16M
innodb_max_dirty_pages_pct = 80
innodb_rollback_on_timeout = on
innodb_lock_wait_timeout = 28800
innodb_data_file_path=ibdata1:1024M:autoextend
innodb_undo_tablespaces=3
lower_case_table_names = 1
skip_name_resolve = on
#####====================================[log]==============================
log_error = /app/mydata/logs/mysql-error.log 
slow_query_log = 1
long_query_time = 20 
log-queries-not-using-indexes
slow_query_log_file = /app/mydata/logs/mysql-slow.log
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
EOF

apt update && apt-get -y install libaio-dev  libncurses5

cd /usr/local/mysql && ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/app/mydata/data --explicit_defaults_for_timestamp && \
cp support-files/mysql.server /etc/init.d/mysqld && echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh && \
source /etc/profile.d/mysql.sh 

/etc/init.d/mysqld start
}

install

安装qpress

root@k8s-node-19:/app/soft# wget -d --user-agent="Mozilla/5.0 (Windows NT x.y; rv:10.0) Gecko/20100101 Firefox/10.0" https://docs-tencentdb-1256569818.cos.ap-guangzhou.myqcloud.com/qpress-11-linux-x64.tar
root@k8s-node-19:/app/soft# tar -xf qpress-11-linux-x64.tar -C /usr/local/bin

安装Percona XtraBackup 8.0

root@k8s-node-19:/app/soft# apt update
root@k8s-node-19:/app/soft# apt install curl
root@k8s-node-19:/app/soft# curl -O https://repo.percona.com/apt/percona-release_latest.generic_all.deb
root@k8s-node-19:/app/soft# apt -y install gnupg2 lsb-release ./percona-release_latest.generic_all.deb
root@k8s-node-19:/app/soft# apt update
root@k8s-node-19:/app/soft# percona-release setup pxb-80
root@k8s-node-19:/app/soft# apt -y install percona-xtrabackup-80
root@k8s-node-19:/app/soft# xtrabackup --version 
2025-02-26T11:03:32.995888+08:00 0 [Note] [MY-011825] [Xtrabackup] recognized server arguments: --server-id=1 --datadir=/app/mydata/data --open_files_limit=65535 --log_bin=mysql-bin --innodb_buffer_pool_size=1024M --innodb_file_per_table=1 --innodb_write_io_threads=4 --innodb_read_io_threads=4 --innodb_flush_log_at_trx_commit=1 --innodb_log_file_size=512M --innodb_log_files_in_group=2 --innodb_log_buffer_size=16M --innodb_max_dirty_pages_pct=80 --innodb_data_file_path=ibdata1:1024M:autoextend --innodb_undo_tablespaces=3 
xtrabackup version 8.0.35-32 based on MySQL server 8.0.35 Linux (x86_64) (revision id: c339924a)

恢复数据

1、创建新目录

root@k8s-node-19:/app/soft# mkdir /app/data
# 上传要恢复的数据文件。从腾讯云下载的备份文件
# 解压备份文件
root@k8s-node-19:/app/soft# xbstream -x -C /app/data < /app/xiaohuniu_mysql_backup_20250225000558.xb
# 由于腾讯云压缩后文件名以.qb结尾因此需要解压
root@k8s-node-19:/app/soft# cd /app/data
root@k8s-node-19:/app/data# ls
backup-my.cnf.qp     ibdata1.qp           mysql-bin.index.qp  sys          xhn_tax                    xtrabackup_info.qp        xtrabackup_tablespaces.qp
__cdb_recycle_bin__  mysql                mysql.ibd.qp        undo_001.qp  xtrabackup_binlog_info.qp  xtrabackup_logfile.qp
ib_buffer_pool.qp    mysql-bin.000116.qp  performance_schema  undo_002.qp  xtrabackup_checkpoints     xtrabackup_slave_info.qp
root@k8s-node-19:/app/data# xtrabackup --decompress --target-dir=/app/data
2025-02-26T10:26:51.549479+08:00 0 [Note] [MY-011825] [Xtrabackup] recognized server arguments: --server-id=1 --datadir=/app/mydata/data --open_files_limit=65535 --log_bin=mysql-bin --innodb_buffer_pool_size=1024M --innodb_file_per_table=1 --innodb_write_io_threads=4 --innodb_read_io_threads=4 --innodb_flush_log_at_trx_commit=1 --innodb_log_file_size=512M --innodb_log_files_in_group=2 --innodb_log_buffer_size=16M --innodb_max_dirty_pages_pct=80 --innodb_data_file_path=ibdata1:1024M:autoextend --innodb_undo_tablespaces=3 
2025-02-26T10:26:51.549819+08:00 0 [Note] [MY-011825] [Xtrabackup] recognized client arguments: --port=3306 --socket=/tmp/mysql.sock --decompress=1 --target-dir=/app/data 
xtrabackup version 8.0.35-32 based on MySQL server 8.0.35 Linux (x86_64) (revision id: c339924a)
2025-02-26T10:26:51.550474+08:00 0 [Note] [MY-011825] [Xtrabackup] decompressing ./xhn_tax/yzh_depart_daili_amount_cash_record.ibd.qp
...
2025-02-26T10:26:52.721956+08:00 0 [Note] [MY-011825] [Xtrabackup] decompressing ./xtrabackup_info.qp
2025-02-26T10:26:52.723406+08:00 0 [Note] [MY-011825] [Xtrabackup] decompressing ./xtrabackup_tablespaces.qp
2025-02-26T10:26:52.758142+08:00 0 [Note] [MY-011825] [Xtrabackup] completed OK!
# 查看解压后的目录文件
root@k8s-node-19:/app/data# ls
backup-my.cnf        ibdata1              mysql-bin.index     sys          xhn_tax                    xtrabackup_info.qp        xtrabackup_tablespaces
backup-my.cnf.qp     ibdata1.qp           mysql-bin.index.qp  undo_001     xtrabackup_binlog_info     xtrabackup_logfile        xtrabackup_tablespaces.qp
__cdb_recycle_bin__  mysql                mysql.ibd           undo_001.qp  xtrabackup_binlog_info.qp  xtrabackup_logfile.qp
ib_buffer_pool       mysql-bin.000116     mysql.ibd.qp        undo_002     xtrabackup_checkpoints     xtrabackup_slave_info
ib_buffer_pool.qp    mysql-bin.000116.qp  performance_schema  undo_002.qp  xtrabackup_info            xtrabackup_slave_info.qp

2、备份解压出来之后,执行如下命令进行 apply log 操作。

root@k8s-node-19:/app/data# xtrabackup --prepare --target-dir=/app/data
2025-02-26T10:28:01.673279+08:00 0 [Note] [MY-011825] [Xtrabackup] recognized server arguments: --innodb_checksum_algorithm=crc32 --innodb_log_checksums=1 --innodb_data_file_path=ibdata1:12M:autoextend --innodb_log_file_size=1048576000 --innodb_page_size=16384 --innodb_undo_directory=./ --innodb_undo_tablespaces=2 --server-id=0 --innodb_log_checksums=ON --innodb_redo_log_encrypt=0 --innodb_undo_log_encrypt=0 
2025-02-26T10:28:01.673362+08:00 0 [Note] [MY-011825] [Xtrabackup] recognized client arguments: --prepare=1 --target-dir=/app/data 
xtrabackup version 8.0.35-32 based on MySQL server 8.0.35 Linux (x86_64) (revision id: c339924a)
2025-02-26T10:28:01.673373+08:00 0 [Note] [MY-011825] [Xtrabackup] cd to /app/data/
2025-02-26T10:28:01.673399+08:00 0 [Note] [MY-011825] [Xtrabackup] This target seems to be not prepared yet.
2025-02-26T10:28:01.677695+08:00 0 [Note] [MY-011825] [Xtrabackup] xtrabackup_logfile detected: size=8388608, start_lsn=(1070030577)
2025-02-26T10:28:01.678039+08:00 0 [Note] [MY-011825] [Xtrabackup] using the following InnoDB configuration for recovery:
2025-02-26T10:28:01.678057+08:00 0 [Note] [MY-011825] [Xtrabackup] innodb_data_home_dir = .
2025-02-26T10:28:01.678060+08:00 0 [Note] [MY-011825] [Xtrabackup] innodb_data_file_path = ibdata1:12M:autoextend
2025-02-26T10:28:01.678097+08:00 0 [Note] [MY-011825] [Xtrabackup] innodb_log_group_home_dir = .
2025-02-26T10:28:01.678100+08:00 0 [Note] [MY-011825] [Xtrabackup] innodb_log_files_in_group = 1
.....
2025-02-26T10:28:03.508334+08:00 0 [Note] [MY-012980] [InnoDB] Shutdown completed; log sequence number 1070038038
2025-02-26T10:28:03.510993+08:00 0 [Note] [MY-011825] [Xtrabackup] completed OK!
root@k8s-node-19:/app/data# ls
 backup-my.cnf         ibdata1         mysql-bin.000116      mysql.ibd.qp         undo_002                    xtrabackup_checkpoints   xtrabackup_slave_info
 backup-my.cnf.qp      ibdata1.qp      mysql-bin.000116.qp   performance_schema   undo_002.qp                 xtrabackup_info          xtrabackup_slave_info.qp
 __cdb_recycle_bin__   ibtmp1          mysql-bin.index       sys                  xhn_tax                     xtrabackup_info.qp       xtrabackup_tablespaces
 ib_buffer_pool       '#innodb_redo'   mysql-bin.index.qp    undo_001             xtrabackup_binlog_info      xtrabackup_logfile       xtrabackup_tablespaces.qp
 ib_buffer_pool.qp     mysql           mysql.ibd             undo_001.qp          xtrabackup_binlog_info.qp   xtrabackup_logfile.qp

3、修改配置文件

修改backup-my.cnf文件

root@k8s-node-19:/app/data# vim backup-my.cnf
# This MySQL options file was generated by innobackupex.

# The MySQL server
[mysqld]
innodb_checksum_algorithm=crc32
innodb_log_checksums=1
innodb_data_file_path=ibdata1:12M:autoextend
innodb_log_file_size=1048576000
innodb_page_size=16384
innodb_undo_directory=./
innodb_undo_tablespaces=2
innodb_log_checksums=ON
innodb_redo_log_encrypt=OFF
innodb_undo_log_encrypt=OFF
lower_case_table_names=1

4、修改目录属性

由于解压后属主和属组都是root,要给它修改成MySQL用户

root@k8s-node-19:/app/data# chown -R mysql.mysql /app/data/
root@k8s-node-19:/app/data# ll 
total 211244
-rw-r----- 1 mysql mysql       56 Feb 26 10:29  auto.cnf
-rw-r--r-- 1 mysql mysql      397 Feb 26 10:32  backup-my.cnf
-rw-r----- 1 mysql mysql      443 Feb 26 10:26  backup-my.cnf.qp
-rw-r----- 1 mysql mysql      180 Feb 26 10:30  binlog.000001
-rw-r----- 1 mysql mysql      180 Feb 26 10:32  binlog.000002
-rw-r----- 1 mysql mysql      157 Feb 26 10:32  binlog.000003
-rw-r----- 1 mysql mysql       48 Feb 26 10:32  binlog.index
-rw------- 1 mysql mysql     1705 Feb 26 10:30  ca-key.pem
-rw-r--r-- 1 mysql mysql     1112 Feb 26 10:30  ca.pem
drwxr-x--- 2 mysql mysql       37 Feb 26 10:26  __cdb_recycle_bin__/
-rw-r--r-- 1 mysql mysql     1112 Feb 26 10:30  client-cert.pem
-rw------- 1 mysql mysql     1701 Feb 26 10:30  client-key.pem
-rw-r----- 1 mysql mysql   196608 Feb 26 10:38 '#ib_16384_0.dblwr'
-rw-r----- 1 mysql mysql  8585216 Feb 26 10:30 '#ib_16384_1.dblwr'
-rw-r----- 1 mysql mysql     5517 Feb 26 10:32  ib_buffer_pool
-rw-r----- 1 mysql mysql     1015 Feb 26 10:26  ib_buffer_pool.qp
-rw-r--r-- 1 mysql mysql 12582912 Feb 26 10:38  ibdata1
-rw-r----- 1 mysql mysql   161187 Feb 26 10:26  ibdata1.qp
...

5、启动数据库

上面的操作都修改好后就可以启动数据库了

root@k8s-node-19:/app/data# mysqld_safe --defaults-file=/app/data/backup-my.cnf  --user=mysql --datadir=/app/data &
root@k8s-node-19:/app/data# ss -tnl 
State              Recv-Q             Send-Q                           Local Address:Port                            Peer Address:Port             Process             
LISTEN             0                  128                                  127.0.0.1:6010                                 0.0.0.0:*                                    
LISTEN             0                  4096                             127.0.0.53%lo:53                                   0.0.0.0:*                                    
LISTEN             0                  128                                    0.0.0.0:22                                   0.0.0.0:*                                    
LISTEN             0                  70                                           *:33060                                      *:*                                    
LISTEN             0                  128                                      [::1]:6010                                    [::]:*                                    
LISTEN             0                  128                                       [::]:22                                      [::]:*                                    
LISTEN             0                  151                                          *:3306                                       *:*                        

随后就可以用Navicat 等数据库软件连接了

相关推荐

虚拟机“播放器”:VMware Player 12.0.1下载

IT之家讯免费虚拟机软件VMwareWorkstationPlayer更新至12.0.1,本次更新为维护性更新,同今天更新的WorkstationPro版本一样,解决了某些情况下Skylake...

ZLG嵌入式笔记 | 移动硬盘和虚拟机的那些事儿

在Linux开发中,编译内核是一项常见任务,但不少开发者在移动硬盘或虚拟机环境下尝试时会遭遇失败。本文将简要探讨这些问题的成因,并介绍一些虚拟机使用技巧,帮助大家更好地应对相关问题。在移动硬盘里编译...

IT 技术小课堂「虚拟机」

你知道什么是虚拟机吗?虚拟机就是可以用一台电脑变成多台电脑的计算机魔法。推荐上手最快的虚拟机软件VMwareWorkstation。如果你的物理机是Win10以下的可以安装VMwareWo...

最简单的虚拟机安装黑苹果

给所有还没体验过Macos的人们。本文会以最简单的方式介绍虚拟机安装黑苹果的教程,准备四个工具,一次性安装黑苹果,中间基本不会有出现任何问题。一、准备工具,先把以下工具都准备好,网上搜索即可。1、...

VMware虚拟机与物理机文件交互,这8种方法送给你

上一期我们讲解过用VMWare虚拟机打造一个金刚不坏之身的操作系统,小伙伴们反应说方法不错,只是虚拟机与物理机文件交互方式不好操作,这期应小伙伴们的要求,给大家讲解下虚拟机与物理机文件交互的8种方式,...

VMware虚拟机:第二节安装VMware并创建Win10虚拟机

VMware虚拟机:第二节安装VMware并创建Windows10虚拟机本节我们介绍如何安装VMwareWorkStationPro17.6.2,以及利用VMware创建Windows10虚拟机。...

vSphere环境虚拟机安装VMware Tools(Windows操作系统)

本文描述Windows操作系统的VMwareTools安装。Windows操作系统的VMwareTools安装操作步骤Windows操作系统安装完成后,VMwarevSphere控制台会提示“该...

windows7虚拟机VMware tools报错——一招解决

前言最近,把VMwareworkstation更新到版本17后,在一次安装windows7虚拟机的时候,发现VMwaretools各种安装报错,安装无法正常进行,这可愁坏了我,让我把自己本来不多的...

博通紧急修复VMware Tools高危认证绕过漏洞

IT之家3月26日消息,博通昨日(3月25日)发布安全公告,修复VMwareToolsforWindows中存在的高危认证绕过漏洞(CVE-2025-22230)。该漏洞由俄罗...

linux中如何安装VMwaretools--一定要保存

安装好VMware后,有时候我想把真机的文件放到虚拟机了,除了使用远程工具外,也会用到VMwaretools,今天给大家说说如何安装VMwaretools。其实在网上也可以搜到,只是网上的东西太乱了。...

精品博文解决安装Ubuntu14.04不弹出图像界面的问题

今天遇到一个糗事,不写出来不足以解恨。暑期开始了,给一个辅导班做嵌入式系统的讲座。问题就出在讲完以后系统的安装这个环境。班里面好些同学安装好以后进入不了图像界面,举手问我啊,oh,mygod!就到了这...

虚拟机VMware Workstation 17教程,从入门到精通No.3-vmwaretools

安装VMwareTools是创建新的虚拟机过程的一部分,而升级VMwareTools是使虚拟机更易于使用的关键。尽管客户机操作系统在未安装VMwareTools的情况下仍可运行,但许...

国产操作系统虚拟机安装vm-tools

使用vmwareworkstation安装国产化操作系统虚拟机后,不会像windows一样自动安装vmtools工具,按vmware默认的由虚拟机菜单中安装的方法步骤比较复杂,且最终结果不理想,经实...

VmwareTools的安装 这个功能很强大 #计算机

vmwareTools的安装。现在装完之后,这么点看起来也别扭,怎么办?装工具:vmwaretools。·在这个地方选择虚拟机,虚拟机这里边有安装vmwaretools,安装这个工具点击一下。·下边这...

VMware Tools安装失败?手把手教你本地安装

VMwareTools安装失败?作为虚拟机与物理机之间的"桥梁",VMwareTools能实现文件拖拽、剪贴板共享、分辨率自适应等关键功能。没有它,VMware虚拟机就变得非常不好...

取消回复欢迎 发表评论: