MyCat configuration and use

Mysql should also be installed on the MyCat host.

I. read write separation configuration:

1. Upload or download:

wget http://dl.mycat.io/1.6.7.1/Mycat-server-1.6.7.1-release-20190627191042-linux.tar.gz

2. Decompress:

tar xzvf /srv/ftp/Mycat-server-1.6.7.1-release-20190627191042-linux.tar.gz -C /usr/local/

3. Open the configuration file "conf/schema.xml" for configuration: vim /usr/local/mycat/conf/schema.xml

Processing relationship: < schema > define datanode > < datanode > define data host > < datahost >

<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
        <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1"/>
        <dataNode name="dn1" dataHost="localhost1" database="yootk" />
        <dataHost name="localhost1" maxCon="1000" minCon="10" balance="0"
                          writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
                <heartbeat>select user()</heartbeat>
                <writeHost host="hostM1" url="192.168.28.224:3306" user="root"
                                   password="123456">
                        <readHost host="hostS2" url="192.168.28.225:3306" user="root" password="123456" />
                </writeHost>
        </dataHost>
</mycat:schema>

4. Start MyCat. Before starting, ensure that all MySQL services have been started normally, and configure the master-slave relationship:

/usr/local/mycat/bin/mycat start

5. View MyCat connection account: vim /usr/local/mycat/conf/server.xml

<user name="root" defaultAccount="true">
                <property name="password">123456</property>
                <property name="schemas">TESTDB</property>
</user>

6. Log in to the console: mysql -uroot -p123456 -P9066 -DTESTDB -h127.0.0.1

The default port of the console is 9066, and the logical database to be used is TESTDB.

7. Use the operation command:

show @@help ;            //View operation command
show @@database ;        //View all database information and return to MyCat Defined logical database
show @@datasource ;      //View all host connection information

8. If you want to perform data operation at this time, you must not complete it in the console, then apply the login data control port (8066):

mysql -uroot -p123456 -P8066 -DTESTDB -h192.168.28.223

9. If you want to test the read-write separation operation mechanism, you can store some data information in the database:

Script added to MySQL host:

/usr/local/mysql/bin/mysqld_safe --user=root > /dev/null 2>&1 &
mysql -h127.0.0.1 -uroot -p123456
DROP DATABASE IF EXISTS yootk ;
CREATE DATABASE yootk CHARACTER SET UTF8 ;
use yootk ;
CREATE TABLE member(
   mid VARCHAR(50) ,
   name VARCHAR(50) ,
   age INT ,
   salary DOUBLE ,
   birthday DATE ,
   note  TEXT ,
   CONSTRAINT pk_mid PRIMARY KEY(mid)
) ;
INSERT INTO member(mid,name,age,salary,birthday,note) VALUES ('lee','hello',10,2000.0,'2005-11-11','very good') ;
INSERT INTO member(mid,name,age,salary,birthday,note) VALUES ('admin','administrator',10,2000.0,'2005-11-11','very good') ;
INSERT INTO member(mid,name,age,salary,birthday,note) VALUES ('guest','administrator',10,2000.0,'2005-11-11','very good') ;

Execute sql on MyCat:

use yootk ;
INSERT INTO member(mid,name,age,salary,birthday,note) VALUES ('lee-1','hello',10,2000.0,'2005-11-11',@@hostname) ;
INSERT INTO member(mid,name,age,salary,birthday,note) VALUES ('lee-2','hello',10,2000.0,'2005-11-11',@@hostname) ;
INSERT INTO member(mid,name,age,salary,birthday,note) VALUES ('lee-3','hello',10,2000.0,'2005-11-11',@@hostname) ;
INSERT INTO member(mid,name,age,salary,birthday,note) VALUES ('lee-4','hello',10,2000.0,'2005-11-11',@@hostname) ;
INSERT INTO member(mid,name,age,salary,birthday,note) VALUES ('lee-5','hello',10,2000.0,'2005-11-11',@@hostname) ;

Perform data query operation on MyCat: select * from member;

If you want both wirteHost and readHost to participate in the read operation, you can change the balance="0" in the "conf/schema.xml" configuration file to 2, and then restart mycat.

Close / usr/local/mycat/bin/mycat stop

Start / usr/local/mycat/bin/mycat start

Keywords: PHP mycat MySQL Database xml

Added by hmmm on Thu, 31 Oct 2019 21:02:45 +0200