微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

MySQL 部署分布式架构 MyCAT (一)

架构

环境

主机名 IP
db1 192.168.31.205
db2 192.168.31.206

前期准备

开启防火墙,安装配置 MysqL (db1,db2)

firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.31.0/24" accept"
firewall-cmd --reload

mkdir /software
# 把软件 MysqL-5.7.20-linux-glibc2.12-x86_64.tar.gz 上传到  /software
cd /usr/local/
tar zxf /software/MysqL-5.7.20-linux-glibc2.12-x86_64.tar.gz
mv MysqL-5.7.20-linux-glibc2.12-x86_64 MysqL

# 初始化数据
mkdir /data/33{07..10}/data -p
MysqLd --initialize-insecure  --user=MysqL --datadir=/data/3307/data --basedir=/usr/local/MysqL
MysqLd --initialize-insecure  --user=MysqL --datadir=/data/3308/data --basedir=/usr/local/MysqL
MysqLd --initialize-insecure  --user=MysqL --datadir=/data/3309/data --basedir=/usr/local/MysqL
MysqLd --initialize-insecure  --user=MysqL --datadir=/data/3310/data --basedir=/usr/local/MysqL

配置 MysqL my.cnf配置 及启动脚本(db1)

cat >/data/3307/my.cnf<<EOF
[MysqLd]
basedir=/usr/local/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=/usr/local/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=/usr/local/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=/usr/local/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=/usr/local/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=/usr/local/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=/usr/local/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=/usr/local/MysqL/bin/MysqLd --defaults-file=/data/3310/my.cnf
LimitNOFILE = 5000
EOF

配置 MysqL my.cnf配置 及启动脚本(db2)

cat >/data/3307/my.cnf<<EOF
[MysqLd]
basedir=/usr/local/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=/usr/local/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=/usr/local/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=/usr/local/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=/usr/local/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=/usr/local/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=/usr/local/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=/usr/local/MysqL/bin/MysqLd --defaults-file=/data/3310/my.cnf
LimitNOFILE = 5000
EOF

修改权限,启动多实例(db1, db2)

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'"

分片规划

shard1:
    Master:192.168.31.205:3307
    slave1:192.168.31.205:3309
    Standby Master:192.168.31.206:3307
    slave2:192.168.31.206:3309
shard2:
    Master:192.168.31.206:3308
    slave1:192.168.31.206:3310
    Standby Master:192.168.31.205:3308
    slave2:192.168.31.205:3310

配置 shard1

# 192.168.31.205:3307 <-----> 192.168.31.206:3307(互为主从)
# db2:
MysqL  -S /data/3307/MysqL.sock -e "grant replication slave on *.* to repl@'192.168.31.%' identified by '123';"
MysqL  -S /data/3307/MysqL.sock -e "grant all  on *.* to root@'192.168.31.%' identified by '123'  with grant option;"
# db1:
MysqL  -S /data/3307/MysqL.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.31.206', 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"
# db2:
MysqL  -S /data/3307/MysqL.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.31.205', 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.31.205:3309 ------> 192.168.31.205:3307(3307 为主库,3309为从库)
# db1:
MysqL  -S /data/3309/MysqL.sock  -e "CHANGE MASTER TO MASTER_HOST='192.168.31.205', 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.31.206:3309 ------> 192.168.31.206:3307(3307 为主库,3309为从库)
# db2:
MysqL  -S /data/3309/MysqL.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.31.206', 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"

配置 shard2

# 192.168.31.206:3308 <-----> 192.168.31.205:3308(互为主从)
# db2:
MysqL  -S /data/3308/MysqL.sock -e "grant replication slave on *.* to repl@'192.168.31.%' identified by '123';"
MysqL  -S /data/3308/MysqL.sock -e "grant all  on *.* to root@'10.0.0.%' identified by '123'  with grant option;"
# db1: 
MysqL  -S /data/3308/MysqL.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.31.206', 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"
# db2:
MysqL  -S /data/3308/MysqL.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.31.205', 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.31.206:3310 -----> 192.168.31.206:3308(3308 为主库,3310为从库)
# db2:
MysqL  -S /data/3310/MysqL.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.31.206', 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.31.205:3310 -----> 192.168.31.205:3308(3308 为主库,3310为从库)
# db1: 
MysqL  -S /data/3310/MysqL.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.31.205', 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"

检测主从状态

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