Mycat中间件综合部署高可用-读写分离-分库分表(1.6)

news/2024/11/23 23:21:58/

Mycat中间件综合部署(1.6)

实施拓扑

请添加图片描述

一,环境搭建

1.服务分配

主机服务
192.168.2.1MySQL-cluster-1
192.168.2.2MySQL-cluster-2
192.168.2.3Mycat

2.MySQL-cluster1分配

server-id端口角色
13306master1
23307slave1
33308master2
43309slave2

3.MySQL-cluster2分配

server-id端口角色
53306master1
63307slave1
73308master2
83309slave2

4.结构

请添加图片描述

二,MySQL-cluster-1搭建

1.搭建MySQL单台多实例

rpm -e mariadb-libs-5.5.52-1.el7.x86_64 --nodeps
yum install -y ncurses-devel libaio-devel autoconf
tar zxf mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.36-linux-glibc2.12-x86_64 mysql
mv mysql /usr/local/
useradd -s /sbin/nologin -M -r mysqlmkdir -p /mysql/330{6,7,8,9}/data
chown -R mysql:mysql  /mysql/
chown -R mysql:mysql /usr/local/mysql
ln -s /usr/local/mysql/bin/* /usr/local/bin/

2.MySQL配置文件

cat > /mysql/3306/my.cnf <<EOF
[mysqld]
basedir=/usr/local/mysql
datadir= /mysql/3306/data/
socket= /mysql/3306/mysql.sock
log_error= /mysql/3306/
port=3306
server_id=1
log_bin= /mysql/3306/data/mysql-bin
log-slave-updates=1
EOFcat > /mysql/3307/my.cnf <<EOF
[mysqld]
basedir=/usr/local/mysql
datadir= /mysql/3307/data/
socket= /mysql/3307/mysql.sock
log_error= /mysql/3307/
port=3307
server_id=2
log_bin= /mysql/3307/data/mysql-bin
EOFcat > /mysql/3308/my.cnf <<EOF
[mysqld]
basedir=/usr/local/mysql
datadir= /mysql/3308/data/
socket= /mysql/3308/mysql.sock
log_error= /mysql/3308/
port=3308
server_id=3
log_bin= /mysql/3308/data/mysql-bin
log-slave-updates=1
EOFcat > /mysql/3309/my.cnf <<EOF
[mysqld]
basedir=/usr/local/mysql
datadir= /mysql/3309/data/
socket= /mysql/3309/mysql.sock
log_error= /mysql/3309/
port=3309
server_id=4
log_bin= /mysql/3309/data/mysql-bin
EOF

3.数据库初始化

mysqld --initialize-insecure  --user=mysql --datadir=/mysql/3306/data --basedir=/usr/local/mysql
mysqld --initialize-insecure  --user=mysql --datadir=/mysql/3307/data --basedir=/usr/local/mysql
mysqld --initialize-insecure  --user=mysql --datadir=/mysql/3308/data --basedir=/usr/local/mysql
mysqld --initialize-insecure  --user=mysql --datadir=/mysql/3309/data --basedir=/usr/local/mysql

4.数据库启动配置

cat > /etc/systemd/system/mysqld3306.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=/usr/local/mysql/bin/mysqld  --defaults-file=/mysql/3306/my.cnf --user=mysql
LimitNOFILE = 5000
EOFcat > /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=/usr/local/mysql/bin/mysqld  --defaults-file=/mysql/3307/my.cnf --user=mysql
LimitNOFILE = 5000
EOFcat > /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=/usr/local/mysql/bin/mysqld  --defaults-file=/mysql/3308/my.cnf --user=mysql
LimitNOFILE = 5000
EOFcat > /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=/usr/local/mysql/bin/mysqld  --defaults-file=/mysql/3309/my.cnf --user=mysql
LimitNOFILE = 5000
EOF

5.启动所有MySQL

systemctl start mysqld3306.service
systemctl start mysqld3307.service
systemctl start mysqld3308.service
systemctl start mysqld3309.service

6.主从配置

注意:master的二进制日志的名称和位置,开启从后检查状态

mysql -S /mysql/3306/mysql.sock
mysql> show master status;mysql-bin.000004 |      439grant all on *.* to root@'%' identified by '1234.Com';
exitmysql -S /mysql/3307/mysql.sock
change master to master_host='192.168.2.1',master_user='root',master_password='1234.Com',master_port=3306 ,master_log_file='mysql-bin.000004',master_log_pos=439;
start slave;
grant all on *.* to root@'%' identified by '1234.Com';mysql -S /mysql/3308/mysql.sock
grant all on *.* to root@'%' identified by '1234.Com';
mysql> show master status;
mysql-bin.000001 |      439change master to master_host='192.168.2.1',master_user='root',master_password='1234.Com',master_port=3306 ,master_log_file='mysql-bin.000004',master_log_pos=439;
start slave;mysql -S /mysql/3309/mysql.sock
change master to master_host='192.168.2.1',master_user='root',master_password='1234.Com',master_port=3308 ,master_log_file='mysql-bin.000001',master_log_pos=439;
start slave;
grant all on *.* to root@'%' identified by '1234.Com';mysql -S /mysql/3306/mysql.sock
change master to master_host='192.168.2.1',master_user='root',master_password='1234.Com',master_port=3308 ,master_log_file='mysql-bin.000001',master_log_pos=439;

三,MySQL-cluster-2搭建

1.搭建MySQL单台多实例

rpm -e mariadb-libs-5.5.52-1.el7.x86_64 --nodeps
yum install -y ncurses-devel libaio-devel autoconf
tar zxf mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.36-linux-glibc2.12-x86_64 mysql
mv mysql /usr/local/
useradd -s /sbin/nologin -M -r mysqlmkdir -p /mysql/330{6,7,8,9}/data
chown -R mysql:mysql  /mysql/
chown -R mysql:mysql /usr/local/mysql
ln -s /usr/local/mysql/bin/* /usr/local/bin/

2.MySQL配置文件

cat > /mysql/3306/my.cnf <<EOF
[mysqld]
basedir=/usr/local/mysql
datadir= /mysql/3306/data/
socket= /mysql/3306/mysql.sock
log_error= /mysql/3306/
port=3306
server_id=5
log_bin= /mysql/3306/data/mysql-bin
log-slave-updates=1
EOFcat > /mysql/3307/my.cnf <<EOF
[mysqld]
basedir=/usr/local/mysql
datadir= /mysql/3307/data/
socket= /mysql/3307/mysql.sock
log_error= /mysql/3307/
port=3307
server_id=6
log_bin= /mysql/3307/data/mysql-bin
EOFcat > /mysql/3308/my.cnf <<EOF
[mysqld]
basedir=/usr/local/mysql
datadir= /mysql/3308/data/
socket= /mysql/3308/mysql.sock
log_error= /mysql/3308/
port=3308
server_id=7
log_bin= /mysql/3308/data/mysql-bin
log-slave-updates=1
EOFcat > /mysql/3309/my.cnf <<EOF
[mysqld]
basedir=/usr/local/mysql
datadir= /mysql/3309/data/
socket= /mysql/3309/mysql.sock
log_error= /mysql/3309/
port=3309
server_id=8
log_bin= /mysql/3309/data/mysql-bin
EOF

3.数据库初始化

mysqld --initialize-insecure  --user=mysql --datadir=/mysql/3306/data --basedir=/usr/local/mysql
mysqld --initialize-insecure  --user=mysql --datadir=/mysql/3307/data --basedir=/usr/local/mysql
mysqld --initialize-insecure  --user=mysql --datadir=/mysql/3308/data --basedir=/usr/local/mysql
mysqld --initialize-insecure  --user=mysql --datadir=/mysql/3309/data --basedir=/usr/local/mysql

4.数据库启动配置

cat > /etc/systemd/system/mysqld3306.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=/usr/local/mysql/bin/mysqld  --defaults-file=/mysql/3306/my.cnf --user=mysql
LimitNOFILE = 5000
EOFcat > /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=/usr/local/mysql/bin/mysqld  --defaults-file=/mysql/3307/my.cnf --user=mysql
LimitNOFILE = 5000
EOFcat > /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=/usr/local/mysql/bin/mysqld  --defaults-file=/mysql/3308/my.cnf --user=mysql
LimitNOFILE = 5000
EOFcat > /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=/usr/local/mysql/bin/mysqld  --defaults-file=/mysql/3309/my.cnf --user=mysql
LimitNOFILE = 5000
EOF

5.启动所有MySQL

systemctl start mysqld3306.service
systemctl start mysqld3307.service
systemctl start mysqld3308.service
systemctl start mysqld3309.service

6.主从配置

mysql -S /mysql/3306/mysql.sock
mysql> show master status;
mysql-bin.000001 |       434grant all on *.* to root@'%' identified by '1234.Com';mysql -S /mysql/3307/mysql.sock
change master to master_host='192.168.2.2',master_user='root',master_password='1234.Com',master_port=3306 ,master_log_file='mysql-bin.000001',master_log_pos= 434;
start slave;
grant all on *.* to root@'%' identified by '1234.Com';mysql -S /mysql/3308/mysql.sock
grant all on *.* to root@'%' identified by '1234.Com';
mysql> show master status;
mysql-bin.000001 |      434change master to master_host='192.168.2.2',master_user='root',master_password='1234.Com',master_port=3306 ,master_log_file='mysql-bin.000001',master_log_pos=434;
start slave;mysql -S /mysql/3309/mysql.sock
change master to master_host='192.168.2.2',master_user='root',master_password='1234.Com',master_port=3308 ,master_log_file='mysql-bin.000001',master_log_pos=434;
start slave;
grant all on *.* to root@'%' identified by '1234.Com';mysql -S /mysql/3306/mysql.sock
change master to master_host='192.168.2.2',master_user='root',master_password='1234.Com',master_port=3308 ,master_log_file='mysql-bin.000001',master_log_pos=434;
start slave;

四,MyCat搭建

1.安装MyCat

[root@localhost ~]# ls
anaconda-ks.cfg  jdk-8u171-linux-x64.tar.gz  Mycat-server-1.6.7.3-release-20210913163959-linux.tar.gz  mysql-connector-java-8.0.22.jar 
tar -zxf jdk-8u171-linux-x64.tar.gz
mv jdk1.8.0_171/ /usr/local/java
vi /etc/profile
source /etc/profile
tar zxf Mycat-server-1.6.7.3-release-20210913163959-linux.tar.gz -C /usr/local/
mv mysql-connector-java-8.0.22.jar /usr/local/mycat/lib/
chmod 777 /usr/local/mycat/lib/* -R

2.schema结构

请添加图片描述

3.schema配置

<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/"><schema name="DB01" checkSQLschema="true" sqlMaxLimit="100"><table name="TB_ORDER" dataNode="dn1,dn2" rule="mod-long" /><table name="TB01" dataNode="dn1,dn2" rule="mod-long" /></schema><dataNode name="dn1" dataHost="dhost1" database="db01" /><dataNode name="dn2" dataHost="dhost2" database="db01" /><dataHost name="dhost1" maxCon="1000" minCon="10" balance="1"writeType="0" dbType="mysql" dbDriver="jdbc" switchType="1"  slaveThreshold="100"><heartbeat>select user()</heartbeat><writeHost host="master1" url="jdbc:mysql://192.168.2.1:3306?useSSL=false&amp;serverTimezone=Asia/Shanghai&amp;characterEncoding=utf8" user="root" password="1234.Com"><readHost host="slave1" url="jdbc:mysql://192.168.2.1:3307?useSSL=false&amp;serverTimezone=Asia/Shanghai&amp;characterEncoding=utf8" user="root" password="1234.Com" /></writeHost><writeHost host="master2" url="jdbc:mysql://192.168.2.1:3308?useSSL=false&amp;serverTimezone=Asia/Shanghai&amp;characterEncoding=utf8" user="root" password="1234.Com"><readHost host="slave2" url="jdbc:mysql://192.168.2.1:3309?useSSL=false&amp;serverTimezone=Asia/Shanghai&amp;characterEncoding=utf8" user="root" password="1234.Com" /></writeHost></dataHost><dataHost name="dhost2" maxCon="1000" minCon="10" balance="1"writeType="0" dbType="mysql" dbDriver="jdbc" switchType="1"  slaveThreshold="100"><heartbeat>select user()</heartbeat><writeHost host="master3" url="jdbc:mysql://192.168.2.2:3306?useSSL=false&amp;serverTimezone=Asia/Shanghai&amp;characterEncoding=utf8" user="root" password="1234.Com"><readHost host="slave3" url="jdbc:mysql://192.168.2.2:3307?useSSL=false&amp;serverTimezone=Asia/Shanghai&amp;characterEncoding=utf8" user="root" password="1234.Com" /></writeHost><writeHost host="master4" url="jdbc:mysql://192.168.2.2:3308?useSSL=false&amp;serverTimezone=Asia/Shanghai&amp;characterEncoding=utf8" user="root" password="1234.Com"><readHost host="slave4" url="jdbc:mysql://192.168.2.2:3309?useSSL=false&amp;serverTimezone=Asia/Shanghai&amp;characterEncoding=utf8" user="root" password="1234.Com" /></writeHost></dataHost></mycat:schema>

4.server.xml配置

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

5.rule.xml配置

<function name="mod-long" class="io.mycat.route.function.PartitionByMod"><!-- how many data nodes --><property name="count">2</property></function>

五,测试

create database db01;use db01;create table tb01(id int(11) not null,name varchar(50) not null,sex varchar(1),primary key (id)
)engine=innodb default charset=utf8;insert into tb01(id,name,sex) values(1,'Tom','1');
insert into tb01(id,name,sex) values(2,'Trigger','0');
insert into tb01(id,name,sex) values(3,'Dawn','1');insert into tb01(id,name,sex) values(4,'Jack Ma','1');
insert into tb01(id,name,sex) values(5,'Coco','0');
insert into tb01(id,name,sex) values(6,'Jerry','1');insert into tb01(id,name,sex) values(7,'JackKK Ma','1');
insert into tb01(id,name,sex) values(8,'CCCoco','0');
insert into tb01(id,name,sex) values(9,'JerryYY','1');insert into tb01(id,name,sex) values(10,'JackKK MaAA','1');
insert into tb01(id,name,sex) values(11,'CCCocoAA','0');
insert into tb01(id,name,sex) values(12'JerryYYAA','1');

Dawn’,‘1’);

insert into tb01(id,name,sex) values(4,‘Jack Ma’,‘1’);
insert into tb01(id,name,sex) values(5,‘Coco’,‘0’);
insert into tb01(id,name,sex) values(6,‘Jerry’,‘1’);

insert into tb01(id,name,sex) values(7,‘JackKK Ma’,‘1’);
insert into tb01(id,name,sex) values(8,‘CCCoco’,‘0’);
insert into tb01(id,name,sex) values(9,‘JerryYY’,‘1’);

insert into tb01(id,name,sex) values(10,‘JackKK MaAA’,‘1’);
insert into tb01(id,name,sex) values(11,‘CCCocoAA’,‘0’);
insert into tb01(id,name,sex) values(12,‘JerryYYAA’,‘1’);



http://www.ppmy.cn/news/235182.html

相关文章

通用高拍仪软件_动态展示和教学 良田YL1050AF高拍仪评测

高拍仪的一大特点就是灵活多样&#xff0c;通过不同的工业设计用于文件扫描、视频展示、多媒体教学、信息采集、体温监测等等。良田最新款YL1050AF教学高拍仪通过巧妙的设计和专业的软件&#xff0c;为教学、培训、实时展示等应用带来了新的形式。 动态展示和教学 良田YL1050AF…

高拍仪Twain接口功能

开启任一个支持标准Twain接口的软件即可呼出Twain窗口&#xff0c;如WPS&#xff1b;打开WPS依次选择“插入”---“图片”---“扫描仪”,选择Twain来源设备“WDTwain V1.2”后将出现WDTwain主窗口。&#xff08;此图示与各软件的接口有关&#xff0c;可能会有不同&#xff09; …

JSON与storage

JSON JSON由来 ◼ 在目前的开发中&#xff0c;JSON是一种非常重要的数据格式&#xff0c;它并不是编程语言&#xff0c;而是一种可以在服务器和客户端之间传输的数据格式。 ◼ JSON的全称是JavaScript Object Notation&#xff08;JavaScript对象符号&#xff09;&#xff1a…

计算机培训班价格多少钱?

电脑培训分为许多学科&#xff0c;如软件、硬件、多媒体、网络等多个项目&#xff0c;项目里面又有很多细分学科&#xff0c;如软件中又分为软件开发、java软件、安卓等&#xff0c;不同的专业计算机培训班价格也就不一样&#xff0c;总而言之就是根据大家所选择的课程进行判断…

100ah蓄电池多少度电

1、理论上可以存1.2度电。 2、蓄电池&#xff0c;是指放电后&#xff0c;能够用充电的方式使内部活性物质再生——把电能储存为化学能;需要放电时再次把化学能转换为电能。将这类电池称为蓄电池(Storage Battery)&#xff0c;也称二次电池。 3、瓦是功率单位&#xff0c;符号: …

阿里云语音合成价格是多少?

阿里云语音合成价格从几十元-几万元不等&#xff0c;看您买多大的资源包&#xff0c;还可以再给15个点的优惠&#xff0c;现在各大平台都有语音转文字&#xff0c;文字转语音的转换&#xff0c;越来越普遍&#xff0c;咱们平时用的视频小软件都有这个功能&#xff0c;各位准备上…

微信小程序定制价格多少钱

**微信小程序定制价格需要多少钱&#xff1f;**根据需求的不同会产生不同的价格浮动&#xff0c;一般微信小程序定制根据以下不同的方面进行定制价格评估。 **1.展示型小程序&#xff1a;**展示型的小程序一般都是用于企业的宣传&#xff0c;以达到提高企业的知名度的目的。这…

10kv开关柜价格_10kv开关柜价格一般多少

答&#xff1a;西门子2P32A漏电保护器开关,家用断路器空气开关,带漏电保护器,安全可靠,开关采用静电喷漆,能够搭配家居环境,参考价格:179元 价格来源网络仅供参考 答&#xff1a;10kv广东茂名英达绝缘漆价格120元该漆挺不错的,可以室内使用,没有气味,只是价格高些。绝缘漆是漆类…