注:以下配置基于MySQL8.0
1. 主节点配置
1.1 在my.conf或my.ini中配置节点id
[mysqld]
server_id=100
1.2 重启MySQL服务
service mysqld restart;
1.3 创建与从节点同步的用户并赋权
create user 'follower'@'%' identified by '******';
grant replication slave on *.* to 'follower'@'%';
flush privileges;
1.4 查看主节点状态
show master status;
注:以上操作完成后不要再操作主库,防止状态值变化
2. 从节点配置
2.1 配置节点id:(务必与主节点不一致)
[mysqld]
server_id=101
2.2 重启MySQL服务
service mysqld restart;
2.3 同步命令
change master to
master_host ='8.142.xx.xxx',
master_user ='follower',
master_password ='********',master_log_file ='binlog.000011',
master_log_pos =6342;
注:master_log_file
与master_log_pos
务必与主库状态保持一致
2.4 启用同步
start slave;
2.5 查看同步状态
show slave status;
Slave_IO_Running
和Slave_SQL_Running
均为Yes时表示主从同步配置完成,此时在主库进行数据修改后即可在从库观察到同步。
3.可能遇到的问题
若Slave_IO_Running
与Slave_SQL_Running
不为Yes,可在Last_IO_Error
中查看错误信息进行修改。
- Authentication require secure connection
Last_IO_Errno:2061
Last_IO_Error:error connecting to mater 'follower@8.142.xx.xx:xxxx' - retry-time:60 retries:1 massage:Authentication plugin 'caching_sha2_password' reported error:Authentication require secure connection.
解决方案:
- 方法1
使用复制用户请求服务器公钥
mysql -u follower -p****** -h 8.142.xx.xx -P3306 --get-server-public-key
- 方法2
创建用户时不使用插件caching_sha2_password
create user 'follower'@'%' identified with mysql_native_password by '******'
修改后均需要停止主从复制后重新配置
#停止
stop slave;
#重设
reset slave;
#步骤2.3