1. IntroductionToday’s MySQL is widely used. As users increase and the amount of data increases, high concurrency follows. However, we have many ways to ease the pressure on the database. Distributed database, load balancing, read and write separation, adding cache servers, and more. Here we will use the progress of reading and writing separation technology to ease the pressure on the database. Show
There are many ways to implement the technology of reading and writing separation. Here we will use the intermediate software mysql-proxy. This software contains a read and write separated lua file, which is also a file we must use to achieve read and write separation using mysql-proxy, it requires a lua parser for parsing. So we also need to install a lua parser. 2. The basic environmentThree linux virtual hosts Linux version CentOS6.6, MySQL 5.5 mysql-proxy-0.8.5 lua-5.1.4 IP: 192.168.95.11 (write), 192.168.95.12 (read), 192.168.95.13 (mysql-proxy) 3. Configure master-slave replicationA rough introduction to the configuration of the master-slave replication of the database: First step: Create a MySQL user that can log in to the 192.168.95.12 host in 192.168.95.11. User: mysql12 Password: mysql12 mysql>GRANT REPLICATION SLAVE ON *.* TO ‘mysql12’@’192.168.95.12’ IDENTIFIED BY ‘mysql12’; The second step: View 192.168.95.11 MySQL server binary file name and location mysql>SHOW MASTER STATUS; Third step: Tell the binary file name and location Executed in 192.168.95.12: mysql> change master to The fourth step: In 192.168.95.12 mysql>SLAVE START; #Open copymysql>SHOW SLAVE STATUS\G #View whether the master-slave replication is configured successfully Master-slave replication configuration is successful!
4. MySQL read and write separation configuration4.1. Install luaOfficial website download: http://www.lua.org/download.html Lua is a small scripting language. Lua is written in standard C. The code is simple and beautiful, and can be compiled and run on almost all operating systems and platforms. A complete Lua interpreter is only 200k, and Lua is the fastest in all current scripting engines. All of this determines that Lua is the best choice for embedded scripting. 1) Installing lua depends on many packages. You can check if the following software is installed by rpm -qa | grep name: gcc*、gcc-c++*、autoconf*、automake*、zlib*、libxml*、ncurses-devel*、libmcrypt*、libtool*、flex*、pkgconfig*、libevent*、glib* If the relevant package is missing, you can install it online via yum -y install, or directly from the system installation CD and install it via rpm -ivh. (My words are usually found directly in the system CD software library directly rpm installation, some can not be found, then downloaded online and then passed to ftp in ftp and then installed) 2) After the software is installed, compile and install lua The read-write separation of MySQL-Proxy is mainly implemented by the rw-splitting.lua script, so you need to install lua. Official website download: http://www.lua.org/download.html (download source package) # wget http://www.lua.org/ftp/lua-5.1.4.tar.gz 4.2. Install mysql-proxy1) First check the linux version to confirm whether it is 32-bit or 64-system View the Linux kernel version # cat /etc/issue View linux version # cat /proc/version 2) Download by system digits 3) Installation # tar –zxvf mysql-proxy-0.8.5- linux-rhel5-x86-64bit.tar.gz Successful installation 5. MySQL read and write separation test1. Modify the rw-splitting.lua fileModify the default connection and perform a quick test. If you do not modify it, you must enable the read/write separation when the number of connections is 4. #cp /usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua ./ # vi rw-splitting.lua 2. After the modification is complete, start mysql-proxy# cd /usr/local/mysql/bin parameter: — proxy-read-only-backend-addresses #read-only server address (ip) — proxy-backend-addresses #server address (primary server) — proxy-lua-script #luaScript Road & # indicates background execution 3. Create a database connection user for read and write separationUsername: proxy1 Password: 321 mysql>grant all on *.* to 'proxy1'@'192.168.95.13' identified by '321'; [Because the master-slave replication has been enabled, this user is created in the 11 and 12 hosts mysql] 4. Test login account to add dataYou can use any ip client to login to this account. Log in at 192.168.95.13: # ./mysql -u proxy1 -P4040 -h292.168.95.13 –p View results in two mysql: consistent The result shows: account usage (ps: id is self-growth. Before the high master and master replication, the configuration file was changed. If it has not been changed, it will be used first.) 5. Close 12mysql copymysql> stop slave; 6. Proof of writing separationOpen multiple clients to open data using account Open three mysql clients to insert 2 pieces of data: mysql> insert into tab1 (name) values('stop_slave11111');….mysql> insert into tab1 (name) values('stop_slave6666’); View: Log in 11mysql and 12mysql respectively to view the data in aa.tab1 Primary database: From the database: The result shows that the inserted data exists with the primary database, but not from the database, so the proof write can be separated. 7. Prove reading separationLog in to mysql using the account to view the data in aa.tab1. mysql>use aa; The results show that only the data from the database, combined with the above test, can prove read separation. 6. SuggestionsIn order to facilitate the startup and management of mysql-proxy, you can create a mysql-proxy service management script. The following management script is only suitable for the installation path location I gave above. [This management script needs to be modified according to its own installation path to be used] #!/bin/sh# Some related parameters: PROXY_PATH=/usr/local/mysql-proxy/bin //Define the mysql-proxy service binary path — proxy-read-only-backend- addresses = 192.168.95.12: 3306 //
define backend server address from the read-only PROXY_PID=/usr/local/mysql-proxy/run/mysql-proxy.pid //Define the mysql-proxy PID file path — daemon / / start to start in the daemon mode — (The above are some of my own insights and conclusions. If there are any shortcomings or mistakes, please point out) |