mysql入坑之路(4)阿里云RDS数据备份本地恢复

参考链接
将阿里云RDS备份的qe.xb后缀文件部署到本地服务器

操作系统:Centos7
数据库:mysql5.6

因为RDS是5.6版本,所以本地安装mysql5.6(配合阿里云上的RDS数据库版本,这里我是重新搭建的数据库,没有数据)
(此文件要在mysql的安装目录下,一般是/var/lib/mysql,这里我的数据存放路径保持默认)

一、下载RDS备份

rds备份下载参考:云产品学习之路(阿里云01)阿里云rds数据库导出

二、安装mysql数据库

mysql5.6(配合阿里云上的RDS数据库版本,这里我是重新搭建的数据库,没有数据)

rpm -ivh http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm 
yum -y install mysql-server mysql

三、本地进行恢复

3.1、安装qpress

参考连接:
https://blog.csdn.net/xfxfxfxfxf666/article/details/100602936
https://www.cnblogs.com/muzlei/p/16555163.html

innobackupex对xtrabackup的封装,是percona推出的可靠物理备份工具,对于mysql,逻辑备份可以使用mysqldump、mysqlpump、mysqldumper,物理备份可以直接使用innobackupex。
我这里为了方便,这里使用centos7.x的yum安装(如果感兴趣可以到官网下载linux generc版本,直接解压就能使用)

[root@centos7 ~]# yum -y install https://repo.percona.com/yum/percona-release-latest.noarch.rpm
[root@centos7 ~]# yum -y install percona-xtrabackup-24  
[root@centos7 ~]# yum -y install qpress  ##innobackupex支持压缩导出,这里安装压缩工具
 
[root@centos7 ~]# innobackupex -version  ###如果出现如下提示表示安装成功,我这里2.4.15
xtrabackup: recognized server arguments: --datadir=/var/lib/mysql 
innobackupex version 2.4.26 Linux (x86_64) (revision id: 19de43b)


3.2、对于_qp.zb文件的处理:

1. 解包:
#示例: cat (此处省略)_qp.xb | xbstream -x -v -C /var/lib/mysql
 cat hins22182444_data_20220805000612_qp.xb  | xbstream -x -v -C /var/lib/mysql

2.解压:
#将文件中的.qp文件解压,这里就需要提前装好qpress
   innobackupex --decompress --remove-original  /var/lib/mysql
#输出:
...省略
220805 18:05:13 completed OK!

查看一下/data/mysql/data文件


ls -l /var/lib/mysql  
  执行完之后可以看到备份文件已经都在里面了
ls -l /var/lib/mysql 输出:
[root@localhost mysql]# ls -l /var/lib/mysql  
总用量 209776
-rw-r--r--. 1 root root       481 8月   5 18:05 backup-my.cnf
drwxr-x---. 2 root root     20480 8月   5 18:05 bpmx
drwxr-x---. 2 root root      8192 8月   5 18:05 customer
drwxr-x---. 2 root root      4096 8月   5 18:05 dyhr
-rw-r--r--. 1 root root 209715200 8月   5 18:05 ibdata1
drwxr-x---. 2 root root      4096 8月   5 18:05 imsad
drwxr-x---. 2 root root      4096 8月   5 18:05 imsec
drwxr-x---. 2 root root     12288 8月   5 18:05 imshr
drwxr-x---. 2 root root      4096 8月   5 18:05 imsmm
drwxr-x---. 2 root root     12288 8月   5 18:05 imsoa
drwxr-x---. 2 root root      4096 8月   5 18:05 imspmp
drwxr-x---. 2 root root      4096 8月   5 18:05 imsrun
-rw-r--r--. 1 root root   4565747 8月   5 18:05 log000000000024.tokulog29
drwxr-x---. 2 root root      4096 8月   5 18:05 mysql
drwxr-x---. 2 root root      4096 8月   5 18:05 performance_schema
drwxr-x---. 2 root root        74 8月   5 18:05 plancp
drwxr-x---. 2 root root      8192 8月   5 18:05 project
-rw-r-----. 1 root root    333372 8月   5 18:04 rds_table_info_json_120218.log
drwxr-x---. 2 root root        20 8月   5 18:05 test
-rw-r--r--. 1 root root     16384 8月   5 18:05 tokudb.directory
-rw-r--r--. 1 root root     16384 8月   5 18:05 tokudb.environment
-rw-r--r--. 1 root root     16384 8月   5 18:05 tokudb.rollback
-rw-r--r--. 1 root root       166 8月   5 18:05 xtrabackup_binlog_info
-rw-r-----. 1 root root       119 8月   5 18:04 xtrabackup_checkpoints
-rw-r--r--. 1 root root       924 8月   5 18:05 xtrabackup_info
-rw-r--r--. 1 root root      3584 8月   5 18:05 xtrabackup_logfile
-rw-r--r--. 1 root root        78 8月   5 18:05 xtrabackup_slave_filename_info
-rw-r--r--. 1 root root       209 8月   5 18:05 xtrabackup_slave_info

