Mycat environment
MyCAT is written and developed in JAVA language. Because MyCAT uses some features in JDK7, it must be run on a version above JDK7.
- Install a version above JDK7
- Download the installation package from the official website and decompress the configuration for direct use
- The official recommendation is placed in the usr/local/Mycat directory
Directory explanation
- The bin program directory stores the window version and linux version. In addition to the version packaged as a service, it also provides the shell script command of nowrap
- ./mycat console, first chmod +x *
- Commands supported by mycat {console | start | stop | restart | status | dump}
- The configuration file is stored in the conf directory
- schema.xml is the configuration file of logic library definition and table and shard definition
- rule.xml is the configuration file of sharding rules
- server.xml almost saves all the system configuration information and user permissions required by mycat
- The lib directory mainly stores some jar files that mycat depends on
- The logs directory stores log files
Configuration parameter
schema.xml
Define the mycat logic library and data sharding (this piece belongs to the concept of mycat logic) to
define the connection to the real server of the mycat backend
2 special attributes of dataHost
Both are for the dual-master and dual-slave scenario
server.xml
Define user permissions
Only the parameter configuration related to the separation of read and write is written, and the others can be viewed on the official website for detailed instructions.
Official reference: http://www.mycat.org.cn/document/mycat-definitive-guide.pdf
Configuration example
1. Configure master-slave replication
2. Install JDK environment
3. Configure mycat
Configure mycat
wget http://dl.mycat.org.cn/1.6.7.1/Mycat-server-1.6.7.1-release-20200209222254-linux.tar.gz
tar -xzf Mycat-server-1.6.7.1-release-20200209222254-linux.tar.gz -C /usr/local/
#下载mycat安装包并解压
Edit the schema.xml file
vi /usr/local/mycat/conf/schema.xml
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="mytest" checkSQLschema="false" sqlMaxLimit="100" dataNode='dn1'>
</schema>
<dataNode name="dn1" dataHost="localhost1" database="test" />
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="1"
writeType="0" dbType="mysql" dbDriver="native" switchType="-1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="192.168.26.100:3306" user="root"
password="111111"></writeHost>
<writeHost host="hostS1" url="192.168.26.101:3306" user="root"
password="111111" />
</dataHost>
</mycat:schema>
1. Because I did not configure the table tag, all schemas need to add dataNode as the default node.
2. The schema dataNode dataHost has field associations, and the bottom layer is dataHost.
3. It is recommended that the switchType be -1 for single master and slave, otherwise it will lead to inconsistent master and slave data. problem
Problem:
The subset of writeHost is readhost, and if writeHost fails, readhost cannot be read. This is unreasonable. The solution is to contact dual master and slave and treat the master slave as a dual master relationship. This needs to be changed. balance="1" and writeType="0", balance is to allow the secondary primary to participate in the read strategy, writeType is to allow the first writeHost configuration to be the write library, so that there is no subset relationship
Edit the server.xml file
<user name="root" defaultAccount="true">
<property name="password">123456</property>
<property name="schemas">mytest</property>
#必配的信息,privileges可选
#把多余关于user的信息删除,不然可能启动mycat报错
Start login mycat
cd /usr/local/mycat/bin
./mycat console start
#先看下是否启动正常
./mycat start/stop/restart
#后台启动
mysql -uroot -p123456 -h127.0.0.1 -P8066 -Dmytest
#连接mycat的mytest数据库
How to test?
By inserting the database variable @@hostname, it will display its host name, provided that binlog_format=STATEMENT, so that the sql statement can be executed
INSERT INTO mytab VALUES(109,@@hostname);