amoeba 读写分离

二次信任 提交于 2019-12-01 13:40:27

实验环境:

MySQL amoeba:192.168.200.116

MySQL-master:192.168.200.111

MySQL-slave1:192.168.200.112

MySQL-slave2:192.168.200.113

MySQL  client:192.168.200.100

先实现MySQL数据库的主从复制,方法见上一篇。

1.安装Amoeba上安装java环境(推荐jdk1.5或1.6)

[root@localhost ~]# ls
amoeba-mysql-binary-2.2.0.tar.gz initial-setup-ks.cfg nohup.out 
anaconda-ks.cfg jdk-6u14-linux-x64.bin yum.sh

[root@localhost ~]# chmod +x jdk-6u14-linux-x64.bin

[root@localhost ~]# jdk-6u14-linux-x64.bin

一路空格,输入yes,回车完成

[root@localhost ~]# mv jdk1.6.0_14/ /usr/local/jdk1.6
[root@localhost ~]# vim /etc/profile

末行添加

export JAVA_HOME=/usr/local/jdk1.6
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin
export AMOEBA_HOME=/usr/local/amoeba/
export PATH=$PATH:$AMOEBA_HOME/bin

[root@localhost ~]# source /etc/profile
[root@localhost ~]# java -version
openjdk version "1.8.0_131"
OpenJDK Runtime Environment (build 1.8.0_131-b12)
OpenJDK 64-Bit Server VM (build 25.131-b12, mixed mode)
[root@localhost ~]# rm -rf /usr/bin/java
[root@localhost ~]# source /etc/profile
[root@localhost ~]# java -version
java version "1.6.0_14"
Java(TM) SE Runtime Environment (build 1.6.0_14-b08)
Java HotSpot(TM) 64-Bit Server VM (build 14.0-b16, mixed mode)

备注自带高版本java问题

[root@localhost ~]# java -version

openjdk version "1.8.0_161"

OpenJDK Runtime Environment (build 1.8.0_161-b14)

OpenJDK 64-Bit Server VM (build 25.161-b14, mixed mode)

方法一:卸载高版本

[root@localhost ~]# rpm -qa | grep -i openjdk

java-1.7.0-openjdk-1.7.0.171-2.6.13.2.el7.x86_64

java-1.7.0-openjdk-headless-1.7.0.171-2.6.13.2.el7.x86_64

java-1.8.0-openjdk-1.8.0.161-2.b14.el7.x86_64

java-1.8.0-openjdk-headless-1.8.0.161-2.b14.el7.x86_64

[root@localhost ~]# rpm -e java-1.7.0-openjdk-headless java-1.7.0-openjdk java-1.8.0-openjdk-headless java-1.8.0-openjdk –nodeps

方法二:删除高版本java程序文件

[root@localhost ~]# which java

/usr/bin/java

[root@localhost ~]# /usr/bin/java -version

openjdk version "1.8.0_161"

OpenJDK Runtime Environment (build 1.8.0_161-b14)

OpenJDK 64-Bit Server VM (build 25.161-b14, mixed mode)

[root@localhost ~]#

[root@localhost ~]# rm -rf /usr/bin/java

[root@localhost ~]# source /etc/profile

 

2.安装配置Amoeba

[root@localhost ~]# mkdir /usr/local/amoeba
[root@localhost ~]# tar xf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba/
[root@localhost ~]# chmod -R 755 /usr/local/amoeba/
[root@localhost ~]# /usr/local/amoeba/bin/amoeba
amoeba start|stop                                                                  //出现此行,证明配置无误

3.配置amoeba实现mysql-slave1,slave2读负载均衡

在三台MySQL服务器上对amoeba授权

MariaDB [(none)]> grant all on *.* to 'admin'@'192.168.200.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

编写amoeba.xml配置文件,设置读写分离
[root@localhost ~]# cd /usr/local/amoeba/conf/
[root@localhost conf]# cp amoeba.xml amoeba.xml.bak
[root@localhost conf]# vim amoeba.xml

