vlambda博客
学习文章列表

第十一章 分布式架构(MyCAT)

一、MyCAT基础架构图

最经典的:

将表拆分,外部仍然感觉在一个表上

双主双从,

转:https://www.jianshu.com/p/5e0062f6cf62

二、基础架构准备

1、环境准备

两台虚拟机 db01 db02

一组IP:db01 192.168.198.132;db02 192.168.198.146

一组IP:db01 192.168.40.137;db02 192.168.40.155

每台创建四个mysql实例:3307 3308 3309 3310

2、删除历史环境:

#pkill mysqld

#rm -rf /data/330* 

#mv /etc/my.cnf /etc/my.cnf.bak

3、创建相关目录初始化数据

#useradd -s /sbin/nologin mysql

#mkdir /data/33{07..10}/data -p

mysqld --initialize-insecure  --user=mysql --datadir=/data/3307/data --basedir=/application/mysql

mysqld --initialize-insecure  --user=mysql --datadir=/data/3308/data --basedir=/application/mysql

mysqld --initialize-insecure  --user=mysql --datadir=/data/3309/data --basedir=/application/mysql

mysqld --initialize-insecure  --user=mysql --datadir=/data/3310/data --basedir=/application/mysql

4、准备配置文件和启动脚本

######db01######

cat >/data/3307/my.cnf<<EOF

[mysqld]

basedir=/application/mysql

datadir=/data/3307/data

socket=/data/3307/mysql.sock

port=3307

log-error=/data/3307/mysql.log

log_bin=/data/3307/mysql-bin

binlog_format=row

skip-name-resolve

server-id=7

gtid-mode=on

enforce-gtid-consistency=true

log-slave-updates=1

EOF


cat >/data/3308/my.cnf<<EOF

[mysqld]

basedir=/application/mysql

datadir=/data/3308/data

port=3308

socket=/data/3308/mysql.sock

log-error=/data/3308/mysql.log

log_bin=/data/3308/mysql-bin

binlog_format=row

skip-name-resolve

server-id=8

gtid-mode=on

enforce-gtid-consistency=true

log-slave-updates=1

EOF


cat >/data/3309/my.cnf<<EOF

[mysqld]

basedir=/application/mysql

datadir=/data/3309/data

socket=/data/3309/mysql.sock

port=3309

log-error=/data/3309/mysql.log

log_bin=/data/3309/mysql-bin

binlog_format=row

skip-name-resolve

server-id=9

gtid-mode=on

enforce-gtid-consistency=true

log-slave-updates=1

EOF


cat >/data/3310/my.cnf<<EOF

[mysqld]

basedir=/application/mysql

datadir=/data/3310/data

socket=/data/3310/mysql.sock

port=3310

log-error=/data/3310/mysql.log

log_bin=/data/3310/mysql-bin

binlog_format=row

skip-name-resolve

server-id=10

gtid-mode=on

enforce-gtid-consistency=true

log-slave-updates=1

EOF


cat >/etc/systemd/system/mysqld3307.service<<EOF

[Unit]

Description=MySQL Server

Documentation=man:mysqld(8)

Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html

After=network.target

After=syslog.target

[Install]

WantedBy=multi-user.target

[Service]

User=mysql

Group=mysql

ExecStart=/application/mysql/bin/mysqld --defaults-file=/data/3307/my.cnf

LimitNOFILE = 5000

EOF


cat >/etc/systemd/system/mysqld3308.service<<EOF

[Unit]

Description=MySQL Server

Documentation=man:mysqld(8)

Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html

After=network.target

After=syslog.target

[Install]

WantedBy=multi-user.target

[Service]

User=mysql

Group=mysql

ExecStart=/application/mysql/bin/mysqld --defaults-file=/data/3308/my.cnf

LimitNOFILE = 5000

EOF


cat >/etc/systemd/system/mysqld3309.service<<EOF

[Unit]

Description=MySQL Server

Documentation=man:mysqld(8)

Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html

After=network.target

After=syslog.target

[Install]

WantedBy=multi-user.target

[Service]

User=mysql

Group=mysql

ExecStart=/application/mysql/bin/mysqld --defaults-file=/data/3309/my.cnf

LimitNOFILE = 5000

EOF


