• RDS mysql版本为5.6.29 x86_64

1、下载数据备份、binlog备份

  • 内网中转?数据量不大,直接下载
  • 下载数据备份(外网下载链接) wget -c "https://rdslog-st.oss-cn-shenzhen.aliyuncs.com/xxx" -O xxx.tar.gz

2、内网测试机安装mysql5.6

  • wget -c "http://cdn.mysql.com//Downloads/MySQL-5.6/mysql-5.6.33-linux-glibc2.5-x86_64.tar.gz"
    tar xf mysql-5.6.-linux-glibc2.-x86_64.tar.gz -C /usr/local/
    cd /usr/local && ln -s mysql-5.6.-linux-glibc2.-x86_64 mysql
    mkdir -p /home/mysql/data
    chown -R mysql.mysql /home/mysql && chmod -R o=--- /home/mysql

3、恢复备份到内网测试机

  • 下载解压脚本 rds_backup_extract.sh
  • 下载恢复工具 Percona-XtraBackup
  • sh rds_backup_extract.sh -f hins1438123_xtra_20160912032142.tar.gz -C /home/mysql/data
    /home/backup/percona-xtrabackup-2.4.-Linux-x86_64/bin/innobackupex --defaults-file=/home/mysql/data/backup-my.cnf --apply-log /home/mysql/data
  • 编辑mysql配置文件,开启gtid
cd /home/mysql/data
cp backup-my.cnf slave-my.cnf
vim slave-my.cnf
[mysqld]
# from rds backup-my.cnf
innodb_checksum_algorithm=innodb
innodb_data_file_path=ibdata1:200M:autoextend
innodb_log_files_in_group=2
innodb_log_file_size=524288000
innodb_undo_directory=.
innodb_undo_tablespaces=0
# need for slave
server-id = 17
master-info-repository = file
relay-log-info_repository = file
binlog-format = ROW
gtid-mode = ON
enforce-gtid-consistency = true
log-bin = hostname-bin
relay-log = hostname-relay-bin
log-slave-updates

  • 启动mysqld

/usr/local/mysql/bin/mysqld_safe --defaults-file=/home/mysql/data/slave-my.cnf --user=mysql --basedir=/usr/local/mysql --datadir=/home/mysql/data &

  • 清除slave信息
sql> reset slave;
# 报错
ERROR 1794 (HY000): Slave is not configured or failed to initialize properly. You must at least set --server-id to enable either a master or a slave. Additional error messages can be found in the MySQL error log.
# 原因是由于RDS的备份文件中包含了RDS的主从复制关系,需要把这些主从复制关系清理掉,清理方法:
sql> truncate table mysql.slave_relay_log_info;
sql> truncate table mysql.slave_master_info;
# 然后重启服务
/usr/local/mysql/bin/mysqladmin -S /tmp/mysql.sock -uroot shutdown
  • 恢复完成的mysql.user 是不包含rds中创建的用户的,需要重新创建
  • 重建用户前先执行一下sql

delete from mysql.db where user<>'root' and char_length(user)>0;
delete from mysql.tables_priv where user<>'root' and char_length(user)>0;
flush privileges;


4、配置主从同步

  • 尝试 master_auto_position=1 让主从自行寻找开始复制的pos

sql> change master to
master_host=’rdsxfjwiofjwofe.mysql.rds.aliyuncs.com’,
master_user=’user_name’,master_port=3306,master_password=’xxxxx’,
master_auto_position=1;
# 结果 start slave 报错
Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'The slave is connecting using CHANGE MASTER TO MASTER_AUTO_POSITION = 1, but the master has purged binary logs containing GTIDs that the slave requires.'

  • 上面的报错有两种解决方法:

    • 设置 master_auto_position=0 并指定 binlog 文件和 pos 位置开始复制
# binlog pos 可以从恢复的备份文件中获得
cat xtrabackup_slave_filename_info
CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000348', MASTER_LOG_POS='5569205'

  

