mycat separate read and write sub-library sub-table and

BACKGROUND: using shots from mysql5.7 replication, do mycat arranged separate read and write sub-library sub-table and

Resource Deployment:
System: Centos7.2 ip: 192.168.117.152 (installed mysql - Main)
System: Centos7.2 ip: 192.168.117.161 (installed mysql - from)
the system: Centos7.2 ip: 192.168.117.150 (installation jdk, mysql , mycat)

Download the official source rpm package
https://dev.mysql.com/downloads/repo/yum/

Install mysql, I am here to download the mysql8.0 version
rpm -ivh mysql80-community-release- el7-3.noarch.rpm

Select the release series
yum repolist all | grep mysql

关闭mysql8.0,开启mysql5.7
yum-config-manager --disable mysql80-community
yum-config-manager --enable mysql5.7-community

Mysql service installation and start
yum -y install mysql-Community Community-Server
systemctl Start mysqld

The default login password mysql5.7 in /var/log/mysqd.log
first log in to change your password, non-production environment can be changed under password restrictions
modify the global password policy: set global validate_password_policy = 0;
then modify the length of the password: set global validate_password_length . 8 =;
the ALTER the USER 'the root' @ 'localhost' the IDENTIFIED BY 'picc1234';

Mysql configuration master, add the following modification /etc/my.cnf end of the file:
Server 152-id = id # main library from the library and not as
log-bin = mysql-bin
log-Slave-Updates to true # = open from journal

Authorization from the server to
MySQL> Grant Slave Replication ON  .  To 'myslave'@'192.168.2.%' IDENTIFIED by '123123';

Refresh privilege
flush privileges;

Obtain master information

 

Configuration from the server, Vim /etc/my.cnf
Relay Relay = log-log-bin-
Relay-log-index = bin.index-Slave-Relay
server-id = // master 12 is not the same as the server from the server-id!

Restart mysql service
executed on a Slave:
mysql> Change Master to
MASTER_HOST = '192.168.2.11', MASTER_USER = 'myslave', master_password = '123123', MASTER_LOG_FILE = 'mysql-bin.000002', MASTER_LOG_POS = 337;
start Slave
mysql > start slave;
see the slave state, io / sql thread is a yes
Show slave status \ G;

MyCAT is mysql middleware, is in java, so you need to install java in a laboratory environment, the official suggested jdk1.7 and above.

In the official website to download mycat Source Package: http://www.mycat.io/
extract the source packet
tar -xzvf Mycat-server-1.6.6.1- release-20181031195535-linux.tar.gz -C / usr / loca /

After completion of decompression, the directory is as follows:

table of Contents Explanation
bin mycat command to start, restart, stop, etc.
catlet catlet function as an extension of Mycat
conf Mycat configuration information, focus
lib Mycat cited jar package, Mycat is java development
logs Log files, including log and the log start running Mycat

Mycat configuration files in the conf directory, here are a few commonly used file:

file Explanation
server.xml Mycat profiles, account settings, parameters, etc.
schema.xml Mycat corresponding physical configuration database and database tables
rule.xml Mycat slicing (sub-library sub-table) RULES
                < The User name = "test_mycat" >      #user User Configuration node, user login name name, which is connected Mycat username
                 < Property name = "password" > 123456 </ Property >   # login password, which is connected to the Mycat password
                 < Property name = "schemas" > the TESTDB </ Property >   # database name, there will schema.xml and associated configuration, a plurality of separated by a comma, for example, requires the user to manage two databases DB1, DB2, configure DB1, DBS 

                <-! table-level permissions DML -> 
                <-!             
                <= privileges Check "to false">  
                        <Schema name = "TESTDB" dml="0110" >
                                <name = table "tb01" dml="0000"></table>
                                <table name="tb02" dml="1111"></table>
                        </schema>
                </privileges>           
                 -->
        </user>

        <user name="dev_mycat">
                <property name="password">user</property>
                <property name="schemas">TESTDB</property>
                <property name="readOnly">true</property>
        </user>


Here test_mycat configure an account password is 123456, against the database TESTDB, read and write permissions have not done any special privileges for the table.

schema.xml
schema.xml is the most important configuration items, first of all look at my profile.

