Mysql-- separate read and write

Content Highlights:

A, Mysql principle of separate read and write

Second, on the Amoeba

Third, the configuration examples

Step 1: Configure Server Amoeba

Step two: add permissions on three mysql server, open access to the amoeba (ie amoeba three mysql allow them access)

Step Three: Configure separate read and write functions on the server amoeba

Step four: Client Test

IV Summary

A, Mysql principle of separate read and write

  • Separate read and write only on the primary server is written, only read from the service;

  • The main transactional database query processing from database processing select queries;

  • Database replication is used to query the transaction due to changes sync to the cluster from the database

Second, on the Amoeba

  • (1) Amoeba is a MySQL to as the underlying data store, and MySQL applications Proxy protocol interface. It is concentrated in response to the application request, the user according to the rules set in advance, the SQL execution request to a specific database. Based on this load balancing can be achieved, separate read and write, and high availability requirements.

  • (2) Amoeba SQL request is the router, for the purpose of load balancing, separate read and write, provides a mechanism for high availability, they are not fully realized. It requires a combination of MySQL Replication and other mechanisms to achieve replica synchronization.


Third, the configuration examples

Environment Description:


  • Prepare three Mysql server (a server-based, the other two are from the server);

  • A Amoeba, for separate read and write, the write operation to the host server, both a read operation issued from the server;

  • Experiments: on the client write operations, in the main, to verify from the server.

image.png

Description: From the replication and read and write functions are closely related to the main Mysql, data synchronization is achieved by copying from the primary, and then to enhance the load capability database concurrency by separate read and write.


Application client 192.168.220.131
Amoeba 192.168.220.129
Primary server 192.168.220.141
From the server 1 192.168.220.140
From the server 2 192.168.220.136


Step 1: Configure Server Amoeba

(1) turn off the firewall:

 systemctl stop firewalld.service
 setenforce 0

(2) Installation Management jdk:

1, the installation jdk

 cp jdk-6u14-linux-x64.bin  /usr/local/     //复制
./jdk-6u14-linux-x64.bin                    
//安装jdk ,注意这一步,一路按回车到最后,提示输入yes,等待安装

2、方便管理,将jdk包重新命名:


 mv jdk1.6.0_14/ /usr/local/jdk1.6


3、修改 profile 文件


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

使环境变量生效:

source /etc/profile


(3)解压 amoeba包:


1、先创建一个文件

mkdir /usr/local/amoeba

2、解压

tar zxvf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba/


3、赋权,并验证是否安装成功

chmod -R 755 /usr/local/amoeba/


输入:/usr/local/amoeba/bin/amoeba ,按回车检查是否成功

2019112217202888.png


第二步:在三台 mysql 服务器上添加权限,开放给 amoeba 访问(即三台mysql允许amoeba对它们进行访问)

进入数据库,输入以下命令:


grant all on *.* to test@'192.168.220.%' identified by '123.com';
//允许ip为192.168.220网段从任意终端通过123.com的密码进行访问


第三步:在 amoeba 服务器上配置读写分离功能

1、cd /usr/local/amoeba
vim conf/amoeba.xml
第30行:<property name="user">amoeba</property>
第32行:<property name="password">123456</property>
117行去掉注释:
 <property name="writePool">master</property>
 <property name="readPool">slaves</property>


2、vim conf/dbServers.xml

26-29行:去掉注释,设置登录用户名和密码
<property name="user">test</property>
<property name="password">123.com</property>
找到主服务器和从服务器模块,修改好名称和对应IP地址:
主服务器:
<dbServer name="master" parent="abstractServer">
<property name="ipAddress">192.168.220.141</property>
第一台从服务器:
<dbServer name="slave1" parent="abstractServer">
<property name="ipAddress">192.168.220.140</property>
第二台从服务器:
<dbServer name="slave2" parent="abstractServer">
<property name="ipAddress">192.168.220.136</property>
指定从服务器池:
<dbServer name="slaves" virtual="true">
<property name="poolNames">slave1,slave2</property>

3、启动 amoeba

/usr/local/amoeba/bin/amoeba start&        //&表示将进程放到后台,时间较长,稍等待


第四步:客户端测试

1、客户端虚拟机,可以直接用yum安装一个mysql服务

yum install -y mysql


2、连接 amoeba 服务器


(1)指定密码和端口连接 amoeba

mysql -u amoeba -p 123456 -h 192.168.220.129 -P8066


(2)创建一个 名为 school 的库,再创建一个叫 info 的表


MySQL [school]> create table info (
    -> id int(4) not null primary key,      //指定id号,为primary key,不为空
    -> name varchar(10) not null,           //名字
    -> score decimal(4,1) not null);        //成绩
Query OK, 0 rows affected (0.03 sec)


3、三台mysql服务器验证实验结果


首先验证写的操作是否分离:


(1)此时,我们在主服务器上,直接进入数据库,就会发现也多了一个名为 info的表

image.png

(2)但是,两台从服务器上并没有:

image.png

所以,我们在客户端写入的语句,在主服务器上会查询到,但是两台从服务器上并没有,此时写的操作就直接交给了主服务器。


接下来是验证读的操作:


(1)在两台从服务器上的各自也创建一个 info 表,也都各自写入两条信息。

image.png

image.png

(2)客户端查询 info 表的内容,因为做了读写分离操作,所以查询的内容应该是 两台从服务器上 info表的内容。

image.png

因此,用户读取数据,同时通过从服务器,这样就减轻了主服务器的并发量。


四、总结

This blog, read and write in order to show the effect of the separation experiments, and did not call the shots from the synchronization while, in the enterprise network, master-slave synchronization and separation of reading and writing are inextricably linked. In this way, the customer writes to the database operations, is assumed by the master server; when read, query and other operations, will be borne by the server (as do a master-slave synchronization, each of the data from the server and the main server the same)

At high concurrent requests, achieving load balancing to ensure the efficient operation of data security, server.

Guess you like

Origin blog.51cto.com/14475876/2461893