恢复简介 这里采用的是MySQL 8.0版本,腾讯云云上数据库备份默认是qpress结合 Percona Xtrabackup 备份的。新找台空闲服务器来操作,这里演示的服务器系统为Ubuntu 22.04 LTS版本。需要提前安装好MySQL 8.0以上的数据库版本、Percona Xtrabackup和qpress
数据库安装 这里采用脚本的方式来安装数据库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 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 $" OK " elif [ $2 = "failure" -o $2 = "1" ] ;then ${SETCOLOR_FAILURE} echo -n $"FAILED" else ${SETCOLOR_WARNING} echo -n $"WARNING" fi ${SETCOLOR_NORMAL} echo -n "]" echo } install() { softpath="/app/soft" mysqlversion="mysql-8.0.39-linux-glibc2.28-x86_64.tar.xz" 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/my data/{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 /u sr/local/my sql/ cat >> /etc/my .cnf <<EOF [client] port = 3306 socket = /tmp/my sql.sock [mysqld] server_id=1 port = 3306 user = mysql character-set-server = utf8 default_storage_engine = innodb log_timestamps = SYSTEM socket = /tmp/my sql.sock basedir = /usr/ local/mysql datadir = /app/my data/data pid-file = /app/my data/data/my sql.pid max_connections = 10000 max_connect_errors = 1000 table_open_cache = 1024 max_allowed_packet = 128 M open_files_limit = 65535 log_bin = mysql-bin binlog_format = MIXED sort_buffer_size = 16 M join_buffer_size = 16 M skip_ssl innodb_buffer_pool_size = 1024 M 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 = 512 M innodb_log_files_in_group = 2 innodb_log_buffer_size = 16 M innodb_max_dirty_pages_pct = 80 innodb_rollback_on_timeout = on innodb_lock_wait_timeout = 28800 innodb_data_file_path=ibdata1:1024 M:autoextend innodb_undo_tablespaces=3 lower_case_table_names = 1 skip_name_resolve = on log_error = /app/my data/logs/my sql-error.log slow_query_log = 1 long_query_time = 20 log-queries-not-using-indexes slow_query_log_file = /app/my data/logs/my sql-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=/u sr/local/my sql --datadir=/app/my data/data --explicit_defaults_for_timestamp && \ cp support-files/mysql.server / etc/init.d/my sqld && echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/ profile.d/mysql.sh && \ source /etc/ profile.d/mysql.sh /etc/i nit.d/mysqld start } install
安装qpress 1 2 root@k8s -node-19 :/app/soft root@k8s -node-19 :/app/soft
安装Percona XtraBackup 8.0 1 2 3 4 5 6 7 8 9 10 root@k8s -node-19 :/app/soft root@k8s -node-19 :/app/soft root@k8s -node-19 :/app/soft root@k8s -node-19 :/app/soft root@k8s -node-19 :/app/soft root@k8s -node-19 :/app/soft root@k8s -node-19 :/app/soft root@k8s -node-19 :/app/soft 2025 -02 -26 T11: 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=1024 M --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=512 M --innodb_log_files_in_group=2 --innodb_log_buffer_size=16 M --innodb_max_dirty_pages_pct=80 --innodb_data_file_path=ibdata1: 1024 M: 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、创建新目录
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 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 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 操作。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 root@k8s-node-19 :/app/data# xtrabackup --prepare --target-dir=/app/data 2025-02 -26 T10: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 -26 T10: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 -26 T10:28:01.673373+08 :00 0 [Note] [MY-011825 ] [Xtrabackup] cd to /app/data/ 2025-02 -26 T10:28:01.673399+08 :00 0 [Note] [MY-011825 ] [Xtrabackup] This target seems to be not prepared yet. 2025-02 -26 T10:28:01.677695+08 :00 0 [Note] [MY-011825 ] [Xtrabackup] xtrabackup_logfile detected: size=8388608, start_lsn=(1070030577) 2025-02 -26 T10:28:01.678039+08 :00 0 [Note] [MY-011825 ] [Xtrabackup] using the following InnoDB configuration for recovery: 2025-02 -26 T10:28:01.678057+08 :00 0 [Note] [MY-011825 ] [Xtrabackup] innodb_data_home_dir = . 2025-02 -26 T10:28:01.678060+08 :00 0 [Note] [MY-011825 ] [Xtrabackup] innodb_data_file_path = ibdata1:12M:autoextend 2025-02 -26 T10:28:01.678097+08 :00 0 [Note] [MY-011825 ] [Xtrabackup] innodb_log_group_home_dir = . 2025-02 -26 T10:28:01.678100+08 :00 0 [Note] [MY-011825 ] [Xtrabackup] innodb_log_files_in_group = 1 ..... 2025-02 -26 T10:28:03.508334+08 :00 0 [Note] [MY-012980 ] [InnoDB] Shutdown completed; log sequence number 1070038038 2025-02 -26 T10: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文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 root@k8s-node-19:/app/data# vim backup-my.cnf [mysqld]innodb_checksum_algorithm =crc32innodb_log_checksums =1innodb_data_file_path =ibdata1:12M:autoextendinnodb_log_file_size =1048576000innodb_page_size =16384innodb_undo_directory =./innodb_undo_tablespaces =2innodb_log_checksums =ONinnodb_redo_log_encrypt =OFFinnodb_undo_log_encrypt =OFFlower_case_table_names =1
4、修改目录属性
由于解压后属主和属组都是root,要给它修改成MySQL用户
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 root@k8s-node-19:/app/data root@k8s-node-19:/app/data 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 ' -rw-r----- 1 mysql mysql 8585216 Feb 26 10:30 ' -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、启动数据库
上面的操作都修改好后就可以启动数据库了
1 2 3 4 5 6 7 8 9 10 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 等数据库软件连接了