参考shell脚本
#!/bin/bash ##################################################### #参数说明:1.-root 数据库root密码(必填) # 2.-nms64admin nms64admin密码(必填) ####################################################### rootpwd="123456" nms64adminpwd="nms64admin" #获取参数 while [ -n "$1" ] do case "$1" in -root) rootpwd=$2 shift ;; -nms64admin) nms64adminpwd=$2 shift ;; *) #echo "$1 参数不存在" #exit; echo "{\"code\":1,\"msg\":\"$1 参数不存在\"}" exit 1; ;; esac shift done cnt=$(ps -ef | egrep "mysqld" | grep -v grep | wc -l) dbtype="" if [ "$cnt" = 0 ]; then #echo 没有安装mysql或maiardb,或服务没有启动 #exit; echo "{\"code\":2,\"msg\":\"没有安装mysql或maiardb,或服务没有启动\"}" exit 2; else if [ "$(mysql --version | grep -c 'MariaDB')" = 1 ]; then dbtype=mariadb else dbtype=mysql fi fi modmycnf() { #先判断配置文件是否存在 if [ ! -e /etc/my.cnf ]; then #echo "/etc/my.cnf 不存在" #exit echo "{\"code\":3,\"msg\":\"/etc/my.cnf 不存在\"}" exit 3; fi #备份原有my.cnf \cp /etc/my.cnf /etc/my.cnf.bak #检查有没有[mysqld] havemysqld=$(grep -E '^\[mysqld\]$' /etc/my.cnf | wc -l) if [ "$havemysqld" = 0 ]; then #直接添加 echo -e "[mysqld]\nmax_connections=10000\ncharacter-set-server=utf8\ngroup_concat_max_len = 102400">> /etc/my.cnf else #先修改最大连接数 #先检查要修改的配置项是否存在 cfgnum=$(grep -n max_connections /etc/my.cnf | wc -l) if [ "$cfgnum" = 0 ]; then #配置项不存在 直接添加 sed -i '/^\[mysqld\]$/ a max_connections=1000' /etc/my.cnf elif [ "$cfgnum" = 1 ]; then #配置项存在 修改原配置项 maxc=$(grep -n max_connections /etc/my.cnf) num=${maxc%:*} sed -i "${num}"cmax_connections=1000 /etc/my.cnf else #存在多个配置项,退出 #echo /etc/my.cnf 存在多个 max_connections 配置项,请检查 # exit echo "{\"code\":4,\"msg\":\"/etc/my.cnf 存在多个 max_connections 配置项,请检查\"}" exit 4; fi #再修改utf8字符集 charsetnum=$(grep -n character-set-server /etc/my.cnf | wc -l) if [ "$charsetnum" = 0 ]; then #配置项不存在 直接添加 sed -i '/^\[mysqld\]$/ a character-set-server=utf8' /etc/my.cnf elif [ "$charsetnum" = 1 ]; then #配置项已存在 直接修改 charsetc=$(grep -n character-set-server /etc/my.cnf) num=${charsetc%:*} sed -i "${num}"ccharacter-set-server=utf8 /etc/my.cnf else #echo /etc/my.cnf 存在多个 character-set-server 配置项,请检查 #exit echo "{\"code\":5,\"msg\":\"/etc/my.cnf 存在多个 character-set-server 配置项,请检查\"}" exit 5; fi #group_concat_max_len = 102400 gcml=$(grep -n group_concat_max_len /etc/my.cnf | wc -l) if [ "$gcml" = 0 ]; then #配置项不存在 直接添加 sed -i '/^\[mysqld\]$/ a group_concat_max_len=102400' /etc/my.cnf elif [ "$gcml" = 1 ]; then #配置项已存在 直接修改 gcmlc=$(grep -n group_concat_max_len /etc/my.cnf) gcnum=${gcmlc%:*} sed -i "${gcnum}"cgroup_concat_max_len=102400 /etc/my.cnf else #echo /etc/my.cnf 存在多个 character-set-server 配置项,请检查 #exit echo "{\"code\":13,\"msg\":\"/etc/my.cnf 存在多个 group_concat_max_len 配置项,请检查\"}" exit 13; fi fi #设置跳过密码登录 #sed -i '/^\[mysqld\]$/ a skip-grant-tables' /etc/my.cnf } chkmycnf() { #检查最大连接数 maxcon=$(grep -E 'max_connections=1000' /etc/my.cnf | wc -l) if [ ! "$maxcon" = 1 ]; then #echo 设置mysql最大连接数失败 #exit echo "{\"code\":6,\"msg\":\"设置mysql最大连接数失败\"}" exit 6; fi #检查字符集 css=$(grep -E 'character-set-server=utf8' /etc/my.cnf | wc -l) if [ ! "$css" = 1 ]; then #echo 设置字符集失败 #exit echo "{\"code\":7,\"msg\":\"设置字符集失败\"}" exit 7; fi } stopsql() { if [ $dbtype = "mysql" ]; then systemctl stop mysql.service >/dev/null 2>&1 status=$(ps -ef | egrep "mysqld" | grep -v grep | wc -l) echo "$status" else systemctl stop mariadb.service >/dev/null 2>&1 status=$(ps -ef | egrep "mysqld" | grep -v grep | wc -l) echo "$status" fi } startsql() { if [ $dbtype = "mysql" ]; then systemctl start mysql.service >/dev/null 2>&1 status=$(ps -ef | egrep "mysqld" | grep -v grep | wc -l) echo "$status" else systemctl start mariadb.service >/dev/null 2>&1 status=$(ps -ef | egrep "mysqld" | grep -v grep | wc -l) echo "$status" fi } modmysqlcfg() { #停止数据库服务 if [ ! "$(stopsql)" = 0 ]; then #echo 停止数据库失败 #exit echo "{\"code\":8,\"msg\":\"停止数据库失败\"}" exit 8; fi modmycnf chkmycnf } modmariadbsvr() { #首先查看有没有[Service]节点 havesvr=$(grep -E '^\[Service\]$' ${1} | wc -l) if [ "$havesvr" = 0 ]; then #直接添加 echo -e "[Service]\nLimitNOFILE=10000\nLimitNPROC=10000">>"$1" else lfile=$(grep -E '^'"${2}"'=' ${1} | wc -l) if [ "$lfile" = 0 ]; then #没有该配置项,直接在[Service]节点下添加 sed -i '/^\[Service\]/a'"$2"'=10000' "${1}" elif [ "$lfile" = 1 ]; then lfilen=$(grep -n '^'"$2"'=' "$1") filenum=${lfilen%:*} sed -i "${filenum}"c"${2}"=10000 "${1}" else #echo "${1}"存在多个"${2}"配置项,请检查 #exit; echo "{\"code\":9,\"msg\":\"${1}存在多个${2}配置项,请检查\"}" exit 9; fi fi } modMaiardbMaxConn() { mdbcfgpath=/usr/lib/systemd/system/mariadb.service if [ ! -e $mdbcfgpath ]; then #echo ${mdbcfgpath}文件不存在 #exit; echo "{\"code\":10,\"msg\":\"${mdbcfgpath}文件不存在\"}" exit 10; else #备份原文件 \cp /usr/lib/systemd/system/mariadb.service /usr/lib/systemd/system/mariadb.service.bak modmariadbsvr ${mdbcfgpath} LimitNOFILE modmariadbsvr ${mdbcfgpath} LimitNPROC fi } resetRoot() { #mysql -uroot <<EOF #flush privileges; #GRANT ALL PRIVILEGES ON *.* TO root@'localhost' IDENTIFIED BY '$rootpwd' WITH GRANT OPTION; #flush privileges; #EOF #sed -i '/skip-grant-tables/d' /etc/my.cnf #if [[ -z "${usedrootpwd}" ]]; #then # mysql -uroot -e "GRANT ALL PRIVILEGES ON *.* TO root@'localhost' IDENTIFIED BY '$rootpwd' WITH GRANT OPTION;" # mysql -uroot -e "delete from mysql.user where password='';" #else # mysql -uroot -p"${usedrootpwd}" -e "GRANT ALL PRIVILEGES ON *.* TO root@'localhost' IDENTIFIED BY '$rootpwd' WITH GRANT OPTION;" # mysql -uroot -p"${usedrootpwd}" -e "delete from mysql.user where password='';" #fi mysql -uroot -e "GRANT ALL PRIVILEGES ON *.* TO root@'localhost' IDENTIFIED BY '$rootpwd' WITH GRANT OPTION;" mysql -uroot -e "delete from mysql.user where password='';" } resetRootTry(){ mysql -uroot -p"${rootpwd}" -e "GRANT ALL PRIVILEGES ON *.* TO root@'localhost' IDENTIFIED BY '$rootpwd' WITH GRANT OPTION;" mysql -uroot -p"${rootpwd}" -e "delete from mysql.user where password='';" } modmysqlcfg if [ "$dbtype" = "mariadb" ]; then modMaiardbMaxConn fi #启动mysql if [ "$(startsql)" = 0 ]; then #echo 启动数据库失败 #exit echo "{\"code\":10,\"msg\":\"启动数据库失败\"}" exit 10; fi #执行初始化root密码 resetRoot resetret1="$?" if [ ! "$resetret1" = 0 ] then #echo "{\"code\":15,\"msg\":\"数据库似乎已经设置了密码,请重新执行,并加上-usedrootpasswd 数据库密码参数\"}" #exit 15; resetRootTry resetret2="$?" if [ ! "$resetret2" = 0 ]; then echo "{\"code\":15,\"msg\":\"数据库已经设置了密码\"}" exit 15; fi fi #检查nms64admin用户是否存在 msg=$(mysql -uroot -p"$rootpwd" -e "SELECT User, HOST FROM mysql.user WHERE User='nms64admin'") name=$(echo "$msg" | grep -c "nms64admin") if [ "$name" = 0 ]; then mysql -uroot -p"$rootpwd" <<EOF flush privileges; create user 'nms64admin'@'%' identified by '$nms64adminpwd'; GRANT ALL PRIVILEGES ON *.* TO 'nms64admin'@'localhost' IDENTIFIED BY '$nms64adminpwd' WITH GRANT OPTION; GRANT ALL PRIVILEGES ON *.* TO 'nms64admin'@'%' IDENTIFIED BY '$nms64adminpwd' WITH GRANT OPTION; flush privileges; EOF else mysql -uroot -p"$rootpwd" <<EOF flush privileges; GRANT ALL PRIVILEGES ON *.* TO 'nms64admin'@'localhost' IDENTIFIED BY '$nms64adminpwd' WITH GRANT OPTION; GRANT ALL PRIVILEGES ON *.* TO 'nms64admin'@'%' IDENTIFIED BY '$nms64adminpwd' WITH GRANT OPTION; flush privileges; EOF fi #设置开机自启 if [ ${dbtype} = "mysql" ]; then chkconfig mysqld on else systemctl enable mariadb fi #重启 systemctl --system daemon-reload if [ ! "$(stopsql)" = 0 ]; then #echo 停止数据库失败 #exit echo "{\"code\":11,\"msg\":\"停止数据库失败\"}" exit 11; fi if [ "$(startsql)" = 0 ]; then #echo 启动数据库失败 #exit echo "{\"code\":12,\"msg\":\"启动数据库失败\"}" exit 12; fi echo "{\"code\":0,\"msg\":\"成功\"}"
不想平凡,奈何太懒 T_T