cat >/etc/systemd/system/mysqld3310.service<<EOF

[Unit]

Description=MySQL Server

Documentation=man:mysqld(8)

Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html

After=network.target

After=syslog.target

[Install]

WantedBy=multi-user.target

[Service]

User=mysql

Group=mysql

ExecStart=/application/mysql/bin/mysqld --defaults-file=/data/3310/my.cnf

LimitNOFILE = 5000

EOF

######db02######

cat >/data/3307/my.cnf<<EOF

[mysqld]

basedir=/application/mysql

datadir=/data/3307/data

socket=/data/3307/mysql.sock

port=3307

log-error=/data/3307/mysql.log

log_bin=/data/3307/mysql-bin

binlog_format=row

skip-name-resolve

server-id=17

gtid-mode=on

enforce-gtid-consistency=true

log-slave-updates=1

EOF


cat >/data/3308/my.cnf<<EOF

[mysqld]

basedir=/application/mysql

datadir=/data/3308/data

port=3308

socket=/data/3308/mysql.sock

log-error=/data/3308/mysql.log

log_bin=/data/3308/mysql-bin

binlog_format=row

skip-name-resolve

server-id=18

gtid-mode=on

enforce-gtid-consistency=true

log-slave-updates=1

EOF


cat >/data/3309/my.cnf<<EOF

[mysqld]

basedir=/application/mysql

datadir=/data/3309/data

socket=/data/3309/mysql.sock

port=3309

log-error=/data/3309/mysql.log

log_bin=/data/3309/mysql-bin

binlog_format=row

skip-name-resolve

server-id=19

gtid-mode=on

enforce-gtid-consistency=true

log-slave-updates=1

EOF


cat >/data/3310/my.cnf<<EOF

[mysqld]

basedir=/application/mysql

datadir=/data/3310/data

socket=/data/3310/mysql.sock

port=3310

log-error=/data/3310/mysql.log

log_bin=/data/3310/mysql-bin

binlog_format=row

skip-name-resolve

server-id=20

gtid-mode=on

enforce-gtid-consistency=true

log-slave-updates=1

EOF


cat >/etc/systemd/system/mysqld3307.service<<EOF

[Unit]

Description=MySQL Server

Documentation=man:mysqld(8)

Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html

After=network.target

After=syslog.target

[Install]

WantedBy=multi-user.target

[Service]

User=mysql

Group=mysql

ExecStart=/application/mysql/bin/mysqld --defaults-file=/data/3307/my.cnf

LimitNOFILE = 5000

EOF


cat >/etc/systemd/system/mysqld3308.service<<EOF

[Unit]

Description=MySQL Server

Documentation=man:mysqld(8)

Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html

After=network.target

After=syslog.target

[Install]

WantedBy=multi-user.target

[Service]

User=mysql

Group=mysql

ExecStart=/application/mysql/bin/mysqld --defaults-file=/data/3308/my.cnf

LimitNOFILE = 5000

EOF


cat >/etc/systemd/system/mysqld3309.service<<EOF

[Unit]

Description=MySQL Server

Documentation=man:mysqld(8)

Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html

After=network.target

After=syslog.target

[Install]

WantedBy=multi-user.target

[Service]

User=mysql

Group=mysql

ExecStart=/application/mysql/bin/mysqld --defaults-file=/data/3309/my.cnf

LimitNOFILE = 5000

EOF


cat >/etc/systemd/system/mysqld3310.service<<EOF

[Unit]

Description=MySQL Server

Documentation=man:mysqld(8)

Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html

After=network.target

After=syslog.target

[Install]

WantedBy=multi-user.target

[Service]

User=mysql

Group=mysql

ExecStart=/application/mysql/bin/mysqld --defaults-file=/data/3310/my.cnf

LimitNOFILE = 5000

EOF


5、授权并启动

#chown -R mysql.mysql /data/*

systemctl start mysqld3307

systemctl start mysqld3308

systemctl start mysqld3309

systemctl start mysqld3310


mysql -S /data/3307/mysql.sock -e "show variables like 'server_id'"

mysql -S /data/3308/mysql.sock -e "show variables like 'server_id'"

mysql -S /data/3309/mysql.sock -e "show variables like 'server_id'"

