mysql集群,两主两从,使用mysql-proxy实现读写分离

主从复制

一、IP规划

服务器 IP 备注
master1 192.168.100.131 master2的从
master2 192.168.100.132 master1的从
slave1 192.168.100.134 slave1的从
slave2 192.168.100.135 slave2的从
mysql-proxy 192.168.100.137

二、具体配置

1.master1

​ 配置ip:192.168.100.131

​ 关闭防护墙,selinux

sh 复制代码
# 关闭防火墙
systemctl stop firewalld

# 关闭selinux
setenforce 0

​ 下载mariadb

sh 复制代码
yum -y install mariadb mariadb-server

​ 编辑mariadb配置文件

sh 复制代码
vi /etc/my.cnf

​ 添加以下内容

sh 复制代码
# 标识(0-65535范围)
server-id=1

# 日志文件名称前缀
log-bin=mysql-bin

# 排除不复制的库
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignore-db=sys

# 配置中继日志名称
relay-log=mysql-relay-bin

# 配置排除的库
replicate-ignore-db=mysql
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
replicate-ignore-db=sys

# 编码格
init-connect='SET NAMES UTF8'
skip-character-set-client-handshake
character-set-server=utf8

​ 修改完成重启服务

sh 复制代码
# 重启服务
systemctl restart mariadb

# 配置mysql密码,123456
mysql_secure_installation 

​ 授权

mariadb 复制代码
# 进入mysql
mysql -uroot -p123456

# 创建用户
create user "backup"@"%" identified by '123456';

# 授权
grant replication slave on *.* to "backup"@"%" identified by "123456";
grant all privileges on *.* to 'root'@'%' identified by '123456';

# 刷新权限
flush privileges;

# 查询Log_file,log_pos
show master status;
+------------------+----------+--------------+-------------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB                                |
+------------------+----------+--------------+-------------------------------------------------+
| mysql-bin.000003 |     1947 |              | mysql,information_schema,performance_schema,sys |
+------------------+----------+--------------+-------------------------------------------------+
1 row in set (0.00 sec)

# 从库连接主库,MASTER_LOG_FILE与MASTER_LOG_POS的值由主库信息获得
change master to 
MASTER_HOST='192.168.100.132', 
MASTER_USER='backup', 
MASTER_PASSWORD='123456', 
MASTER_PORT=3306, 
MASTER_LOG_FILE='mysql-bin.000003', 
MASTER_LOG_POS=1947;

# 开启复制
start slave;

# 查看slave状态,Slave_IO_Running,Slave_SQL_Running的状态要为Yes
show slave status \G;

2.master2

​ 配置ip:192.168.100.132

​ 关闭防护墙,selinux

sh 复制代码
# 关闭防火墙
systemctl stop firewalld

# 关闭selinux
setenforce 0

​ 下载mariadb

sh 复制代码
yum -y install mariadb mariadb-server

​ 编辑mariadb配置文件

sh 复制代码
vi /etc/my.cnf

​ 添加以下内容

sh 复制代码
# 标识(0-65535范围)
server-id=2

# 日志文件名称前缀
log-bin=mysql-bin

# 排除不复制的库
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignore-db=sys

# 配置中继日志名称
relay-log=mysql-relay-bin

# 配置排除的库
replicate-ignore-db=mysql
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
replicate-ignore-db=sys

# 编码格
init-connect='SET NAMES UTF8'
skip-character-set-client-handshake
character-set-server=utf8

​ 修改完成重启服务

sh 复制代码
# 重启服务
systemctl restart mariadb

# 配置mysql密码,123456
mysql_secure_installation 

​ 授权

mariadb 复制代码
# 进入mysql
mysql -uroot -p123456

# 创建用户
create user "backup"@"%" identified by '123456';

# 授权
grant replication slave on *.* to "backup"@"%" identified by "123456";
grant all privileges on *.* to 'root'@'%' identified by '123456';

# 刷新权限
flush privileges;

