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;"
相关推荐
锐策4 小时前
〔 MySQL 〕数据库基础
数据库·mysql
日月星宿~5 小时前
【MySQL】summary
数据库·mysql
希忘auto7 小时前
详解MySQL安装
java·mysql
运维佬7 小时前
在 MySQL 8.0 中,SSL 解密失败,在使用 SSL 加密连接时出现了问题
mysql·adb·ssl
Runing_WoNiu7 小时前
MySQL与Oracle对比及区别
数据库·mysql·oracle
天道有情战天下7 小时前
mysql锁机制详解
数据库·mysql
CodingBrother7 小时前
MySQL 中单列索引与联合索引分析
数据库·mysql
布川ku子8 小时前
[2024最新] java八股文实用版(附带原理)---Mysql篇
java·mysql·面试
晴天飛 雪9 小时前
Spring Boot MySQL 分库分表
spring boot·后端·mysql
yc_xym9 小时前
【MySQL】MySQL基础知识复习(上)
数据库·mysql