获取内网 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 $innerIpListdoecho "内网 IP 地址:$ip"done}# getInnerNet
获取外网地址(公网地址)
# 获取外网 IP 地址function getOuterNet(){outIp=`curl members.3322.org/dyndns/getip`echo "外网 IP 地址为:$outIp"}# getOuterNet
Redis 开机启动
安全删除文件 rm -rf
# 执行 rm 操作时会有二次确认提示,避免手抖造成重大操作事故。vim ~/.bashrcalias rm='read -p "请确认是否彻底删除?[输入 'y'/'Y' 进行确认,或者输入 'n'/'N' 取消。]" y && [ $y == "y" ] && rm -i'
保存退出后,在终端执行 source ~/.bashrc 使上面的修改生效。如仍未生效,重新连接服务即可。
登录执行 MySQL 文件/语句(密码免输入/免登录)
function executeSql(){# 定义变量,用来保存数据库名dbName=DB_NEW# 注意事项,如果变量值是以特殊字符开头的,需要用"单引号"将其包起(见下),若传递过来的以特殊字符开头的变量,则不用做特殊处理。dbPwd='123456' # 使用'单引号' 括起来是为了避免有特殊字符引起错误。dbUser=$1sqlFilePath=$2mysql -u $dbUser -p$dbPwd --connect-expired-password <<EOFuse $dbName;source ${sqlFilePath}.sql;exitEOF}# executeSql
复制当前服务器文件到远端服务器(密码免输入)
涉及知识点:sshpass
# 远端服务器的 ipremoteIp=# 远端服务器的端口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=$1if [ ! -d "${newBasePath}/${proServer}" ];thenecho "没有该服务:[[ $proServer ]],无需启动" >> $logPathelse# echo "有该文件,$proServer ..."echo "$proServer 判断执行,... " >> $logPathnumServer=`ps -aux | grep ${newBasePath}/${proServer}/bin | grep -v "grep" | wc -l`if [[ ${numServer} -lt 1 ]];thenecho "${proServer} `date "+%Y%m%d %T"` 没有启动 正在启动" >> $logPathcd ${newBasePath}/${proServer}${newBasePath}/${proServer}/bin/startup.shcd -elseecho "${proServer} `date "+%Y%m%d %T"` 已启动" >> $logPathfiecho "$proServer 判断执行,end ... " >> $logPathfi}# 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=$3serverPath=${newBasePath}/${proServer}echo "proServer=$1,jarName=$2,limitXmx=$3,serverPath=${newBasePath}/${proServer}" >> $logPathif [ ! -d "${newBasePath}/${proServer}" ];thenecho "没有该服务:[[ $proServer ]],无需启动" >> $logPathelse#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 ]];thenecho "`date "+%Y%m%d %T"` [[[ ${proServer} ]]] 未启动,正在启动" >> $logPathcd ${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 -elseecho "`date "+%Y%m%d %T"` [[ ${proServer} ]] 已启动" >> $logPathfiecho "execute_$proServer ,end ... " >> $logPathfi}# startJarServer 服务名 jar 包名 占用的最大内存startJarServer ${pro_httpServer} ${httpServer_jarName} 1
清理 catalina.out 日志
# 检索所有的 catalina.out 日志文件并清理function cleanLog(){logFileList=`find / -name catalina.out`for logFile in $logFileListdoecho "清理的文件为:$logFile,大小是:`du -sh $logFile`"echo "" > $logFiledone}# 调用cleanLog
启动 redis
# 启动 redisfunction start_redis(){echo "function start_redis 开始执行 ... " >> $logPath# path=${oldBasePath}/redispath=${newBasePath}/redisredis_file=${path}/redis-serverredis_conf=${path}/redis.conf#判断对应 redis 进程是否已经启动if `ps -ef | grep ${path} | grep -q redis-server`; thenecho "Redis state is OK: `ps -ef | grep -v "grep" | grep ${path} | grep redis-server`"echo "redis 已经启动 ... " >> $logPathreturnfi_echo "--------------------------------------------------------"_echo "-------------starting Redis Server ------------"echo "redis 启动,开始 ... " >> $logPathcd ${path}_echo "changed to dir: `pwd`"echo "${redis_file} ${redis_conf} &"${redis_file} ${redis_conf} &echo "redis 启动,完成 ... " >> $logPathcd -echo "start_redis 开始结束。 "}
启动 kafka
function start_kafka_zookeeper(){#path=${oldBasePath}/kafkapath=${newBasePath}/kafkazookeeper_start_file=${path}/bin/zookeeper-server-start.shzookeeper_stop_file=${path}/bin/zookeeper-server-stop.shzookeeper_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"`; thenif `ps -ef | grep -v "grep" | grep ${path} | grep -q "zookeeper.properties"`; thenecho "zookeeper state is OK: `ps -ef | grep -v "grep" | grep ${path} | grep "zookeeper.properties" | cut -f-20 -d" "`"returnfiecho "--------------------------------------------------------"echo "-------------Starting Kafka Zookeeper------------"echo "启动 zookeeper 开始,... " >> $logPathcd ${path}nohup bin/zookeeper-server-start.sh config/zookeeper.properties &echo "启动 zookeeper 完成。。。" >> $logPathcd -sleep 10}function start_kafka(){#path=${oldBasePath}/kafkapath=${newBasePath}/kafkaKafka_start_file=${path}/bin/kafka-server-start.shKafka_stop_file=${path}/bin/kafka-server-stop.shKafka_cfg_file=${path}/config/server.properties#判断对应 kafka Server 进程是否已经启动if `ps -ef | grep ${path} | grep -q "kafka.Kafka"`; thenecho "Kafka state is OK: `ps -ef | grep -v "grep" | grep ${path} | grep "kafka.Kafka"| cut -f-20 -d" "`"returnfiecho "--------------------------------------------------------"echo "-------------starting Kafka Server ------------"echo "启动 Kafka 开始,... " >> $logPathcd ${path}nohup bin/kafka-server-start.sh config/server.properties &echo "启动 Kafka 完成 ... " >> $logPathcd -}
卸载 MySQL
#!/bin/bashfunction removerMySQL(){count=$(rpm -qa | grep mysql)for x in $countdorpm -e $x --nodepsdonecount1=$(find / -name mysql)for i in $count1dorm -rf $idonerm -rf /etc/my.cnfrm -rf /var/log/mysqld.logrm -rf /etc/my.cnf.rpmsaverpm -qa|grep mysql}# 调用removerMySQL
安装 MySQL
卸载和安装 MySQL
#!/bin/bash# 此脚本未验证,方法可以参考function verb(){echo "############################################################################################# ###### ###### # # ######### ########## # ## # # # # # # # # # # ## # # # # # # # # # # ## # # # # ########## ######### ########## # ## # # # # # # # ## # # # # # # ## # # # ######### # ######### #############################################################################################"echo "############################################################################################# # # ########## ########## # ######### ## # # # # # # # # ## # # # # # # # # ## # # ########## # # # ######### ## # # # # # # # # ## # # # # # # # # ## ## ########## ########## # ######### #############################################################################################"}# 执行方法介绍function explanation(){echo "############################################################################################脚本说明:脚本进行可进行传参执行1---install_mysql //安装mysql8.0.152---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 ];thenecho "本服务器上已经安装运行着mysql,是否需要卸载,一定要认真考虑!!!!!!"read -p "请问是否需要删除现在运行的mysql[yes|no]:" numif [ $num = 'yes'];thensystemctl stop mysqldservice mysql stopcount_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_mysqldorpm -a $i --nodepsdonefor b in count_install_maindbdorpm -e $b --nodepsdonefor c in mysql_filedorm -rf $cdoneecho "卸载数据库完成"fifi}# 安装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 -nfile="$msg/opt"file1="$file/software"file2="/tmp/mysql.log"file3="/etc/my.cnf"hostname=`hostname`if [ ! -f $file];thenmkdir -p $fileif [ ! -f $file1 ];thenmkdir -p $file1if [ ! -f $file2 ];thentouch $file2chmod 666 $file2fificd $filewget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.15-el7-x86_64.tar.gztar -xvf *.tar.gzmv *.tar.gz $file1mv mysql* mysqlgroupadd mysqluseradd -r -g mysql mysqlcd mysqlchown -R mysql:mysql ./bin/mysql --initialize --user=mysql --basedir=$file --datadir=$file/datadir > $file2str=$(cat $file2)mysql_passwd=${str##*root@localhost:}# 当/etc/my.cnf不存在的时候需要手动创建对应的my-defalut.cnf文件并重命名if [ ! -f $file3 ];thentouch my-defalut.cnfchmod 755 my-defalut.cnfcp my-defalut.cnf $file3fi# 修改对应的mysql配置文件echo "[mysqld]basedir=$filedatadir=$file/dataport=3306socket=/tmp/mysql.sockpid-file=$file/$hostname.pidsql_mode=NO_ENGINE_SUBSTITUTION.STRICT_TRANS_TABLES" >> file3# mysql启动脚本中修改对应的安装路径和对应的数据存储路径sed -i "s#basedir=#basedir=$file#g" $file/support-filessed -i "s#datadir=#datadir=$file/datadir#" $file/support-filescp $file/support-files/mysql.server /etc/init.d/mysqlchmod +x /etc/init.d/mysqlchkconfig --add mysqlchkconfig --list >> $file2# 该步骤一定需要核实,服务器重启,SELINUX=enforcing,会造成mysql无法启动sed -i "s#SELINUX=enforcing#SELINUX=disabled#g" /etc/selinux/config# 临时关闭selinuxsetenforce 0service mysql startcount=$?if [ $count -eq 0 ];thenecho "mysql数据库启动成功" >> $file3elsetail -f /var/log/mysqld.logexitfi# 重制mysql root@localhost密码,创建root@%用户mysql -u root -p $mysql_passwd <<EOFalter 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(){verbexplanationread -p "输入需要执行操作【 1|2 】:" num1case $num1 in1|1)echo "欢迎进入安装mysql环节"install_mysqlbreak;;2|2)echo "欢迎进入卸载mysql环节,该步骤请慎重操作!!"remove_mysqlbreak;;*)echo "输入错误,请按照重新运行script"break;;esac}#main
