How to use amoeba to separate reading and writing of mysql database
There are many read-write separation architectures about mysql. Baidu almost all uses mysql_proxy to implement it. Since the proxy is implemented based on the Lua script language, many netizens on the Internet said that the proxy is not efficient and unstable, and is not recommended for use in a production environment;
amoeba is a project developed by Alibaba that separates database reading and writing (read Write separation is only a small function of it). Since it is written based on java, the running environment needs to install jdk;
Related tutorials: mysql video tutorial
Preliminary preparation Work:
1. Two databases, one master and one slave, master-slave synchronization;
master: 172.22.10.237:3306; The main database is responsible for writing operations;
slave: 10.4.66.58:3306; Slave The library is responsible for reading operations;
amoeba: 172.22.10.237:8066; I installed amoeba on the server where the main library is located. Of course, you can also install it on the third server;
All server operating systems are centos7;
2. Configure and install jdk on the server where amoeba is located;
I installed jdk1.8;
The path is: JAVA_HOME=/usr/local/java/jdk1.8.0_131
Be sure to build and configure the above by yourself. The master and slave work normally. Add the jdk environment variable: /etc/profile;
There are many ways to install amoeba, so I won’t waste any time on the installation here. Download the installation package of amoeba-mysql-3.0.5-RC-distribution and unzip it directly to use;
Unzip directory: /usr/local/amoeba/
Obviously the configuration file is in conf, and the startup program is in bin;
As mentioned just now, amoeba has more functions than reading and writing separation, but if you only use the reading and writing separation function, you only need to configure these files: conf/dbServers.xml conf/amoeba.xml and bin/launcher;
conf/dbServers.xml:
`<property name="port">3306</property> #设置Amoeba要连接的mysql数据库的端口,默认是3306 <property name="schema">testdb</property> #设置缺省的数据库,当连接amoeba时,操作表必须显式的指定数据库名,即采用dbname.tablename的方式,不支持 use dbname指定缺省库,因为操作会调度到各个后端dbserver <property name="user">test1</property> #设置amoeba连接后端数据库服务器的账号和密码,因此需要在所有后端数据库上创建该用户,并授权amoeba服务器可连接 <property name="password">111111</property> <property name="maxActive">500</property> #最大连接数,默认500 <property name="maxIdle">500</property> #最大空闲连接数 <property name="minIdle">1</property> #最新空闲连接数 <dbServer name="writedb" parent="abstractServer"> #设置一个后端可写的数据库,这里定义为writedb,这个名字可以任意命名,后面还会用到 <property name="ipAddress">172.22.10.237</property> #设置后端可写dbserver的ip <dbServer name="slave01" parent="abstractServer"> #设置后端可读数据库 <property name="ipAddress">10.4.66.58</property> <dbServer name="myslave" virtual="true"> #设置定义一个虚拟的dbserver,实际上相当于一个dbserver组,这里将可读的数据库ip统一放到一个组中,将这个组的名字命名为myslave <property name="loadbalance">1</property> #选择调度算法,1表示复制均衡,2表示权重,3表示HA, 这里选择1 <property name="poolNames">slave01</property> #myslave组成员`
conf/amoeba.xml:
#启动脚本,需要配置jdk环境变量; #在注释后的第一行添加: JAVA_HOME=/usr/local/java/jdk1.8.0_131
launcher is the startup script, if If JAVA_HOME is not configured, even if you configure environment variables in /etc/profile, you may get an error: jdk environment variables are not configured;
There is also a configuration file: jvm.properties
#占用内存配置文件 # -Xss参数有最小值要求,必须大于228才能启动JVM #修改: JVM_OPTIONS="-server -Xms1024m -Xmx1024m -Xss256k -XX:PermSize=16m -XX:MaxPermSize=96m"
Experienced Everyone in operation and maintenance knows that anything related to jdk is basically related to memory tuning, and amoeba is no exception;
It can be started now:
After startup, you can see the 8066 port of the machine:
At this time, you only need to pass the 8066 port of the machine ip and Just use the account and password set in your configuration file to connect to the database. The data written will go to the master, and the data read will be read from the slave;
Test:
Close the master database and it can still be read. : Execute the select view command;
or
Close the slave database, you can still write: Execute the update and inster commands;
The above is the detailed content of How to use amoeba to separate reading and writing of mysql database. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

When developing an e-commerce website using Thelia, I encountered a tricky problem: MySQL mode is not set properly, causing some features to not function properly. After some exploration, I found a module called TheliaMySQLModesChecker, which is able to automatically fix the MySQL pattern required by Thelia, completely solving my troubles.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.
