ubuntu20.04
:mysql主库
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
# 修改完毕重启
sudo service mysql stop
sudo service mysql start
主库mysqld.cnf
配置
[mysqld]
...
# bind-address>->---= 127.0.0.1 # 注释掉,允许外部连接
# mysqlx-bind-address>--= 127.0.0.1
log-bin=mysql-bin #[必须]启用二进制日志
server-id=139 #[必须]服务器唯一ID(唯一即可)
...
进入mysql配置远程账号,并允许复制权限
mysql -uroot -p
CREATE USER 'mjq2'@'%' IDENTIFIED WITH mysql_native_password BY '123'; # 创建账号mjq@%, 密码123
GRANT REPLICATION SLAVE ON *.* TO 'mjq2'@'%'; # 为用户 mjq@% 分配主从复制权限
# select Repl_slave_priv from user where user='mjq2'; # 理应为Y
flush PRIVILEGES;
查看当前主库二进制日志坐标
show master status; # 从库连接后需要从记录的坐标开始复制
# 查询到 File :mysql-bin.000007 Position:4074
ubuntu20.04
:mysql从库
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
# 修改完毕重启
sudo service mysql stop
sudo service mysql start
从库mysqld.cnf
配置
[mysqld]
server-id=140 #[必须]服务器唯一ID(唯一即可)
进入mysql配置从库复制信息
change master to master_host='192.168.137.139',master_user='mjq2',master_password='123',master_log_file='mysql-bin.000007',master_log_pos=4074; # 连接主库 File :mysql-bin.000007 Position:4074
start slave; # 启动从库复制
show slave status; # 查询主从复制状态,Slave_IO_Runnig和Slave_SQL_Running都要为Yes
停止主从复制
stop slave; # 停止从库复制
RESET SLAVE ALL; # 清除从库的同步复制信息