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;"
相关推荐
难以触及的高度37 分钟前
mysql中between and怎么用
数据库·mysql
小技与小术3 小时前
数据库表设计范式
数据库·mysql
安迁岚3 小时前
【SQL Server】华中农业大学空间数据库实验报告 实验三 数据操作
运维·服务器·数据库·sql·mysql
安迁岚3 小时前
【SQL Server】华中农业大学空间数据库实验报告 实验九 触发器
数据库·sql·mysql·oracle·实验报告
无敌岩雀3 小时前
MySQL中的索引
数据库·mysql
东阳马生架构4 小时前
MySQL原理简介—1.SQL的执行流程
mysql
安迁岚4 小时前
【SQL Server】华中农业大学空间数据库实验报告 实验六 视图
数据库·sql·mysql·oracle·实验报告
xoxo-Rachel5 小时前
(超级详细!!!)解决“com.mysql.jdbc.Driver is deprecated”警告:详解与优化
java·数据库·mysql
JH30735 小时前
Oracle与MySQL中CONCAT()函数的使用差异
数据库·mysql·oracle
路有瑶台7 小时前
MySQL数据库学习(持续更新ing)
数据库·学习·mysql