MySQL/MariaDB读写分离配置
DB读写分离描述
数据库的读写分离其实就是为了加减少数据库的压力;数据库的写入操作由主数据库来进行,读取操作由从数据库来进行操作。
实现数据库读写分离技术是有很多方法的,在这里我就用一个比较简单的mysql-proxy这个中间件来实现数据库的读写分离;使用mysql-proxy实现mysql的读写分离,mysql-proxy实际上是作为后端mysql主从服务器的代理,它直接接受客户端的请求,对SQL语句进行分析,判断出是读操作还是写操作,然后分发至对应的mysql服务器上。
数据库读写分离比较实用的还有Amoeba等相关程序。
基本环境
此环境需要三台主机(可以是虚拟主机)
Linux 操作系统 版本: CentOS8.0
软件版本:
数据库: mariadb
lua: lua.X86_64
mysql-proxy: mysql-proxy-0.8.5
这里因为需要用三台主机,我电脑配置有点上愁,所以我这里就使用容器(docker)代替三台虚拟主机了
root@uduntu:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8aea2ebdf1a5 centos minutes ago Up minutes mysql-proxy
9a24bbeec012 centos minutes ago Up minutes DB2
5495e5cf36c3 centos minutes ago Up minutes DB1
root@uduntu:~#
这是我的三个容器,IP地址分别是:
DB1: 172.18.0.2
DB2: 172.18.0.3
mysql-proxy: 172.18.0.4
好了基础环境已经介绍完成了,接下来开始真正的部署操作吧!
数据库部署
MariaDB描述:MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可 MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品。MariaDB基于事务的Maria存储引擎,替换了MySQL的MyISAM存储引擎,它使用了Percona的 XtraDB,InnoDB的变体,分支的开发者希望提供访问即将到来的MySQL 5.4 InnoDB性能。这个版本还包括了 PrimeBase XT (PBXT) 和 FederatedX存储引擎。
安装
[root@DB1 /]# apt -y install mariadb-server
Setting up mariadb-client-:-) ...
Setting up libdbd-mysql-perl:amd64 (4.050-2build1) ...
Setting up libhtml-parser-perl (3.72-3build2) ...
Setting up mariadb-server-:-) ...
Created symlink /etc/systemd/system/mysql.service → /lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → /lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /lib/systemd/system/mariadb.service.
Setting up libhttp-message-perl () ...
Setting up libcgi-pm-perl () ...
Setting up libhtml-template-perl () ...
Setting up mariadb-server (:-) ...
Setting up libcgi-fast-perl (:) ...
Processing triggers -7ubuntu3) ...
Processing triggers -) ...
Processing triggers for libc-bin (2.30-0ubuntu2) ...
jia@uduntu:~$ \\出现上面代码表示安装成功
启动
[root@DB1 /]# systemctl start mariadb
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to start 'mariadb.service'.
Authenticating as: jia
Password: \\此处输入密码
==== AUTHENTICATION COMPLETE ===
[root@DB1 /]#
查看是否启动成功:
[root@DB1 /]# ps uax | grep mysqld
mysql ? Ssl : : /usr/sbin/mysqld
jia pts/ S+ : : grep --color=auto mysqld
[root@DB1 /]#
初始化数据库:
[root@DB1 /]# mysql_secure_installation
\\下面是初始化过程
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y
... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
[root@DB1 /]#
初始化完成后,可直接使用自带的mysql客户端进行连接
[root@DB1 /]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id
Server version: -MariaDB- Ubuntu 19.10
Copyright (c) , , Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| ifnormation_schema |
| mysql |
| performance_schema |
+--------------------+
rows in set (0.000 sec)
MariaDB [(none)]>
DB2数据库同DB1数据库一样,同样方法安装mariadb数据库就可以哦!我这里就省略不写了
MySQL-Proxy部署
MySQL-Proxy软件需要部署到单独的mysql-proxy服务器上的
安装依赖软件
MySQL-Proxy中含有一个读写分离的lua文件,这也是我们使用mysql-proxy实现读写分离必用的文件,它需要lua解析器进行解析。因此我们需要安装一个lua解析器。
[root@mysql-proxy /]# yum -y install lua* mdadm-.el8.x86_64 mozjs52--.el8.x86_64 mtools--.el8.x86_64 nfs-utils-:-.el8_0..x86_64 numactl-libs--.el8.x86_64 numad-.20150602git.el8.x86_64 parted-.el8.x86_64 pciutils--.el8.x86_64 pciutils-libs--.el8.x86_64 policycoreutils-2.8-16.1.el8.x86_64 polkit-.el8.x86_64 polkit-libs-.el8.x86_64 polkit-pkla-compat-.el8.x86_64 psmisc-.el8.x86_64 python3-dateutil-:-.el8.noarch python3-dnf-plugins-core-.el8.noarch quota-:.el8.x86_64 quota-nls-:.el8.noarch rdma-core--.el8.x86_64 rpcbind--.el8.x86_64 syslinux-.el8.x86_64 syslinux-extlinux-.el8.x86_64 syslinux-extlinux-nonlinux-.el8.noarch syslinux-nonlinux-.el8.noarch systemd-container--.el8.x86_64 userspace-rcu--.el8.x86_64 xml-common--.el8.noarch Complete! [root@mysql-proxy /]# \\出现以上输出表示安装成功
安装Mysql-Proxy
在安装完成lua之后,我们就可以安装今天的主角软件了
百度网盘链接: https://pan.baidu.com/s/1_SqHKMv4JqYxN9yEDNI-8g 提取码: 3ygn
这里就不给大家演示如何下载了
[root@mysql-proxy opt]# ls
mysql-proxy--linux-rhel5-x86-64bit.tar.gz
[root@mysql-proxy opt]# //上面软件就是我们的主角软件
安装软件包
#安装软件包
#将安装包解压
[root@mysql-proxy opt]# tar zxf mysql-proxy--linux-rhel5-x86-64bit.tar.gz
#解压后的目录
[root@mysql-proxy mysql-proxy--linux-rhel5-x86-64bit]# ls
bin include lib libexec licenses share
# 更改名称,方便之后对其进行操作
[root@mysql-proxy opt]# mv mysql-proxy--linux-rhel5-x86-64bit mysql-proxy
#将mysql-proxy中lua配置文件cp到软件根目录
[root@mysql-proxy mysql-proxy]# cp share/doc/mysql-proxy/rw-splitting.lua .
#再次查看目录结构
[root@mysql-proxy mysql-proxy]# ls
bin include lib libexec licenses rw-splitting.lua share
#修改lua配置文件,只需要修改下面代码就可以
if not proxy.global.config.rwsplit then
proxy.global.config.rwsplit = {
min_idle_connections = , //此处值改为1
max_idle_connections = , //此处值改为1
is_debug = false
}
#修改完成后保存退出,启动mysql-proxy
[root@mysql-proxy mysql-proxy]# cd bin/ //切换目录
[root@mysql-proxy bin]# ./mysql-proxy --proxy-read-only-backend-addresses= --proxy-backend-addresses= --proxy-lua-script=/opt/mysql-proxy/rw-splitting.lua &
[]
[root@mysql-proxy bin]# -- ::: (critical) plugin proxy started
[root@mysql-proxy bin]#
#启动成功
注意:
–proxy-read-only-backend-addresses //指定执行读取操作的数据库IP地址以及端口
–proxy-backend-addresses //指定执行写入操作的数据库IP地址以及端口
–proxy-lua-script //指定mysql-proxy软件中lua配置文件路径
#查看是否启动成功
[root@mysql-proxy bin]# ps aux | grep mysql-proxy
root pts/ S : : /opt/mysql-proxy/libexec/mysql-proxy --proxy-read-only-backend-addresses= --proxy-backend-addresses= --proxy-lua-script=/opt/mysql-proxy/rw-splitting.lua
#出现第一行表示启动成功
接下来需要为mysql-proxy程序创建可以读取和写入数据库的用户
[root@DB2 /]# [root@DB2 /]# mysql -u root -p //登陆数据库 Enter password: //输入数据库密码 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id Server version: -MariaDB MariaDB Server Copyright (c) , , Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> grant all on *.* to 'proxy'@'%' identified by 'redhat'; //创建用户并设置权限 Query OK, rows affected (0.105 sec) MariaDB [(none)]> flush privileges; //更新权限列表 Query OK, rows affected (0.090 sec) MariaDB [(none)]>
注意:用户需要在两个数据库上创建,名称密码要一致;
测试
好了到这里,数据库的部署,以及使用代理对数据库的读写进行限制已经做好了其实也并没有复杂,接下来让我们测试一下。
#找一个客户端,宿主机也可以,安装一个数据库客户端或则数据库的连接工具进行连接,我这里就使用mysql客户端进行测试了 root@uduntu:~# mysql -uproxy -predhat -h 172.18.0.4 -P4040 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id Server version: -MariaDB MariaDB Server Copyright (c) , , Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> #查看到登陆数据库表示连接成功
注意: mysql -uproxy -predhat -h 172.18.0.4 -P4040 其中-u指的是数据库的用户名;-p指的是密码;-h指定mysql-proxy的服务器地址;-P表示mysql-proxy的端口
接下来我们试着创建一个数据库看下效果:
DB1数据库默认所拥有的数据库
[root@DB1 /]# mysql -u root -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id Server version: -MariaDB MariaDB Server Copyright (c) , , Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show databases ; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ rows in set (0.197 sec) MariaDB [(none)]>
DB2数据库默认所拥有的数据库:
[root@DB2 /]# mysql -u root -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id Server version: -MariaDB MariaDB Server Copyright (c) , , Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ rows in set (0.275 sec) MariaDB [(none)]>
使用代理mysql-proxy创建新数据库:
root@uduntu:~# root@uduntu:~# mysql -uproxy -predhat -h 172.18.0.4 -P4040 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id Server version: -MariaDB MariaDB Server Copyright (c) , , Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> create database test; //创建test数据库 Query OK, row affected (0.056 sec) MariaDB [(none)]>
查看DB1数据库:发现多了test数据库
MariaDB [(none)]> show databases ; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ rows in set (0.001 sec) MariaDB [(none)]>
查看DB2数据库:发现没有新的数据库;
MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ rows in set (0.000 sec) MariaDB [(none)]>
OK搞定,说明数据库只是往DB1里面写数据库并不往DB2中写入数据
优化MySQL-Proxy
你会发现在启动mysql-proxy时需要写那么多东西,需要指定两个参数,每次启动都要写太麻烦了,为了解决这个问题,我这里写了一个启动脚本,希望可以帮到大家
#!/bin/sh
# chkconfig: -
# processname: mysql-proxy
# description: mysql-proxy is a proxy daemon to mysql
. /etc/rc.d/init.d/functions
PROXY_PATH=/opt/mysql-proxy/bin
prog="mysql-proxy"
. /etc/sysconfig/network
[ ${NETWORKING} =
PROXY_OPTIONS="--proxy-read-only-backend-addresses=172.18.0.3:3306 --proxy-backend-addresses=172.18.0.2:3306 --proxy-lua-script=/opt/mysql-proxy/rw-splitting.lua"
PROXY_PID=/opt/mysql-proxy/run/mysql-proxy.pid
if [ -f /etc/sysconfig/mysql-proxy ]; then
. /etc/sysconfig/mysql-proxy
fi
PATH=$PATH:/usr/bin:/opt/bin:$PROXY_PATH
RETVAL=
case "$1" in
start)
echo -n $"Starting $prog: "
$NICELEVEL $PROXY_PATH/mysql-proxy $PROXY_OPTIONS --daemon --pid-file=$PROXY_PID --user=root --log-level=debug --log-file=/opt/mysql-proxy/log/mysql-proxy.log
RETVAL=$?
echo
]; then
touch /var/lock/subsys/mysql-proxy]
echo "ok"
fi
;;
stop)
echo -n $"Stopping $prog: "
killproc $prog
RETVAL=$?
echo
]; then
rm -f /var/lock/subsys/mysql-proxy
rm -f $PROXY_PID
fi
;;
restart)
$ stop
sleep
$ start
;;
condrestart)
[ -e / restart
;;
status)
status mysql-proxy
RETVAL=$?
;;
*)
echo "Usage: $0 {start|stop|restart|status|condrestart}"
RETVAL=
;;
esac
exit $RETVAL
我将mysql-proxy服务管理脚本放在了/opt/mysql-proxy/init.d/文件夹里
给执行权限,建立相应目录
[root@mysql-proxy ~]chmod +x /opt/mysql-proxy/init.d/mysql-proxy
[root@mysql-proxy ~]mkdir /opt/mysql-proxy/run
[root@mysql-proxy ~]mkdir /opt/mysql-proxy/log
[root@mysql-proxy ~]cd /opt/mysql-proxy/init.d/
启动mysql-proxy
[root@mysql-proxy init.d ~]./mysql-proxy start
停止mysql-proxy
[root@mysql-proxy init.d ~]./mysql-proxy stop
重启mysql-proxy
[root@mysql-proxy init.d~]./mysql-proxy restart
这样启动停止mysql-proxy就很简单了,在这里提醒大家一下,单独的数据库读写分离是没有任何作用的,只有结合数据主从复制来进行才有意义
数据库读写分离可以参考我的另一篇文章:https://blog.csdn.net/KH_FC/article/details/102974084
MySQL/MariaDB读写分离配置的更多相关文章
- Mysql数据库读写分离配置
环境模拟 实现读写分离 减轻数据库的负荷 主服务器 master 10.0.0.12 从服务器 slave 10.0.0.66 配置主服务器: 在10.0.0.12服务器操作 创建数据 ...
- (转)Mysql数据库读写分离配置
环境模拟 实现读写分离 减轻数据库的负荷 主服务器 master 10.0.0.12从服务器 slave 10.0.0.66 ------------------------------------- ...
- CentOS系统 Amoeba+MySql主从读写分离配置 适合新手傻瓜式教程!-----仅供参考!
废话不说,直接开始: 一.安装mysql的三种方式,这里采用第2种(安装方式不再详解,请参照) http://www.cnblogs.com/babywaa/articles/4837946.html ...
- Amoeba实现mysql主从读写分离
Amoeba实现mysql主从读写分离 这段在网上看了下关于amoeba的文章,总体感觉好像要比mysql-proxy好的多,也参考了不少的资料,此文章可能与其他文章作者会有雷同的地方,请谅解,但是此 ...
- 通过mycat实现mysql的读写分离
mysql的主从配置沿用上一篇博客的配置:https://www.cnblogs.com/MasterSword/p/9434169.html mycat下载地址:http://www.mycat.i ...
- Mysql一主多从和读写分离配置简记
近期开发的系统中使用MySQL作为数据库,由于数据涉及到Money,所以不得不慎重.同时,用户对最大访问量也提出了要求.为了避免Mysql成为性能瓶颈并具备很好的容错能力,特此实现主从热备和读写分离. ...
- MySQL主从同步、读写分离配置步骤
现在使用的两台服务器已经安装了MySQL,全是rpm包装的,能正常使用. 为了避免不必要的麻烦,主从服务器MySQL版本尽量保持一致; 环境:192.168.0.1 (Master) 192.168. ...
- mysql读写分离配置,利用mybatis实现,解释为什么dynamicDataSource不行
之前发布了mysql主从配置的博客,配置完成之后,那么我们肯定要拿主从来做点什么. 我第一想到的就是mysql的读写分离,让读写分离之后可以大大的提供mysql的性能,比单纯用mysql主从做备份好很 ...
- mysql读写分离配置(整理)
mysql读写分离配置 环境:centos7.2 mysql5.7 场景描述: 数据库Master主服务器:192.168.206.100 数据库Slave从服务器:192.168.206.200 M ...
随机推荐
- Git推送到多个远程仓库
Git推送到多个远程仓库 Grey 原文地址 准备工作 在码云和Github上分别新建两个不包括任何文件的空仓库(若是两个已经有文件的仓库,请参见关联已经存在的项目) https://github.c ...
- windows下安装scoop
scoop是windows下的包管理工具,类似与linux下的yum和python的pip. scoop可以在windows下方便的进行软件的管理,尤其是对开发者提供了很大的遍历. cmd下执行如下红 ...
- pycharm中如何为项目设置默认执行器?
一般来说,在python中如果定义了test开头的函数,pycharm会使用默认的执行器.如下方法可以修改默认执行器: file->Setting:打开setting设置页面
- Linux面试题-8
1.Linux文件系统的文件都按其作用分门别类地放在相关的目录中,对于磁盘这种外部设备文件,一般应将其放在(C)目录中. A./bin B./etc C./dev D./lib 2.当使用mount进 ...
- LeetCode初级算法--设计问题01:Shuffle an Array (打乱数组)
LeetCode初级算法--设计问题01:Shuffle an Array (打乱数组) 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:h ...
- 关于举办【福州】《K8S社区线下技术交流会》的问卷调查
近年来,容器.Kubernetes.DevOps.微服务.Serverless等一系列云原生技术受到越来越多的关注,云原生为企业数字化转型提供了创新源动力,基于云原生技术构建企业技术中台在各行业也 ...
- Apollo报错找不到apollo.meta的问题解决方案
问题描述 Apollo报错,找不到apoll.meta,但是明明配置了apollo-env.properties到apollo-client内了. apollo-env.properties pro. ...
- day03课堂练习
简述变量的组成 变量由变量名,赋值符号,和变量值三个部分组成 简述变量名的命名规范 a.变量名必须有意义,要能反映变量值所描述的状态 b.变量名以字母.数字和下划线组成,不能用数字开头 c.不能以关键 ...
- mysql的一些常用操作(二)
紧跟上一节,我们创建了四个表: Student.Teacher.Course.Score 接下来就是实际的一些操作了:1.求每门课程的学生人数. select course.cname '课程名称', ...
- Scrapy 框架入门简介
一.Scrapy框架简介 Scrapy 是用 Python 实现的一个为了爬取网站数据.提取结构性数据而编写的应用框架. Scrapy 常应用在包括数据挖掘,信息处理或存储历史数据等一系列的程序中. ...