Mysql主从复制
环境展示
192.168.200.1 master
192.168.200.2 slave
安装Mysql (两个节点)
# 添加仓库
yum-config-manager --add-repo http://mirrors.aliyun.com/repo/Centos-7.repo
# 安装mysql
yum install -y mariadb-server
配置master
# vim /etc/my.cnf
[mysqld]
log_bin = mysql
binlog_ignore_db = mysql
server_id = 1
启动Mysql
systemctl enable --now mariadb
配置读写分离用户
# 配置从节点复制用户
mysql -e "grant replication slave on *.* to slave@'%' identified by 'slave';"
配置Slave
# vim /etc/my.cnf
[mysqld]
server_id = 2 # 两个节点server_id不要相同
配置Slave连接Master
# 启动mysql
systemctl enable --now mariadb
# 连接master
mysql -e "change master to master_host='192.168.200.1',maste_user='slave',master_password='slave';"
# 开启主从复制
mysql -e "start slave;"
检查连接状态
mysql -e "show slave status;"
# 嫌乱可以
mysql -e "show slave status \G;"
仅供学习参考,若有不足请和善指出。