集群服務(wù)部署
1、配置主庫映射文件
鍵名MySQLd.cnf
## The Percona Server 5.7 configuration file.
### * IMPORTANT: Additional settings that can override those from this file!
# The files must end with '.cnf', otherwise they'll be ignored.
# Please make any edits and changes to the Appropriate sectional files# included below.
[mysqld]
server_id = 1
log-bin= mysql-binread-only=0
replicate-ignore-db=mysql
replicate-ignore-db=sys
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
[client]
user = root
password =123456
配置從庫映射文件:
## The Percona Server 5.7 configuration file.
### * IMPORTANT: Additional settings that can override those from this file!# The files must end with '.cnf', otherwise they'll be ignored.
# Please make any edits and changes to the appropriate sectional files
# included below.
[mysqld]
server_id = 2
log-bin= mysql-binread-only=0
replicate-ignore-db=mysql
replicate-ignore-db=sys
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
[client]
user = root
password =123456
文件掛載目錄 /etc/my.cnf.d/
2、部署服務(wù)mysql-1,mysql-2
部署服務(wù),type=statefulset,port:3306 默認(rèn)模式 444
部署完成需要給每個(gè)工作負(fù)載添加一個(gè)sidecar
選擇init容器
執(zhí)行命令 入口:/usr/bin/chown 命令:-R mysql /var/lib/mysql
建好后,重新部署mysql-1,mysql-2。
分別進(jìn)入mysql-1,mysql-2,命令行界面,查看mysql服務(wù)是否正常運(yùn)行;
3、正常運(yùn)行后,設(shè)置集群腳本 setgroup.sh
設(shè)置主從前須保證主從能正常通訊且數(shù)據(jù)一致,否則主從關(guān)系無法建立或操作不一致的數(shù)據(jù)時(shí)會(huì)導(dǎo)致主從關(guān)系Error。
進(jìn)入mysql-1或者mysql-2命令行,新建腳本 setgroup.sh(文件名自行命名):
#!/bin/bash
MASTERNAME="mysql-1" #數(shù)據(jù)庫信息
SLAVE_2="mysql-2"
#SLAVE_3="mysql-3"
PORT="3306"
USERNAME="root"
PWD="634634"
touch tmp.txt
mysql -h${MASTERNAME} << EOF
reset master;
grant replication slave on *.* to '${MASTERNAME}'@'%' identified by '${PWD}';
flush privileges;
tee ./tmp.txt;
show master statusG;
notee;
EOF
FILE_bin=`awk '/File/ {print $2}' tmp.txt`
POSIT=`awk '/Position/ {print $2}' tmp.txt`
rm -rf ./tmp.txt
touch tmp1.txt
mysql -h${SLAVE_2} << EOF
stop slave;
change master to master_host='${MASTERNAME}',master_port=${PORT},master_user='${USERNAME}',master_password='${PWD}',master_log_file='${FILE_bin}',master_log_pos=${POSIT};
start slave;
tee ./tmp1.txt;
show slave statusG;
show variables like "server_id";
notee;
EOF
id_1=`awk '/server_id/' tmp1.txt`
IO_sta_1=`awk '/Slave_IO_Running/' tmp1.txt`
SQL_sta_1=`awk '/Slave_SQL_Running:/' tmp1.txt`
rm -rf tmp1.txt
#touch tmp2.txt
#mysql -h${SLAVE_3} << EOF
#stop slave;
#change master to master_host='${MASTERNAME}',master_port=${PORT},master_user='${USERNAME}',master_password='${PWD}',master_log_file='${FILE_bin}',master_log_pos=${POSIT};
#start slave;
#tee ./tmp2.txt;
#show slave statusG;
#show variables like "server_id";
#notee;
#EOF
#id_2=`awk '/server_id/' tmp2.txt`
#IO_sta_2=`awk '/Slave_IO_Running/' tmp2.txt`
#SQL_sta_2=`awk '/Slave_SQL_Running:/' tmp2.txt`
#rm -rf tmp2.txt
#clear
echo ${id_1}
echo ${IO_sta_1}
echo ${SQL_sta_1}
#echo ${id_2}
#echo ${IO_sta_2}
#echo ${SQL_sta_2}
執(zhí)行./setgroup,進(jìn)入mysql-2查看鏈接狀態(tài):
[root@mysql-2-0 app]# mysql;
mysql>show slave statusG
4、驗(yàn)證
user=root password=634634,服務(wù)器配置文件已經(jīng)在[client]字段添加好了,所以直接mysql就可以進(jìn)入控制臺。
進(jìn)入mysql-1,新建一個(gè)database ,再進(jìn)入mysql-2驗(yàn)證是否同步;
[root@mysql-1-0 app]# mysql;
mysql>create database test;
mysql>show databases;
[root@mysql-2-0 app]# mysql;
mysql>show databases;