# 配置复制 
sql> change master to
master_host=’rdsxdjsfiwfojief.mysql.rds.aliyuncs.com’,
master_user=’user_name’,master_port=3306,master_password=’xxxxx’,
master_auto_position=0,master_log_file='mysql-bin.000348',master_log_pos=5569205;
sql> start slave;

# 可以正常开始复制,但重新设置 master_auto_position=1 又会报同样的错
* 手工修改 GTID_PURGED 值
# 同样的备份文件里有信息
cat /home/mysql/data/xtrabackup_slave_info
SET GLOBAL gtid_purged='016ced19-9d47-11e5-8f1b-1051721bd1ff:1-1875724, 068bb241-3474-11e6-a8e5-8038bc0c695e:1-1858400, 0e1a732e-9d47-11e5-8f1b-d89d672b932c:1-4';
CHANGE MASTER TO MASTER_AUTO_POSITION=

# 配置主从
sql> change master to

master_host=’rdsxfslfiewfiewfji.mysql.rds.aliyuncs.com’,
master_user=’user_name’,master_port=3306,master_password=’xxxxx’,
master_auto_position=1;
sql> start slave; # 报错
sql> stop slave;
sql> reset master;
sql> SET GLOBAL gtid_purged='016ced19-9d47-11e5-8f1b-1051721bd1ff:1-1875724, 068bb241-3474-11e6-a8e5-8038bc0c695e:1-1858400, 0e1a732e-9d47-11e5-8f1b-d89d672b932c:1-4';
sql> start slave;

5、最后看下slave状态
一开始 Seconds_Behind_Master 值会很大,是因为使用的数据备份是前一天的,等它慢慢同步完就正常了

mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: rdsxxxxxxxxxxxxx.mysql.rds.aliyuncs.com
Master_User: user_name
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000350
Read_Master_Log_Pos: 1717466
Relay_Log_File: hostname-relay-bin.000006
Relay_Log_Pos: 1717596
Relay_Master_Log_File: mysql-bin.000350
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 1717466
Relay_Log_Space: 1717886
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 2224019206
Master_UUID: 068bb241-3474-11e6-a8e5-8038bc0c695e
Master_Info_File: /home/mysql/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 068bb241-3474-11e6-a8e5-8038bc0c695e:1858401-1875134
Executed_Gtid_Set: 016ced19-9d47-11e5-8f1b-1051721bd1ff:1-1875724,
068bb241-3474-11e6-a8e5-8038bc0c695e:1-1875134,
0e1a732e-9d47-11e5-8f1b-d89d672b932c:1-4
Auto_Position: 1
1 row in set (0.00 sec) ERROR:
No query specified mysql>

作者:Eraz
链接:https://www.jianshu.com/p/4582db124579
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