3.3、恢复解压好的备份文件

innobackupex --defaults-file=/var/lib/mysql/backup-my.cnf --apply-log /var/lib/mysql
#输出:
...省略
220805 18:10:29 completed OK!

3.4、修改配置文件(使用backup-my.cnf文件启动失败待测试)

1.vim /home/mysql/data/backup-my.cnf
将一些功能注释掉:
#innodb_log_checksum_algorithm
#innodb_fast_checksum
#innodb_log_block_size
#innodb_doublewrite_file
#rds_encrypt_data
#innodb_encrypt_algorithm
#redo_log_version
#master_key_id
添加一些功能:
[mysqld]
datadir=/var/lib/mysql
port=3306
server_id=3
socket=/var/lib/mysql/mysqld.sock
max_allowed_packet=32M
log-error=/var/lib/mysql/error.log
default-storage-engine=INNODB
[mysqld_safe]
socket=/var/lib/mysql/mysql.sock
#mysqld.log和mysqld.pid加不加都行
log-error=/var/log/mysqld.log   //加不加都行
pid-file=/var/run/mysqld/mysqld.pid   //加不加都行
skip-grant-tables     //跳过登陆,以便需要密码

3.5、注意权限

chown -R mysql:mysql /var/lib/mysql

3.6、启动mysql进程

使用systemctl启动管理

systemctl restart mysqld
使用的配置文件是:/etc/my.cnf

使用mysqld_safe命令启动,正确的mysql路径进行启动,

[root@localhost ~]# find / -name mysqld_safe
/usr/bin/mysqld_safe
[root@localhost ~]# /usr/bin/mysqld_safe  --defaults-file=/etc/my.cnf & //将配置文件指定好就可以了

mysqld_safe指定backup-my.cnf文件,配置文件参考3.4部分配置文件

mysqld_safe --defaults-file=/var/lib/mysql/backup-my.cnf --user=mysql --datadir=/var/lib/mysql &
mysqld_safe指定backup-my.cnf文件启动失败的输出(使用3.4部分配置文件就不会报错)
启动报错
[root@localhost ~]# mysqld_safe --defaults-file=/var/lib/mysql/backup-my.cnf --user=mysql --datadir=/var/lib/mysql &
[1] 66093
[root@localhost ~]# 220805 18:25:28 mysqld_safe Logging to '/var/lib/mysql/localhost.localdomain.err'.
220805 18:25:29 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
220805 18:25:31 mysqld_safe mysqld from pid file /var/lib/mysql/mysql.pid ended

[1]+  完成                  mysqld_safe --defaults-file=/var/lib/mysql/backup-my.cnf --user=mysql --datadir=/var/lib/mysql


#日志/var/lib/mysql/localhost.localdomain.err
[root@localhost ~]# tailf -n 200  /var/lib/mysql/localhost.localdomain.err
2022-08-05 18:25:29 66245 [Note] Plugin 'FEDERATED' is disabled.
2022-08-05 18:25:29 66245 [Note] InnoDB: Using atomics to ref count buffer pool pages
2022-08-05 18:25:29 66245 [Note] InnoDB: The InnoDB memory heap is disabled
2022-08-05 18:25:29 66245 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2022-08-05 18:25:29 66245 [Note] InnoDB: Memory barrier is not used
2022-08-05 18:25:29 66245 [Note] InnoDB: Compressed tables use zlib 1.2.11
2022-08-05 18:25:29 66245 [Note] InnoDB: Using Linux native AIO
2022-08-05 18:25:29 66245 [Note] InnoDB: Using CPU crc32 instructions
2022-08-05 18:25:29 66245 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2022-08-05 18:25:29 66245 [Note] InnoDB: Completed initialization of buffer pool
2022-08-05 18:25:29 66245 [Note] InnoDB: Highest supported file format is Barracuda.
2022-08-05 18:25:29 66245 [Note] InnoDB: 128 rollback segment(s) are active.
2022-08-05 18:25:29 66245 [Note] InnoDB: Waiting for purge to start
2022-08-05 18:25:29 66245 [Note] InnoDB: 5.6.51 started; log sequence number 5812851242
2022-08-05 18:25:29 66245 [ERROR] Aborting

