Mysql Cluster 1: master-slave replication, using mysql-proxy for load balancing Mysql Cluster
There are many mysql Cluster architecture methods, and different architectures are implemented according to different requirements. Simply put, mysql replication is the replication function of Mysql. the modes include master-slaves, master-slaves, master-slaves, and so on can have multiple layers, so now I want to talk about the master-slaves mode (the principles of other modes are basically the same ), then, use the mysql-proxy officially provided by Mysql to implement read/write splitting to achieve load balancing.
Environment:
Host: master: 192.168.1.109, slave1: 192.168.1.110, mysqlProxy: 192.168.1.112. (Create virtual machine simulation on workstation10 platform)
Operating system: Red Hat Enterprise Linux Server release 5.8
Mysql: mysql-5.5.37-linux2.6-i686.tar.gz
Mysql-proxy: mysql-proxy-0.8.4-linux-glibc2.3-x86-32bit.tar.gz
1. install mysql:
[Root @ localhost ~] # Cd/usr/local // enter the installation directory
[Root @ localhost local] # tar-zxvf mysql-5.5.37-linux2.6-i686.tar.gz // Unzip the mysql binary package
[Root @ localhost local] # ln-s mysql-5.5.37-linux2.6-i686 mysql // Unzip and add the link
[Root @ localhost local] # groupadd mysql // add a mysql User Group
[Root @ localhost local] # useradd-g mysql // add a mysql User account
[Root @ localhost local] # cd mysql // enter the mysql directory after installation
[Root @ localhost mysql] # chown-R root: mysql. // modify the file Group and owner. Note that the command is followed by a vertex indicating the current directory
[Root @ localhost mysql] # chown-R mysql: mysql data // change the owner of the data directory to mysql
[Root @ localhost mysql] # cp support-files/my-large.cnf/etc/my. cnf // Copy the mysql configuration file under the etc directory and rename it my. cnf
[Root @ localhost mysql] # cp support-files/mysql. server/etc/init. d/mysqld // Copy the mysql startup script to the/etc/init. d/directory.
[Root @ localhost mysql] # service mysqld start // start mysql
So far, mysql installation is complete, followed by the master-slave configuration of mysql. (Install mysql on the other two hosts)
2. configure master:
[Root @ localhost mysql] # vi/etc/my. cnf // edit the configuration file
Server-id = 1 // The default value is 1. if not, change it to 1.
Thread_concurrency = 2 // number of concurrent threads (CPU * 2)
Log-bin = mysql-bin // enable the binary log function
Save and exit, and restart mysql.
Log on to mysql:
[Root @ localhost mysql] # mysql-uroot-p // the default password is null. press enter to enter
Mysql> grant replication slave on *. * to repl @ '192. 192.% 'identified by '123 ';
Mysql> flush privileges;
Mysql> show grants for 'repl' @ '192. 192.% '; // view the authorization. if there is a record, OK
Mysql> show master status/G; // You can view the status of the master server, including the binary log file name and position)
So far, the mysql configuration is complete.
3. configure slave
[Root @ localhost mysql] # vi/etc/my. cnf // edit the configuration file
Server-id = 11 // Set it to be different from the master,
Thread_concurrency = 2 // number of concurrent threads (CPU * 2)
Relay-log = mysql-log // enable relay log
Relay-log-index = mysql-log.index // Set relay-log-index file name
Save and exit, and restart mysql.
Log on to mysql:
[Root @ localhost mysql] # mysql-uroot-p // the default password is null. press enter to enter
Mysql> change master
-> Master_host = '192. 168.1.109 ', // master server ip address
-> Mastet_user = 'repl', // User name
-> Mastet_password = '000000', // password
-> Master_log_file = 'MySQL-bin.000001 ', // binary log name of the master server
-> Master_log_pos = 107;
Mysql> slave start; // start slave
Mysql> show slave status/G; // view the slave STATUS
* *************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.1.109
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 4
Relay_Log_File: mysql-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: yes
Slave_ SQL _Running: yes
... Omitted...
Seconds_Behind_Master: NULL
If Slave_IO_Running and Slave_ SQL _Running are displayed as yes, the configuration is OK, which may occur during the process:
Slave I/O: error connecting to master 'repl @ 192.168.1.109: 3306 '-retry-time: 60 retries: 86400, Error_code: 2003
The cause is that the firewall is blocked. the solution is to modify the firewall configuration. The simple and direct method is to turn off the firewall of the master server and run the/etc/init. d/iptables stop command.
Next, create a database db_test in the master to check whether db_test exists in the slave.
Now the slave configuration is complete, and other slave servers are configured in this way.
4. configure mysql-proxy. it is best to deploy another machine for the proxy service, especially after it is launched. Therefore, a separate virtual machine is used for testing and is not deployed on the master or slave machine.
[Root @ localhost local] # tar-zxvf mysql-proxy-0.8.4-linux-glibc2.3-x86-32bit.tar.gz // Unzip
[Root @ localhost local] # ln-s mysql-proxy-0.8.4-linux-glibc2.3-x86-32bit mysql-proxy // add a shortcut
[Root @ localhost local] # groupadd mysql-proxy // Create a user group
[Root @ localhost local] # useradd-g mysql-proxy // Create a user
[Root @ localhost local] # cd mysql-proxy // enter the mysql-proxy Directory
[Root @ localhost mysql-proxy] # chown-R root: mysql-proxy. // change the directory owner and group. a dot (.) after the command indicates the current directory
[Root @ localhost mysql-proxy] # vi/etc/profile. d/mysql-proxy.sh // add the bin directory of mysql to the PATH variable,
export
PATH=$PATH:
/usr/local/mysql-proxy/bin
[Root @ localhost mysql-proxy] # source/etc/profile // Make the configuration file take effect immediately
[Root @ localhost mysql-proxy] # mysql-proxy -- help-all // view the mysql-proxy command, the following information is displayed:
Next we will start mysql-proxy for a simple test, but we should first briefly introduce the mysql-proxy command.
Mysql-proxy command
-- Help-all ---- get all help information
-- Proxy-address = host: port ---- address and port of the proxy service listener
-- Admin-address = host: port ---- address and port listened by the management module
-- Proxy-backend-addresses = host: port ---- address and port of the backend mysql server (master server)
-- Proxy-read-only-backend-addresses = host: port ---- address and port of the backend read-only mysql server (slave server)
-- Proxy-lua-script = file ---- Lua script for completing the mysql proxy function
-- Daemon ---- start mysql-proxy in daemon mode
-- Defaults-file =/path/to/conf_file_name ---- default configuration file path
-- Log-file =/path/to/log_file_name ---- log file name
-- Log-level = level ---- log level
-- Log-use-syslog ---- syslog-Based Logging
-- User = user_name ---- user who runs the mysql-proxy process
[Root @ localhost mysql-proxy] # mysql-proxy -- daemon -- log-level = debug -- log-file =/var/log/mysql-proxy.log -- plugins = "proxy" -- proxy- backend-addresses = "192.168.1.109: 3306 "-- proxy-read-only-backend-addresses =" 192.168.1.110: 3306 "// start mysql-proxy [root @ localhost mysql-proxy] # tail/var/log/mysql-proxy.log // View startup log
2014-05-00: 09: 22: (critical) plugin proxy 0.8.4 started
2014-05-00: 09: 22: (debug) max open file-descriptors = 1024
2014-05-00: 09: 22: (message) proxy listening on port: 4040
2014-05-00: 09: 22: (message) added read/write backend: 192.168.1.109: 3306
2014-05-00: 09: 22: (message) added read-only backend: 192.168.1.110: 3306
[Root @ localhost mysql-proxy] # netstat-ntulp | grep: 4040 // view the listening port
Tcp 0 0 0.0.0.0: 4040 0.0.0.0: * LISTEN 10056/mysql-proxy
Now we need to create a remote login mysql account in the master server
Master:
mysql> GRANT ALL ON *.* TO root@
'192.168.1.%'
IDENTIFIED BY
'123456'
;
Query OK, 0 rows affected (0.07 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.04 sec)
Perform a test on the slave server:
Slave:
[root@bogon ~]
# mysql -uroot -p123456 -h192.168.1.112 --port=4040
In this way, the Mysql-Proxy server is connected. If an ERROR occurs: ERROR 2003 (HY000): Can't connect to MySQL server on '100. 168.1.112 '(113). modify or disable the firewall. The following describes how to implement read/write splitting. Mysql-Proxy itself does not implement read/write splitting, mainly based on the Lua script. [Root @ localhost mysql-proxy] # killall mysql-proxy // kill the mysql-proxy process [root @ localhost mysql-proxy] # netstat-ntlup | grep 4040 // check whether the process is stop [root @ localhost mysql-proxy] # cd share/doc/mysql-proxy/[root @ localhost mysql-proxy] # ls | grep rw-splitting.lua // view the read/write splitting script, lua language rw-splitting.lua [root @ localhost mysql-proxy] # mysql-proxy -- daemon -- log-level = debug -- log-file =/var/log/mysql-proxy.log -- plugins = "prox Y "-- proxy-backend-addresses =" 192.168.1.109: 3306 "-- proxy-read-only-backend-addresses =" 192.168.1.110: 3306 "-- proxy-lua-script ="/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua "[root @ localhost mysql-proxy] # In slave test on the server, OK. the mysql-proxy implements the read/write splitting of the mysql Cluster. this mode is the most basic mode of the MySQL Cluster and can also achieve load balancing, follow-up research on the deeper MySQL Cluster and share the progress together. Pai_^