超实用!!MySQL数据库读写分离

  • MySQL读写分离原理
  • 应用场景
  • 一 、安装Amoeba
    • 1. 所有服务器关闭Firewalld或者进行规则设置并搭建Mysql主从复制环境
          • 搭建主从复制详细步骤链接
    • 2.Amoeba服务器环境安装
          • 补充
    • 3.解压Amoeba
    • 4.配置启动脚本,并设置为系统服务
  • 二、配置Amoeba读写分离,两个Slave读负载均衡
    • 1.在master、slave1和slave2中执行以下操作开放权限给Amoeba
    • 2.修改amoeba.xml配置文件
    • 3.修改代表dbServers.xml配置文件
    • 4.启动Amoeba
  • 三、验证试验结果
    • 1.首先关闭主从同步,去两台从服务器slave1和slave2,关闭同步复制,否则会影响试验验证
    • 2.验证两台从服务器的“只读”效果
      • 2.1在master中插入数据
      • 2.2在slave1中插入数据
      • 2.4去客户机测试
    • 3. 验证master的“只写”功能
      • 3.1首先在客户机写入数据
      • 3.2去master查看表数据
      • 3.3去slave1和slave2查看表数据

MySQL读写分离原理

  • 读写分离就是只在主服务器上写,只在从服务器上读
  • 主数据库处理事务性查询,而从数据库处理select查询
  • 数据库复制被用来把事务性查询导致的变更同步到集群中的从数据库
  • 超实用!!MySQL数据库读写分离_第1张图片

应用场景

  • 在企业应用中,在大量的数据请求下,单台数据库将无法承担所有的读写操作
  • 配置多台数据库服务器以实现读写分离
  • 在主从复制的基础上实现读写分离

一 、安装Amoeba

1. 所有服务器关闭Firewalld或者进行规则设置并搭建Mysql主从复制环境

搭建主从复制详细步骤链接

2.Amoeba服务器环境安装

安装jdk(因为amoeba是Java写的,所以我们需要安装jdk)

[root@localhost opt]# java -version   //检查java版本
-bash: java: command not found    //没有安装
[root@localhost ~]# ls   //查看jdk安装包
amoeba-mysql-3.0.5-RC-distribution.zip   jdk-8u144-linux-x64.tar.gz
[root@localhost ~]# tar xzvf jdk-8u144-linux-x64.tar.gz -C /opt
[root@localhost ~]# cd /opt
[root@localhost opt]# cp -rv jdk1.8.0_144/ /usr/local/java  //拷贝到/usr/local/目录下便于管理
[root@localhost ~]# vi /etc/profile    //设置环境变量
……省略部分
export JAVA_HOME=/usr/local/java
export JRE_HOME=/usr/local/java/jre
export PATH=$PATH:/usr/local/java/bin
export CLASSPATH=./:/usr/local/java/lib:/usr/local/java/jre/lib
[root@localhost ~]# java -version
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)
补充

因为Amoeba并不是支持所有的Java环境,支持1.5和1.6版本和少部分的1.8版本,因此在安装前需要检查Java版本,若不合适,则需要卸载重新安装。以下是卸载办法
卸载原有的java环境

[root@localhost ~]# java -version    ##确定java版本  1.8.0_181的 有点高
openjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-b13)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)

[root@localhost ~]# rpm -qa |grep java
java-1.8.0-openjdk-headless-1.8.0.181-7.b13.el7.x86_64          ####卸载
tzdata-java-2018e-3.el7.noarch
python-javapackages-3.4.1-11.el7.noarch
java-1.8.0-openjdk-1.8.0.181-7.b13.el7.x86_64                          ####卸载
javapackages-tools-3.4.1-11.el7.noarch
java-1.7.0-openjdk-1.7.0.191-2.6.15.5.el7.x86_64                      ####卸载
java-1.7.0-openjdk-headless-1.7.0.191-2.6.15.5.el7.x86_6       ####卸载

