Separated from the master copy with a reader
First, the database master-slave replication
MySQL is primary master and MySQL data synchronization (synchronization data from the master server) is copied from the server
Principle: 1.Master before the completion of each transaction update data, record these changes in the binary log, notify the storage engine commits the transaction after the completion of the write binary files
2.Slave copied from the server to the Master of Binary log relays logs, open an I / O thread, to establish a common connection on the Master primary server, reads the binary log events from the master server Master, and recorded their the relay log (with the master server will continue to wait for the update)
3.SQL slave thread (SQL from the thread) reads events from the relay log, replay these events, to synchronize with the Master primary server
Master-slave replication configurations
1. To achieve the primary synchronization time from the server, MySQL installation (MySQL view before installation notes) on the server 3
2. The main server configuration
Configuration changes mysql configuration file (remember to restart mysql service): vim /etc/my.cnf
To the authorization from the server
grant replication slave on *.* to 'myslave'@'192.168.0.%' identified by '123456';
Configuring the Slave (both from the same server) from the server
Configuration changes mysql configuration file (remember to restart mysql service): vim /etc/my.cnf
Log Database: MySQL-uroot--p123456
Master primary server synchronization (sync remember after the restart database):
mysql>change master to master_host='192.168.0.101',master_user='myslave',master_password='123456',master_log_file='master-bin.000001',master_log_pos=572;
MASTER_LOG_FILE : Master master binary log name
MASTER_LOG_POS : binary log position variable
See master_log_file and master_log_pos value (view on the master server, as shown in the FIG.)
mysql> show master status;
Supplementary : master_log_pos set to 0 value will automatically get the master_log_pos
See data synchronization Slave state (shown below)
mysql> show slave status\G
Two coils together to ensure that the red values are Yes
4. Test
Create a library on the Master primary server creates a table to add data to see whether the synchronization from the server
Master primary server:
Slave from the server:
Test data synchronization success
Second, the database separate read and write
Separation can not be separated from the master copy read, to be based on the master copy from
Benefits: You can reduce the primary server relieve work pressure
Read and write separation of two software: Amoeba and MySQL-Proxy
Configuration environment as shown below
Amoeba Configuration Installation
Amoeba need JDK support, is based on jdk1.5 development, it would be best to use a version jdk1.5 or jdk1.6
1. Download and install jdk
./jdk-6u14-linux-x64.bin
2. Create a folder, move the file decompression to the next /usr/local/jdk1.6
mkdir /usr/local/jdk1.6》mv jdk1.6.0_14 /usr/local/jdk1.6
3. In the / etc / profile file configuration Adding an environment variable, run the file
vim /etc/profile
Then source / etc / profile
4. Installation amoeba
Extract to give the new amoeba folder permissions
tar zxf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba/
chmod -R 755 /usr/local/amoeba/
Start (the result is displayed in FIG successful installation)
5. The authorization given to the three database servers
mysql>grant all on *.* to test@'192.168.0.%' identified by '123456';
6. Configure amoeba.xml profile
vim /usr/local/amoeba/conf/amoeba.xml
7. Configure dbServers.xml profile
vim /usr/local/amoeba/conf/dbServers.xml
8. Start Testing Services
/usr/local/amoeba/bin/amoeba start
Log test database on a test machine
Separate read and write function test
Master primary server to create a library closed two Slave, then write different data from server to server functions from slave two, to see what data to see whether to implement read from the functions of the server, and then write the data, not the opportunity to test inquiry to this message, from the slave because the slave closed functions of a server are not synchronized
From the server 192.168.0.103
From the server 192.168.0.104
Master primary server 192.168.0.101
Test machine
It found that only two inquiries from the data on the server, only the query in the main server native data on the success of this machine
Installation and Configuration MySQL-Proxy (Source Package Installation)
1. Download extracting installation package
tar zxf mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz
mv mysql-proxy-0.8.5-linux-el6-x86-64bit /usr/local/mysql-proxy
2. Create a storage configuration files and log directory
cd /usr/local/mysql-proxy》mkdir logs》mkdir conf
3. Create modify the configuration files and startup scripts
vim /usr/local/mysql-proxy/conf/mysql-proxy.conf
[mysql-proxy]
user=root
proxy-address=0.0.0.0:3306 \\所有IP的3306端口
proxy-backend-addresses=192.168.0.101:3306 \\设置master的ip,写数据
proxy-read-only-backend-addresses=192.168.0.103:3306 \\设置slave的ip,读数据
proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua \\脚本所在位置
log-file=/usr/local/mysql-proxy/logs/msyql-proxy.log \\日志存放位置
log-level=debug \\日志模式
daemon=true \\打入后台
keepalive=true
4. Modify the script management process
vim /usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua
min_idle_connections = 1,
max_idle_connections = 2,
5. Set the configuration file permissions and start the service
chmod 660 /usr/local/mysql-proxy/conf/mysql-proxy.conf
/usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/mysql-proxy.conf
6. Review and test port
netstat -antlp | grep mysql-proxy
Log database read and write operations test