I. Introduction
1.1 Distributed Database
With the rapid development of IT industry, the size of the data application system for explosive growth, the data processing capabilities of the database requirements are high, it is a distributed database came into being.
Distributed database features include:
Transparency: users do not have specific back-end database implementation
Scalability: The system can be extended pressure conditions
Reliability: If one server goes down, the standby server can continue to provide services to automatically switch
high performance:
Introduction 1.2 Mycat
MyCat ( http://www.mycat.io/ ) is supported by a database cluster mysql, sql server and other middleware deployment. It developed from Amoeba and cobar.
Second, load balancing
Third, the test environment
server |
Mycat |
Mysql-master |
Mysql-slave |
Mysql-master-standby |
Mysql-master-standby |
IP |
127.0.0.1 |
10.3.30.161 |
10.3.30.238 |
120.77.80.215 |
10.3.6.84 |
Port |
8066/9066 |
3306 |
3306 |
3306 |
3306 |
Account |
root 123456 |
test 123456 |
test 123456 |
test 123456 |
test 123456 |
Fourth, the test script
CREATE TABLE `employee` (
`ID` int(11) NOT NULL,
`NAME` varchar(100) default NULL,
`SHARDING_ID` int(11) NOT NULL,
PRIMARY KEY (`ID`)
)
INSERT INTO employee(ID,NAME,SHARDING_ID) VALUES ('6', 'amos', '10000');
Fifth, software installation
First install mycat-server, mycat-eye, zookeeper and navicat for mysql
Sixth, the service starts
6.1 Starting mycat
6.1.1 installed java environment
1, the installation jdk
2, configure the environment variables
JAVA_HOME=C:\Program Files\Java\jdk1.8.0_131
CLASSPATH=.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;
Path =%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;
6.1.2 Start mycat Service
Start mycat service on windows8 operating system, the following command
cmd
cd /d D:\EC-OFC\Java\Mycat\Mycat-server-1.6\mycat\bin
startup_nowrap.bat
6.1.3 connection mycat
6.1.3.1navicat connection
Use navicat connection mycat, the default port is 8066, password 123456
6.1.3.2DOS link
cmd
cd /d C:\Program Files\MySQL\MySQL Server 5.7\bin
mysql -uroot -p123456 -P8066
Note: If you need to use a command such as show @@ datasource, you need to connect port 9066 instead of 8066
6.2 start zookeeper
D: \ EC-OFC \ tools \ zookeeper-3.4.10 \ bin \ zkServer.bat
6.3 Start mycat-eye
D:\EC-OFC\Java\Mycat\mycat-web\start.bat
Seven, write the cluster
7.1 Topology
7.2 configuration files
<?xml version="1.0"?>
<!DOCTYPEmycat:schemaSYSTEM"schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
<table name="employee" primaryKey="ID" dataNode="dn1,dn2"/>
</schema>
<dataNode name="dn1" dataHost="host1" database="db1"/>
<dataNode name="dn2" dataHost="host2" database="db1"/>
<dataHost name="host1" maxCon="1000" minCon="10" balance="1"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="120.77.80.215:3306" user="test" password="123456">
</writeHost>
</dataHost>
<dataHost name="host2" maxCon="1000" minCon="10" balance="1"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM2" url="10.3.30.238:3306" user="test" password="123456">
</writeHost>
</dataHost>
</mycat:schema>
Eight, master-slave configuration
8.1 Business Requirements
Hang up when Master, Slave also provide reading services.
8.2 Configuration Steps
Note that each server mysql server-id must be unique
first step:
Create a host 10.3.30.238 10.3.30.161 can log in the MySQL user
User: mysql238
Password: 123456
mysql>GRANT REPLICATION SLAVE ON *.* TO ‘mysql238’@’10.3.30.238’ IDENTIFIED BY ‘123456’;
mysql>FLUSH PRIVILEGES;
Step two:
You need to be in C: \ ProgramData \ MySQL \ MySQL Server 5.7 \ my.ini file, add log-bin = mysql-bin
View 10.3.30.161MySQL server binary file name and location
mysql>SHOW MASTER STATUS;
third step:
Inform binary file name and location
Performed in 10.3.30.238:
mysql>CHANGE MASTER TO
MASTER_HOST='10.3.30.161',
MASTER_USER='mysql238',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mybin.000001',
MASTER_LOG_POS=154;
完成主从复制配置
8.3 验证配置
在10.3.30.238中
mysql> START SLAVE; #开启复制
mysql>SHOW SLAVE STATUS\G #查看主从复制是否配置成功
当看到Slave_IO_Running: YES、Slave_SQL_Running: YES才表明状态正常
8.4 配置文件
switchType='-1'意味着当主挂掉的时候,不进行自动切换,即hostS1并不会被提升为主,仍只提供读的功能。这就避免了将数据写进slave的可能性,单纯的MySQL主从集群并不允许将数据写进slave中,除非配置的是双master。
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
<table name="employee" primaryKey="ID" dataNode="dn1"/>
</schema>
<dataNode name="dn1" dataHost="host1" database="db1"/>
<dataHost name="host1" maxCon="1000" minCon="10" balance="1"
writeType="0" dbType="mysql" dbDriver="native" switchType="-1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="10.3.30.161:3306" user="test" password="123456">
<readHost host="hostR1" url="120.77.80.215:3306" user="test" password="123456"/>
</writeHost>
<writeHost host="hostS1" url="10.3.30.238:3306" user="test" password="123456">
</writeHost>
</dataHost>
</mycat:schema>
九、Haproxy配置
9.1 拓扑图
9.2 配置文件
此处为haproxy配置文件
#
global
maxconn 1500
nbproc 1
daemon
defaults
mode tcp
retries 3
option abortonclose
maxconn 32000
timeout connect 300000ms
timeout client 300000ms
timeout server 300000ms
log 127.0.0.1 local0 err
listen admin_stats
bind :48800
stats uri /admin-status
stats auth admin:admin
mode http
option httplog
listen mycat_servers
bind :3307 ## 绑定端口
mode tcp
option tcplog
#balance source
server server238 10.3.30.238:8066 check port 8066 weight 1 check
server server84 10.3.6.84:8066 check port 8066 weight 1 check
mysql客户端或者应用程序可以通过如上配置的127.0.0.1:3307端口链接haproxy实现链接mycat集群的目的。
十、注意事项
1、主主复制配置文件中auto_increment_increment和auto_increment_offset只能保证主键不重复,却不能保证主键有序。
2、当配置完成Slave_IO_Running、Slave_SQL_Running不全为YES时,show slave status\G信息中有错误提示,可根据错误提示进行更正。
3、Slave_IO_Running、Slave_SQL_Running不全为YES时,大多数问题都是数据不统一导致。
常见出错点:
1、两台数据库都存在db数据库,而第一台MySQL db中有tab1,第二台MySQL db中没有tab1,那肯定不能成功。
2、已经获取了数据的二进制日志名和位置,又进行了数据操作,导致POS发生变更。在配置CHANGE MASTER时还是用到之前的POS。
3、stop slave后,数据变更,再start slave。出错。
终极更正法:重新执行一遍CHANGE MASTER就好了。