如果实在云平台中记得在安全组中内网的ip要放开不然无法进进行同步。
如果是线上的项目可能要进行停服进行设置
主服务器:内网ip 172.17.1.17
salve服务器:内网ip 172.17.0.16
1.master配置
vim /etc/my.cnf
2.在mysqld标签下面进行添加如下
- [mysqld]
- server-id = 1 #唯一id
- log-bin = mysql-bin #日志前缀名称
- binlog_format = MIXED #日志模式建议混合 另外还有两种可以查询下具体含义
- expire_logs_days = 90 #日志过期时间
- max_binlog_size = 200m #最大打个日志大小
- binlog_cache_size = 10m #日志缓存
- max_binlog_cache_size = 512m #最大缓存
- binlog-do-db = master-db #需要记录的数据库,多个的话重复此行
3.重启服务,如果是MariaDB 执行 mariadb服务
- service mysqld restart
-
- 或
-
- systemctl restart mariadb.service
4.登录数据库
mysql -u root -p
5.新建slave中要使用的账号
- grant replication slave on *.* to 'testsync'@'172.17.0.16' identified by 'testsync123';
- flush privileges;
6.继续执行
show master status\G;
得到
- *************************** 1. row ***************************
- File: mysql-bin.000001
- Position: 123
- Binlog_Do_DB: master-db
- Binlog_Ignore_DB:
说明主数据库已经成功设置。
7.slave的设置
同样进入
vim /etc/my.cnf
- server-id = 2
- log-bin = mysql-bin #日志前缀名称
- binlog_format = MIXED #日志模式建议混合 另外还有两种可以查询下具体含义
- expire_logs_days = 90 #日志过期时间
- max_binlog_size = 200m #最大打个日志大小
- binlog_cache_size = 10m #日志缓存
- max_binlog_cache_size = 512m #最大缓存
- replicate-do-db = master-db #copy主库的数据库名称,多个可以重复此行
8.进行服务器的重启
- service mysqld restart
-
- 或
-
- systemctl restart mariadb.service
9.登录管理员账号
mysql -u root -p
10.设置刚刚新建的账号,并开启slave
- change master to master_host='172.17.1.17',master_user='testsync',master_password='testsync123',master_log_file='mysql-bin.000001',master_log_pos=123;
-
- start slave;
11.查看是否开启成功
show slave status\G;
12.如果看到这两个这两个都是yes,说明建立成功主从同步关系