阿里云RDS同步到本地自建mysql数据库从库的更多相关文章

  1. 使用阿里云RDS for SQL Server性能洞察优化数据库负载-初识性能洞察

    简介 数据库性能调优通常需要较高数据库水平,并伴随较多的前期准备工作,比如收集各种性能基线.不同种类的性能指标.慢SQL日志等,这通常费时费力且效果一般,当面对多个数据库时总体拥有成本会大幅增加.今天 ...

  2. 阿里云RDS备份在本地mysql快速还原

    本地准备: ##安装和RDS相同的mysql版本,拿mysql5.6为例 http://www.cnblogs.com/37yan/p/7513605.html ##安装Xtrabackup 包 cd ...

  3. 阿里云RDS数据库备份同步到自建库方法(SHELL脚本)

    一.背景: 由于阿里云RDS生产库每天都需要备份且拷贝到自建读库,而如果使用阿里云的自动拷贝到只读实例, 费用太高, 故采用自编写同步脚本方法实现. 二.前提: 1). 已开通阿里云RDS, 且开启定 ...

  4. 阿里云RDS备份 恢复到本地

    目录 一.恢复准备 二.具体操作 一.恢复准备 阿里云RDS默认配置了全备份+binlog,可以精准恢复到某个时间点上. 可以下载备份的包到本地,进行本地恢复,要预留好本地的数据库容量和cpu等规格, ...

  5. 阿里云 RDS for MySQL 物理备份文件恢复到自建数据库

    想把阿里云的Mysql 生成的RAS 文件.tar文件 恢复到本地自建mysql, 遇到的坑.希望帮助大家 阿里云提供的地址 https://help.aliyun.com/knowledge_det ...

  6. 恢复阿里云RDS云数据库MySQL的备份文件到自建数据库

    云数据库MySQL版使用开源软件Percona Xtrabackup对数据库进行备份,所以您可以使用该软件将云数据库MySQL的备份文件恢复到自建数据库中,本文将介绍详细的操作步骤. 关于云数据库My ...

  7. 阿里云RDS SQL Server 2008 R2 使用本地SQL备份文件还原全过程

    最近公司准备全面转向阿里云,写了好几个方案,最终决定购买一台ECS和一台RDS搭配使用.开始对阿里的RDS产品陌生,加上公司的数据库文件近20G,诸多担心,生怕产品买来了不能用,给公司造成损失.后来联 ...

  8. MongoDB自建和阿里云RDS备份还原

    MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功 ...

  9. 阿里云RDS数据库改造迁移方案

    1. 改造原因 (1) 由于历史原因, 本应该是同一个库的表分布在两个数据库中,需要对这两个库进行合并. (2) 已有的数据库性能无法满足业务的增长需要, 查询卡,慢问题突出. (3) 当前自建Mys ...

随机推荐

  1. URAL 1993 This cheeseburger you don't need 模拟题

    This cheeseburger you don't need 题目连接: http://acm.timus.ru/problem.aspx?space=1&num=1993 Descrip ...

  2. 典型案例收集-使用OpenVPN连通多个机房内网(转)(静态路由)

    说明: 1.这篇文章主要是使用静态路由表实现的多个机房通过VPN连接后的子网机房互通. 2.OpenVPN使用的是桥接模式(server-bridge和dev tap),这个是关键点,只有这样设置才可 ...

  3. JMX操作实例--做一回技术控

    我来做一回技术控,这部分内容也是简单的API调用例子而已,做一回技术控,发点小骚文,不过你看了,也许知道JConsole是怎么做出来的了,呵呵! 先不用管他干什么,代码运行后,自己改改自然知道做什么的 ...

  4. iOS开发读取plist文件、iphone中plist文件的

    在Xcode中建立一个iOS项目后,会自己产生一个.plist文件,点击时会看见它显示的是类似于excel表格: 但是,如果打开方式选择Source Code,你会看见它其实是一个xml文件. 我们会 ...

  5. Unused port adds a PWM/analog channel to a microcontroller

    Low-cost, 8-bit, single-chip microcontrollers are stingy when it comes to on-chip PWM (pulse-width-m ...

  6. Handling Errors and Exceptions

    http://delphi.about.com/od/objectpascalide/a/errorexception.htm Unfortunately, building applications ...

  7. CC1150 针对低功耗无线应用设计的高度集成多通道射频发送器

    Low Power Sub-1 GHz RF Transmitter 单片低成本低能耗 RF 发送芯片 应用 极低功率 UHF 无线发送器 315/433/868 和 915MHz ISM/SRD 波 ...

  8. head first---------facade design pattern

    head first----------外观模式或者门面模式         外观模式又名门面模式:提供了一个统一的接口,用来访问子系统中的一群接口.外观模式定义了一个高层接口,从而让子系统更容易使用 ...

  9. NHibernate使用无状态Sessions

    NHibernate 3.0 Cookbook第三章,Using stateless sessions的翻译. 当要处理大量的数据,你通常可能会使用更"底层"的API来改善性能,在 ...

  10. Unity5.0与Android交互

    1. 目标 1) Unity3D可调用Android Java函数(在.jar中) 2) Java可调用Unity3D函数 3) Unity3D可调用Android C函数(在.so中) 2. 测试环 ...