2024年运维最全第二十一周-day91-数据库day13-分布式架构

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


#### 3.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


#### 3.5 修改权限,启动多实例



chown -R mysql.mysql /data/*
systemctl start mysqld3307
systemctl start mysqld3308
systemctl start mysqld3309
systemctl start mysqld3310
systemctl enable mysqld3307
systemctl enable mysqld3308
systemctl enable mysqld3309
systemctl enable 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’”



> 
> ![](https://imgconvert.csdnimg.cn/aHR0cHM6Ly91cGxvYWQtaW1hZ2VzLmppYW5zaHUuaW8vdXBsb2FkX2ltYWdlcy8xNjk1MjE0OS0wODQ4MzA2OGI0ZGQ5OTEwLnBuZw)
> 
> 
> 


#### 3.6 节点主从规划



箭头指向谁是主库
10.0.0.51:3307 <-----> 10.0.0.52:3307
10.0.0.51:3309 ------> 10.0.0.51:3307
10.0.0.52:3309 ------> 10.0.0.52:3307

10.0.0.52:3308  <----->    10.0.0.51:3308
10.0.0.52:3310  ----->     10.0.0.52:3308
10.0.0.51:3310  ----->     10.0.0.51:3308

#### 3.7 分片规划



shard1:
Master:10.0.0.51:3307
slave1:10.0.0.51:3309
Standby Master:10.0.0.52:3307
slave2:10.0.0.52:3309
shard2:
Master:10.0.0.52:3308
slave1:10.0.0.52:3310
Standby Master:10.0.0.51:3308
slave2:10.0.0.51:3310


#### 3.8 开始配置



shard1
10.0.0.51:3307 <-----> 10.0.0.52:3307

db02
mysql -S /data/3307/mysql.sock -e “grant replication slave on . to repl@‘10.0.0.%’ identified by ‘123’;”
mysql -S /data/3307/mysql.sock -e “grant all on . to root@‘10.0.0.%’ identified by ‘123’ with grant option;”

db01
mysql -S /data/3307/mysql.sock -e “CHANGE MASTER TO MASTER_HOST=‘10.0.0.52’, 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=‘10.0.0.51’, 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”

10.0.0.51:3309 ------> 10.0.0.51:3307

db01
mysql -S /data/3309/mysql.sock -e “CHANGE MASTER TO MASTER_HOST=‘10.0.0.51’, 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”

10.0.0.52:3309 ------> 10.0.0.52:3307

db02
mysql -S /data/3309/mysql.sock -e “CHANGE MASTER TO MASTER_HOST=‘10.0.0.52’, 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
10.0.0.52:3308 <-----> 10.0.0.51:3308
db01
mysql -S /data/3308/mysql.sock -e “grant replication slave on . to repl@‘10.0.0.%’ identified by ‘123’;”
mysql -S /data/3308/mysql.sock -e “grant all on . to root@‘10.0.0.%’ identified by ‘123’ with grant option;”

db02
mysql -S /data/3308/mysql.sock -e “CHANGE MASTER TO MASTER_HOST=‘10.0.0.51’, 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=‘10.0.0.52’, 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”

10.0.0.52:3310 -----> 10.0.0.52:3308

db02
mysql -S /data/3310/mysql.sock -e “CHANGE MASTER TO MASTER_HOST=‘10.0.0.52’, 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”

10.0.0.51:3310 -----> 10.0.0.51:3308

db01
mysql -S /data/3310/mysql.sock -e “CHANGE MASTER TO MASTER_HOST=‘10.0.0.51’, 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”


#### 3.9 检测主从状态



mysql -S /data/3307/mysql.sock -e “show slave status\G”|grep Running:
mysql -S /data/3308/mysql.sock -e “show slave status\G”|grep Running:
mysql -S /data/3309/mysql.sock -e “show slave status\G”|grep Running:
mysql -S /data/3310/mysql.sock -e “show slave status\G”|grep Running:



> 
> ![](https://imgconvert.csdnimg.cn/aHR0cHM6Ly91cGxvYWQtaW1hZ2VzLmppYW5zaHUuaW8vdXBsb2FkX2ltYWdlcy8xNjk1MjE0OS0yOGJiYTViNTk2NTUzMmE4LnBuZw)
> 
> 
> 


#### 3.10 MySQL分布式架构介绍


![oldguo导师的图](https://imgconvert.csdnimg.cn/aHR0cHM6Ly91cGxvYWQtaW1hZ2VzLmppYW5zaHUuaW8vdXBsb2FkX2ltYWdlcy8xNjk1MjE0OS04ODQ5NWYzODIxN2JkN2IwLnBuZw)



  1. schema拆分及业务分库
  2. 垂直拆分-分库分表
  3. 水平拆分-分片

## 4. MyCAT安装


#### 4.1 预先安装Java运行环境



[root@db02 ~]# yum install -y java


#### 4.2 上传到/application



[root@db02 /server/tools]# wget http://192.168.0.163/mysql/Mycat-server-1.6.5-release-20180117003034-linux.tar.gz
[root@db02 /server/tools]# tar xf Mycat-server-1.6.5-release-20180117003034-linux.tar.gz



[root@db02 /server/tools]# ll mycat/
total 12
drwxr-xr-x 2 root root 190 Aug 21 17:10 bin
drwxrwxrwx 2 root root 6 Mar 1 2016 catlet
drwxrwxrwx 4 root root 4096 Aug 21 17:10 conf
drwxr-xr-x 2 root root 4096 Aug 21 17:10 lib
drwxrwxrwx 2 root root 6 Jan 17 2018 logs
-rwxrwxrwx 1 root root 219 Jan 17 2018 version.txt



[root@db02 /server/tools/mycat]# ll conf
autopartition-long.txt
auto-sharding-long.txt
auto-sharding-rang-mod.txt
cacheservice.properties
dbseq.sql
ehcache.xml
index_to_charset.properties
log4j2.xml
migrateTables.properties
myid.properties
partition-hash-int.txt
partition-range-mod.txt
rule.xml
schema.xml
sequence_conf.properties
sequence_db_conf.properties
sequence_distributed_conf.properties
sequence_time_conf.properties
server.xml
sharding-by-enum.txt
wrapper.conf
zkconf
zkdownload


#### 4.3 启动和连接



移动到/application下
[root@db02 ~]# mv /server/tools/mycat /application/

配置环境变量
vim /etc/profile
export PATH=/application/mycat/bin:$PATH
source /etc/profile

启动

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以点击这里获取!

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 9
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值