RDS mysql 与ECS自建mysql做主从备份

由于公司要组建一个数据中心,简而言之就是把各个地方的数据都同步到一个地方,做BI建模和数据分析。

一般来说这种需求是由hadoop来实现的,但由于预算不够。。所以,来个low点的办法吧

以下主要是讲rds与mysql主从的搭建

通常来讲,mysql主从分为binlog主从,gtid模式的主从,我这篇主要是讲GTID的主从部署模式(当然里面也是需要binlog的)

1、在ECS上搭建mysql(这块不需要说多少了吧,配置好第三方yum源,直接yum installvim /etc/my.cnf

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
116
117
118
[client]
mysqladmin=/usr/bin/mysqladmin
port            = 13307                #定义端口
default-character-set=utf8               #定义数据库字符集
 
socket  = /data/var/run/mysqld/mysqld13307.sock    #定义sock文件,如果是多实例,可以直接mysql -S 指定sock文件链接数据库
 
[mysqld]
port            = 13307          #端口
skip-external-locking            #下面的配置我也不是很懂,大体都是优化配置的地方,可以直接复制我的配置文件
key_buffer_size = 256M
max_allowed_packet = 200M
table_open_cache = 40000
table_definition_cache = 40000
sort_buffer_size = 20M
net_buffer_length = 102400
read_buffer_size = 20M
read_rnd_buffer_size = 32M
bulk_insert_buffer_size = 50M
myisam_sort_buffer_size = 8M
max_connections = 2500
max_tmp_tables = 3200
lower_case_table_names = 1
thread_cache_size=64
query_cache_size=200M
query_cache_limit = 10M
join_buffer_size=20M
character-set-server=utf8
max_heap_table_size = 64M
thread_cache_size = 20
thread_concurrency = 32
transaction_isolation = READ-COMMITTED
back_log = 600
skip-name-resolve
open_files_limit = 250000
max_heap_table_size = 512G
tmp_table_size = 1G
server-id = 4            #注意,这里的serverid 不能是1,因为默认master节点的serverid是1,因此后面的id,就是不能为1的任何数字
 
gtid_mode=ON              #gtid模式要开启
log_slave_updates=true         #要开启 
enforce-gtid-consistency=true    #必须写
binlog_format=row            #一般来说格式都是row
sync-master-info=1          #写1就可以
 
innodb_buffer_pool_size = 3G
innodb_log_file_size=1G
innodb_flush_log_at_trx_commit=2
innodb_write_io_threads = 16
innodb_file_io_threads=4
innodb_read_io_threads = 16
innodb_log_buffer_size = 20M
innodb_max_dirty_pages_pct = 80
innodb_lock_wait_timeout = 500
innodb_flush_method = O_DIRECT
innodb_io_capacity=2000
innodb_io_capacity_max=6000
innodb_lru_scan_depth=2000
innodb_thread_concurrency = 0
innodb_additional_mem_pool_size=16M
innodb_autoinc_lock_mode = 2
innodb_file_per_table = 1
innodb_buffer_pool_instances=2
innodb_open_files = 2048
wait_timeout=28800
interactive_timeout= 600
 
#从下面开始配置就不需要怎么解释了吧,会运维的都明白,和上面也有一些重复的地方               
#basedir = /usr
#password       = your_password
# Here follows entries for some specific programs
basedir = /usr
datadir = /data/mysql_data13307
log-error = /data/logs/mysql13307_error.log
pid-file = /data/var/run/mysqld/mysqld13307.pid
socket  = /data/var/run/mysqld/mysqld13307.sock
log-bin = /data/logs/binlog13307/binlog
binlog_cache_size = 512M
binlog_format = MIXED
max_binlog_cache_size = 512M
max_binlog_size = 200M
relay-log-index = /data/logs/relaylog13307/relaylog
relay-log-info-file = /data/logs/relaylog13307/relaylog
relay-log = /data/logs/relaylog13307/relaylog
expire_logs_days = 7
innodb_flush_log_at_trx_commit=0
sync_binlog=0
slave_skip_errors = 1062,1032
#下面是指定哪些数据库需要被同步,哪些不需要被同步,一般来讲,只有数据的库才需要被同步,用户信息一般不同步。
# The MySQL server
replicate-do-db         =  aaa
replicate-do-db         =  bbbb
replicate-do-db         =  ccccc
replicate-do-db         =  12ead
replicate-do-db         =  naiuqn
replicate-ignore-db     =  mysql
replicate-ignore-db     =  information_schema
replicate-ignore-db     =  performance_schema
 
#slow_query_log = 1
#slow_query_log_file=/data/mysql/logs/slowquery.log
#long_query_time=2
#log_queries_not_using_indexes
 
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
 
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
 
[mysqlhotcopy]
interactive-timeout

2、上面的步骤配置完了以后

将RDS的白名单,设置好,允许ECS远程连接RDS,然后对各个表的数据进行dump

一般来讲,高权限用户只能逐个的mysqldump各个数据库

1
mysqldump -h rds内网地址 -u用户 -p --database 数据库名 >> /tmp/数据库名1.sql
1
mysqldump -h rds内网地址 -u用户 -p --database 数据库名 >> /tmp/数据库名2.sql
1
mysqldump -h rds内网地址 -u用户 -p --database 数据库名 >> /tmp/数据库名3.sql

这一步主要操作是将现阶段的数据库都备份出来,然后执行下面的操作。

mysql> show master statusG      #请注意,下面的信息全都要留好,因为后面会在自建数据库中需要指定这些数据

*************************** 1. row ***************************
File: mysql-bin.001461
Position: 15351962
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set: 3fb72bdd-02d5-11e6-a554-6c92bf2c0469:1-1751591,
4e5c900f-02d5-11e6-a555-ecf4bbded8cc:1-54349982,
ac5fa92d-5b22-11e8-afcd-7cd30abead5e:1-135523638,
d3615618-7f50-11e6-9110-d89d672b73e0:1-4
1 row in set (0.01 sec)

在主库上我们需要操作的就这么多,下面我们来配置从库

3、在从库中,将上面的各个数据库导入

1
2
3
4
5
6
7
8
9
10
11
#先查看dump出来的sql里面有没有create语句,如果有的话,忽略这些create语句。
create database 数据库1;
create database 数据库2;
create database 数据库3;
 
use 数据库1;
source /tmp/数据库1.sql
use 数据库2;
source /tmp/数据库2.sql
use 数据库3;
source /tmp/数据库3.sql#source语句执行不报错,就算导入成功了。

 然后我们进行chang master 语句

1
2
3
4
5
change master to MASTER_HOST='RDS内网地址',
                       MASTER_USER='slave',
                       MASTER_PASSWORD='slave用户的密码',
                       MASTER_LOG_FILE='在rds上show master status的File列内容',
                       MASTER_LOG_POS=15351962; #这个log pos是从上面show master status中获取  

然后直接执行以下命令

1
1.start slave;#注意,如果之前进行过changemaster 这块start slave的时候就会报如下错误。

  ERROR 1776 (HY000): Parameters MASTER_LOG_FILE, MASTER_LOG_POS, RELAY_LOG_FILE and RELAY_LOG_POS cannot be set when MASTER_AUTO_POSITION is active.

1
2
3
需要执行以下语句(如果不报错,请忽略这一步)  change master to master_auto_position=0;
 
2.show slave status;
1
#当看到这两项为YES的时候,证明主从复制已经成功,自己进行测试即可
1
<br>Slave_IO_Running:YES   Slave_SQL_Running:YES
原文地址:https://www.cnblogs.com/xiami2046/p/12778577.html