架构
环境
主机名 | 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</data/3308/my.cnf</data/3309/my.cnf</data/3310/my.cnf</etc/systemd/system/mysqld3307.service</etc/systemd/system/mysqld3308.service</etc/systemd/system/mysqld3309.service</etc/systemd/system/mysqld3310.service<
配置 MySQL my.cnf配置 及启动脚本(db2)
cat >/data/3307/my.cnf</data/3308/my.cnf</data/3309/my.cnf</data/3310/my.cnf</etc/systemd/system/mysqld3307.service</etc/systemd/system/mysqld3308.service</etc/systemd/system/mysqld3309.service</etc/systemd/system/mysqld3310.service<
修改权限,启动多实例(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