##查出openjdk相关的文件并且删除它###
[root@localhost ~]#  rpm -e --nodeps java-1.8.0-openjdk-headless-1.8.0.181-7.b13.el7.x86_64
[root@localhost ~]#  rpm -e --nodeps java-1.8.0-openjdk-1.8.0.181-7.b13.el7.x86_64
[root@localhost ~]#  rpm -e --nodeps java-1.7.0-openjdk-1.7.0.191-2.6.15.5.el7.x86_64
[root@localhost ~]#  rpm -e --nodeps java-1.7.0-openjdk-headless-1.7.0.191-2.6.15.5.el7.x86_64

3.解压Amoeba

[root@localhost ~]# yum -y install unzip  ##zip格式压缩包需要unzip程序解压
[root@localhost ~]# unzip amoeba-mysql-3.0.5-RC-distribution.zip -d /usr/local/
[root@localhost ~]# mv /usr/local/amoeba-mysql-3.0.5-RC/ /usr/local/amoeba
[root@localhost ~]# chmod -R 755 /usr/local/amoeba/   ##提权限
[root@localhost ~]# vi /usr/local/amoeba/jvm.properties
JVM_OPTIONS="-server -Xms1024m -Xmx1024m -Xss256k" 
//将32行JVM_OPTIONS="-server -Xms256m -Xmx1024m -Xss196k -XX:PermSize=16m -XX:MaxPermSize=96m" 进行修改

4.配置启动脚本,并设置为系统服务

[root@localhost ~]# vi /etc/init.d/amoeba   ##编写amoeba启动程序脚本
#!/bin/bash
#chkconfig: 35 62 62
#
export JAVA_HOME=/usr/local/java
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
NAME=Amoeba
AMOEBA_BIN=/usr/local/amoeba/bin/launcher
SHUTDOWN_BIN=/usr/local/amoeba/bin/shutdown
PIDFILE=/usr/local/amoeba/Amoeba-MySQL.pid
SCRIPTNAME=/etc/init.d/amoeba

case "$1" in
start)
echo -n "Starting $NAME... "
$AMOEBA_BIN
echo " done"
;;
stop)
echo -n "Stoping $NAME... "
$SHUTDOWN_BIN
echo " done"
;;
restart)
$SHUTDOWN_BIN
sleep 1
$AMOEBA_BIN
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart}"
exit 1
;;
esac
[root@localhost ~]# chmod +x /etc/init.d/amoeba
[root@localhost ~]# chkconfig --add amoeba
[root@localhost ~]# chkconfig --list amoeba  ##成功加入启动项
Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.
      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

amoeba         	0:off	1:off	2:off	3:on	4:off	5:on	6:off
[root@localhost ~]# service amoeba start  //开启Amoeba
Ctrl+c    ##放到后台
[root@localhost ~]# netstat -anpt | grep 8066    //检查监听状况,开启成功
tcp6    0     0 :::8066          :::*           LISTEN      28116/java    

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

1.在master、slave1和slave2中执行以下操作开放权限给Amoeba

[root@localhost ~]# mysql -uroot -p
……省略部分
mysql> create database test;   //注意,一定要创建和授权账号同名的库,否则后面会出错
Query OK, 1 row affected (0.01 sec)
mysql> GRANT ALL  ON *.* TO test@'20.0.0.%' IDENTIFIED BY 'abc123';   //授权账号test,密码abc123
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;    //重新加载授权
Query OK, 0 rows affected (0.01 sec)

2.修改amoeba.xml配置文件

(代码前面的数字是对应配置文件中的行数,方便查找)

[root@localhost ~]# cd /usr/local/amoeba/conf
[root@localhost conf]# vim amoeba.xml
     28                                         <property name="user">amoeba</property>
     29 
     30                                         <property name="password">abc123</property>
     83                 <property name="defaultPool">master</property>
     84                 <property name="writePool">master</property>  //去掉这里的注释
     85                 <property name="readPool">slaves</property>   //去掉这里的注释

3.修改代表dbServers.xml配置文件