2022-08-05 18:25:29 66245 [Note] Binlog end
2022-08-05 18:25:29 66245 [Note] Shutting down plugin 'partition'
2022-08-05 18:25:29 66245 [Note] Shutting down plugin 'PERFORMANCE_SCHEMA'
2022-08-05 18:25:29 66245 [Note] Shutting down plugin 'INNODB_SYS_DATAFILES'
2022-08-05 18:25:29 66245 [Note] Shutting down plugin 'INNODB_SYS_TABLESPACES'
2022-08-05 18:25:29 66245 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN_COLS'
2022-08-05 18:25:29 66245 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN'
2022-08-05 18:25:29 66245 [Note] Shutting down plugin 'INNODB_SYS_FIELDS'
2022-08-05 18:25:29 66245 [Note] Shutting down plugin 'INNODB_SYS_COLUMNS'
2022-08-05 18:25:29 66245 [Note] Shutting down plugin 'INNODB_SYS_INDEXES'
2022-08-05 18:25:29 66245 [Note] Shutting down plugin 'INNODB_SYS_TABLESTATS'
2022-08-05 18:25:29 66245 [Note] Shutting down plugin 'INNODB_SYS_TABLES'
2022-08-05 18:25:29 66245 [Note] Shutting down plugin 'INNODB_FT_INDEX_TABLE'
2022-08-05 18:25:29 66245 [Note] Shutting down plugin 'INNODB_FT_INDEX_CACHE'
2022-08-05 18:25:29 66245 [Note] Shutting down plugin 'INNODB_FT_CONFIG'
2022-08-05 18:25:29 66245 [Note] Shutting down plugin 'INNODB_FT_BEING_DELETED'
2022-08-05 18:25:29 66245 [Note] Shutting down plugin 'INNODB_FT_DELETED'
2022-08-05 18:25:29 66245 [Note] Shutting down plugin 'INNODB_FT_DEFAULT_STOPWORD'
2022-08-05 18:25:29 66245 [Note] Shutting down plugin 'INNODB_METRICS'
2022-08-05 18:25:29 66245 [Note] Shutting down plugin 'INNODB_BUFFER_POOL_STATS'
2022-08-05 18:25:29 66245 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE_LRU'
2022-08-05 18:25:29 66245 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE'
2022-08-05 18:25:29 66245 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX_RESET'
2022-08-05 18:25:29 66245 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX'
2022-08-05 18:25:29 66245 [Note] Shutting down plugin 'INNODB_CMPMEM_RESET'
2022-08-05 18:25:29 66245 [Note] Shutting down plugin 'INNODB_CMPMEM'
2022-08-05 18:25:29 66245 [Note] Shutting down plugin 'INNODB_CMP_RESET'
2022-08-05 18:25:29 66245 [Note] Shutting down plugin 'INNODB_CMP'
2022-08-05 18:25:29 66245 [Note] Shutting down plugin 'INNODB_LOCK_WAITS'
2022-08-05 18:25:29 66245 [Note] Shutting down plugin 'INNODB_LOCKS'
2022-08-05 18:25:29 66245 [Note] Shutting down plugin 'INNODB_TRX'
2022-08-05 18:25:29 66245 [Note] Shutting down plugin 'InnoDB'
2022-08-05 18:25:29 66245 [Note] InnoDB: FTS optimize thread exiting.
2022-08-05 18:25:29 66245 [Note] InnoDB: Starting shutdown...
2022-08-05 18:25:31 66245 [Note] InnoDB: Shutdown completed; log sequence number 5812851252
2022-08-05 18:25:31 66245 [Note] Shutting down plugin 'BLACKHOLE'
2022-08-05 18:25:31 66245 [Note] Shutting down plugin 'ARCHIVE'
2022-08-05 18:25:31 66245 [Note] Shutting down plugin 'MRG_MYISAM'
2022-08-05 18:25:31 66245 [Note] Shutting down plugin 'MyISAM'
2022-08-05 18:25:31 66245 [Note] Shutting down plugin 'MEMORY'
2022-08-05 18:25:31 66245 [Note] Shutting down plugin 'CSV'
2022-08-05 18:25:31 66245 [Note] Shutting down plugin 'sha256_password'
2022-08-05 18:25:31 66245 [Note] Shutting down plugin 'mysql_old_password'
2022-08-05 18:25:31 66245 [Note] Shutting down plugin 'mysql_native_password'
2022-08-05 18:25:31 66245 [Note] Shutting down plugin 'binlog'
2022-08-05 18:25:31 66245 [Note] 

3.7、登陆mysql

mysql -uroot -p123456

3.8、这里如果出现了root账号没有权限登陆:(参考部分可以忽略)

1.关闭mysq进程: ps -aux mysql;kill -9 mysql进程号
2.使用跳过权限登录mysql
   /usr/local/mysql/bin/mysqld_safe --defaults-file=/home/mysql/data/backup-my.cnf --skip-grant-tables &
3.直接使用 mysql 就可以登陆了
4.使用 mysqldump -uroot -p123456 --databases mytest > mytest.sql  //将mysql里面的mytest库导成,sql文件
5.重复步骤1,关闭mysql进程
6.使用正确的mysql路径进行启动, 
[root@localhost ~]# find / -name mysqld_safe
/usr/bin/mysqld_safe
[root@localhost ~]# /usr/bin/mysqld_safe  --defaults-file=/etc/my.cnf & //将配置文件指定好就可以了
7.启动完成之后,mysql -uroot -p123456 < mytest.sql就可以将表导入到本地数据库了
8.启动mysql:mysql -uroot -p123456就可以正常登陆了
posted @ 2022-08-05 18:58  muzlei  阅读(612)  评论(0编辑  收藏  举报