# 查询Log_file,log_pos
show master status;
+------------------+----------+--------------+-------------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB                                |
+------------------+----------+--------------+-------------------------------------------------+
| mysql-bin.000003 |     1947 |              | mysql,information_schema,performance_schema,sys |
+------------------+----------+--------------+-------------------------------------------------+
1 row in set (0.00 sec)

# 从库连接主库,MASTER_LOG_FILE与MASTER_LOG_POS的值由主库信息获得
change master to 
MASTER_HOST='192.168.100.131', 
MASTER_USER='backup', 
MASTER_PASSWORD='123456', 
MASTER_PORT=3306, 
MASTER_LOG_FILE='mysql-bin.000003', 
MASTER_LOG_POS=1947;

# 开启复制
start slave;

# 查看slave状态,Slave_IO_Running,Slave_SQL_Running的状态要为Yes
show slave status \G;

3.slave1

​ 配置ip:192.168.100.134

​ 关闭防护墙,selinux

sh 复制代码
# 关闭防火墙
systemctl stop firewalld

# 关闭selinux
setenforce 0

​ 下载mariadb

sh 复制代码
yum -y install mariadb mariadb-server

​ 编辑mariadb配置文件

sh 复制代码
vi /etc/my.cnf

​ 添加以下内容

sh 复制代码
# 标识(0-65535范围)
server-id=3

# 配置中继日志名称
relay-log=mysql-relay-bin

# 配置排除的库
replicate-ignore-db=mysql
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
replicate-ignore-db=sys

# 编码格式
init-connect='SET NAMES UTF8'
skip-character-set-client-handshake
character-set-server=utf8

​ 修改完成重启服务

sh 复制代码
# 重启服务
systemctl restart mariadb

# 配置mysql密码,123456
mysql_secure_installation 

​ 授权

mariadb 复制代码
# 连接数据库
mysql -uroot -p123456

# 授权
grant all privileges on *.* to "root"@"%" identified by "123456"; 

# 刷新权限
flush privileges;

# 从库连接主库,MASTER_LOG_FILE与MASTER_LOG_POS的值由主库信息获得
change master to 
MASTER_HOST='192.168.100.131', 
MASTER_USER='backup', 
MASTER_PASSWORD='123456', 
MASTER_PORT=3306, 
MASTER_LOG_FILE='mysql-bin.000003', 
MASTER_LOG_POS=1947;

# 开启复制
start slave;

# 查看slave状态,Slave_IO_Running,Slave_SQL_Running的状态要为Yes
show slave status \G;

4.slave2

​ 配置ip:192.168.100.135

​ 关闭防护墙,selinux

sh 复制代码
# 关闭防火墙
systemctl stop firewalld

# 关闭selinux
setenforce 0

​ 下载mariadb

sh 复制代码
yum -y install mariadb mariadb-server

​ 编辑mariadb配置文件

sh 复制代码
vi /etc/my.cnf

​ 添加以下内容

sh 复制代码
# 标识(0-65535范围)
server-id=4

# 配置中继日志名称
relay-log=mysql-relay-bin

# 配置排除的库
replicate-ignore-db=mysql
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
replicate-ignore-db=sys

# 编码格
init-connect='SET NAMES UTF8'
skip-character-set-client-handshake
character-set-server=utf8

​ 修改完成重启服务

sh 复制代码
# 重启服务
systemctl restart mariadb

# 配置mysql密码,123456
mysql_secure_installation 

​ 授权

mariadb 复制代码
# 连接数据库
mysql -uroot -p123456

# 授权
grant all privileges on *.* to "root"@"%" identified by "123456"; 

# 刷新权限
flush privileges;

# 从库连接主库,MASTER_LOG_FILE与MASTER_LOG_POS的值由主库信息获得
change master to 
MASTER_HOST='192.168.100.132', 
MASTER_USER='backup', 
MASTER_PASSWORD='123456', 
MASTER_PORT=3306, 
MASTER_LOG_FILE='mysql-bin.000003', 
MASTER_LOG_POS=1947;

# 开启复制
start slave;

# 查看slave状态,Slave_IO_Running,Slave_SQL_Running的状态要为Yes
show slave status \G;

5.mysql-proxy

