上一篇文章介绍了mysql8的数据库搭建,下面介绍mysql的主从搭建,非常简单
- 首先改my.cnf文件,
主库增加如下内容:[mysqld]下面增加
server-id=1
log-bin=master-bin
log-bin-index=master-bin.index
从库1增加
server-id=2
relay-log-index=slave-relay-bin.index
relay-log=slave-relay-bin
从库2增加
server-id=2
relay-log-index=slave-relay-bin.index
relay-log=slave-relay-bin
- 主库创建复制账号和密码
mysql>create user repuser1 IDENTIFIED BY 'test123456';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repuser1'@'192.168.0.123';
mysql>FLUSH PRIVILEGES;
mysql>create user repuser2 IDENTIFIED BY 'test123456';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repuser2'@'192.168.0.124';
mysql>FLUSH PRIVILEGES;
- 重启主库和从库,然后进入mysqld主库,看文件 master-bin.000001和位置155,用于从库设置文件和起点
mysql> show master status\G;
*************************** 1. row ***************************
File: master-bin.000001
Position: 155
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
4.设置从库
change master to master_host='192.168.0.122',master_port=3306, master_user='repuser1',master_password='test123456',master_log_file=' master-bin.000001',master_log_pos=155;
reset slave;
start slave;
看到下面连个配置是YES就ok了
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
- 监控主从
主要也是监控 Slave_IO_Running 和 Slave_SQL_Running就可以了
网友评论