Introduction separate read and write
Separate read and write particularly suitable for reading the scene, a write-only, read-only one, improve the efficiency of reading.
Realization of ideas
premise:
Separate read and write is built on two machines, and the two machines is done from the master copy, write only the main library, read from the library, in order to achieve.
achieve:
The first:
In the main library users to create a write-only, write-only create a user from the library, let the program to connect to a different server can read and write to achieve separation.
The second:
Through a proxy software, this benefit is does not need to care about which server write and read operations are connected, you can just send the machine to the proxy, the agent sent to a different judge mysql server.
Achieved by software agents are: mysql-proxy, amoeba
Implementing a proxy mode
Graphic:
surroundings:
We need to open three machines.
192.168.101 # Main Library - Write-only
192.168.102 # from the library - read-only
Acting 192.168.100 #
Install lua scripting language:
# This is the mysql-proxy need
Official Download:
http://www.lua.org/ftp/
( 1 ) mounted reliance yum the install the glibc the glibc binutils CPP -kernheaders the glibc-devel-GCC Common the glibc the make the readline-devel - Y ( 2 ) download package wget HTTP: // www.lua.org/ftp/lua-5.3.5. -P the tar.gz / opt / ( . 3 ) extracting archive CD / opt / the tar XF Lua - 5.3 . . 5 .tar.gz ( . 4 ) to modify the Makefile CD / opt / lua- 5.3 . . 5 / VI Makefile # set INSTALL_TOP = / usr / local / Lua ( 5 ) compiled the make Linux &&install the make ( 6 ) Add the environment variable vim / etc / Profile added: Export LUA_HOME = / usr / local / Lua Export the PATH = $ the PATH: $ LUA_HOME / bin environment variables to take effect: Source / etc / Profile
Install mysql-proxy:
(1) Download mysql-proxy package
wget https://cdn.mysql.com/archives/mysql-proxy/mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz -P /opt/
(2) unzip the package and create a soft link
tar xf mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz
ln -s mysql-proxy-0.8.5-linux-el6-x86-64bit mysql-proxy085
(3) create logs directory
mkdir /opt/mysql-proxy085/logs
(4) Add the environment variable
echo "PATH=/opt/mysql-proxy085/bin/:$PATH" >> /etc/profile
source /etc/profile
(5) configuration file proxy
vim /etc/mysql-proxy.cnf
Configuring Content:
[mysql- proxy] # run mysql - proxy user the User = root # mysql - proxy connect to the backend server mysql user ADMIN -username = mysql_proxy_user # mysql - password proxy connection mysql backend server ADMIN -password = 123456 # agents monitor address, the default port 4040. Proxy -address = 0.0 . 0.0 : 3307 # specify the rear end of the main data is written master Proxy -backend-addresses = 192.168 . 1.101 : 3306 # slave reads data from a backend Proxy -read-only- Addresses =-backend 192.168 .1.102 : 3306 # separate read and write the configuration file location specified Proxy -lua-Script = / opt / MySQL-proxy085 / Share / DOC / MySQL-Proxy / rw- splitting.lua # log location log -file = / opt / MySQL-proxy085 / logs / mysql- proxy.log # define log log levels, from high to low, respectively (error | warning | info | the Message | Debug) log -Level = Debug # to run in daemon mode daemon = to true #mysql - Proxy collapse when trying to restart Keepalive = to true
# Self-configuration parameter modified
(6) modify the configuration file permissions
chmod 660 /etc/mysql-proxy.cnf
(7) the main library add authorized users
grant all privileges on *.* to 'mysql_proxy_user'@'192.168.1.%' identified by '123456';
flush privileges;
(8) Boot Agent
mysql-proxy --defaults-file=/etc/mysql-proxy.cnf
(9) connected through a proxy connection broker accounts opened ip and port
Note: This machine premise of your connection get hold mysql client
# Test if: mysql-extracting package, and you can add environment variables
# Backend words: 1.100 directly connected to the machine through the 3307 port module
mysql -umysql_proxy_user -p123456 -h192.168.1.100 --port=3307
(10) Success
Note: lua is a must to be installed
test results:
Note: A write operation from the main two machines are operating, because they are master-slave replication.
Want to see results need to modify the lua script, because it is limited (indicates how many connections before opening separate read and write):
vim /opt/mysql-proxy085/share/doc/mysql-proxy/rw-splitting.lua
# Min_idle_connnections parameter indicates the minimum number of connections, began to separate read and write
Query the sql statement to read and write times:
show global status where Variable_name in('com_select','com_insert','com_delete','com_update');
show global staus like 'com_select'; # single