mysql -S /data/3310/mysql.sock -e "show variables like 'server_id'"

6、 节点主从规划

(1)箭头指向谁是主库

    192.168.40.137:3307    <----->  192.168.40.155:3307

    192.168.40.137:3309    ------>  192.168.40.137:3307

    192.168.40.155:3309    ------>  192.168.40.155:3307


    192.168.40.155:3308  <----->    192.168.40.137:3308

    192.168.40.155:3310  ----->     192.168.40.155:3308

    192.168.40.137:3310  ----->     192.168.40.137:3308


(2)分片规划

shard1:

    Master:192.168.40.137:3307

    slave1:192.168.40.137:3309

    Standby Master:192.168.40.155:3307

    slave2:192.168.40.155:3309

shard2:

    Master:192.168.40.155:3308

    slave1:192.168.40.155:3310

    Standby Master:192.168.40.137:3308

    slave2:192.168.40.137:3310


7、开始配置

(1)碎片一

第一步:192.168.40.137:3307 <-----> 192.168.40.155:3307


db02:

  mysql  -S /data/3307/mysql.sock -e "grant replication slave on *.* to repl@'192.168.40.%' identified by '123';"

 mysql  -S /data/3307/mysql.sock -e "grant all  on *.* to root@'192.168.40.%' identified by '123'  with grant option;"


db01:

  mysql  -S /data/3307/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.40.155', MASTER_PORT=3307, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"

  mysql  -S /data/3307/mysql.sock -e "start slave;"

  mysql  -S /data/3307/mysql.sock -e "show slave status\G"


db02:

  mysql  -S /data/3307/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.40.137', MASTER_PORT=3307, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"

mysql  -S /data/3307/mysql.sock -e "start slave;"

mysql  -S /data/3307/mysql.sock -e "show slave status\G"


第二步:192.168.40.137:3309 ------> 192.168.40.137:3307


db01:

  mysql  -S /data/3309/mysql.sock  -e "CHANGE MASTER TO MASTER_HOST='192.168.40.137', MASTER_PORT=3307, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"

mysql  -S /data/3309/mysql.sock  -e "start slave;"

mysql  -S /data/3309/mysql.sock  -e "show slave status\G"


第三步:192.168.40.155:3309 ------> 192.168.40.155:3307

db02:

  mysql  -S /data/3309/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.40.155', MASTER_PORT=3307, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"

mysql  -S /data/3309/mysql.sock -e "start slave;"

mysql  -S /data/3309/mysql.sock -e "show slave status\G"


(2)碎片二

第一步:192.168.40.155:3308 <-----> 192.168.40.137:3308

db01:

  mysql  -S /data/3308/mysql.sock -e "grant replication slave on *.* to repl@'192.168.40.%' identified by '123';"

  mysql  -S /data/3308/mysql.sock -e "grant all  on *.* to root@'192.168.40.%' identified by '123'  with grant option;"

db02:

  mysql  -S /data/3308/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.40.137', MASTER_PORT=3308, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"

  mysql  -S /data/3308/mysql.sock -e "start slave;"

  mysql  -S /data/3308/mysql.sock -e "show slave status\G"


db01:

  mysql  -S /data/3308/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.40.155', MASTER_PORT=3308, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"

  mysql  -S /data/3308/mysql.sock -e "start slave;"

  mysql  -S /data/3308/mysql.sock -e "show slave status\G"


第二步:192.168.40.155:3310 -----> 192.168.40.155:3308

db02:

  mysql  -S /data/3310/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.40.155', MASTER_PORT=3308, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"

  mysql  -S /data/3310/mysql.sock -e "start slave;"

  mysql  -S /data/3310/mysql.sock -e "show slave status\G"


第三步:192.168.40.137:3310 -----> 192.168.40.137:3308

db01:

  mysql  -S /data/3310/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.40.137', MASTER_PORT=3308, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"

mysql  -S /data/3310/mysql.sock -e "start slave;"

mysql  -S /data/3310/mysql.sock -e "show slave status\G"


8、检测主从状态

mysql -S /data/3307/mysql.sock -e "show slave status\G"|grep Yes

mysql -S /data/3308/mysql.sock -e "show slave status\G"|grep Yes

