Implementation of high-performance mysql read-write separation (5)

One: MySQL read and write separation

1.1: Overview of mysql read-write separation

1. What is read-write separation?
Read and write separation, the basic principle is to let the main database handle transactional add, modify, and delete operations (INSERT, UPDATE, DELETE), and handle SELECT query operations from the database.

Database replication is used to synchronize changes caused by transactional operations to the slave database in the cluster.

2. Why should read and write separation?
Because the database "write" (write 10,000 data to Oracle may take 3 minutes) operation is relatively time-consuming.
But the "read" of the database (read 10000 data from oracle may only take 5 seconds).

Therefore, the separation of reading and writing, the solution is that the writing of the database affects the efficiency of the query.

1.2: Principle of mysql read-write separation

In an actual production environment, reading and writing to the database are in the same database server, which cannot meet actual needs.

Whether it is in terms of security, high availability, or high concurrency, it is completely unable to meet actual needs.

Therefore, the data is synchronized through master-slave replication, and the concurrent load capacity of the database is improved through read-write separation.

It's a bit similar to the rsync we learned earlier, but the difference is that rsync backs up disk files, while mysql master-slave replication backs up data and statements in the database.
Insert picture description here

Two: MySQL read-write separation configuration

2.1: mysql middleware-Amoeba

Amoeba (amoeba) project, the open source framework released a software Amoeba for mysql in 2008.

The software is dedicated to the front-end proxy layer of mysql's distributed database. Its main function is to act as a SQL routing function when application services access the mysql server. It has load balancing, high availability, SQL filtering, read-write separation, and can route related SQL to the target. Database, can concurrently request multiple databases to merge the results.

Through Amoeba, the functions of high availability, load balancing, and data slicing of multiple data sources can be completed.

Amoeba is currently used on the production lines of many companies; its version can be downloaded on the official website. The working principle diagram is as follows:Insert picture description here

2.2: MySQL server configuration with read-write separation

1: Install the gcc environment

yum -y install gcc*

2: Install jdk

rpm -ivh jdk-8u131-linux-x64_.rpm

Set environment variables:
vim /etc/profile

export JAVA_HOME=/usr/java/jdk1.8.0_131
export PATH=${JAVA_HOME}/bin:${JAVA_HOME}/jre/bin:$PATH

Environmental variables take effect:

source  /etc/profile

3: Install amoeba

下载软件包:
wget https://jaist.dl.sourceforge.net/project/amoeba/Amoeba%20for%20mysql/3.x/amoeba-mysql-3.0.5-RC-distribution.zip
unzip amoeba-mysql-3.0.5-RC-distribution.zip
重命名:
mv amoeba-mysql-3.0.5-RC /usr/local/amoeba-mysql

4: Configure amoeba

The configuration file of amoeba is located in the conf directory under the installation directory, to realize the read-write separation function, only need to modify two configuration files, dbServers.xml and amoeba.xml.

Add authorized users on the master and slave nodes (amoeba:123):

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

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

Edit the amoeba.xml configuration file:

The user name and password (amoeba:123456)
Insert picture description here
to access amoeba are set to access the address pool:

The write operation points to the master address pool and the
read operation points to the slaves address pool.
Insert picture description here
Modify the dbServers.xml configuration file:

Configure the username and password for logging in to mysql.

 25                         <!-- mysql user -->
 26                         <property name="user">amoeba</property>
 27 
 28                         <property name="password">123</property>

 43         <dbServer name="master"  parent="abstractServer">
 44                 <factoryConfig>
 45                         <!-- mysql ip -->
 46                         <property name="ipAddress">192.168.10.130</property>
 47                 </factoryConfig>
 48         </dbServer>
 49 
 50         <dbServer name="slave1"  parent="abstractServer">
 51                 <factoryConfig>
 52                         <!-- mysql ip -->
 53                         <property name="ipAddress">192.168.10.131</property>
 54                 </factoryConfig>
 55         </dbServer>
 56 
 57         <dbServer name="slaves" virtual="true">
 58                 <poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
 59                         <!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->
 60                         <property name="loadbalance">1</property>
 61 
 62                         <!-- Separated by commas,such as: server1,server2,server1 -->
 63                         <property name="poolNames">slave1</property>
 64                 </poolConfig>
 65         </dbServer>

Modify jvm.properties:

vim amoeba-mysql/jvm.properties

JVM_OPTIONS="-server -Xms1024m -Xmx1024m -Xss256k -XX:PermSize=16m -XX:MaxPermSize=96m"

Start the service:

[root@localhost ~]# nohup /usr/local/amoeba-mysql/bin/launcher &
[2] 37937
[root@localhost ~]# nohup: 忽略输入并把输出追加到"nohup.out"

2.3: MySQL read-write separation test

1: Stop master-slave replication

slave stop;

In order to test the read and write must, first stop the master-slave replication of mysql, in order to more clearly see which server is the read-write server

2: Log in to the amoeba service

[root@localhost local]# mysql -uamoeba -p123456 -h192.168.10.130 -P8066
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 1326836667
Server version: 5.1.45-mysql-amoeba-proxy-3.0.4-BETA MariaDB Server

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

3: Insert a piece of test data

MySQL [test]> insert into s1 values (2,'haha');
Query OK, 1 row affected (0.00 sec)

#读的是从库
MySQL [test]> select * from s1;
+------+------+
| id   | name |
+------+------+
|    1 | hehe |
+------+------+
1 row in set (0.00 sec)

4: Log in to the main library and execute

#新增数据已经在master节点了

MariaDB [test]> select * from s1;
+------+------+
| id   | name |
+------+------+
|    1 | hehe |
|    2 | haha |
+------+------+
2 rows in set (0.01 sec)

The experimental results show that: writing is only performed on the master, and reading is performed on the slave

Guess you like

Origin blog.csdn.net/zhangshaohuas/article/details/108996338