<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">

        <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
                <table name="student" primaryKey="id" dataNode="dn1,dn2,dn3" rule="mod-long" />
                <table name="grade" primaryKey="id" dataNode="dn4"/>
        </schema>
        <!-- <dataNode name="dn1$0-743" dataHost="localhost1" database="db$0-743"
                /> -->
        <dataNode name="dn1" dataHost="localhost1" database="beijing" />
        <dataNode name="dn2" dataHost="localhost1" database="shanghai" />
        <dataNode name="dn3" dataHost="localhost1" database="guangzhou" />
        <dataNode name="dn4" dataHost="localhost1" database="basic" />
        <dataHost name="localhost1" maxCon="1000" minCon="10" balance="3"
                          writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
                <heartbeat>select user()</heartbeat>
                <!-- can have multi write hosts -->
                <writeHost host="hostM1" url="192.168.117.152:3306" user="root"
                                   password="picc1234">
                        <!-- can have multi read hosts -->
                        <readHost host="hostS2" url="192.168.117.161:3306" user="root" password="picc1234" />
                </writeHost>
                <!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> -->
        </dataHost>
</mycat:schema>

 

This section of the configuration file which is to do a slice (that is, sub-table)

 

Explanation:
In the mycat TESTDB logic library can be seen that two student, grade, student table corresponds dn1, dn2, dn3 three data nodes, each node data mapped beijing, shanghai, guangzhou database instance three, three library has a student table, depending on the rule patterns (segmentation rules table, total ten kinds)

This section of the configuration file is read in order to achieve separation, may be added from a multi-master and multi-

Explained: url: the address is connected to the mysql, user: a user login data, password: password is

Separate read and write test
Log mycat wanted to connect mysql the same, mycat has two ports, 8066 port is connected, the management port 9066 is connected mycat if successful, to see all the tables jammed, there may be mycat not have permission to connect mysql, need and then from the main library to mycat authorized
as long turn off the master-slave synchronization function, write a check data in a data on mycat can, if select not see the newly inserted data then the read and write separation effect.

Test sub-library sub-table
to create the actual database
First of all, we certainly need to create four database: beijing, shanghai, guangzhou, basic, and generate the corresponding table. As can be seen, and student grade tables exist in a different database, and the data in the student table, distributed and stored in three different databases:

create database beijing;
use beijing;
create table student(
    id    int primary key,
    name   varchar(8) not null,
    grade  int  not null
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

create database shanghai;
use shanghai;
create table student(
    id    int primary key,
    name   varchar(8) not null,
    grade  int  not null
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

create database guangzhou;
use guangzhou;
create table student(
    id    int primary key,
    name   varchar(8) not null,
    grade  int  not null
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

create database basic;
use basic;
create table grade(
    id    int primary key,
    name   varchar(8) not null
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

连接mycat写入测试数据

insert into grade (id,name) values (1,'一年级');
insert into grade (id,name) values (2,'二年级');
insert into grade (id,name) values (3,'三年级');

insert into student (id,name,grade) values (1,'张三',2);
insert into student (id,name,grade) values (2,'李四',1);
insert into student (id,name,grade) values (3,'王五',3);
insert into student (id,name,grade) values (4,'',3);
insert into student (id,name,grade) values (5,'',2);
insert into student (id,name,grade) values (6,'',1);

 

我们插入了6条student记录,这时应该根据不同的取模结果,存在不同的实际数据库的student表中,所以我们切换到实际数据库,分别查看实际数据库里的数据:

Database changed
mysql> show tables;
+-------------------+
| Tables_in_beijing |
+-------------------+
| student           |
+-------------------+
1 row in set (0.00 sec)

mysql> select * from beijing.student;
+----+--------+-------+
| id | name   | grade |
+----+--------+-------+
|  3 | 王五   |     3 |
|  6 ||     1 |
+----+--------+-------+
2 rows in set (0.00 sec)

mysql> select * from shanghai.student;
+----+--------+-------+
| id | name   | grade |
+----+--------+-------+
|  1 | 张三   |     2 |
|  4 ||     3 |
+----+--------+-------+
2 rows in set (0.00 sec)

mysql> select * from guangzhou.student;
+----+--------+-------+
| id | name   | grade |
+----+--------+-------+
|  2 | 李四   |     1 |
|  5 ||     2 |
+----+--------+-------+
2 rows in set (0.01 sec)

mysql>

 

再回到mycat看逻辑数据库:
mysql主从复制+mycat读写分离和分库分表

逻辑数据库,查询grade和student两张表,已经数据聚合,还可以进行排序;
mysql主从复制+mycat读写分离和分库分表

如果我们需要将2张表进行关联,也是可以的:
/!mycat:catlet=io.mycat.catlets.ShareJoin /select * from student s,grade g on s.grade=g.id where g.name='一年级';
mysql主从复制+mycat读写分离和分库分表

Guess you like

Origin www.cnblogs.com/fighting-ljy/p/10955159.html