1, an overview of separate read and write
MySQL as the most widely used database in the world for free, I believe that all those engaged in the operation and maintenance of systems engineers must contact. But in the actual production environment, there is a single MySQL can not meet the actual needs of the time as a standalone database, whether it is in all aspects of security, high availability and high concurrency. Thus, in general are copied to by the master-slave mode (master-slave) synchronization data, then prompts concurrent load capability database by separate read and write, to deploy such a scheme in the embodiment
2, separate read and write works
The basic principle is to let the Lord handle transactional database add, change, delete operations, and from a database query processing operations. Transactional database replication is used to synchronize operations resulting from changes to the cluster from the database.
3, separate read and write mode:
1) the application layer implementation, website programming. Application layer implements sub-application refers to the internal connector and implemented in separate read and write
2) to achieve an intermediate layer, the intermediate layer is achieved middleware program refers to read and write in the external separation. Commonly used middleware programs are:
Cobar: Alibaba B2B develop relational distributed system management bonus 3000 MySQL instances. Ali withstood the test, the reasons behind off due cobar authors were not maintained, and Ali has also developed tddl instead of cobar
MyCAT: community enthusiasts Ali cobar on the basis of secondary development, to solve some problems that existed cobar, and added many new features in them. Currently mycat community activity is high, there are already some companies use mycat. Overall, the relatively high degree of support, will always maintain the area
oneproxy: database community Daniel, before Alipay total floor team leaders database development, based on the development of the idea of using the mysql official proxy c, OneProxy is a commercial fee middleware, always rounding up some of the features floor-point, focus on performance and stability. A friend of mine said it was stable and tested under high concurrency.
Vitess: This middleware is Youtube in production use, but the architecture is very complex. Middleware different past, the use of relatively large changes Vitess application to use his language provides an API interface, we can learn from some of his design ideas of them.
Kingshard: Kingshard former 360Atlas middleware development team with Philip Chen business hours go with language development, there are currently involved in the development of about 3, the current situation is not yet ripe can use the product, you need to constantly improve.
Atlas: 360 lua team based mysql proxy to rewrite C. The original version is the support points table, has released a sub-library sub-table version. In line to see some of my friends used to say often hang under high concurrency, if you want to use need to be tested in advance.
MaxScale and MySQL Route: Both are considered official middleware bar, MaxScale is mariadb (MySQL maintain a version of the original author) research and development, the current version does not support sub-library sub-table. MySQL Route is now the official MySQL Oracle released out of a middleware.
4, test environment preparation
Prepare three linux virtual machine, as a middleware installation mycat (Ip: 192.168.204.130), one as the master database (Ip: 192.168.204.128), one as a slave database (Ip: 192.168.204.129)
5, the installation configuration mycat
mycat have compiled installation package provided to support the installation and running on Windows, Linux, Mac, Solaris and other systems, the official website address http: //www/mycat.lo
Under linux can download Mycat-server-xxxxx.linux.tar.gz decompress in a directory, note directory can not have spaces, under Linux (Unix), to put the proposal under usr / local / Mycat directory, as follows:
Mycat create a user to manage mycat service, and to just unzip the file modification mycat owner is a group, and create mycat user's password is 123456
[the root @ myCat the src] # the useradd myCat
[the root @ myCat the src] # chown myCat: myCat / usr / local / myCat / -R & lt
[the root @ myCat the src] # PA
Packer the parted Paste
pam_console_apply the partprobe The pathchk
pam_tally2 partx
pam_timestamp_check the passwd
[the root @ src mycat] PAS #
passwd Paste
[root @ mycat src] # passwd mycat
change a user's password mycat.
The new password:
Invalid Password: password less than eight characters
Re-enter the new password:
passwd: all authentication tokens have been successfully updated.
Catalog explained as follows:
bin directory of the program, a window store version and linux version, packaged as services providing treatment outside version also provides nowrap shell script commands to facilitate the selection and modification
Conf directory stored profile, the server.xml parameter adjustment is mycat server and user authorization profile, the schema.xml logical library definition tables as well as fragments and defined profile, rule.xml a regular slice profile, specific information about some of the parameters are stored as a separate file rule, are in this directory, modify the configuration file, you need to restart mycat or reload port by 9066
Lib directory to store some of the main jar file mycat dependent
Log on logs / mycat.log, one day files, logs configuration in conf / log4j.xml, depending on your needs, you can adjust the output substantially debut, debut the next level, it will output more information, convenience inquiries
Note: The deployment under linux install MySQL, the default table name case does not ignore the need to manually configure the next /etc/my.cnf lower_case_table_names = 1 ignore the MySQL table name linux environment-sensitive, otherwise use mycat will prompt can not find the time to the wrong table
mycat need to install jdl1.7 or above
Step One: Download jdk-8u221-linux-x64.tar.gz file
Step Two: Create / usr / java folder, jdk-8u221-linux-x64.tar.gz change into the file and extract to the current directory
Third Step: configure the environment variables add the following in / etc / profile bottom
JAVA_HOME=/usr/java/jdk1.8.0_221
PATH=$JAVA_HOME/bin:$PATH
CLASSPATH=$JAVA_HOME/jre/lib/ext:$JAVA_HOME/lib/tools.jar
export PATH JAVA_HOME CLASSPATH
Step four: Reload profile files, environment variables to take effect
[root@mycat java]# source /etc/profile
When deployed mycat linux starts, first, linux need to configure the system environment variables MYCAT_HOME, operation is as follows
The first step: vim / etc / profile, add the following files in the system environment variables
MYCAT_HOME=/usr/local/mycat
PATH=$MYCAT_HOME/bin:$PATH
Step two: source / etc / profile command, is the environment variable to take effect
[root@mycat java]# source /etc/profile
If it is established in more than one Linux system MyCAT cluster, that you need to configure mappings to other ip and host name on the server MyCAT Server resides, configured as follows:
vi /etc/hosts
For example: I have three machines, configuration is as follows:
IP Host Name:
192.168.204.128 master.cn
192.168.204.129 slave.cn
192.168.204.130 mycat.cn
After editing, save the file.
User account and authorization information mycat configuration in conf / server.xml file
[root@mycat java]# vim /usr/local/mycat/conf/server.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mycat:server SYSTEM "server.dtd">
<mycat:server xmlns:mycat="http://io.mycat/">
<system>
<property name="defaultSqlParser">druidparser</property> <!-- 1为开启实时统计、0为关闭 -->
</system>
<user name="root">
<property name="password">123456</property>
<property name="schemas">TESTDB</property>
</user>
<user name="user">
<property name="password">user</property>
<property name="schemas">TESTDB</property>
<property name="readOnly">true</property>
</user>
</mycat:server>
Mycat edit configuration files schema.xml, configuration information about dataHost as follows:
[root@mycat java]# vim /usr/local/mycat/conf/schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://org.opencloudb/">
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode='dn1'>
</schema>
<dataNode name="dn1" dataHost="dthost" database="ha"/>
<dataHost name="dthost" maxCon="500" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="-1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="master.cn" url="192.168.204.128:3306" user="root" password="123456">
</writeHost>
<writeHost host="slave.cn" url="192.168.204.129:3306" user="root" password="123456" />
</dataHost>
</mycat:schema>
There are two parameters need to pay attention, balance and switchtype
Wherein the type of load balancing balance value, the current value of 4
1) balance = "0", do not open the separate read and write mechanism, all reads are sent to the retaining wall writeHost
2) balance = "1", and all readHost stand by writeHost participate in load balancing select statement, simply, when the dual master-slave mode (M1-S1, M2-S2, M1 and M2 each other and the motherboard), normal the case of M2, S1, S2 are involved in a select statement load balancing
3) balance = "2", all read operations are random in writeHost, the distribution readhost.
4) balance = "3", all read requests to a random distribution corresponding readhost wiriterHost performed, writerHost pressure reading is not burdened
switchType refers to a mode switch, the current also has four values:
1) switchType = '- 1' means no automatic switch
2) switchType = '1' Default, indicates automatic switch
3) switchType = '2' based on MySQL master-slave synchronization decision whether to switch the state of the heartbeat statement show slave status
4) switchType = '3' based on the switching mechanism of the MySQL galary cluster (Cluster adapted) (1.4.1), the heartbeat statement show status like 'wsrep%'.
After the above two configuration steps, you can go to the / usr / local / mycat / bin directory execute:
./mycat start
You can start mycat service! . At this point it mycat server configuration is complete.
6, the master-slave configuration MySQL
As the installation can only be installed mariadb through yum, so I am here for the convenience of use mariadb, compile and install MySQL can use.
[root@slave ~]# yum -y install mariadb mariadb-server
6.1 Configuration master
Edit the configuration file my.cnf
[mysqld]
DATADIR = / var / lib / MySQL
Socket = / var / lib / MySQL / mysql.sock
symbolic links = 0-
log-bin = MySQL-bin-Master # enable binary file
server-id = 1 # local database ID marked
binlog-do-db = TESTDB # can be copied from the server libraries, binaries need to synchronize the database name
binlog-ignore-db-mysql # may not be copied from the server library
lower_case_table_names = 1 # Configure mysql ignore the table name case, mycat need
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
!includedir /etc/my.cnf.d
Start Service
[root@master ~]# systemctl start mariadb
Creating a data path TESTDB
MariaDB [(none)]> create database TESTDB;
Query OK, 1 row affected (0.01 sec)
MariaDB [(none)]> use TESTDB;
Database changed
MariaDB [TESTDB]> create table T1(id int,name varchar(20));
Query OK, 0 rows affected (0.01 sec)
MariaDB [TESTDB]> grant replication slave on *.* to [email protected] identi fied by '123456'; #授权登录
Query OK, 0 rows affected (0.00 sec)
MariaDB [TESTDB]> show master status; #查看master状态
+-------------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------------+----------+--------------+------------------+
| mysql-bin-master.000003 | 596 | TESTDB | mysql |
+-------------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
6.2 Configuring slave
Edit Profile
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
server-id=2
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
master-host=192.168.204.128
master-user=slave
master-password=123456
master-port=3306
master-connect-retry=60
!includedir /etc/my.cnf.d
Start Service [root @ salve ~] # systemctl start mariadb
MariaDB [(none)]> stop slave #先停止slave
-> ;
Query OK, 0 rows affected, 1 warning (0.00 sec)
MariaDB [(none)]> change master to master_host = '192.168.204.128', master_user = 'slave', master_password = '123456'; # connect to the master
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> start slave; #启动slave
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> show slave status \ G # View slave status, there are two yes, it means success, see if you can synchronize database TESTDB
7, the configuration server from the primary connection mycat
7.1 authorized user logs on the primary server mycat
MariaDB [(none)]> grant all on *.* to [email protected] identified by '123456';
Query OK, 0 rows affected (0.01 sec)
7.2 Authorized users login from the server mycat
MariaDB [(none)]> grant all on *.* to [email protected] identified by '123456';
Query OK, 0 rows affected (0.01 sec)
8, test client server connection mycat to see if successful. After the test results and down from the master server, respectively