设为首页 收藏本站
查看: 730|回复: 0

[经验分享] 利用amoeba实现mysql主从复制读写分离

[复制链接]
累计签到:2 天
连续签到:1 天
发表于 2016-8-10 09:40:32 | 显示全部楼层 |阅读模式
    一般大型网站为了缓解大量的并发访问,会在web端实现负载均衡,但是这是远远不够的。到了数据存储层、数据访问层,如果还是传统的架构,或者只是依靠一台服务器,大量的数据库连接操作,会导致数据库面临崩溃的危险。进而造成数据丢失,后果不堪设想。所以我们会考虑如何减少数据库的连接,一方面进行代码的优化,采用优秀的数据缓存技术如memcached,如果资金丰厚的话,必然会想到假设服务器群,来分担主数据库的压力。今天我们就利用MySQL主从配置,实现读写分离,分散数据库的压力。这种方式,很多网站都有应用,今天只是总结学习一下。
读写分离的环境:
1.mysql集群(一般是一主多从,多主多从,本文一主多从来举例)
2.读写分离中间件(本文以amoeba举例)
3.mysql客户端程序


Amoeba实现读写分离的原理图如下
wKioL1epcqiQrwi5AAFNQM71WkM568.jpg
amoeba作为访问数据库的前端程序,当其接收到来自客户端或者是应用段的sql语句对数据库做处理的时候,将sql语句进行分类,比如sql语句中有select关键词则作为数据库读操作,否则作为数据库写操作。Amoeba作为数据库和客户端的中间件,会同时链接master数据库和slave数据库。由于一般网站读操作远多于写操作,所以我们定义master数据库做为写操作的数据库,slave数据库作为读操作的数据库。这样amoeba就会将来自于客户端的写操作路由到master数据库,将读操作路由到slava数据库,来实现读写分离功能。
1.mysql主从同步环境搭建
    这里就不详细介绍了,网上有很多,也很简单,我们这里是一主多从的环境。

2.amoeba配置读写分离
    1)amoeba需要java环境的支持,首先查看一下系统里是否安装java环境
1
2
3
java version "1.8.0_101"
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)



我这里已经安装了java环境,所以会有上面的信息,如果没有安装的话可以到oracle官网去下载你的系统可用的java安装rpm包。

1
2
3
4
[iyunv@zabbix tools]# wget  http://download.oracle.com/otn-p ... 8u101-linux-x64.rpm
[iyunv@zabbix tools]# ls jd*
jdk-8u101-linux-x64.rpm
[iyunv@zabbix tools]# rpm -ivh jdk-8u101-linux-x64.rpm



    2)下载解压安装amoeba软件包

1
2
3
4
5
6
7
[iyunv@zabbix tools]# wget http://101.96.10.43/jaist.dl.sou ... RC-distribution.zip
[iyunv@zabbix tools]#ls
amoeba-mysql-3.0.5-RC-distribution.zip
[iyunv@zabbix tools]#mkdir -p /tmp/amoeba
[iyunv@zabbix tools]#mkdir -p /usr/local/amoeba
[iyunv@zabbix tools]#unzip -d /tmp/amoeba/ amoeba-mysql-3.0.5-RC-distribution.zip
[iyunv@zabbix tools]#cp -rf /tmp/amoeba/amoeba-mysql-3.0.5-RC/* /usr/local/amoeba/





    2)配置amoeba软件

amoeba的配置文件主要在conf目录下面,其中我们要修改的主要是amoeba.xml和dbServer.xml两个配置文件
amoeba.xml修改以下几部分:
1
2
3
4
5
6
<property name="ipAddress">127.0.0.1</property>     //amoeba服务监听的IP
<property name="user">root</property>               //amoeba的管理账号
<property name="password">root</property>           //amoeba的管理密码
<property name="defaultPool">multiPool</property>   //amoeba的默认数据库集群
<property name="writePool">master</property>        //amoeba的读操作数据库集群
<property name="readPool">slave1</property>         //amoeba的写操作数据库集群