mysql -S /data/3309/mysql.sock -e "show slave status\G"|grep Yes

mysql -S /data/3310/mysql.sock -e "show slave status\G"|grep Yes

注:如果中间出现错误,在每个节点进行执行以下命令

mysql -S /data/3307/mysql.sock -e "stop slave; reset slave all;"

mysql -S /data/3308/mysql.sock -e "stop slave; reset slave all;"

mysql -S /data/3309/mysql.sock -e "stop slave; reset slave all;"

mysql -S /data/3310/mysql.sock -e "stop slave; reset slave all;"

第十一章 分布式架构(MyCAT)

三、MyCAT安装

1、安装依赖包

#yum install -y java

2、下载

官网:http://mycat.org.cn/

Mycat-server-1.6.6.1-release-20181031195535-linux.tar.gz

3、解压后移动(绿色版不需要安装)

#tar xvf Mycat-server-1.6.6.1-release-20181031195535-linux.tar.gz

#cp -r mycat/ /application/

4、配置环境变量、启动和连接

#vim /etc/profile

export PATH=/aplication/mycat/bin:$PATH

#source /etc/profile


#/application/mycat/bin/mycat start

5、配置文件说明

(1)配置文件简介

①逻辑库:schema


<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1"> 

</schema> 

② 数据节点:datanode

<dataNode name="dn1" dataHost="localhost1" database= "world" />  

③ 数据主机:datahost(w和r)

<dataHost name="localhost1" maxCon="1000" minCon="10" balance="1"  writeType="0" dbType="mysql"  dbDriver="native" switchType="1"> 

<heartbeat>select user()</heartbeat>  

 <writeHost host="db1" url="192.168.198.132:3307" user="root" password="123">            

<readHost host="db2" url="192.168.198.1346:3309" user="root" password="123" />   

</writeHost>  

</dataHost>

(2)数据导入

db01:

#mysql -S /data/3307/mysql.sock 

mysql>grant all on *.* to root@'192.168.198.%' identified by '123';

mysql>source /data/world.sql


#mysql -S /data/3308/mysql.sock 

mysql>grant all on *.* to root@'192.168.198.%' identified by '123';

mysql>source /data/world.sql

(3)读写分离结构配置

#vim /application/mycat/conf/schema.xml 


<?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="sh1"> 

</schema>  

        <dataNode name="sh1" dataHost="mifengdiandi" database= "world" />         

        <dataHost name="mifengdiandi" maxCon="1000" minCon="10" balance="1"  writeType="0" dbType="mysql"  dbDriver="native" switchType="1">    

                <heartbeat>select user()</heartbeat>  

        <writeHost host="db1" url="192.168.198.132:3307" user="root" password="123"> 

                        <readHost host="db2" url="192.168.198.132:3309" user="root" password="123" /> 

        </writeHost> 

        </dataHost>  

</mycat:schema>

(4)重启mycat

#/application/mycat/bin/mycat restart

(5)读写分离测试

 #mysql -uroot -p -h 127.0.0.1 -P8066

 mysql> select @@server_id;

mysql> begin;select @@server_id;


总结: 

以上案例实现了1主1从的读写分离功能,写操作落到主库,读操作落到从库.如果主库宕机,从库不能在继续提供读服务了。

6、配置读写分离+高可用

(1)配置文件

# vim schema.xml


<?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="sh1"> 

</schema>  

    <dataNode name="sh1" dataHost="mifengdiandi" database= "world" />  

    <dataHost name="mifengdiandi" maxCon="1000" minCon="10" balance="1"  writeType="0" dbType="mysql"  dbDriver="native" switchType="1"> 

        <heartbeat>select user()</heartbeat>  

    <writeHost host="db1" url="192.168.198.132:3307" user="root" password="123"> 

            <readHost host="db2" url="192.168.198.132:3309" user="root" password="123" /> 

    </writeHost> 

    <writeHost host="db3" url="192.168.198.146:3307" user="root" password="123"> 

            <readHost host="db4" url="192.168.198.146:3309" user="root" password="123" /> 

    </writeHost>        

    </dataHost>  

</mycat:schema>


说明:

  真正的 writehost:192.168.198.132:3307负责写操作的writehost;