(代码前面的数字是对应配置文件中的行数,方便查找)

     22                         <!-- mysql schema -->
     23                         <property name="schema">mysql</property>
     24 
     25                         <!-- mysql user -->
     26                         <property name="user">test</property>
     27 
     28                         <property name="password">abc123</property>
     43         <dbServer name="master"  parent="abstractServer">
     44                 <factoryConfig>
     45                         <!-- mysql ip -->
     46                         <property name="ipAddress">20.0.0.12</property>
     47                 </factoryConfig>
     48         </dbServer>
     49 
     50         <dbServer name="slave1"  parent="abstractServer">
     51                 <factoryConfig>
     52                         <!-- mysql ip -->
     53                         <property name="ipAddress">20.0.0.18</property>
     54                 </factoryConfig>
     55         </dbServer>
     56 
     57         <dbServer name="slave2"  parent="abstractServer">
     58                 <factoryConfig>
     59                         <!-- mysql ip -->
     60                         <property name="ipAddress">20.0.0.19</property>
     61                 </factoryConfig>
     62         </dbServer>
     64         <dbServer name="slaves" virtual="true">
     65                 <poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
     66                         <!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->
     67                         <property name="loadbalance">1</property>
     68 
     69                         <!-- Separated by commas,such as: server1,server2,server1 -->
     70                         <property name="poolNames">slave1,slave2</property>

4.启动Amoeba

[root@localhost ~]# service amoeba restart
[root@localhost ~]#  netstat -anpt | grep java   //端口开启成功
tcp6       0      0 :::8066                 :::*                    LISTEN      19029/java          
……省略部分

三、验证试验结果

1.首先关闭主从同步,去两台从服务器slave1和slave2,关闭同步复制,否则会影响试验验证

[root@localhost ~]# mysql -uroot -p
……省略部分
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)

2.验证两台从服务器的“只读”效果

验证设计:
在关闭主从同步的前提下,我们同时在三台服务器中写入数据,客户机应当只读两台从服务器slave的数据,而不会读取master中的数据

2.1在master中插入数据

mysql> use test;
mysql> create table list (name char(16),id char(16));
Query OK, 0 rows affected (0.00 sec)
mysql> insert into list values('master',1);
Query OK, 1 row affected (0.00 sec)
mysql> select * from list;
+-------------+------+
| name   | id   |
+--------+------+
| master | 1    |
+--------+------+
1 row in set (0.00 sec)

2.2在slave1中插入数据

mysql> use test;
mysql> create table list (name char(16),id char(16));
Query OK, 0 rows affected (0.00 sec)
mysql> insert into list values('slave1',2);
Query OK, 1 row affected (0.00 sec)
mysql> select * from list;
+--------+------+
| name   | id   |
+--------+------+
| slave1 | 2    |
+--------+------+
1 row in set (0.00 sec)2.3)在slave2中插入数据
mysql> use test;
mysql> create table list (name char(16),id char(16));
Query OK, 0 rows affected (0.00 sec)
mysql> insert into list values('slave2',3);
Query OK, 1 row affected (0.00 sec)
mysql> select * from list;
+--------+------+
| name   | id   |
+--------+------+
| slave2 | 3    |
+--------+------+

2.4去客户机测试

MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| bbs                |
| myadm              |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
7 rows in set (0.01 sec)

MySQL [(none)]> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

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

MySQL [test]> select * from list;
+--------+------+
| name   | id   |
+--------+------+
| slave2 | 3    |
+--------+------+
1 row in set (0.01 sec)

3. 验证master的“只写”功能

验证设计:
在客户端写入数据,应该只是在master服务器中写入数据,在没有主从同步的前提下,从服务器应该是没有客户机写入的数据的。

3.1首先在客户机写入数据

MySQL [test]> create table zhang1 (id int(10),name varchar(10),address varchar(20));
Query OK, 0 rows affected (0.03 sec)
MySQL [test]>  insert into zhang1 values('4','zhang','write_test');
Query OK, 1 row affected (0.04 sec)

3.2去master查看表数据

mysql> select * from zhang1;
+------+-------+------------+
| id   | name  | address    |
+------+-------+------------+
|    4 | zhang | write_test |
+------+-------+------------+
1 row in set (0.00 sec)

3.3去slave1和slave2查看表数据

mysql> select * from zhang1;
ERROR 1146 (42S02): Table 'test.zhang1' doesn't exist

你可能感兴趣的:(数据库管理,数据库,mysql,linux)