获取内网 IP 地址
Shell脚本中获取本机ip地址的3个方法
# 获取内网 IP 地址
function getInnerNet(){
# /sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"
innerIpList=`ip addr |grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:" | awk -F '/' '{print $1}'`
echo "内网 IP 地址(集合):$innerIpList"
for ip in $innerIpList
do
echo "内网 IP 地址:$ip"
done
}
# getInnerNet
获取外网地址(公网地址)
# 获取外网 IP 地址
function getOuterNet(){
outIp=`curl members.3322.org/dyndns/getip`
echo "外网 IP 地址为:$outIp"
}
# getOuterNet
Redis 开机启动
安全删除文件 rm -rf
# 执行 rm 操作时会有二次确认提示,避免手抖造成重大操作事故。
vim ~/.bashrc
alias rm='read -p "请确认是否彻底删除?[输入 'y'/'Y' 进行确认,或者输入 'n'/'N' 取消。]" y && [ $y == "y" ] && rm -i'
保存退出后,在终端执行 source ~/.bashrc
使上面的修改生效。如仍未生效,重新连接服务即可。
登录执行 MySQL 文件/语句(密码免输入/免登录)
function executeSql(){
# 定义变量,用来保存数据库名
dbName=DB_NEW
# 注意事项,如果变量值是以特殊字符开头的,需要用"单引号"将其包起(见下),若传递过来的以特殊字符开头的变量,则不用做特殊处理。
dbPwd='123456' # 使用'单引号' 括起来是为了避免有特殊字符引起错误。
dbUser=$1
sqlFilePath=$2
mysql -u $dbUser -p$dbPwd --connect-expired-password <<EOF
use $dbName;
source ${sqlFilePath}.sql;
exit
EOF
}
# executeSql
复制当前服务器文件到远端服务器(密码免输入)
涉及知识点:sshpass
# 远端服务器的 ip
remoteIp=
# 远端服务器的端口
remotePort=
# 远端服务器的密码
remotePwd=
# 远端服务器保存文件的路径
remoteDirPath=
# 本服务器需要复制的文件路径
copyFilePath=
# 安装 sshpass 命令
yum -y install sshpass
#sshpass -p 远端服务器密码 scp -o StrictHostKeyChecking=no -P 远端服务器端口 -r 本地要拷贝的路径 root@远端服务器IP:远端服务器要保存的路径
sshpass -p ${remotePwd} scp -o StrictHostKeyChecking=no -P ${remotePort} -r ${copyFilePath} root@${remoteIp}:${remoteDirPath}
复制远端服务器文件到当前服务器(密码免输入)
#sshpass -p 远端服务器密码 scp -o StrictHostKeyChecking=no -P 当前服务器的端口 -r 远端服务器的用户名@远端服务器IP:远端服务器要拷贝的文件路径 当前服务器要保存的路径
sshpass -p ${remotePwd} scp -o StrictHostKeyChecking=no -P ${localPort} -r ${remoteUser}@${remoteIp}:${remoteDirPath}/* ${localDirPath}/
启动 war 包
# 如果存在当前服务文件夹,则启动当前服务
function startWarServer() {
# 服务名称(new)
proServer=$1
if [ ! -d "${newBasePath}/${proServer}" ];then
echo "没有该服务:[[ $proServer ]],无需启动" >> $logPath
else
# echo "有该文件,$proServer ..."
echo "$proServer 判断执行,... " >> $logPath
numServer=`ps -aux | grep ${newBasePath}/${proServer}/bin | grep -v "grep" | wc -l`
if [[ ${numServer} -lt 1 ]];then
echo "${proServer} `date "+%Y%m%d %T"` 没有启动 正在启动" >> $logPath
cd ${newBasePath}/${proServer}
${newBasePath}/${proServer}/bin/startup.sh
cd -
else
echo "${proServer} `date "+%Y%m%d %T"` 已启动" >> $logPath
fi
echo "$proServer 判断执行,end ... " >> $logPath
fi
}
# startWarServer 服务名
startWarServer ${pro_ftpServer}
启动 jar 包(限制内存启动)
# 调用 jar 包服务(限制内存启动)
JAVA_OPTS="-Xss512k -XX:MaxMetaspaceSize=1024M -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+UseCMSCompactAtFullCollection -XX:+UseParNewGC -XX:+CMSParallelRemarkEnabled -XX:CMSInitiatingOccupancyFraction=75 -XX:ParallelGCThreads=4 -XX:+HeapDumpOnOutOfMemoryError";
function startJarServer() {
# 服务名称(new)
proServer=$1
# jar 包名称
jarName=$2
# 限制内存大小
limitXmx=$3
serverPath=${newBasePath}/${proServer}
echo "proServer=$1,jarName=$2,limitXmx=$3,serverPath=${newBasePath}/${proServer}" >> $logPath
if [ ! -d "${newBasePath}/${proServer}" ];then
echo "没有该服务:[[ $proServer ]],无需启动" >> $logPath
else
#num_MessageServer=`ps -aux | grep MessageServer.jar | grep -v "grep" | wc -l`
numServer=`ps -aux | grep ${newBasePath}/${proServer}/${jarName}.jar | grep -v "grep" | wc -l`
if [[ ${numServer} -lt 1 ]];then
echo "`date "+%Y%m%d %T"` [[[ ${proServer} ]]] 未启动,正在启动" >> $logPath
cd ${newBasePath}/${proServer}
nohup java -server -Xms512m -Xmx${limitXmx}G $JAVA_OPTS -XX:HeapDumpPath=/logs/${proServer}.dump -jar ${newBasePath}/${proServer}/${jarName}.jar >/dev/null 2>&1 &
cd -
else
echo "`date "+%Y%m%d %T"` [[ ${proServer} ]] 已启动" >> $logPath
fi
echo "execute_$proServer ,end ... " >> $logPath
fi
}
# startJarServer 服务名 jar 包名 占用的最大内存
startJarServer ${pro_httpServer} ${httpServer_jarName} 1
清理 catalina.out 日志
# 检索所有的 catalina.out 日志文件并清理
function cleanLog(){
logFileList=`find / -name catalina.out`
for logFile in $logFileList
do
echo "清理的文件为:$logFile,大小是:`du -sh $logFile`"
echo "" > $logFile
done
}
# 调用
cleanLog
启动 redis
# 启动 redis
function start_redis(){
echo "function start_redis 开始执行 ... " >> $logPath
# path=${oldBasePath}/redis
path=${newBasePath}/redis
redis_file=${path}/redis-server
redis_conf=${path}/redis.conf
#判断对应 redis 进程是否已经启动
if `ps -ef | grep ${path} | grep -q redis-server`; then
echo "Redis state is OK: `ps -ef | grep -v "grep" | grep ${path} | grep redis-server`"
echo "redis 已经启动 ... " >> $logPath
return
fi
_echo "--------------------------------------------------------"
_echo "-------------starting Redis Server ------------"
echo "redis 启动,开始 ... " >> $logPath
cd ${path}
_echo "changed to dir: `pwd`"
echo "${redis_file} ${redis_conf} &"
${redis_file} ${redis_conf} &
echo "redis 启动,完成 ... " >> $logPath
cd -
echo "start_redis 开始结束。 "
}
启动 kafka
function start_kafka_zookeeper(){
#path=${oldBasePath}/kafka
path=${newBasePath}/kafka
zookeeper_start_file=${path}/bin/zookeeper-server-start.sh
zookeeper_stop_file=${path}/bin/zookeeper-server-stop.sh
zookeeper_cfg_file=${path}/config/zookeeper.properties
#判断对应Kafka zookeeper进程是否已经启动
# ps -ef | grep -v "grep"
# ps -ef | grep -v "grep" | grep /MNT/ICC/dahua/fire/kafka
# ps -ef | grep -v "grep" | grep /MNT/ICC/dahua/fire/kafka | grep -q "zookeeper.properties"
# if `ps -ef | grep -v "grep" | grep /MNT/ICC/dahua/fire/kafka | grep -q "zookeeper.properties"`; then
if `ps -ef | grep -v "grep" | grep ${path} | grep -q "zookeeper.properties"`; then
echo "zookeeper state is OK: `ps -ef | grep -v "grep" | grep ${path} | grep "zookeeper.properties" | cut -f-20 -d" "`"
return
fi
echo "--------------------------------------------------------"
echo "-------------Starting Kafka Zookeeper------------"
echo "启动 zookeeper 开始,... " >> $logPath
cd ${path}
nohup bin/zookeeper-server-start.sh config/zookeeper.properties &
echo "启动 zookeeper 完成。。。" >> $logPath
cd -
sleep 10
}
function start_kafka(){
#path=${oldBasePath}/kafka
path=${newBasePath}/kafka
Kafka_start_file=${path}/bin/kafka-server-start.sh
Kafka_stop_file=${path}/bin/kafka-server-stop.sh
Kafka_cfg_file=${path}/config/server.properties
#判断对应 kafka Server 进程是否已经启动
if `ps -ef | grep ${path} | grep -q "kafka.Kafka"`; then
echo "Kafka state is OK: `ps -ef | grep -v "grep" | grep ${path} | grep "kafka.Kafka"| cut -f-20 -d" "`"
return
fi
echo "--------------------------------------------------------"
echo "-------------starting Kafka Server ------------"
echo "启动 Kafka 开始,... " >> $logPath
cd ${path}
nohup bin/kafka-server-start.sh config/server.properties &
echo "启动 Kafka 完成 ... " >> $logPath
cd -
}
卸载 MySQL
#!/bin/bash
function removerMySQL(){
count=$(rpm -qa | grep mysql)
for x in $count
do
rpm -e $x --nodeps
done
count1=$(find / -name mysql)
for i in $count1
do
rm -rf $i
done
rm -rf /etc/my.cnf
rm -rf /var/log/mysqld.log
rm -rf /etc/my.cnf.rpmsave
rpm -qa|grep mysql
}
# 调用
removerMySQL
安装 MySQL
卸载和安装 MySQL
#!/bin/bash
# 此脚本未验证,方法可以参考
function verb(){
echo "
############################################################################################
# ###### ###### # # ######### ########## # #
# # # # # # # # # # # #
# # # # # # # # # # # #
# # # # # ########## ######### ########## # #
# # # # # # # # #
# # # # # # # #
# # # # ######### # ######### #
############################################################################################
"
echo "
############################################################################################
# # # ########## ########## # ######### #
# # # # # # # # # #
# # # # # # # # # #
# # # ########## # # # ######### #
# # # # # # # # # #
# # # # # # # # # #
# ## ########## ########## # ######### #
############################################################################################
"
}
# 执行方法介绍
function explanation(){
echo "
############################################################################################
脚本说明:脚本进行可进行传参执行
1---install_mysql //安装mysql8.0.15
2---remove_mysql //卸载当前服务器的mysql,一定要谨慎使用
############################################################################################
"
}
# 卸载mysql方法
function remove_mysql(){
count_mysql=`ps -ef|grep -e "mysql\|mysqld"|grep -v grep|wc -l`
if [ $count_mysql -gt 1 ];then
echo "本服务器上已经安装运行着mysql,是否需要卸载,一定要认真考虑!!!!!!"
read -p "请问是否需要删除现在运行的mysql[yes|no]:" num
if [ $num = 'yes'];then
systemctl stop mysqld
service mysql stop
count_install_mysql=$(rpm -qa|grep -e "mysql\|mysqld")
count_install_maindb=$(rpm -qa|grep "maindb")
mysql_file=$(find / -name mysql*)
for a in count_install_mysql
do
rpm -a $i --nodeps
done
for b in count_install_maindb
do
rpm -e $b --nodeps
done
for c in mysql_file
do
rm -rf $c
done
echo "卸载数据库完成"
fi
fi
}
# 安装mysql方法
function install_mysql(){
msg=`df -k | awk '{print $6}'` ; for i in /opt/* ; do echo $msg | grep $i 1>/dev/null || du -sk $i ; done | sort -n
file="$msg/opt"
file1="$file/software"
file2="/tmp/mysql.log"
file3="/etc/my.cnf"
hostname=`hostname`
if [ ! -f $file];then
mkdir -p $file
if [ ! -f $file1 ];then
mkdir -p $file1
if [ ! -f $file2 ];then
touch $file2
chmod 666 $file2
fi
fi
cd $file
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.15-el7-x86_64.tar.gz
tar -xvf *.tar.gz
mv *.tar.gz $file1
mv mysql* mysql
groupadd mysql
useradd -r -g mysql mysql
cd mysql
chown -R mysql:mysql ./
bin/mysql --initialize --user=mysql --basedir=$file --datadir=$file/datadir > $file2
str=$(cat $file2)
mysql_passwd=${str##*root@localhost:}
# 当/etc/my.cnf不存在的时候需要手动创建对应的my-defalut.cnf文件并重命名
if [ ! -f $file3 ];then
touch my-defalut.cnf
chmod 755 my-defalut.cnf
cp my-defalut.cnf $file3
fi
# 修改对应的mysql配置文件
echo "
[mysqld]
basedir=$file
datadir=$file/data
port=3306
socket=/tmp/mysql.sock
pid-file=$file/$hostname.pid
sql_mode=NO_ENGINE_SUBSTITUTION.STRICT_TRANS_TABLES" >> file3
# mysql启动脚本中修改对应的安装路径和对应的数据存储路径
sed -i "s#basedir=#basedir=$file#g" $file/support-files
sed -i "s#datadir=#datadir=$file/datadir#" $file/support-files
cp $file/support-files/mysql.server /etc/init.d/mysql
chmod +x /etc/init.d/mysql
chkconfig --add mysql
chkconfig --list >> $file2
# 该步骤一定需要核实,服务器重启,SELINUX=enforcing,会造成mysql无法启动
sed -i "s#SELINUX=enforcing#SELINUX=disabled#g" /etc/selinux/config
# 临时关闭selinux
setenforce 0
service mysql start
count=$?
if [ $count -eq 0 ];then
echo "mysql数据库启动成功" >> $file3
else
tail -f /var/log/mysqld.log
exit
fi
# 重制mysql root@localhost密码,创建root@%用户
mysql -u root -p $mysql_passwd <<EOF
alter user 'root'@'localhost' identified by '!Fzb_dahua2019';
CREATE USER 'root'@'%' IDENTIFIED BY '!Fzb_dahua2019';
GRANT ALL PRIVILEGES ON DATABASENAME.TABLENAME TO 'root'@'%';
FLUSH PRIVILEGES;
quit;
EOF
}
# 主方法
function main(){
verb
explanation
read -p "输入需要执行操作【 1|2 】:" num1
case $num1 in
1|1)
echo "欢迎进入安装mysql环节"
install_mysql
break
;;
2|2)
echo "欢迎进入卸载mysql环节,该步骤请慎重操作!!"
remove_mysql
break
;;
*)
echo "输入错误,请按照重新运行script"
break
;;
esac
}
#
main