standby  writeHost  :和readhost一样,只提供读服务。

  当写节点宕机后,后面跟的readhost也不提供服务,这时候standby的writehost就提供写服务,

后面跟的readhost提供读服务。


(2)重启mycat

#/application/mycat/bin/mycat restart

(3)测试

#mysql -uroot -p123456 -h 127.0.0.1 -P 8066


 show variables like 'server_id';

 show variables like 'server_id';

 show variables like 'server_id';

 begin;

 show variables like 'server_id';

7、配置中的属性介绍:

(1)balance

负载均衡类型,目前的取值有3种: 

①balance="0", 不开启读写分离机制,所有读操作都发送到当前可用的writeHost上。 

②balance="1",全部的readHost与standby writeHost参与select语句的负载均衡,简单的说,

  当双主双从模式(M1->S1,M2->S2,并且M1与 M2互为主备),正常情况下,M2,S1,S2都参与select语句的负载均衡。 

③balance="2",所有读操作都随机的在writeHost、readhost上分发。

(2)writeType属性

负载均衡类型,目前的取值有2种: 

①writeType="0", 所有写操作发送到配置的第一个writeHost,

第一个挂了切到还生存的第二个writeHost,重新启动后已切换后的为主,切换记录在配置文件中:dnindex.properties . 

②writeType=“1”,所有写操作都随机的发送到配置的writeHost,但不推荐使用

(3)switchType属性

-1 表示不自动切换 

①1默认值,自动切换 

②2基于MySQL主从同步的状态决定是否切换 ,心跳语句为 show slave status 

(4)datahost其他配置

<dataHost name="localhost1" maxCon="1000" minCon="10" balance="1"  writeType="0" dbType="mysql"  dbDriver="native" switchType="1"> 

maxCon="1000":最大的并发连接数

minCon="10" :mycat在启动之后,会在后端节点上自动开启的连接线程

tempReadHostAvailable="1"

这个一主一从时(1个writehost,1个readhost时),可以开启这个参数,如果2个writehost,2个readhost时

<heartbeat>select user()</heartbeat>  监测心跳


8、垂直分表

(1)配置文件

#mv  schema.xml  schema.xml.bak3 

#vim schema.xml

<?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="sh1">

        <table name="user" dataNode="sh1"/>

        <table name="order_t" dataNode="sh2"/>

</schema>

    <dataNode name="sh1" dataHost="mifengdiandi1" database= "taobao" />

    <dataNode name="sh2" dataHost="mifengdiandi2" database= "taobao" />

    <dataHost name="mifengdiandi1" maxCon="1000" minCon="10" balance="1"  writeType="0" dbType="mysql"  dbDriver="native" switchType="1">

        <heartbeat>select user()</heartbeat>

    <writeHost host="db1" url="192.168.198.132:3307" user="root" password="123">

            <readHost host="db2" url="192.168.198.132:3309" user="root" password="123" />

    </writeHost>

    <writeHost host="db3" url="192.168.198.146:3307" user="root" password="123">

            <readHost host="db4" url="192.168.198.146:3309" user="root" password="123" />

    </writeHost>

    </dataHost>

    <dataHost name="mifengdiandi2" maxCon="1000" minCon="10" balance="1"  writeType="0" dbType="mysql"  dbDriver="native" switchType="1">

        <heartbeat>select user()</heartbeat>

    <writeHost host="db1" url="192.168.198.132:3308" user="root" password="123">

            <readHost host="db2" url="192.168.198.132:3310" user="root" password="123" />

    </writeHost>

    <writeHost host="db3" url="192.168.198.146:3308" user="root" password="123">

            <readHost host="db4" url="192.168.198.146:3310" user="root" password="123" />

    </writeHost>

    </dataHost>

</mycat:schema>


(2)创建测试库和表:

mysql -S /data/3307/mysql.sock -e "create database taobao charset utf8;"

mysql -S /data/3308/mysql.sock -e "create database taobao charset utf8;"

mysql -S /data/3307/mysql.sock -e "use taobao;create table user(id int,name varchar(20))";

mysql -S /data/3308/mysql.sock -e "use taobao;create table order_t(id int,name varchar(20))"

(3)测试

mysql -uroot -p123456 -h 127.0.0.1 -P8066