基于 amoeba 读写分离

喜夏-厌秋 提交于 2019-12-01 13:34:43

环境配置:

MySQL Master IP: 192.168.200.111

MySQL Slave1 IP: 192.168.200.112

MySQL Slave2 IP: 192.168.200.113

MySQL Amoeba IP: 192.168.200.114

MySQL Client IP: 192.168.200.115

1、在主机Amoeba上安装java环境

因为Amoeba是基于jdk1.5版本开发的,所以官方推荐使用1.5或者1.6版本,高版本不建议使用。

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

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

[root@localhost ~]# mv jdk1.6.0_31/ /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/lib:$JAVA_HOME/jre/bin:$HOME/bin

 

export AMOEBA_HOME=/usr/local/amoeba

export PATH=$PATH:$AMOEBA_HOME/bin

 

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

[root@localhost ~]# java -version

java version "1.6.0_31"

Java(TM) SE Runtime Environment (build 1.6.0_31-b04)

Java HotSpot(TM) 64-Bit Server VM (build 20.6-b01, 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

 

3、配置Amoeba读写分离,两个Slave读负载均衡

在Master、Slave1、Slave2服务器中配置Amoeba的访问授权

MariaDB [(none)]> grant all on *.* to 'test'@'192.168.200.%' identified by '123.com';

Query OK, 0 rows affected (0.00 sec)

 

MariaDB [(none)]> flush privileges;

Query OK, 0 rows affected (0.01 sec)

 

编辑amoeba.xml配置文件

[root@localhost ~]# cp /usr/local/amoeba/conf/amoeba.xml{,.bak}

[root@localhost ~]# vim /usr/local/amoeba/conf/amoeba.xml

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配置文件

[root@localhost ~]# cp /usr/local/amoeba/conf/dbServers.xml{,.bak}

[root@localhost ~]# vim /usr/local/amoeba/conf/dbServers.xml

                            <!-- mysql port -->

                            <property name="port">3306</property>

                           

                            <!-- mysql schema -->

                            <property name="schema">test</property>

                           

                            <!-- mysql user -->

                            <property name="user">test</property>

                           

                            <!--  mysql password -->

                            <property name="password">123.com</property>

 

         <dbServer name="master"  parent="abstractServer">

                   <factoryConfig>

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

                   </factoryConfig>

         </dbServer>

        

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

                   <factoryConfig>

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

                   </factoryConfig>

         </dbServer>

        

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

                   <factoryConfig>

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

                   </factoryConfig>

         </dbServer>

        

         <dbServer name="slaves" virtual="true">

                   <poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">

                            <!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->

                            <property name="loadbalance">1</property>

                           

                            <!-- Separated by commas,such as: server1,server2,server1 -->

                            <property name="poolNames">slave1,slave2</property>

                   </poolConfig>

         </dbServer>

 

配置无误后,启动Amoeba软件,默认端口是TCP协议8066

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

[root@localhost ~]# netstat -anpt |grep :8066

tcp6       0      0 :::8066                 :::*                    LISTEN 

65320/java

 

[root@localhost ~]# netstat -anpt |grep :3306

tcp6       0      0 192.168.200.114:7232    192.168.200.113:3306    ESTABLIS

HED 65320/java          tcp6       0      0 192.168.200.114:36284   192.168.200.112:3306    ESTABLIS

HED 65320/java          tcp6       0      0 192.168.200.114:36280   192.168.200.112:3306    ESTABLIS

HED 65320/java          tcp6       0      0 192.168.200.114:7234    192.168.200.113:3306    ESTABLIS

HED 65320/java          tcp6       0      0 192.168.200.114:6938    192.168.200.111:3306    ESTABLIS

HED 65320/java          tcp6       0      0 192.168.200.114:6936    192.168.200.111:3306    ESTABLIS

HED 65320/java

 

在Client上进行访问测试

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

 

然后可以通过代理访问MySQL

[root@client ~]# mysql -uamoeba -p123456 -h192.168.200.114 -P8066

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MySQL connection id is 1305701886

Server version: 5.1.45-mysql-amoeba-proxy-2.2.0 MariaDB Server

 

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 

MySQL [(none)]>

 

在Mysql主服务器上创建一个表,会自动同步到各个从服务器上,然后关掉各个从服务器上的Slave功能,在分别插入语句测试。

 

主服务器

MariaDB [(none)]> create database db_test;

Query OK, 1 row affected (0.01 sec)

 

MariaDB [(none)]> use db_test;

Database changed

MariaDB [db_test]> create table student (id int(10),name varchar(10),address varchar(20));

Query OK, 0 rows affected (0.04 sec)

 

分别在两台从服务器上

MariaDB [(none)]> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| db_test            |

| mysql              |

| performance_schema |

| sampdb             |

| test               |

+--------------------+

6 rows in set (0.01 sec)

 

MariaDB [(none)]> use db_test;

Database changed

MariaDB [db_test]> show tables;

+-------------------+

| Tables_in_db_test |

+-------------------+

| student           |

+-------------------+

1 row in set (0.00 sec)

 

MySQL [(none)]> stop slave;

Query OK, 0 rows affected (0.10 sec)

 

master

MySQL [(none)]> insert into student values('1','crushlinux','this_is_master');

Query OK, 1 row affected (0.18 sec)

 

slave1

MySQL [(none)]> use db_test

MySQL [(none)]> insert into student values('2','crushlinux','this_is_slave1');

Query OK, 1 row affected (0.07 sec)

 

slave2

MySQL [(none)]> use db_test

MySQL [(none)]> insert into student values('3','crushlinux','this_is_slave2');

Query OK, 1 row affected (0.07 sec)

 

测试读操作:

在测试机上第1次查询结果

MySQL [db_test]> select * from student;

+------+-------+----------------+

| id   | name  | address        |

+------+-------+----------------+

|    2 | crushlinux | this_is_slave1 |

+------+-------+----------------+

1 row in set (0.01 sec)

 

在测试机上第2次查询结果

MySQL [db_test]> select * from student;

+------+-------+----------------+

| id   | name  | address        |

+------+-------+----------------+

|    3 | crushlinux | this_is_slave2 |

+------+-------+----------------+

1 row in set (0.02 sec)

 

在测试机上第3次查询结果

MySQL [db_test]> select * from student;

+------+-------+----------------+

| id   | name  | address        |

+------+-------+----------------+

|    2 | crushlinux | this_is_slave1 |

+------+-------+----------------+

1 row in set (0.01 sec)

 

测试写操作:

在Client上插入一条语句:

MySQL [(none)]> use db_test

MySQL [(none)]> insert into student values('4','crushlinux','write_test');

 

MySQL [db_test]> select * from student;

+------+-------+----------------+

| id   | name  | address        |

+------+-------+----------------+

|    2 | crushlinux | this_is_slave1 |

+------+-------+----------------+

1 row in set (0.01 sec)

 

MySQL [db_test]> select * from student;

+------+-------+----------------+

| id   | name  | address        |

+------+-------+----------------+

|    3 | crushlinux | this_is_slave2 |

+------+-------+----------------+

1 row in set (0.01 sec)

 

但在Client上查询不到,最终只有在Master上才能查看到这条语句内容,说明写操作在master服务器上

MySQL [(none)]> select * from student;

+------+-------+----------------+

| id   | name  | address        |

+------+-------+----------------+

|    1 | crushlinux | this_is_master |

|    4 | crushlinux | write_test     |

+------+-------+----------------+

1 row in set (0.00 sec)

由此验证,已经实现了Mysql读写分离,目前所有的写操作都在Master主服务器上,用来避免数据的不同步,所有的读操作都平分给了Slave从服务器,用来分担数据库压力。

 

分别在两台从服务器上启用slave功能

MariaDB [db_test]> start slave;

Query OK, 0 rows affected (0.00 sec)

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