日常运维中的一键安装或启停脚本
在日常运维中,我们通常会利用shell的"function函数+if逻辑判断+case选择语句"来实现中间件服务一键部署或一键启停。下面分享几个常用运维的一键脚本:
一、Tomcat一键启停服务脚本 [如有多个tomcat实例,就在/etc/init.d/下配置多个针对每个tomcat端口的启停脚本。下面为tomcat_8080脚本文件]
#!/bin/bash
#chkconfig: 345 99 10
#description: Auto-starts tomcat
# /etc/init.d/tomcatd
# Tomcat auto-start
# Source function library.
source /etc/init.d/functions
# source networking configuration.
#. /etc/sysconfig/network
RETVAL=0
project_tomcat=tomcat_8080
#这里的tomcat采用结尾以端口号方式命令。即tomcat_port
#如果本机由多个tomcat示例,则这种结尾以端口号命名的方式比较好,
#执行"echo $JAVA_HOME"可以查看到本机JAVA主目录路径
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.232.b09-0.el7_7.x86_64/
export CATALINA_HOME=/usr/local/${project_tomcat}
#export CATALINA_BASE=/usr/local/${project_tomcat}
#(centos7系统)重新加载服务配置文件。只有当该脚本被修改的时候需要执行该项,可以不配置。
systemctl daemon-reload
#TOMCATUSER=tomcat
start()
{
if [ -f ${CATALINA_HOME}/bin/startup.sh ];
then
echo $"Starting Tomcat"
#这里tomcat使用root启动的,如果不是root用户启动的,这里就修改下用户。
if [ root = `whoami` ];
then
${CATALINA_HOME}/bin/startup.sh
else
su - root -c "${CATALINA_HOME}/bin/startup.sh"
fi
RETVAL=$?
echo " OK"
return ${RETVAL}
fi
}
stop()
{
if [ -f ${CATALINA_HOME}/bin/shutdown.sh ];
then
echo $"Stopping Tomcat"
${CATALINA_HOME}/bin/shutdown.sh
RETVAL=$?
sleep 1
#ps -ef|grep $project_tomcat|egrep -v 'grep|init'|awk '{print$2}'|xargs kill -9
#egrep -v "grep|init" 的过滤效果等同于 grep -v "grep\|init"
ps -ef|grep ${project_tomcat}|egrep -v 'grep|init' | grep ${CATALINA_HOME}|awk '{print $2}'|xargs kill -9
#下面可根据自己tomcat日志的实际路径进行更改
mv ${CATALINA_HOME}/logs/catalina.out ${CATALINA_HOME}/logs/catalina.out_`date '+%Y%m%d_%H_%M_%S'`
find ${CATALINA_HOME}/logs/ -mtime +10 -exec rm -rf {} \;
echo " OK"
# [ $RETVAL -eq 0 ] && rm -f /var/lock/...
return ${RETVAL}
fi
}
status()
{
#这里tomcat实例名结尾是以端口号命名的。如果实例名中没有显示端口号,则取tomcat端口号如下:
#port=$(cat ${CATALINA_HOME}/conf/server.xml |grep -w 'URIEncoding="UTF-8"'|awk '{print $2}'|cut -d"=" -f2|cut -d"\"" -f2)
port=$(echo ${project_tomcat}|awk -F _ '{print$2}')
#打印"lsof -i:port"结果中第二行的第二列,即是该端口所属服务的pid
pid=$(`which lsof` -i:$port|awk 'NR==2{print$2}')
if [ -z $pid ] #判断${pid}字符串是否为空串,空串为真。
then
#使用下面echo方式会将打印内容标红!
echo -e "\033[40;31m${project_tomcat} is stopped\033[0m"
else
echo ${project_tomcat} is running
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
echo $"Restaring Tomcat"
stop
sleep 1
start
;;
status)
status
;;
*)
echo $"Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac
exit ${RETVAL}
执行脚本:
[root@localhost ~]# /etc/init.d/tomcat_8080
Usage: /etc/init.d/tomcat_8080 {start|stop|restart|status}
[root@localhost ~]# chmod 755 /etc/init.d/tomcat_8080
[root@localhost ~]# /etc/init.d/tomcat_8080 restart
Restarting tomcat_8080 (via systemctl): [ OK ]
[root@localhost ~]# /etc/init.d/tomcat_8080 status
tomcat_8080 is running
[root@localhost ~]# /etc/init.d/tomcat_8080 stop
Stopping tomcat_8080 (via systemctl): [ OK ]
[root@localhost ~]# /etc/init.d/tomcat_8080 status
tomcat_8080 is stopped
[root@localhost ~]# /etc/init.d/tomcat_8080 start
Starting tomcat_8080 (via systemctl): [ OK ]
[root@localhost ~]# /etc/init.d/tomcat_8080 status
tomcat_8080 is running
二、Zookeeper集群环境一键部署脚本 [生产环境中推荐使用该脚本]
[root@bobo zookeeper]# cat install_zookeeper.sh
#!/bin/bash
source /etc/profile
java -version
if [ "$?" -ne 0 ]; then
echo "JDK未安装,请先安装JDK"
exit 1
fi
while getopts "a:b:n:l:c:f:m:h" opts
do
case $opts in
a)
#APP_NAME:项目编码
APP_NAME=$OPTARG
;;
b)
#MODULE_NAME:模块名称
MODULE_NAME=$OPTARG
;;
n)
#ZK_SRVNUM:ZOOKEEPER数量
ZK_SRVNUM=$OPTARG
;;
l)
#ZK_IPLIST:ZOOKEEPER服务器IP地址列表
ZK_IPLIST=$OPTARG
;;
c)
#ZKCLIENT_PORT:客户端访问 zookeeper 的端口号
ZKCLIENT_PORT=$OPTARG
;;
f)
#ZKLEADER_PORT:ZOOKEEPER的F和L通信端口号
ZKLEADER_PORT=$OPTARG
;;
m)
#ZKCOM_PORT:ZOOKEEPER选举端口号
ZKCOM_PORT=$OPTARG
;;
h)
echo -e "OPTIONS:\n-a:项目编码(必选)\n-b:模块名称(可选,默认为空)\n-n:ZooKeeper服务器数量(可选,默认为3)"
echo -e "-l:ZooKeeper服务器IP地址列表(必选,IP地址以英文逗号分隔)"
echo -e "-c:Client-Port(可选,默认为2181,多个端口以英文逗号分隔,且与IP地址一一对应)"
echo -e "-f:ZooKeeper的F和L通信端口号(可选,默认为2888,多个端口以英文逗号分隔,且与IP地址一一对应)"
echo -e "-m:ZooKeeper选举端口号(可选,默认为3888,多个端口以英文逗号分隔,且与IP地址一一对应)"
exit 1
;;
?)
echo "missing options,pls check!"
exit 1
;;
esac
done
#可选参数赋值
ZK_SRVNUM=${ZK_SRVNUM:-3}
ZKCLIENT_PORT=${ZKCLIENT_PORT:-2181}
ZKLEADER_PORT=${ZKLEADER_PORT:-2888}
ZKCOM_PORT=${ZKCOM_PORT:-3888}
#定义公共变量
#zookeep安装包存放位置
ZKSAVDIR="/usr/local/src/zookeeper"
#zookeeper安装包名(不带扩展名)
ZKNAME="zookeeper-3.4.8"
#必选参数存在性及参数合法性判断
#if [ -z ${APP_NAME} ]||[ -z ${MODULE_NAME} ]||[ -z ${ZK_IPLIST} ];then
if [ -z ${APP_NAME} ]||[ -z ${ZK_IPLIST} ];then
echo "Missing options,exit"
exit 1
elif [ ${ZK_SRVNUM} -ne 1 ]&&[ ${ZK_SRVNUM} -ne 3 ]&&[ ${ZK_SRVNUM} -ne 5 ];then
echo "Wrong server num,exit"
exit 1
fi
IPLIST_NUM=`echo ${ZK_IPLIST}|awk -F"," '{print NF}'`
if [ ${ZK_SRVNUM} -ne ${IPLIST_NUM} ];then
echo "IP list and server num do not match,exit"
exit 1
fi
APP_NAME=`echo ${APP_NAME} | tr '[A-Z]' '[a-z]'`
#多个端口时判断端口数与IP地址数量是否一致
CPORT_NUM=`echo ${ZKCLIENT_PORT}|awk -F"," '{print NF}'`
LPORT_NUM=`echo ${ZKLEADER_PORT}|awk -F"," '{print NF}'`
EPORT_NUM=`echo ${ZKCOM_PORT}|awk -F"," '{print NF}'`
if [ ${CPORT_NUM} -gt 1 ];then
if [ ${IPLIST_NUM} -ne ${CPORT_NUM} ]||[ ${IPLIST_NUM} -ne ${LPORT_NUM} ]||[ ${IPLIST_NUM} -ne ${EPORT_NUM} ];then
echo "IP list and Port list number do not match,exit"
exit 1
fi
#获取IP地址和端口对应关系
rm -f /home/workapp/zkinfo.cfg
for ((i=1;i<=${ZK_SRVNUM};i++)); do
eval IP_$i='`echo ${ZK_IPLIST}|awk -F, "{ print $"$i" }"`'
eval PORT_$i='`echo ${ZKCLIENT_PORT}|awk -F, "{ print $"$i" }"`'
eval LPORT_$i='`echo ${ZKLEADER_PORT}|awk -F, "{ print $"$i" }"`'
eval EPORT_$i='`echo ${ZKCOM_PORT}|awk -F, "{ print $"$i" }"`'
# eval echo "server.${i}=\$IP_$i:${ZKLEADER_PORT}:${ZKCOM_PORT}">>${ZKHOME}/conf/zoo.cfg
# eval IPTMP=\$IP_$i
eval PORTTMP=\$PORT_$i
#zookeeper HOME路径
[ -z ${MODULE_NAME} ]&&eval ZKHOME="/opt/${APP_NAME}/zookeeper_\$PORT_$i"||eval ZKHOME="/opt/${APP_NAME}/zookeeper_${MODULE_NAME}_\$PORT_$i"
#zookeeper日志存储路径
[ -z ${MODULE_NAME} ]&&eval DATA_LOGDIR="/var/log/${APP_NAME}/zookeeper_\$PORT_$i"||eval DATA_LOGDIR="/var/log/${APP_NAME}/zookeeper_${MODULE_NAME}_\$PORT_$i"
#zookeeper数据存储路径
DATA_DIR="${ZKHOME}/data"
#生成参数列表
eval echo "$i,\$IP_$i,\$PORT_$i,\$LPORT_$i,\$EPORT_$i,${ZKHOME},${DATA_LOGDIR},${DATA_DIR}">>/home/workapp/zkinfo.cfg
done
cat /home/workapp/zkinfo.cfg
else
#zookeeper HOME路径
[ -z ${MODULE_NAME} ]&&ZKHOME="/opt/${APP_NAME}/zookeeper"||ZKHOME="/opt/${APP_NAME}/zookeeper_${MODULE_NAME}"
echo "ZKHOME is ${ZKHOME}"
#zookeeper日志存储路径
[ -z ${MODULE_NAME} ]&&DATA_LOGDIR="/var/log/${APP_NAME}/zookeeper"||DATA_LOGDIR="/var/log/${APP_NAME}/zookeeper_${MODULE_NAME}"
echo "ZK log dir is ${DATA_LOGDIR}"
#zookeeper数据存储路径
DATA_DIR="${ZKHOME}/data"
echo "ZK data dir is ${DATA_DIR}"
fi
#安装日志
INSTALL_LOG="/home/workapp/zookeeperinstall.log"
#打印变量值
echo "APP_NAME is ${APP_NAME}"|tee -a ${INSTALL_LOG}
echo "MODULE_NAME is ${MODULE_NAME}"|tee -a ${INSTALL_LOG}
echo "ZK_Server_num is ${ZK_SRVNUM}"|tee -a ${INSTALL_LOG}
echo "ZK_Server IP is ${ZK_IPLIST}"|tee -a ${INSTALL_LOG}
echo "ZK_Client Port is ${ZKCLIENT_PORT}"|tee -a ${INSTALL_LOG}
echo "ZK_Leader Port is $ZKLEADER_PORT"|tee -a ${INSTALL_LOG}
echo "ZK_COM Port is ${ZKCOM_PORT}"|tee -a ${INSTALL_LOG}
#获取本机IP地址
HOST_IP=`ip a|grep global|awk '{print $2}'|awk -F"/" '{print $1}'`
echo "Local IP is ${HOST_IP}"|tee -a ${INSTALL_LOG}
#安装包MD5校验
md5Now=`md5sum ${ZKSAVDIR}/${ZKNAME}.tar.gz|awk '{print $1}'`
md5Save=`cat ${ZKSAVDIR}/${ZKNAME}.tar.gz.md5`
if [ "${md5Now}" != "${md5Save}" ];then
echo "MD5 check Failed!"|tee -a ${INSTALL_LOG}
echo "the md5 now is ${md5Now}"|tee -a ${INSTALL_LOG}
echo "the md5 saved is ${md5Save}"|tee -a ${INSTALL_LOG}
exit 1
else
echo "MD5 check success!"|tee -a ${INSTALL_LOG}
fi
#安装zookeeper
function Install_zk {
echo "=================`date '+%Y%m%d %H:%M:%S'`Start Install ZooKeeper....==============="|tee -a ${INSTALL_LOG}
#解压缩安装包至项目编码安装路径
if [ ! -e /opt/${APP_NAME}/ ]; then
mkdir -p /opt/${APP_NAME}
fi
tar -xzf ${ZKSAVDIR}/${ZKNAME}.tar.gz -C /opt/${APP_NAME}/
mv /opt/${APP_NAME}/${ZKNAME} ${ZKHOME}
mkdir -p ${DATA_DIR}
mkdir -p ${DATA_LOGDIR}
cp ${ZKHOME}/conf/zoo_sample.cfg ${ZKHOME}/conf/zoo.cfg
#客户化zoo.cfg配置
sed -i "s/clientPort=2181/clientPort=${ZKCLIENT_PORT}/g" ${ZKHOME}/conf/zoo.cfg
sed -i "s#dataDir=/tmp/zookeeper#dataDir=${DATA_DIR}#g" ${ZKHOME}/conf/zoo.cfg
sed -i "/dataLogDir/s/^/#/" ${ZKHOME}/conf/zoo.cfg
echo "dataLogDir=${DATA_LOGDIR}" >>${ZKHOME}/conf/zoo.cfg
#修改zookeeper-env.sh,指定运行日志zookeeper.log路径
sed -i "s#/var/log/zookeeper#${DATA_LOGDIR}#g" ${ZKHOME}/conf/zookeeper-env.sh
#修改java.env,设置jvm参数,指定gc日志路径
sed -i "s#/var/log/zookeeper#${DATA_LOGDIR}#g" ${ZKHOME}/conf/java.env
#服务器数量为3个或5个为集群模式
if [ ${ZK_SRVNUM} -eq 3 ]||[ ${ZK_SRVNUM} -eq 5 ];then
#根据端口数量判断安装方式
if [ ${CPORT_NUM} -eq 1 ];then
#拆分IP地址列表,获取本机ZK_ID
for ((i=1;i<=${ZK_SRVNUM};i++));do
eval IP_$i='`echo ${ZK_IPLIST}|awk -F, "{ print $"$i" }"`'
# eval echo \$IP_$i
eval IPTMP=\$IP_$i
eval echo "server.${i}=\$IP_$i:${ZKLEADER_PORT}:${ZKCOM_PORT}">>${ZKHOME}/conf/zoo.cfg
if [ "$HOST_IP" == "$IPTMP" ];then
#当列表中的IP地址等于本机地址时,获取当前i值作为ID
ZK_ID=${i}
else
continue
fi
done
else
ZK_ID=${NUM}
while read ZK_INFO;do
echo ${ZK_INFO}|awk -F, '{print "server."$1"="$2":"$4":"$5}'>>${ZKHOME}/conf/zoo.cfg
done</home/workapp/zkinfo.cfg
fi
#客户化myid
echo "${ZK_ID}" >${DATA_DIR}/myid
echo "zookeeper ID is ${ZK_ID}"|tee -a ${INSTALL_LOG}
fi
chown -R workapp:workapp ${ZKHOME}
chown -R workapp:workapp ${DATA_LOGDIR}
cat ${ZKHOME}/conf/zoo.cfg
}
function Check_install {
retval=$?
if [ $retval -eq 0 ];then
echo "`date '+%Y%m%d %H:%M:%S'` zookeeper install SUCCESS!|${APP_NAME} ${MODULE_NAME} ${HOST_IP} ${ZKCLIENT_PORT} ${ZK_ID}|0"|tee -a ${INSTALL_LOG}
else
echo "`date '+%Y%m%d %H:%M:%S'` zookeeper install FAILED!|${APP_NAME} ${MODULE_NAME} ${HOST_IP} ${ZKCLIENT_PORT} ${ZK_ID}|1"|tee -a ${INSTALL_LOG}
fi
}
function Start_check {
su - workapp -c "sh ${ZKHOME}/bin/zkServer.sh start"
sleep 10
su - workapp -c "sh ${ZKHOME}/bin/zkServer.sh status"
netstat -anp|grep ${ZKCLIENT_PORT}
}
#根据端口数量判断安装方式,1个端口为standalone或集群模式,正常安装;
if [ ${CPORT_NUM} -eq 1 ];then
Install_zk
Check_install
Start_check
else
#多个端口为伪集群模式,读取zkinfo.cfg文件
while read ZK_INFO;do
NUM=`echo ${ZK_INFO}|awk -F, '{print $1}'`
IP=`echo ${ZK_INFO}|awk -F, '{print $2}'`
ZKCLIENT_PORT=`echo ${ZK_INFO}|awk -F, '{print $3}'`
ZKHOME=`echo ${ZK_INFO}|awk -F, '{print $6}'`
DATA_LOGDIR=`echo ${ZK_INFO}|awk -F, '{print $7}'`
DATA_DIR=`echo ${ZK_INFO}|awk -F, '{print $8}'`
if [ "$IP" == "$HOST_IP" ];then
Install_zk
Check_install
Start_check
else
continue
fi
done</home/workapp/zkinfo.cfg
fi
rm -f /home/workapp/zkinfo.cfg
查看脚本帮助信息
#!/bin/bash
# Date: 2018-09-08
# Mail: kevin@163.com
# Founder: kevin
# Describe: This is a one - button installation service script
function CDAN(){
cat << One_button_install
+------------------------------------------------+
| |
| _o0o_ 1. 安装Nginx |
| 08880 2. 安装Apache |
| 88"."88 3. 安装MySQL |
| (|-_-|) 4. 安装PHP |
| 0\=/0 5. 部署LNMP环境 |
| __/ \__ 6. 安装zabbix监控 |
| ‘\ ///‘ 7. 退出此管理程序 |
| / Linux一键 \ 8. 关闭计算机 |
| || Server || ================================|
| \ //// 一键安装服务 |
| ||| i i i ||| by Kevin |
| ___ ___ ================================|
|___‘. /--.--\ .‘___ |
+------------------------------------------------+
One_button_install
}
CDAN
source /etc/init.d/functions
BASE_DIR=/usr/local/src
read -p "请您输入1-8任意字符:" NUM
expr $NUM + 1 >/dev/null 2>&1
if [ "$?" -ne 0 ]
then
echo "请您输入{1|2|3|4|5|6|7|8}"
exit 1
fi
function Nginx_server (){
echo "开始安装Nginx,请喝杯咖啡,耐心等待一段时间...."
yum install -y gcc gcc-c++ pcre-devel zlib-devel openssl-devel >/dev/null 2>&1
cd $BASE_DIR
wget http://nginx.org/download/nginx-1.12.2.tar.gz >/dev/null 2>&1
useradd -M -s /sbin/nologin nginx
tar -zvxf nginx-1.12.2.tar.gz >/dev/null 2>&1
cd $BASE_DIR/nginx-1.12.2/
./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre --with-http_ssl_module --with-http_gzip_static_module --user=nginx --group=nginx >/dev/null 2>&1
if [ "$?" -eq 0 ];then
make && make install >/dev/null 2>&1
fi
if [ -f /usr/local/nginx/sbin/nginx ];then
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
/usr/local/nginx/sbin/nginx
echo "恭喜您! Nginx安装成功!"
fi
}
function Apache_server (){
echo "开始安装Apache,请喝杯咖啡,耐心等待一段时间...."
yum install httpd -y >/dev/null 2>&1
if [ "$?" -eq 0 ]
then
echo "恭喜您! Apache安装成功!"
else
echo "很抱歉, Apache安装失败! 请仔细检查安装脚本,可能有点问题."
fi
}
function Mysql_server (){
echo "开始安装Mysql,请喝杯咖啡,耐心等待一段时间...."
rpm -e $(/bin/rpm -qa | grep mysql|xargs) --nodeps >/dev/null 2>&1
rpm -e $(/bin/rpm -qa | grep mariadb|xargs) --nodeps >/dev/null 2>&1
yum -y install make gcc-c++ cmake bison-devel ncurses-devel >/dev/null 2>&1
mkdir -p /usr/local/boost && cd /usr/local/boost
wget http://www.sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz >/dev/null 2>&1
tar -zvxf boost_1_59_0.tar.gz >/dev/null 2>&1
/usr/sbin/groupadd mysql
/usr/sbin/useradd -g mysql mysql -M -s /sbin/nologin
mkdir -p /data/mysql/data
cd $BASE_DIR
wget -c http://ftp.ntu.edu.tw/MySQL/Downloads/MySQL-5.7/mysql-5.7.28.tar.gz >/dev/null 2>&1
tar -zxvf mysql-5.7.28.tar.gz >/dev/null 2>&1
cd mysql-5.7.28/
#mysql安装目录和数据目录最好分开,配置文件为/usr/local/mysql/my.cnf
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql/data -DSYSCONFDIR=/usr/local/mysql -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_BOOST=/usr/local/boost >/dev/null 2>&1
if [ "$?" -eq 0 ];then
make && make install >/dev/null 2>&1
fi
chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /data/mysql
# 执行初始化配置脚本,创建系统自带的数据库和表
/usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql/data --user=mysql --initialize >/dev/null 2>&1
## 配置my.cnf, 注意下面EOF输入的内容需要顶格写,否则输入无效!
cat > /usr/local/mysql/my.cnf << EOF
[client]
port = 3306
socket = /usr/local/mysql/var/mysql.sock
[mysqld]
port = 3306
socket = /usr/local/mysql/var/mysql.sock
basedir = /usr/local/mysql/
datadir = /data/mysql/data
pid-file = /data/mysql/data/mysql.pid
user = mysql
bind-address = 0.0.0.0
server-id = 1
sync_binlog=1
log_bin = mysql-bin
skip-name-resolve
#skip-networking
back_log = 600
max_connections = 3000
max_connect_errors = 3000
##open_files_limit = 65535
table_open_cache = 512
max_allowed_packet = 16M
binlog_cache_size = 16M
max_heap_table_size = 16M
tmp_table_size = 256M
read_buffer_size = 1024M
read_rnd_buffer_size = 1024M
sort_buffer_size = 1024M
join_buffer_size = 1024M
key_buffer_size = 8192M
thread_cache_size = 8
query_cache_size = 512M
query_cache_limit = 1024M
ft_min_word_len = 4
binlog_format = mixed
expire_logs_days = 30
log_error = /data/mysql/data/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /data/mysql/data/mysql-slow.log
performance_schema = 0
explicit_defaults_for_timestamp
##lower_case_table_names = 1
skip-external-locking
default_storage_engine = InnoDB
##default-storage-engine = MyISAM
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 40960M
innodb_write_io_threads = 1000
innodb_read_io_threads = 1000
innodb_thread_concurrency = 8
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 4M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
bulk_insert_buffer_size = 8M
#myisam_sort_buffer_size = 8M
#myisam_max_sort_file_size = 1G
#myisam_repair_threads = 1
interactive_timeout = 28800
wait_timeout = 28800
[mysqldump]
quick
max_allowed_packet = 16M
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
port = 3306
EOF
#启动mysql服务
cd /usr/local/mysql && mkdir var
chown -R mysql.mysql var
cp support-files/mysql.server /etc/init.d/mysql
chmod 755 /etc/init.d/mysql
#由于是拷贝到/etc/init.d下的启动脚本,这里不管是centos6或centos6都使用chkconfig添加开机启动
chkconfig mysql on
service mysql start >/dev/null 2>&1
#设置环境变量
echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
source /etc/profile
mkdir -p /var/lib/mysql
ln -s /usr/local/mysql/var/mysql.sock /var/lib/mysql/mysql.sock
chown -R mysql:mysql /var/lib/mysql
#设置初始密码为"mysql@123"
sed -i '/\[mysqld]/a\skip-grant-tables' /usr/local/mysql/my.cnf
service mysql restart >/dev/null 2>&1
#mysql无密码登陆,可以使用mysql -p" "直接登陆(即密码为空)。如果直接使用mysql -p回车,然后无密码,但还是需要触发一次回车。
/usr/local/mysql/bin/mysql -p" " -e "update mysql.user set authentication_string=password('mysql@123') where user='root';" -e "flush privileges;"
sed -i '/skip-grant-tables/d' /usr/local/mysql/my.cnf
MYSQL_VERSION=$(basename $(ls $BASE_DIR/mysql-*.tar.gz)|cut -d"-" -f2|awk -F".tar" '{print $1}')
chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /data/mysql
service mysql restart >/dev/null 2>&1
if [ "$?" -eq 0 ];then
echo "恭喜您! Mysql${MYSQL_VERSION}安装成功!目前Mysql服务已启动,初始密码为mysql@123"
fi
}
case $NUM in
1)
Nginx_server
;;
2)
Apache_server
;;
3)
Mysql_server
;;
esac
执行脚本
需要注意:
上面One_button_install.sh一键部署脚本中涉及服务安装包的下载,考虑到网络因素,可以提前将包下载下来放到指定的${BASE_DIR}目录下,这样有利于提高脚本执行效率。
执行脚本,必须选择安装mysql
[root@localhost ~]# chmod 755 One_button_install.sh
[root@localhost ~]# sh One_button_install.sh
+------------------------------------------------+
| |
| _o0o_ 1. 安装Nginx |
| 08880 2. 安装Apache |
| 88"."88 3. 安装MySQL |
| (|-_-|) 4. 安装PHP |
| 0\=/0 5. 部署LNMP环境 |
| __/ \__ 6. 安装zabbix监控 |
| ‘\ ///‘ 7. 退出此管理程序 |
| / Linux一键 \ 8. 关闭计算机 |
| || Server || ================================|
| \ //// 一键安装服务 |
| ||| i i i ||| by Kevin |
| ___ ___ ================================|
|___‘. /--.--\ .‘___ |
+------------------------------------------------+
请您输入1-8任意字符:3
开始安装Mysql,请喝杯咖啡,耐心等待一段时间....
接着就是耐心等待就好了,本脚本经过线上测试可用。
转载至:https://www.cnblogs.com/kevingrace/p/6086426.html