shell脚本部署redis,mysql,elasticsearch
这里用到的包为elasticsearch-6.8.0.tar.gz,elasticsearch-analysis-ik-6.8.0.zip ,elasticsearch-sql-6.8.0.0.zip,redis-5.0.4.zip,libaio-0.3.109-13.el7.x86_64.rpm,mysql-5.7.18-1.el7.x86_64.rpm-bundle.tar,net-tools-2.0-0.24.20131004git.el7.x86_64.rpm
#!/bin/bash
currentDIR=$(cd `dirname $0`; pwd)
installDIR=/usr/local
#REDIS
function REDIS(){
#这行命令可能会有问题
rm -rf ${installDIR}/redis
echo "正在部署REDIS"
mkdir -p ${installDIR}/redis/{logs,nodes,conf,bin}
cd ${currentDIR}
unzip redis*.zip >/dev/null 2>&1
cd redis*/src && make >/dev/null 2>&1
if [[ $? -ne 0 ]]; then
echo "编译出错"
else
cp redis-cli redis-server ${installDIR}/redis/bin
echo "步骤执行完成百分之二十......"
cp redis-trib.rb ${installDIR}/redis
echo "步骤执行完成百分之四十......"
cp ../redis.conf ${installDIR}/redis/conf
echo "步骤执行完成百分之六十......"
cd ${installDIR}/redis/conf
#后台
#sed -i 's/daemonize no/daemonize yes/' redis.conf
echo "步骤执行完成百分之八十......"
#日志
sed -i 's/logfile ""/logfile "\/usr\/local\/redis\/logs\/redis.logs"' redis.conf
echo "步骤执行完成百分之九十......"
#启动
cd ../
echo "./bin/redis-server conf/redis.conf" > start.sh
chmod +x start.sh
echo "步骤执行完成百分之一百......"
fi
fi
}
#MYSQL
function MYSQL(){
echo "正在部署MYSQL"
olddb=`rpm -qa| grep mysql`
rpm -e ${olddb} --nodeps
olddb1=`rpm -qa|grep mariadb`
rpm -e ${olddb1} --nodeps
rm -rf /var/lib/mysql/
rm -rf /usr/lib64/mysql/
rm -rf /var/log/mysqld.log
cd ${currentDIR}/MySQL
#tar -xvf mysql-5.7.18-1.el7.x86_64.rpm-bundle.tar
#如果事先没有安装下面的两个依赖,则放开下面两行的注释
#rpm -ivh libaio-0.3.109-13.el7.x86_64.rpm
#rpm -ivh net-tools-2.0-0.24.20131004git.el7.x86_64.rpm
rpm -ivh mysql-community-common-5.7.18-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.18-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.18-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.18-1.el7.x86_64.rpm
#初始化mysql
mysqld --initialize
chown mysql:mysql /var/lib/mysql -R
#启动服务
cd /var/lib/mysql
systemctl start mysqld.service
#获取初始化密码
password1=`cat /var/log/mysqld.log | grep password | awk -F ' ' '{print$NF}'`
echo ${password1}
password2="asd@123456"
mysql --protocol=TCP -u root -p''${password1}'' --connect-expired-password -e "flush PRIVILEGES;" >/dev/null 2>&1
mysql --protocol=TCP -u root -p''${password1}'' --connect-expired-password -e "alter user 'root'@'localhost' identified by '${password2}';" >/dev/null 2>&1
mysql --protocol=TCP -u root -p''${password2}'' --connect-expired-password -e "grant all privileges on *.* to root@'%' identified by '${password2}' with grant option; flush privileges;exit;" >/dev/null 2>&1
echo "新的密码:${password2}"
echo "部署完成"
}
#ES
function ELASTICSEARCH(){
adduser es
password 123456
rm -rf /software/elasticsearch-6.8.0
echo "正在部署ElasticSearch"
cd ${currentDIR}
tar -zxvf elasticsearch-6.8.0.tar.gz
chown -R es:es elasticsearch-6.8.0
#建议在第一次执行脚本的时候放开下面的注释,用来解决es启动过程中的无法创建本地文件问题
#echo "* soft nofile 65536">>/etc/security/limits.conf
#echo "* hard nofile 131072">>/etc/security/limits.conf
#echo "* soft nproc 2048">>/etc/security/limits.conf
#echo "* hard nproc 4096">>/etc/security/limits.conf
#用来解决最大虚拟内存太小问题
#echo "vm.max_map_count=655360">>/etc/sysctl.conf
#sysctl -p
HOST_IP=$(ifconfig|grep "inet"|awk '{print $2}'|awk 'NR==1{print}')
echo "network.host: ${HOST_IP}">>${currentDIR}/elasticsearch-6.8.0/config/elasticsearch.yml
echo "network.bind_host: ${HOST_IP}">>${currentDIR}/elasticsearch-6.8.0/config/elasticsearch.yml
echo "network.publish_host: ${HOST_IP}">>${currentDIR}/elasticsearch-6.8.0/config/elasticsearch.yml
echo "http.port: 9200">>${currentDIR}/elasticsearch-6.8.0/config/elasticsearch.yml
echo "transport.tcp.port: 9300">>${currentDIR}/elasticsearch-6.8.0/config/elasticsearch.yml
echo "http.cors.enabled: true">>${currentDIR}/elasticsearch-6.8.0/config/elasticsearch.yml
echo "http.cors.allow-origin: \"*\"">>${currentDIR}/elasticsearch-6.8.0/config/elasticsearch.yml
echo "bootstrap.memory_lock: false">>${currentDIR}/elasticsearch-6.8.0/config/elasticsearch.yml
echo "bootstrap.system_call_filter: false">>${currentDIR}/elasticsearch-6.8.0/config/elasticsearch.yml
echo "正在安装ik分词器"
mkdir ${currentDIR}/elasticsearch-6.8.0/plugins/ik
cp elasticsearch-analysis-ik-6.8.0.zip elasticsearch-6.8.0/plugins/ik/
cd ${currentDIR}/elasticsearch-6.8.0/plugins/ik
unzip elasticsearch-analysis-ik-6.8.0.zip
rm -f elasticsearch-analysis-ik-6.8.0.zip
echo "正在安装es_sql"
cd ${currentDIR}
mkdir ${currentDIR}/elasticsearch-6.8.0/plugins/sql
cp elasticsearch-sql-6.8.0.0.zip elasticsearch-6.8.0/plugins/sql/
cd elasticsearch-6.8.0/plugins/sql/
unzip elasticsearch-sql-6.8.0.0.zip
rm -f elasticsearch-sql-6.8.0.0.zip
chown -R es:es /software/elasticsearch-6.8.0/
#su -l elk -c
su es<<!
cd /software/elasticsearch-6.8.0/bin
./elasticsearch -d
!
echo "ElasticSearch成功启动"
}
function main(){
process1=`ps -ef | grep redis| grep -v grep`
if [ "${process1}" == "" ]; then
echo "Redis不存在,开始安装";
REDIS
else
echo "Redis进程已经存在";
break;
fi
process2=`ps -ef| grep mysqld | grep -v grep`
if [ "${process2}" == "" ]; then
echo "MySQL不存在,开始安装";
MYSQL
else
echo "MySQL进程已经存在";
break;
fi
#REDIS
#MYSQL
process3=`ps -ef| grep elasticsearch| grep -v grep`
if [ "${process3}" == "" ]; then
echo "ElasticSearch不存在,开始安装";
ELASTICSEARCH
else
echo "ElasticSearch进程已经存在";
break;
fi
}
main