阿里云RDS(主)与ECS服务器mysql(从)主从复制步骤
前提:RDS开外网能连接
主机
1.创建用户
在RDS上创建两个用户:
一个是具备所有权限,类似于管理员的用户(earthquake);
一个是用于主从复制的只读用户(master);
2.设置白名单
将ECS的服务器的IP地址添加进去
3.授权ECS服务器mysql用户
语法:
mysql>GRANT REPLICATION SLAVE ON . TO ‘username’@‘X.X.X.X’ IDENTIFIED BY ‘password’;
4.执行以下命令锁定数据库以防止写入数据
mysql>FLUSH TABLES WITH READ LOCK;
5.查看主机状态
mysql>SHOW MASTER STATUS;
从机
1.修改配置文件
系统版本
[root@abcdocker ~]# cat /etc/redhat-release
CentOS release 6.8 (Final)
数据库版本
[root@abcdocker ~]# mysql -V
mysql Ver 14.14 Distrib 5.6.35, for Linux (x86_64) using EditLine wrapper
MySQL配置文件
[root@abcdocker ~]# grep ‘1’ /etc/my.cnf
server_id=2
read_only = 1
port = 3306
replicate-wild-ignore-table = mysql.%
replicate-wild-ignore-table = test.%
binlog_format = ROW
log-bin = 从机上对应的名字
log-bin-index = 从机上对应的名字.index
relay-log = 从机上对应的名字
relay_log_index = 从机上对应的名字.index
修改完后重启数据库!
2.设置主库
CHANGE MASTER TO
MASTER_HOST=‘主库的ip’,
MASTER_PORT=主库的端口号,
MASTER_USER=‘主库的用于主从复制的用户名’,
MASTER_PASSWORD=‘主库的用于主从复制的密码’
MASTER_LOG_FILE=‘主库status的File’,
MASTER_LOG_POS=主库status的Position;
3.设置mysql全局变量参数
>set GLOBAL GTID_MODE = OFF_PERMISSIVE;
>set GLOBAL GTID_MODE = ON_PERMISSIVE;
>set GLOBAL GTID_MODE = ON;
4.开启从库
mysql> start slave;
5.查看从库状态
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: rm-2ze5i3w4h0182kua0o.mysql.rds.aliyuncs.com
Master_User: mmm
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 4276127
Relay_Log_File: relay-log.000006
Relay_Log_Pos: 33642
Relay_Master_Log_File: mysql-bin.000004
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: mysql.%,test.%
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 4276127
Relay_Log_Space: 33809
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: 3482796583
Master_UUID: 29fe5004-315c-11e7-9a0f-6c92bf477043
Master_Info_File: /usr/local/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: 29fe5004-315c-11e7-9a0f-6c92bf477043:1-7376
Executed_Gtid_Set: 29fe5004-315c-11e7-9a0f-6c92bf477043:1-7376
Auto_Position: 1
1 row in set (0.00 sec)
当Slave_IO_Running,Slave_SQL_Running两个都为yes时,表示主从复制操作成功!
6.对主库进行解锁操作
mysql>unlock tables;
遇到的问题及处理方案:
在对主库导入数据库时,由于生成数据文件的mysql版本较高,主库版本较低,不太兼容,需要对sql文件进行修改。在我的版本中,我需要将
DEFINER约束直接删除;
utf8mb4_0900_ai_ci全部替换为utf8_general_ci;
utf8mb4替换为utf8;
最后数据导入成功,从库复制成功!
a-z ↩︎