三台centos机器 搭建
#!/bin/bash
echo '请严格注意kafka zookeeper '
benji_ip=$(ifconfig ens33 |awk 'NR==2 {print $2}')
ip_host1=192.168.197.5
ip_host2=192.168.197.6
ip_host3=192.168.197.7
wangduan=192.168.197.0/24
gpmall_repo="/opt/gpmall-repo"? ? ? ? #gpmall源的存放地址
nginx_wed=/opt/disk/? ? ? ? ? ? #nginx-wed文件地址
MYCAT=/opt/Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz #mycat二进制包文件地址
zk=/opt/apache-zookeeper-3.8.1-bin.tar.gz
zk_flie=/opt/apache-zookeeper-3.8.1-bin
kafka=/opt/kafka_2.11-2.4.1.tgz
kafka_file=/opt/kafka_2.11-2.4.1
mysqlname=gpmall
gpmallsql=/opt/gpmall.sql
mysql_denglu="mysql -uroot -p123456"
shopping_provider=/opt/shopping-provider-0.0.1-SNAPSHOT.jar &
user_provider=/opt/user-provider-0.0.1-SNAPSHOT.jar &
gpmall_shopping=/opt/gpmall-shopping-0.0.1-SNAPSHOT.jar &
gpmall_user=/opt/gpmall-user-0.0.1-SNAPSHOT.jar &
echo "#########################################基础搭建############################################"
rm -rf /etc/yum.repos.d/*? &&? curl -o /etc/yum.repos.d/CentOS-Base.repo https://repo.huaweicloud.com/repository/conf/CentOS-7-reg.repo
yum install bash-completion vim net-tools.x86_64 psmisc -y? &> /dev/null? # 下载命令补全
systemctl stop firewalld.service && systemctl disable firewalld.service
sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config # 修更 配置文件? 关于隐藏
setenforce 0 && getenforce
clear
echo "#############---menu---#############"
echo "# 1.修改mycat主机名 "
echo "# 2.修改db1主机名 "
echo "# 3.修改db2主机名 "
echo "####################################"
read -p "修改主机名请扣以上序号:" -t 300 hostname
case $hostname in
1)
hostnamectl set-hostname mycat
;;
2)
hostnamectl set-hostname db1
;;
3)
hostnamectl set-hostname db3
;;
esac
cat > /etc/yum.repos.d/gpmall.repo <<EOF
[gpmall]
name=gpmall
baseurl=file://$gpmall_repo
gpgcheck=0
enabled=1
EOF
yum clean all && yum repolist
sleep 5
echo "############################################基础搭建完成#########################################"
clear
echo "#############---chrony---#############"
echo "# 1.主节点chrony "
echo "# 2.从节点chrony"
echo "####################################"
read -p "安装主节点chrony请扣1 ,安装从节点chrony请扣2: " -t 500 ntp
case $ntp in
1)
? yum install chrony.x86_64 -y
sed -i 's/^server.*//g' /etc/chrony.conf
sleep 5
sed -i '$a server ntp.aliyun.com iburst' /etc/chrony.conf
sleep 3
sed -i '$a allow '${wangduan}'' /etc/chrony.conf
sleep 2
sed -i '$a local stratum 10' /etc/chrony.conf
sleep 2
systemctl restart chronyd && systemctl enable chronyd
chronyc sources
sleep 5
chronyc sources
;;
2)
? yum install chrony.x86_64 -y
sed -i 's/^server.*//g' /etc/chrony.conf
sed -i '$a server '${ip_host1}' iburst' /etc/chrony.conf
systemctl restart chronyd && systemctl enable chronyd
chronyc sources
sleep 5
chronyc sources
;;
esac
cat >> /etc/hosts << EOF
$ip_host1 mycat
$ip_host2 db1
$ip_host3 db2
EOF
#配置互信
ssh-keygen
ssh-copy-id $benji_ip
ssh-copy-id $ip_host2
ssh-copy-id $ip_host3
yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
? ? ? ? ? ? ? ? #查看java版本
java -version
? ? ? ? ? ? ? ? #检查java环境是否ok
if [ $-eq 0 ];
then
echo -e "\033[36m==========java环境安装成功==========\033[0m"
elif
? echo -e "\033[31m**********安装环境失败,请检查**********\033[0m";then
yum repolist
fi
sleep 5
echo 建议在mycat节点上停下,先把db1,db2节点的数据库安装完成
read -p "是否要安装mycat(建议先把db1/2节点运行完shell后,在回来执行mycat,顺序为db1,db2,mycat1): " -t 500 mycat
if [ "${mycat}" == "是" -o "${mycat}" == "Y" -o "${mycat}" == "y" ];then
tar -zxvf $MYCAT -C /usr/local/
chmod -R 777 /usr/local/mycat/
echo export MYCAT_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="$mysqlname" checkSQLschema="true" sqlMaxLimit="100" dataNode="dn1"></schema>
<dataNode name="dn1" dataHost="localhost1" database="$mysqlname" />?
<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="$ip_host2:3306" user="root" password="123456">
? ? ? ? <readHost host="hostS1" url="$ip_host3:3306" user="root" password="123456" />
? ? </writeHost>
</dataHost>
</mycat:schema>
eof
sed -i '82c <property name="schemas">'$mysqlname'</property>' /usr/local/mycat/conf/server.xml
sed? -i '95,99d' /usr/local/mycat/conf/server.xml
#sed? -i '96d' /usr/local/mycat/conf/server.xml
#sed? -i '97d' /usr/local/mycat/conf/server.xml
#sed? -i '98d' /usr/local/mycat/conf/server.xml
#sed? -i '99d' /usr/local/mycat/conf/server.xml
/bin/bash /usr/local/mycat/bin/mycat start
netstat -tunlp |grep 8066 && netstat -tunlp |grep 9066
jps
sleep 5
elif [ "${mycat}" == "否" ] || [ "${mycat}" == "N" ] || [ "${mycat}" == "n" ];then
echo 撤退
fi
read -p "是要否要验证mycat(mycat节点): " -t 500 MariaDB
if [ "${MariaDB}" == "是" -o "${MariaDB}" == "Y" -o "${MariaDB}" == "y" ];then
yum install -y MariaDB-client
mysql -h127.0.0.1 -P8066 -uroot -p123456 <<EOF
show databases;? ? ?
show tables;
EOF
mysql -h127.0.0.1 -P9066 -uroot -p123456 -e 'show? @@datasource;'
sleep 5
elif [ "${mycat}" == "否" ] || [ "${mycat}" == "N" ] || [ "${mycat}" == "n" ];then
echo 撤退
fi
read -p "是否需要安装db节点的mariadb 扣Y/y:(db1/2节点)" -t 500 db
if [ "${db}" == "是" -o "${db}" == "Y" -o "${db}" == "y" ];then
clear
echo ####################################建议先安装db1,在安装db2####################
echo "#############---mariadb---#############"
echo "# 1.db1 "
echo "# 2.db2"
#echo "# 3.exit"
echo "####################################"
read -p "mariadb: db1 请扣1 ,mariadb:db2请扣2,不需要的话扣3 运行到这里的时候db2开始运行shell: " -t 500 mariadb
case $mariadb in
1)
yum install? mariadb-server.x86_64 mariadb.x86_64 -y
cat >> /etc/my.cnf <<eof
[mysqld]
log_bin = mysql-bin? ? ? ? ? ? ? ? ? ? ? #记录操作日志
binlog_ignore_db = mysql? ? ? ? ? ? ? ? ? #不同步MySQL系统数据库
server_id = 18? ? ? ? ? ? ? ? ? ? ? ? ? #数据库集群中的每个节点id都要不同,一般使用IP地址的最后段的数字,例如172.16.51.18,server_id就写18? ? ? ? 不能写零因为mysqlID是从1开始的
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
eof
systemctl start mariadb && systemctl enable mariadb
sleep 5
echo y n y y y
mysql_secure_installation
sleep 5
$mysql_denglu -e 'grant all privileges? on *.* to root@"%" identified by "123456";'
#mysql -uroot -p123456 <<EOF
#grant all privileges? on *.* to root@"%" identified by "123456";
#grant replication slave on *.* to 'user'@'db2' identified by '123456';
#create database test;
#EOF
$mysql_denglu -e "grant replication slave on *.* to 'user'@'db2' identified by '123456';"
$mysql_denglu -e 'create database test;'
;;
2)
yum install? mariadb-server.x86_64 mariadb.x86_64 -y
cat >> /etc/my.cnf <<eof
[mysqld]
log_bin = mysql-bin? ? ? ? ? ? ? ? ? ? ? #记录操作日志
binlog_ignore_db = mysql? ? ? ? ? ? ? ? ? #不同步MySQL系统数据库
server_id = 28? ? ? ? ? ? ? ? ? ? ? ? ? ? #数据库集群中的每个节点id都要不同,一般使用IP地址的最后段的数字,例如172.16.51.18,server_id就写18? ? ? ? 不能写零因为mysqlID是从1开始的
read-only = 1
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
eof
systemctl start mariadb && systemctl enable mariadb
sleep 5
echo y n y y y
mysql_secure_installation
sleep 5
#mysql -uroot -p123456 <<EOF
#change master to master_host='db1',master_user='user',master_password='123456';
#start slave;
#show slave status\G
#show databases;
#EOF
$mysql_denglu -e "change master to master_host='db1',master_user='user',master_password='123456';"
$mysql_denglu -e "start slave;"
$mysql_denglu -e "show slave status\G"
$mysql_denglu -e 'show databases;'
sleep 5
db1_io='$mysql_denglu -e "show slave status\G" | grep "Slave_IO_Running" | cut -d ":" -f2'
db1_sql='$mysql_denglu -e "show slave status\G" | grep "Slave_SQL_Running" | cut -d ":" -f2'
if [ "$db1_io" -eq 0 -o "$db1_sql" -eq 0 ];then
echo mariadb主从读写配置成功!!!
else
$mysql_denglu -e "restart slave;"
$mysql_denglu -e "show slave status\G"
sleep 8
fi
;;
#3)
# echo 跳过安装db1 / 2 节点
#;;
esac
elif [ "${db}" == "否" ] || [ "${db}" == "N" ] || [ "${db}" == "n" ];then
echo 跳过
fi
sleep 5
echo ###############################db2节点的数据库安装完成后在db1上执行####################################
read -p "是否导入gpmall.sql y/n否 在db1节点,db2执行完数据库部分后运行)" -t 500 source_sql
if [ "${source_sql}" == "是" -o "${source_sql}" == "Y" -o "${source_sql}" == "y" ];then
$mysql_denglu -e 'create database gpmall;'
$mysql_denglu <<EOF
use? gpmall;
source $gpmallsql?
show tables;
EOF
elif [ "${source_sql}" == "否" ] || [ "${source_sql}" == "N" ] || [ "${source_sql}" == "n" ];then
echo chetui
fi
echo #####################################zk安装##################################################
sleep 5
tar -zxvf $zk -C /opt/
mv $zk_flie/conf/zoo_sample.cfg $zk_flie/conf/zoo.cfg
cat >> $zk_flie/conf/zoo.cfg <<eof
server.1=$ip_host1:2888:3888
server.2=$ip_host2:2888:3888
server.3=$ip_host3:2888:3888
eof
mkdir /tmp/zookeeper
clear
echo "#############---menu---#############"
echo "# 1.修改mycat主机序号/tmp/zookeeper "
echo "# 2.修改db1主机序号/tmp/zookeeper"
echo "# 3.修改db2主机序号/tmp/zookeeper "
echo "####################################"
read -p "修改序号请扣以上序号:" -t 500 zk_zone
case $zk_zone in
1)
cat? > /tmp/zookeeper/myid <<eof
1
eof
$zk_flie/bin/zkServer.sh start
sleep 5
$zk_flie/bin/zkServer.sh status
;;
2)
cat? > /tmp/zookeeper/myid <<eof
2
eof
$zk_flie/bin/zkServer.sh start
sleep 5
$zk_flie/bin/zkServer.sh status
;;
3)
cat? > /tmp/zookeeper/myid <<eof
3
eof
$zk_flie/bin/zkServer.sh start
sleep 5
$zk_flie/bin/zkServer.sh status
;;
esac
sleep 8
netstat -tunlp |grep 2181
jps
sleep 8
netstat -tunlp |grep 2181
echo #################################kafka安装##############################
sleep 5
tar -zxvf $kafka -C /opt/
clear
echo "#############---menu---#############"
echo "# 1.mycat节点kafka配置安装"
echo "# 2.db1节点kafka配置安装"
echo "# 3.db2节点kafka配置安装"
echo "####################################"
read -p "修改序号请扣以上序号:" -t 500 kafka_zone
case $kafka_zone in
1)
sed? -i '21d' $kafka_file/config/server.properties
sed -i '122d' $kafka_file/config/server.properties
cat? >> $kafka_file/config/server.properties <<eof
broker.id=1
zookeeper.connect=$benji_ip:2181,$ip_host2:2181,$ip_host3:2181
listeners = PLAINTEXT://$benji_ip:9092
eof
$kafka_file/bin/kafka-server-start.sh -daemon $kafka_file/config/server.properties
sleep 8
netstat -tunlp |grep 9092
jps
netstat -tunlp |grep 9092
;;
2)
sed? -i '21d' $kafka_file/config/server.properties
sed -i '122d' $kafka_file/config/server.properties
cat? >> $kafka_file/config/server.properties <<eof
broker.id=2
zookeeper.connect=$ip_host1:2181,$benji_ip:2181,$ip_host3:2181
listeners = PLAINTEXT://$benji_ip:9092
eof
$kafka_file/bin/kafka-server-start.sh -daemon $kafka_file/config/server.properties
sleep 8
jps
netstat -tunlp |grep 9092
;;
3)
sed? -i '21d' $kafka_file/config/server.properties
sed -i '122d' $kafka_file/config/server.properties
cat? >> $kafka_file/config/server.properties <<eof
broker.id=3
zookeeper.connect=$ip_host1:2181,$ip_host2:2181,$benji_ip:2181
listeners = PLAINTEXT://$benji_ip:9092
eof
$kafka_file/bin/kafka-server-start.sh -daemon $kafka_file/config/server.properties
sleep 8
jps
netstat -tunlp |grep 9092
sleep 8
;;
esac
echo #########################################kafka测试####################################
clear
echo "#############---menu---#############"
echo "# 1.mycat创建kafka测试文件"
echo "# 2.在db1查看kafka测试文件"
echo "# 3.在db2查看kafka测试文件"
echo "####################################"
read -p "测试kafka请在mycat上进行创建:" -t 500 kafka_test
case $kafka_test in
1)
$kafka_file/bin/kafka-topics.sh --create --zookeeper $benji_ip:2181 --replication-factor 1 --partitions 1 --topic test
;;
2)
#$kafka_file/bin/kafka-topics.sh --create --zookeeper $benji_ip:2181 --replication-factor 1 --partitions 1 --topic test
$kafka_file/bin/kafka-topics.sh --list --zookeeper $ip_host3:2181
sleep 5
;;
3)
#$kafka_file/bin/kafka-topics.sh --create --zookeeper $benji_ip:2181 --replication-factor 1 --partitions 1 --topic test
$kafka_file/bin/kafka-topics.sh --list --zookeeper $ip_host2:2181
sleep 5
;;
esac
sleep 8
jps
echo 安装es站内搜索
read -p "是否下载es是/n否)" -t 500 es
if [ "${es}" == "是" -o "${es}" == "Y" -o "${es}" == "y" ];then
yum install elasticsearch.noarch -y
sed -i '$a http.cors.enabled: true' /etc/elasticsearch/elasticsearch.yml
sed -i '$a http.cors.allow-origin: "*"' /etc/elasticsearch/elasticsearch.yml
sed -i '$a http.cors.allow-credentials: true' /etc/elasticsearch/elasticsearch.yml
sed -i '$a cluster.name: my-application' /etc/elasticsearch/elasticsearch.yml
sed -i '$a node.name: node-1' /etc/elasticsearch/elasticsearch.yml
sed -i '$a network.host: '$benji_ip'' /etc/elasticsearch/elasticsearch.yml
sed -i '$a http.port: 9200' /etc/elasticsearch/elasticsearch.yml
systemctl start elasticsearch.service && systemctl enable elasticsearch.service && systemctl status elasticsearch.service
elif [ "${es}" == "否" ] || [ "${es}" == "N" ] || [ "${es}" == "n" ];then
echo chetui
fi
sleep 3
#配置redis文件
read -p "是否下载redis是/n否)" -t 500 redis
if [ "${redis}" == "是" -o "${redis}" == "Y" -o "${redis}" == "y" ];then
? yum install redis -y
? sed? -i '61d' /etc/redis.conf
? sed -i '79c protected-mode no' /etc/redis.conf
? systemctl start redis && systemctl enable redis
? netstat -tunlp | grep 6379
elif [ "${redis}" == "否" ] || [ "${redis}" == "N" ] || [ "${redis}" == "n" ];then
echo
fi
sleep 3
cat >> /etc/hosts <<eof
$ip_host1 redis.mall
$ip_host1 mysql.mall
$ip_host1 kafka1.mall
$ip_host2 kafka1.mall
$ip_host3 kafka1.mall
$ip_host1 zk1.mall
$ip_host2 zk1.mall
$ip_host3 zk1.mall
eof
read -p "是否下载nginxy是/n否" -t 500 install
if [ "${install}" == "是" -o "${install}" == "Y" -o "${install}" == "y" ];then
? yum install nginx? -y
? ####配置nginx文件
? rm -rf /usr/share/nginx/html/*
cat > /etc/nginx/conf.d/default.conf << eof
upstream myuser {
? ? server $ip_host2:8082;
? ? ? ? ? ? server $ip_host3:8082;
? ? ip_hash;
}
? ? ? ? upstream myshopping {
? ? ? ? ? ? server $ip_host2:8081;
? ? ? ? ? ? server $ip_host3:8081;
? ? ? ? ? ? ip_hash;
? ? ? ? }
? ? ? ? upstream mycashier {
? ? ? ? ? ? server $ip_host2:8083;
? ? ? ? ? ? server $ip_host3:8083;
? ? ? ? ? ? ip_hash;
? ? ? ? }
server {
? ? listen? ? ? 80;
? ? server_name? localhost;
? ? #charset koi8-r;
? ? #access_log? /var/log/nginx/host.access.log? main;
? ? location / {
? ? ? ? root? /usr/share/nginx/html;
? ? ? ? index? index.html index.htm;
? ? }
? ? location /user {
? ? proxy_pass http://myuser;
? ? ? ? }
? ? location /shopping {
? ? proxy_pass http://myshopping;
? ? ? ? }
? ? location /cashier {
? ? proxy_pass http://mycashier;
? ? ? ? }
? ? #error_page? 404? ? ? ? ? ? ? /404.html;
? ? # redirect server error pages to the static page /50x.html
? ? #
? ? error_page? 500 502 503 504? /50x.html;
? ? location = /50x.html {
? ? ? ? root? /usr/share/nginx/html;
? ? }
? ? # proxy the PHP scripts to Apache listening on 127.0.0.1:80
? ? #
? ? #location ~ \.php$ {
? ? #? ? proxy_pass? http://127.0.0.1;
? ? #}
? ? # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
? ? #
? ? #location ~ \.php$ {
? ? #? ? root? ? ? ? ? html;
? ? #? ? fastcgi_pass? 127.0.0.1:9000;
? ? #? ? fastcgi_index? index.php;
? ? #? ? fastcgi_param? SCRIPT_FILENAME? /scripts$fastcgi_script_name;
? ? #? ? include? ? ? ? fastcgi_params;
? ? #}
? ? # deny access to .htaccess files, if Apache's document root
? ? # concurs with nginx's one
? ? #
? ? #location ~ /\.ht {
? ? #? ? deny? all;
? ? #}
}
eof
systemctl start nginx &&? systemctl enable nginx
netstat -tunlp |grep 80
ps -ef |grep nginx
netstat -tunlp && sleep 5
systemctl restart nginx
? ? ? ? ? ? ? ? #检查服务是否成功安装
elif [ "${install}" == "否" ] || [ "${install}" == "N" ] || [ "${install}" == "n" ];then
? echo
fi
echo? '自行输入 把cd /usr/share/nginx/html/ && cp -rvf $nginx_wed* .? ? systemctl restart nginx'
echo '##########nohup java -jar shopping_provider &'
echo '##########nohup java -jar user_provider &'
echo '##########nohup java -jar gpmall_shopping &'
echo '##########nohup java -jar gpmall_user &'
echo '# 自行jar包路径!!!'