mariadb 复制代码
# 关闭防火墙
systemctl stop firewalld

# 关闭selinux
setenforce 0

# 下载wget工具
yum -y install wget

# 下载mysql-proxy
wget http://ftp.ntu.edu.tw/pub/MySQL/Downloads/MySQL-Proxy/mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz

# 创建用户mysql-proxy
useradd -r mysql-proxy

# 解压文件到local
tar zxvf mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz -C /usr/local

# 修改文件名
mv /usr/local/mysql-proxy-0.8.5-linux-el6-x86-64bit  /usr/local/mysql-proxy

# 加入环境变量
echo 'export PATH=$PATH:/usr/local/mysql-proxy/bin/'  >> /etc/profile

# 刷新环境变量
. /etc/profile


# 修改读写分离配置
vi /usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua

# 找到内容并修改
if not proxy.global.config.rwsplit then
proxy.global.config.rwsplit = {
	min_idle_connections = 1, -- 默认超过4个连接数时,才开始读写分离,改为1
	max_idle_connections = 8, -- 默认最大8个连接数
	is_debug = false
}
end


# 启动mysql-proxy
mysql-proxy --daemon --log-level=debug --user=mysql-proxy --keepalive --log-file=/var/log/mysql-proxy.log --plugins="proxy" --proxy-address="192.168.100.137:4040" --proxy-backend-addresses="192.168.100.131:3306" --proxy-backend-addresses="192.168.100.132:3306" --proxy-read-only-backend-addresses="192.168.100.134:3306" --proxy-read-only-backend-addresses="192.168.100.135:3306" --proxy-lua-script="/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua" --plugins=admin --admin-username="admin" --admin-password="admin" --admin-lua-script="/usr/local/mysql-proxy/lib/mysql-proxy/lua/admin.lua"

三、检测

​ 进入master 、或者slave(安装了maraidb的服务器),输入以下内容,查看state的值是否为up

sh 复制代码
mysql -uadmin -padmin -h192.168.100.137 -P4041 -e "select * from backends;"
+-------------+----------------------+-------+------+------+-------------------+
| backend_ndx | address              | state | type | uuid | connected_clients |
+-------------+----------------------+-------+------+------+-------------------+
|           1 | 192.168.100.131:3306 | up    | rw   | NULL |                 0 |
|           2 | 192.168.100.132:3306 | up    | rw   | NULL |                 0 |
|           3 | 192.168.100.134:3306 | up    | ro   | NULL |                 0 |
|           4 | 192.168.100.135:3306 | up    | ro   | NULL |                 0 |
+-------------+----------------------+-------+------+------+-------------------+

​ 如果不是,可以运行以下命令等待(2分钟左右),在运行查看state的值是否为up

sh 复制代码
mysql -uroot -p123456 -h192.168.100.137 -P4040 -e "show databases;"
相关推荐
Hello.Reader8 分钟前
Redis热点数据管理全解析:从MySQL同步到高效缓存的完整解决方案
redis·mysql·缓存
是程序喵呀40 分钟前
MySQL备份
android·mysql·adb
指尖上跳动的旋律1 小时前
shell脚本定义特殊字符导致执行mysql文件错误的问题
数据库·mysql
苹果醋35 小时前
2020重新出发,MySql基础,MySql表数据操作
java·运维·spring boot·mysql·nginx
先睡5 小时前
MySQL的架构设计和设计模式
数据库·mysql·设计模式
呼啦啦啦啦啦啦啦啦7 小时前
【MySQL篇】事务的认识以及四大特性
数据库·mysql
溟洵9 小时前
Linux下学【MySQL】表中插入和查询的进阶操作(配实操图和SQL语句通俗易懂)
linux·运维·数据库·后端·sql·mysql
苹果醋314 小时前
React源码02 - 基础知识 React API 一览
java·运维·spring boot·mysql·nginx
别致的影分身15 小时前
使用C语言连接MySQL
数据库·mysql
过过过呀Glik15 小时前
在 Ubuntu 上安装 MySQL 的详细指南
mysql·ubuntu