添加主从配置

进入 MySQL 配置文件/etc/my.cnf,在主库配置中添加 log-bin 和 server-id,从库添加 server-id;

# 主库配置
[mysqld]
log-bin    = mysql-bin 
server-id  = 1 
socket     = /tmp/mysql.sock 
port       = 3306 
pid-file   = /usr/local/mysql/data/3306.pid 
datadir    = /usr/local/mysql/data user       = mysql 
# 从库配置 
[mysqld]
server-id  = 2 
socket     = /tmp/mysql.sock 
port       = 3306 
pid-file   = /usr/local/mysql/data/3306.pid 
datadir    = /usr/local/mysql/data 
user       = mysql 

登录主库,执行命令查看主库状态:

mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |     1832 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

在主库创建从库同步用的账户

CREATE USER 'slave'@'slavehost' IDENTIFIED BY 'slavepass';
GRANT REPLICATION SLAVE ON *.* TO 'slave'@'slavehost';
FLUSh PRIVILEGES;

如果主库存在数据,还需要备份主库数据到从库,这里不说了,回头备份的地方再说;

配置从库同步

登录从库,执行命令设置主库位置:

mysql> CHANGE MASTER TO
    ->     MASTER_HOST='master_host_name',
    ->     MASTER_PORT=3306,
    ->     MASTER_USER='replication_user_name',
    ->     MASTER_PASSWORD='replication_password';

设置完成后,开启从库模式,查看状态:

mysql> START SLAVE;
mysql> SHOW SLAVE STATUS;

返回太长,就不写了。
Slave_IO_State 为 Waiting for master to send event,Master_Host,Master_User 等正确,说明成功,在主库添加数据测试一下即可;

最后修改日期: 2020年5月20日

作者

留言

撰写回覆或留言

发布留言必须填写的电子邮件地址不会公开。