Loading

1+x云计算中级--脚本搭建读写分离

脚本搭建数据库读写分离(3台机器)

今天花费了几个小时写了一个小脚本,内容有点不是很美观,新手脚本,贴出来仅供参考,各路大神如果有什么建议,私聊或评论,谢谢!

使用前准备:
① 配置三台主机的IP
② 上传脚本和Mycat安装包和gpmall-repo包(放置在root目录下)
③ 上传read_write.sh脚本
④ chmod +x read_write.sh
⑤ ./read_write.sh运行脚本等待安装

欢迎优化!!!

#!/bin/bash
#Author: willian_1999y@163
#date: 2021-7-17
#version: 1.0

#数据库读写分离集群脚本(三台集群)

#环境变量
#ip变量,使用时请修改IP
NODE1_IP=192.168.17.21  
NODE2_IP=192.168.17.22
NODE3_IP=192.168.17.23
HOST_ONE='db1'
HOST_TWO='db2'
HOST_THREE='mycat'


#配置主机名和关闭防火墙、selinux、主机名映射
	change_host(){
		hostnamectl set-hostname $HOST_ONE
		setenforce 0
		getenforce
		sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
		systemctl stop firewalld;systemctl disable firewalld &> /dev/null
		iptables -F;iptables -X;iptables -Z;iptables-save &>/dev/null;iptables -L &> /root/installlog.log
		cat >/etc/hosts<<EOF
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
$NODE1_IP $HOST_ONE
$NODE2_IP $HOST_TWO
$NODE3_IP $HOST_THREE
EOF
		ssh root@192.168.17.22 "
hostnamectl set-hostname $HOST_TWO
setenforce 0
getenforce
sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
systemctl stop firewalld;systemctl disable firewalld &> /dev/null
iptables -F;iptables -X;iptables -Z;iptables-save &> /root/installlog.log;iptables -L &> /root/installlog.log
mv /etc/yum.repos.d/* /media
"
		ssh root@192.168.17.23 "
hostnamectl set-hostname $HOST_THREE
setenforce 0
getenforce
sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
systemctl stop firewalld;systemctl disable firewalld &> /dev/null
iptables -F;iptables -X;iptables -Z;iptables-save &> /root/installlog.log;iptables -L &> /root/installlog.log
mv /etc/yum.repos.d/* /media
"

		scp /etc/hosts @$NODE2_IP:/etc/hosts &>/dev/null;scp /etc/hosts @$NODE3_IP:/etc/hosts &>/dev/null
}

	yum_install(){
		mv -f /etc/yum.repos.d/* /media
		mkdir -p /mnt/centos
		cp -rf /root/gpmall-repo /mnt
		mount /dev/sr0 /mnt/centos &> /dev/null
		cat >/etc/yum.repos.d/local.repo<<EOF
[base]
name=centos
baseurl=file:///mnt/centos
gpgcheck=0
enabled=1

[gpmall]
name=gpmall
baseurl=file:///mnt/gpmall-repo
gpgcheck=0
enabled=1

EOF

#安装vsftpd,配置ftp源
		yum clean all &>/dev/null;yum install -y vsftpd &> /root/installlog.log
		echo "anon_root=/mnt" >> /etc/vsftpd/vsftpd.conf
		systemctl start vsftpd;systemctl enable vsftpd &> /root/installlog.log
		sed -i 's#file:///mnt#ftp://192.168.17.21#g' /etc/yum.repos.d/local.repo
		yum clean all &>/dev/null
		repolist=`yum repolist | grep repolist | awk '{print $2}'`
		if [ $? -eq 0 ];then
			echo -e "\033[32m ######### yum仓库完成,总共有安装包:$repolist个 ######### \033[0m"
			sleep 2
		else
			echo -e "\033[31m ######### yum仓库搭建失败,请检查... ######### \033[0m"
			exit 1		
		fi

		scp /etc/yum.repos.d/local.repo $NODE2_IP:/etc/yum.repos.d/ &>/dev/null
		scp /etc/yum.repos.d/local.repo $NODE3_IP:/etc/yum.repos.d/ &>/dev/null
		scp /root/Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz 192.168.17.23:/root/ &>/dev/null
		ssh root@$NODE2_IP "
yum clean all &>/dev/null;yum repolist &>/dev/null
"
		ssh root@$NODE3_IP '
yum clean all &>/dev/null;yum repolist &>/dev/null
yum install -y MariaDB-client &>/dev/null
yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel &>/dev/null
yum install -y net-tools &>/dev/null
echo -e "\033[32m ###########+ 已安装mariadb客户端 +########### \033[0m"
'
}


#主服务器安装数据库
		install_mariadb(){
			sleep 2
			echo -e "\033[034m ######### 正在安装MariaDB服务,请稍等... ######### \033[0m"
			sleep 1
			yum install -y mariadb-server mariadb &>/dev/null
			sleep 1
			systemctl start mariadb
			cat >>/etc/my.cnf<<EOF
[mysqld]
log_bin = mysql-bin
binlog_ignore_db = mysql
server_id = 21
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

EOF
			sleep 1
			systemctl restart mariadb;systemctl enable mariadb &>/dev/null
			mysqladmin -uroot password '123456'
			if [ $? -eq 0 ];then
				echo -e "\033[37m ++重启主服务器数据库++++++++++++++++++++++++++\033[0m\033[32mOK \033[0m"
				sleep 2
			else 
				echo -e "\033[37m ++重启主服务器数据库++++++++++++++++++++++++++\033[0m\033[31mNO \033[0m"
				exit 1
			fi
			
			echo -e "\033[35m ######### 正在配置主服务器请稍等.... ######### \033[0m"
			sleep 2
			mysql -uroot -p123456<<EOF
grant all privileges on *.* to root@'%' identified by "123456";
grant replication slave on *.* to 'user'@'db2' identified by '123456';
FLUSH PRIVILEGES;
CREATE DATABASE IF NOT EXISTS  test;
use test;
CREATE TABLE IF NOT EXISTS company(id int not null primary key,name varchar(50),addr varchar(255));
insert into company values(3,"facebook","usa");
exit
EOF
			if [ $? -eq 0 ];then
				echo -e "\033[32m ######### 主服务器配置成功 ######### \033[0m"
				sleep 2
			else
				echo -e "\033[31m ######### 主服务器配置错误 ######### \033[0m"
				exit 1
			fi
			
			sleep 3
#配置从服务器数据库		
			echo -e  "\033[35m ######### 正在配置从服务器,请稍等... ######### \033[0m"
			sleep 2
			ssh root@$NODE2_IP "
yum install mariadb-server mariadb -y &>/dev/null
systemctl start mariadb

mysqladmin -uroot password '123456'
cat >>/etc/my.cnf<<EOF
[mysqld]
server_id = 22
EOF

mysql -uroot -p123456<<EOF
change master to master_host='$NODE1_IP',master_user='user',master_password='123456';
FLUSH PRIVILEGES;
start slave;
show slave status\G;
exit
clear
systemctl restart mariadb;systemctl enable mariadb &>/dev/null

EOF
"	
			if [ $? -eq 0 ];then
				sleep 2
				echo -e "\033[32m ######### 从服务器配置完成 ######### \033[0m"
				sleep 1
				echo -e "\033[33m ######### 查看从服务器中的数据库 ######### \033[0m"
				ssh root@$NODE2_IP "mysql -uroot -p123456 -e 'show databases;'"
			else
				echo -e "\033[31m ######### 从服务器配置错误 ######### \033[0m"
				exit 1
			fi
				
}


#安装mycat中间件服务
		install_mycat(){
			echo -e "\033[33m ######### 正在安装Mycat服务... ######### \033[0m"
			sleep 2

#修改Mycat服务配置文件
			ssh root@$NODE3_IP "
tar zxf /root/Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz  -C /usr/local
chmod -R 777 /usr/local/mycat/
echo export MYCAY_HOME=/usr/local/mycat/ >> /etc/profile
source /etc/profile
cat >/usr/local/mycat/conf/schema.xml<<EOF
<?xml version='1.0'?>
<!DOCTYPE mycat:schema SYSTEM 'schema.dtd'>
<mycat:schema xmlns:mycat='http://io.mycat/'>

        <schema name='USERDB' checkSQLschema='true' sqlMaxLimit='100' dataNode='dn1'></schema>
<dataNode name='dn1' dataHost='localhost1' database='test' />
 <dataHost name='localhost1' maxCon='1000' minCon='10' balance='3' dbType='mysql' dbDriver='native' writeType='0' switchType='1' slaveThreshold='100'>
        <heartbeat>select user()</heartbeat>
        <writeHost host='hostM1' url='192.168.17.21:3306' user='root' password='123456'>
                <readHost host='hostS1' url='192.168.17.22:3306' user='root' password='123456' />
        </writeHost>
 </dataHost>
</mycat:schema>

EOF

chown root:root /usr/local/mycat/conf/schema.xml
cat >/usr/local/mycat/conf/server.xml<<EOF
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE mycat:server SYSTEM 'server.dtd'>
<mycat:server xmlns:mycat='http://io.mycat/'>
        <system>
        <property name='useSqlStat'>0</property>
        <property name='useGlobleTableCheck'>0</property>

                <property name='sequnceHandlerType'>2</property>
                <property name='processorBufferPoolType'>0</property>
                <property name='handleDistributedTransactions'>0</property>
                <property name='useOffHeapForMerge'>1</property>
                <property name='memoryPageSize'>1m</property>
                <property name='spillsFileBufferSize'>1k</property>
                <property name='useStreamOutput'>0</property>
                <property name='systemReserveMemorySize'>384m</property>
                <property name='useZKSwitch'>true</property>
        </system>
        <user name='root'>
                <property name='password'>123456</property>
                <property name='schemas'>USERDB</property>
        </user>
</mycat:server>
EOF

bash /usr/local/mycat/bin/mycat start
bash /usr/local/mycat/bin/mycat status &>/dev/null
clear
netstat -tnlp | grep 9066;netstat -tnlp | grep 8066
"

#验证阶段
		sleep 1
		echo -e "\033[33m ######### 正在验证Mycat服务是否正常... ######### \033[0m"
		sleep 2
		ssh root@$NODE3_IP "mysql -h127.0.0.1 -P9066 -uroot -p123456 -e 'show @@datasource;'"
		if [ $? -eq 0 ];then
			echo -e "\033[32m ######### mycat服务运行正常 ######### \033[0m"
			sleep 2
		else
			echo -e "\033[31m ######### mycat服务运行不正常 ######### \033[0m"
			exit 1
		fi	

}


#主运行程序

	echo -e "\033[35m ######### 正在配置数据库读写分离,请稍等....(大概需要3分钟) ######### \033[0m"
	sleep 1
	change_host
	yum_install
	install_mariadb
	install_mycat
	sleep 2

	if [ $? -eq 0 ];then
		echo -e "\033[32m ######### 读写分离配置完成,请再次检查... ######### \033[0m"
		exit 0
	else
		echo -e "\033[31m ######### 读写分离配置出错哦 ######### \033[0m"
		exit 1
	fi

B站已上传运行过程视频,请移步观看(看到这里觉得还不错记得三连哦 😃 )

视频过程入口——>点它,就点它

posted @ 2022-08-13 10:38  knsec  阅读(47)  评论(0编辑  收藏  举报