• Amoeba 实现 MySQL 读写分离


    实验环境:

    amoeba:192.168.200.116

    MySQL-master:192.168.200.111

    MySQL-slave1:192.168.200.112

    MySQL-slave2:192.168.200.113

    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)

    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)

    在客户机测试

    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)

  • 相关阅读:
    [原创]可动态显示圆形图像或圆形文字的AvatarImageView
    [原创]自定义view之:快速开发一款Material Design风格的dialog的开源项目MDDialog
    [原创]自定义BaseAcitivity的实现,统一activity的UI风格样式
    [原创]android自定义动画的一点感悟
    [原创]Java中的字符串比较,按照使用习惯进行比较
    [原创]android使用代码生成LayerDrawable的方法和注意事项
    [原创]android开源项目源码解析(一)----CircleImageView的源码解析
    [原创]自定义公历农历日期选择器
    自定义android RadioButton View,添加较为灵活的布局处理方式
    实现仿知乎的开场动画,图片zoomin的效果,实现原理,没加动效
  • 原文地址:https://www.cnblogs.com/zhiyuan-yu/p/11678830.html
Copyright © 2020-2023  润新知