11<property name="port">8066</property>             //默认端口8066,无需修改

30<property name="user">amoeba</property>  //设置一个用户,后面客户端访问时使用

32<property name="password">123456</property>       //设置登录密码

115<property name="defaultPool">master</property>    //默认的服务器池

117<property name="writePool">master</property>       //写的服务器池

118<property name="readPool">slaves</property>         //读服务器池

编辑dbServers.xml配置文件,设置登录MySQL服务器的用户及密码,MySQL服务器节点的IP地址,负载均衡算法

[root@localhost conf]# cp dbServers.xml dbServers.xml.bak
[root@localhost conf]# vim dbServers.xml

20<property name="port">3306</property>           //设置MySQL连接端口,默认3306

26<property name="user">admin</property>         //设置访问MySQL服务器所用的用户名

27<property name="password">123456</property>  //设置访问MySQL服务器所用的密码

43<dbServer name="master" parent="abstractServer">  //设置MySQL服务器及IP地址

46<property name="ipAddress">192.168.200.111</property>

50<dbServer name="slave1" parent="abstractServer">

53<property name="ipAddress">192.168.200.112</property>

56<dbServer name="slave2" parent="abstractServer">

59<property name="ipAddress">192.168.200.113</property>

63<dbServer name="slaves" virtual="true">       //在amoeba.xml文件中设置的readPool

69<property name="poolNames">slave1,slave2</property>  //前面定义的服务器节点名

[root@localhost conf]# nohup /usr/local/amoeba/bin/amoeba start &

[root@localhost ~]# vim /usr/local/amoeba/conf/dbServers.xml
[root@localhost ~]# netstat -anpt | grep java   // 查看是否有8066端口

创建测试库,测试表

MariaDB [(none)]> use crushlinux;

Database changed
MariaDB [crushlinux]> insert into test values(2,'slave1');
Query OK, 1 row affected (0.01 sec)

可以在slave1和slave2上查看到该表

MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| crushlinux        |
| db_test             |
| mysql            |
| performance_schema |
| test          |
+--------------------+
6 rows in set (0.01 sec)

在slave1,slave2服务器上停止主从备份,并各自添加内容

MariaDB [crushlinux]> insert into test values(2,'slave1');
Query OK, 1 row affected (0.01 sec)

MariaDB [crushlinux]> insert into test values(3,'slave2');
Query OK, 1 row affected (0.01 sec)

在Client客户机测试

[root@client ~]# yum -y install mairadb-devel mariadb

 

2和3轮询出现

MySQL [(none)]> select * from crushlinux.test;
+------+--------+
| id | name |
+------+--------+
| 3 | slave2 |
+------+--------+
1 row in set (0.02 sec)

MySQL [(none)]> select * from crushlinux.test;
+------+--------+
| id | name |
+------+--------+
| 2 | slave1 |
+------+--------+
1 row in set (0.02 sec)

MySQL [(none)]> insert into crushlinux.test values(4,'client');
Query OK, 1 row affected (0.01 sec)

写入数据后无法查到,实现读写分离

MySQL [(none)]> select * from crushlinux.test;
+------+--------+
| id | name |
+------+--------+
| 2 | slave1 |
+------+--------+
1 row in set (0.01 sec)

MySQL [(none)]> select * from crushlinux.test;
+------+--------+
| id | name |
+------+--------+
| 3 | slave2 |
+------+--------+
1 row in set (0.02 sec)

开启主从复制后,可以查到写入的内容

MySQL [(none)]> select * from crushlinux.test;
+------+--------+
| id | name |
+------+--------+
| 3 | slave2 |
| 1 | master |
| 4 | client |
+------+--------+
3 rows in set (0.01 sec)

MySQL [(none)]> select * from crushlinux.test;
+------+--------+
| id | name |
+------+--------+
| 2 | slave1 |
| 1 | master |
| 4 | client |
+------+--------+
3 rows in set (0.02 sec)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!