dbServer.xml修改以下部分:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<property name="port">3306</property>     //真实数据库服务端口
<property name="schema">test</property>   //真实数据库上存在的database(每个数据库上都存在)
<property name="user">test111</property>  //真实数据库上用于amoeba连接数据库的账号
<property name="password">111111</property>//真实数据库上用于amoeba连接数据库的密码
<dbServer name="master"  parent="abstractServer">   //dbServer name设置为master
       <factoryConfig>
               <!-- mysql ip -->
            <property name="ipAddress">192.168.1.11</property>//设置master的ip
        </factoryConfig>
        </dbServer>
        <dbServer name="slave1"  parent="abstractServer">//dbServer name设置为slave1
                <factoryConfig>
                        <!-- mysql ip -->
                   <property name="ipAddress">192.168.1.12</property>//设置slave1的ip
                </factoryConfig>
        </dbServer>




    3)启动amoeba软件
1
2
[iyunv@zabbix ~]# ./usr/local/amoeba/bin/launcher  
The stack size specified is too small, Specify at least 160kError: Could not create the Java Virtual Machine.





如果启动时报以上错误,请修改amoeba目录下jvm.properties文件中以下条目的Xss数值为1024K
JVM_OPTIONS="-server -Xms256m -Xmx1024m -Xss1024k -XX:PermSize=16m -XX:MaxPermSize=96m"
重启amoeba后正常,检查一下(amoeba默认端口8066)
1
2
3
[iyunv@zabbix amoeba-mysql-3.0.5-RC]# ss -lntup|grep 8066
tcp    LISTEN     0      128      :::8066                 :::*                   users:(("java",pid=8104,fd=66))
[iyunv@zabbix amoeba-mysql-3.0.5-RC]#




    4)连接到amoeba服务器
1
2
3
4
5
6
7
8
9
10
[iyunv@TEST01 ~]# mysql -uroot -proot  -h 192.168.1.13 -P 8066
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 772593148
Server version: 5.1.45-mysql-amoeba-proxy-3.0.4-BETA MySQL Community Server (GPL)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>



从server version: 5.1.45-mysql-amoeba-proxy-3.0.4-BETA可以看出,已经正常连接到amoeba服务


读写分离测试

    1)终止slave1服务器的主从同步
1
2
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)



    2)在slave1服务器的数据库创建一个新的db

1
2
3
4
5
6
7
8
9
10
11
12
13
14
mysql> create database read_db;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| read_db            |
| test               |
+--------------------+
10 rows in set (0.00 sec)
mysql>



    3)连接amoeba服务,查看数据库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Your MySQL connection id is 772593148
Server version: 5.1.45-mysql-amoeba-proxy-3.0.4-BETA MySQL Community Server (GPL)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| read_db            |
| test               |
+--------------------+
10 rows in set (0.00 sec)
mysql>



可以看到read_db数据库,amoeba读操作是从slave1拿的数据
    4)连接amoeba服务,创建一张表,并查看
1
2
3
4
5
6
7
8
9
mysql> use test
Database changed
mysql> creat table write_table(id int);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'creat table write_table(id int)' at line 1
mysql> create table write_table(id int);
Query OK, 0 rows affected (0.04 sec)
mysql> show tables;
Empty set (0.01 sec)
mysql>



发现没有刚刚创建的表,可见数据没有插入到slave1的数据库中

在master数据库查看,发现存在刚才插入的表
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[iyunv@TEST01 ~]# mysql -uroot -poldboy
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2490
Server version: 5.5.32-log MySQL Community Server (GPL)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use test
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| write_table    |
+----------------+
1 row in set (0.01 sec)
mysql>





以上的测试可以发现,amoeba已经将读操作和写操作指向不同的服务器,从而实现mysql的读写分离
目前主流的数据库读写分离软件,大家 有时间可以学习学习

mysql-proxy(不适合生产环境使用)
atlas(360基于mysql-proxy开发)
amoeba(不适合生产环境使用)
tddl(淘宝开发)部分开源,配置复杂
cobar(阿里基于Amoeba开发)不支持事务
dbware(只支持mysql)
mycat(社区活跃,支持大规模使用,且开源)









运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-255792-1-1.html 上篇帖子: 如何查找MySQL中查询慢的SQL语句 下篇帖子: mysql数据迁移采用MySQLdump工具迁移 mysql
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表