shell脚本实现对Mysql数据库的初始化

网友投稿 866 2022-11-23

shell脚本实现对Mysql数据库的初始化

shell脚本实现对Mysql数据库的初始化

#!/bin/bash< /dev/null;then echo "ERROR:Please Install Mysql Client" && exit 1 fi if ! echo ${MYSQL_IP} | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' &> /dev/null;then echo "ERROR:${MYSQL_IP}不合法!!!" && exit 1 fi if ! ${MYSQL_CMD} -e 'show databases;' &> /dev/null;then echo "ERROR:${MYSQL_IP} Database Access Fail" && exit 1 fi}#创建Mysql用户Create_User(){ [[ $# -eq 0 ]] && echo "ERROR:User Info Is Null,Please Excute:bash

#!/bin/bash< /dev/null;then echo "ERROR:Please Install Mysql Client" && exit 1 fi if ! echo ${MYSQL_IP} | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' &> /dev/null;then echo "ERROR:${MYSQL_IP}不合法!!!" && exit 1 fi if ! ${MYSQL_CMD} -e 'show databases;' &> /dev/null;then echo "ERROR:${MYSQL_IP} Database Access Fail" && exit 1 fi}#创建Mysql用户Create_User(){ [[ $# -eq 0 ]] && echo "ERROR:User Info Is Null,Please Excute:bash $0 -h" && exit 1 for info in $@ do local info_array=(${info//:/ }) if [[ ${#info_array[*]} -ne 2 ]];then echo "ERROR:${info} Format Error,Please Excute:bash $0 -h" && exit 1 fi local mysql_user=${info_array[0]} local mysql_pwd=${info_array[1]} echo "INFO:Begin Create Mysql User ${mysql_user}..." ${MYSQL_CMD} -e "create USER if NOT EXISTS '${mysql_user}'@'%' identified by '${mysql_pwd}';" done}#创建schemaCreate_Schema(){ [[ $# -eq 0 ]] && echo "ERROR:Schema Is Null,Please Excute:bash $0 -h" && exit 1 for mysql_schema in $@ do echo "INFO:Begin Create Mysql Schema ${mysql_schema}..." ${MYSQL_CMD} -e "create SCHEMA if NOT EXISTS ${mysql_schema} default character set utf8mb4 collate utf8mb4_bin;" done}#给用户授权Grant_User(){ [[ $# -eq 0 ]] && echo "ERROR:User Info Is Null,Please Excute:bash $0 -h" && exit 1 for info in $@ do local info_array=(${info//:/ }) if [[ ${#info_array[*]} -ne 2 ]];then echo "ERROR:${info} Format Error,Please Excute:bash $0 -h" && exit 1 fi local schema_name=${info_array[0]} local mysql_user=${info_array[1]} echo "INFO:Begin grant schema ${schema_name} to Mysql User ${mysql_user}..." ${MYSQL_CMD} -e "grant ALL on ${schema_name}.* to '${mysql_user}'@'%';" done}#导入sqlImport_Sql(){ [[ $# -ne 5 ]] && echo "ERROR:Invalid Param!!!,Please Excute:bash $0 -h" && exit 1 local mysql_user=$1 local mysql_pwd=$2 local mysql_schema=$3 local jfrog_user_info=$4 local sql_url=$5 curl -u ${jfrog_user_info} -O ${sql_url} local sql_name=$(echo ${sql_url##*/}) echo "INFO:Begin Import ${sql_name} Sql To Schema ${mysql_schema} On Mysql User ${mysql_user}..." mysql -h${MYSQL_IP} -P${MYSQL_PORT} -u${mysql_user} -p${mysql_pwd} ${mysql_schema} < ${sql_name}}#帮助文档Help(){ cat << EOFUsage: bash $0 IP 端口 Mysql管理员用户名 密码 -cWARNING: 当传入的位置参数使用冒号(:)分隔时,冒号两边的值不能包含冒号(:)=======================================================================optional arguments: -h 提供帮助信息 -c 进行创建的相关操作 user 创建Mysql用户,格式:-c user 用户1:密码1 用户2:密码2,如-c user user1:pwd1 user2:pwd2 schema 创建schema,格式:-c schema schema1 schema2 grant 授权,格式:-c grant schema名字1:mysql用户名1 schema名字1:mysql用户名1 import 导入sql,格式:-c import mysql用户名 密码 schema名字 jfrog用户名:密码 sql的urlEXAMPLE: bash $0 192.168.2.1 3306 root 123456-c user user1:pwd1 user2:pwd2EOF}######################主程序######################[[ "x$1" == "x-h" ]] && Help && exit 0[[ $# -le 5 ]] && echo "ERROR:Invalid Param!!!,Please Excute:bash $0 -h" && exit 1Check_Envif [[ "x$5" == "x-c" ]];then shift 5 case $1 in user) shift 1 Create_User $@;; schema) shift 1 Create_Schema $@;; grant) shift 1 Grant_User $@;; import) shift 1 Import_Sql $@;; *) echo "ERROR:Invalid Param!!!,Please Excute:bash $0 -h" && exit 1 esacelse echo "Help" && exit 1fi

-h" && exit 1 for info in $@ do local info_array=(${info//:/ }) if [[ ${#info_array[*]} -ne 2 ]];then echo "ERROR:${info} Format Error,Please Excute:bash

#!/bin/bash< /dev/null;then echo "ERROR:Please Install Mysql Client" && exit 1 fi if ! echo ${MYSQL_IP} | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' &> /dev/null;then echo "ERROR:${MYSQL_IP}不合法!!!" && exit 1 fi if ! ${MYSQL_CMD} -e 'show databases;' &> /dev/null;then echo "ERROR:${MYSQL_IP} Database Access Fail" && exit 1 fi}#创建Mysql用户Create_User(){ [[ $# -eq 0 ]] && echo "ERROR:User Info Is Null,Please Excute:bash $0 -h" && exit 1 for info in $@ do local info_array=(${info//:/ }) if [[ ${#info_array[*]} -ne 2 ]];then echo "ERROR:${info} Format Error,Please Excute:bash $0 -h" && exit 1 fi local mysql_user=${info_array[0]} local mysql_pwd=${info_array[1]} echo "INFO:Begin Create Mysql User ${mysql_user}..." ${MYSQL_CMD} -e "create USER if NOT EXISTS '${mysql_user}'@'%' identified by '${mysql_pwd}';" done}#创建schemaCreate_Schema(){ [[ $# -eq 0 ]] && echo "ERROR:Schema Is Null,Please Excute:bash $0 -h" && exit 1 for mysql_schema in $@ do echo "INFO:Begin Create Mysql Schema ${mysql_schema}..." ${MYSQL_CMD} -e "create SCHEMA if NOT EXISTS ${mysql_schema} default character set utf8mb4 collate utf8mb4_bin;" done}#给用户授权Grant_User(){ [[ $# -eq 0 ]] && echo "ERROR:User Info Is Null,Please Excute:bash $0 -h" && exit 1 for info in $@ do local info_array=(${info//:/ }) if [[ ${#info_array[*]} -ne 2 ]];then echo "ERROR:${info} Format Error,Please Excute:bash $0 -h" && exit 1 fi local schema_name=${info_array[0]} local mysql_user=${info_array[1]} echo "INFO:Begin grant schema ${schema_name} to Mysql User ${mysql_user}..." ${MYSQL_CMD} -e "grant ALL on ${schema_name}.* to '${mysql_user}'@'%';" done}#导入sqlImport_Sql(){ [[ $# -ne 5 ]] && echo "ERROR:Invalid Param!!!,Please Excute:bash $0 -h" && exit 1 local mysql_user=$1 local mysql_pwd=$2 local mysql_schema=$3 local jfrog_user_info=$4 local sql_url=$5 curl -u ${jfrog_user_info} -O ${sql_url} local sql_name=$(echo ${sql_url##*/}) echo "INFO:Begin Import ${sql_name} Sql To Schema ${mysql_schema} On Mysql User ${mysql_user}..." mysql -h${MYSQL_IP} -P${MYSQL_PORT} -u${mysql_user} -p${mysql_pwd} ${mysql_schema} < ${sql_name}}#帮助文档Help(){ cat << EOFUsage: bash $0 IP 端口 Mysql管理员用户名 密码 -cWARNING: 当传入的位置参数使用冒号(:)分隔时,冒号两边的值不能包含冒号(:)=======================================================================optional arguments: -h 提供帮助信息 -c 进行创建的相关操作 user 创建Mysql用户,格式:-c user 用户1:密码1 用户2:密码2,如-c user user1:pwd1 user2:pwd2 schema 创建schema,格式:-c schema schema1 schema2 grant 授权,格式:-c grant schema名字1:mysql用户名1 schema名字1:mysql用户名1 import 导入sql,格式:-c import mysql用户名 密码 schema名字 jfrog用户名:密码 sql的urlEXAMPLE: bash $0 192.168.2.1 3306 root 123456-c user user1:pwd1 user2:pwd2EOF}######################主程序######################[[ "x$1" == "x-h" ]] && Help && exit 0[[ $# -le 5 ]] && echo "ERROR:Invalid Param!!!,Please Excute:bash $0 -h" && exit 1Check_Envif [[ "x$5" == "x-c" ]];then shift 5 case $1 in user) shift 1 Create_User $@;; schema) shift 1 Create_Schema $@;; grant) shift 1 Grant_User $@;; import) shift 1 Import_Sql $@;; *) echo "ERROR:Invalid Param!!!,Please Excute:bash $0 -h" && exit 1 esacelse echo "Help" && exit 1fi

-h" && exit 1 fi local mysql_user=${info_array[0]} local mysql_pwd=${info_array[1]} echo "INFO:Begin Create Mysql User ${mysql_user}..." ${MYSQL_CMD} -e "create USER if NOT EXISTS '${mysql_user}'@'%' identified by '${mysql_pwd}';" done}#创建schemaCreate_Schema(){ [[ $# -eq 0 ]] && echo "ERROR:Schema Is Null,Please Excute:bash

#!/bin/bash< /dev/null;then echo "ERROR:Please Install Mysql Client" && exit 1 fi if ! echo ${MYSQL_IP} | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' &> /dev/null;then echo "ERROR:${MYSQL_IP}不合法!!!" && exit 1 fi if ! ${MYSQL_CMD} -e 'show databases;' &> /dev/null;then echo "ERROR:${MYSQL_IP} Database Access Fail" && exit 1 fi}#创建Mysql用户Create_User(){ [[ $# -eq 0 ]] && echo "ERROR:User Info Is Null,Please Excute:bash $0 -h" && exit 1 for info in $@ do local info_array=(${info//:/ }) if [[ ${#info_array[*]} -ne 2 ]];then echo "ERROR:${info} Format Error,Please Excute:bash $0 -h" && exit 1 fi local mysql_user=${info_array[0]} local mysql_pwd=${info_array[1]} echo "INFO:Begin Create Mysql User ${mysql_user}..." ${MYSQL_CMD} -e "create USER if NOT EXISTS '${mysql_user}'@'%' identified by '${mysql_pwd}';" done}#创建schemaCreate_Schema(){ [[ $# -eq 0 ]] && echo "ERROR:Schema Is Null,Please Excute:bash $0 -h" && exit 1 for mysql_schema in $@ do echo "INFO:Begin Create Mysql Schema ${mysql_schema}..." ${MYSQL_CMD} -e "create SCHEMA if NOT EXISTS ${mysql_schema} default character set utf8mb4 collate utf8mb4_bin;" done}#给用户授权Grant_User(){ [[ $# -eq 0 ]] && echo "ERROR:User Info Is Null,Please Excute:bash $0 -h" && exit 1 for info in $@ do local info_array=(${info//:/ }) if [[ ${#info_array[*]} -ne 2 ]];then echo "ERROR:${info} Format Error,Please Excute:bash $0 -h" && exit 1 fi local schema_name=${info_array[0]} local mysql_user=${info_array[1]} echo "INFO:Begin grant schema ${schema_name} to Mysql User ${mysql_user}..." ${MYSQL_CMD} -e "grant ALL on ${schema_name}.* to '${mysql_user}'@'%';" done}#导入sqlImport_Sql(){ [[ $# -ne 5 ]] && echo "ERROR:Invalid Param!!!,Please Excute:bash $0 -h" && exit 1 local mysql_user=$1 local mysql_pwd=$2 local mysql_schema=$3 local jfrog_user_info=$4 local sql_url=$5 curl -u ${jfrog_user_info} -O ${sql_url} local sql_name=$(echo ${sql_url##*/}) echo "INFO:Begin Import ${sql_name} Sql To Schema ${mysql_schema} On Mysql User ${mysql_user}..." mysql -h${MYSQL_IP} -P${MYSQL_PORT} -u${mysql_user} -p${mysql_pwd} ${mysql_schema} < ${sql_name}}#帮助文档Help(){ cat << EOFUsage: bash $0 IP 端口 Mysql管理员用户名 密码 -cWARNING: 当传入的位置参数使用冒号(:)分隔时,冒号两边的值不能包含冒号(:)=======================================================================optional arguments: -h 提供帮助信息 -c 进行创建的相关操作 user 创建Mysql用户,格式:-c user 用户1:密码1 用户2:密码2,如-c user user1:pwd1 user2:pwd2 schema 创建schema,格式:-c schema schema1 schema2 grant 授权,格式:-c grant schema名字1:mysql用户名1 schema名字1:mysql用户名1 import 导入sql,格式:-c import mysql用户名 密码 schema名字 jfrog用户名:密码 sql的urlEXAMPLE: bash $0 192.168.2.1 3306 root 123456-c user user1:pwd1 user2:pwd2EOF}######################主程序######################[[ "x$1" == "x-h" ]] && Help && exit 0[[ $# -le 5 ]] && echo "ERROR:Invalid Param!!!,Please Excute:bash $0 -h" && exit 1Check_Envif [[ "x$5" == "x-c" ]];then shift 5 case $1 in user) shift 1 Create_User $@;; schema) shift 1 Create_Schema $@;; grant) shift 1 Grant_User $@;; import) shift 1 Import_Sql $@;; *) echo "ERROR:Invalid Param!!!,Please Excute:bash $0 -h" && exit 1 esacelse echo "Help" && exit 1fi

-h" && exit 1 for mysql_schema in $@ do echo "INFO:Begin Create Mysql Schema ${mysql_schema}..." ${MYSQL_CMD} -e "create SCHEMA if NOT EXISTS ${mysql_schema} default character set utf8mb4 collate utf8mb4_bin;" done}#给用户授权Grant_User(){ [[ $# -eq 0 ]] && echo "ERROR:User Info Is Null,Please Excute:bash

#!/bin/bash< /dev/null;then echo "ERROR:Please Install Mysql Client" && exit 1 fi if ! echo ${MYSQL_IP} | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' &> /dev/null;then echo "ERROR:${MYSQL_IP}不合法!!!" && exit 1 fi if ! ${MYSQL_CMD} -e 'show databases;' &> /dev/null;then echo "ERROR:${MYSQL_IP} Database Access Fail" && exit 1 fi}#创建Mysql用户Create_User(){ [[ $# -eq 0 ]] && echo "ERROR:User Info Is Null,Please Excute:bash $0 -h" && exit 1 for info in $@ do local info_array=(${info//:/ }) if [[ ${#info_array[*]} -ne 2 ]];then echo "ERROR:${info} Format Error,Please Excute:bash $0 -h" && exit 1 fi local mysql_user=${info_array[0]} local mysql_pwd=${info_array[1]} echo "INFO:Begin Create Mysql User ${mysql_user}..." ${MYSQL_CMD} -e "create USER if NOT EXISTS '${mysql_user}'@'%' identified by '${mysql_pwd}';" done}#创建schemaCreate_Schema(){ [[ $# -eq 0 ]] && echo "ERROR:Schema Is Null,Please Excute:bash $0 -h" && exit 1 for mysql_schema in $@ do echo "INFO:Begin Create Mysql Schema ${mysql_schema}..." ${MYSQL_CMD} -e "create SCHEMA if NOT EXISTS ${mysql_schema} default character set utf8mb4 collate utf8mb4_bin;" done}#给用户授权Grant_User(){ [[ $# -eq 0 ]] && echo "ERROR:User Info Is Null,Please Excute:bash $0 -h" && exit 1 for info in $@ do local info_array=(${info//:/ }) if [[ ${#info_array[*]} -ne 2 ]];then echo "ERROR:${info} Format Error,Please Excute:bash $0 -h" && exit 1 fi local schema_name=${info_array[0]} local mysql_user=${info_array[1]} echo "INFO:Begin grant schema ${schema_name} to Mysql User ${mysql_user}..." ${MYSQL_CMD} -e "grant ALL on ${schema_name}.* to '${mysql_user}'@'%';" done}#导入sqlImport_Sql(){ [[ $# -ne 5 ]] && echo "ERROR:Invalid Param!!!,Please Excute:bash $0 -h" && exit 1 local mysql_user=$1 local mysql_pwd=$2 local mysql_schema=$3 local jfrog_user_info=$4 local sql_url=$5 curl -u ${jfrog_user_info} -O ${sql_url} local sql_name=$(echo ${sql_url##*/}) echo "INFO:Begin Import ${sql_name} Sql To Schema ${mysql_schema} On Mysql User ${mysql_user}..." mysql -h${MYSQL_IP} -P${MYSQL_PORT} -u${mysql_user} -p${mysql_pwd} ${mysql_schema} < ${sql_name}}#帮助文档Help(){ cat << EOFUsage: bash $0 IP 端口 Mysql管理员用户名 密码 -cWARNING: 当传入的位置参数使用冒号(:)分隔时,冒号两边的值不能包含冒号(:)=======================================================================optional arguments: -h 提供帮助信息 -c 进行创建的相关操作 user 创建Mysql用户,格式:-c user 用户1:密码1 用户2:密码2,如-c user user1:pwd1 user2:pwd2 schema 创建schema,格式:-c schema schema1 schema2 grant 授权,格式:-c grant schema名字1:mysql用户名1 schema名字1:mysql用户名1 import 导入sql,格式:-c import mysql用户名 密码 schema名字 jfrog用户名:密码 sql的urlEXAMPLE: bash $0 192.168.2.1 3306 root 123456-c user user1:pwd1 user2:pwd2EOF}######################主程序######################[[ "x$1" == "x-h" ]] && Help && exit 0[[ $# -le 5 ]] && echo "ERROR:Invalid Param!!!,Please Excute:bash $0 -h" && exit 1Check_Envif [[ "x$5" == "x-c" ]];then shift 5 case $1 in user) shift 1 Create_User $@;; schema) shift 1 Create_Schema $@;; grant) shift 1 Grant_User $@;; import) shift 1 Import_Sql $@;; *) echo "ERROR:Invalid Param!!!,Please Excute:bash $0 -h" && exit 1 esacelse echo "Help" && exit 1fi

-h" && exit 1 for info in $@ do local info_array=(${info//:/ }) if [[ ${#info_array[*]} -ne 2 ]];then echo "ERROR:${info} Format Error,Please Excute:bash

#!/bin/bash< /dev/null;then echo "ERROR:Please Install Mysql Client" && exit 1 fi if ! echo ${MYSQL_IP} | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' &> /dev/null;then echo "ERROR:${MYSQL_IP}不合法!!!" && exit 1 fi if ! ${MYSQL_CMD} -e 'show databases;' &> /dev/null;then echo "ERROR:${MYSQL_IP} Database Access Fail" && exit 1 fi}#创建Mysql用户Create_User(){ [[ $# -eq 0 ]] && echo "ERROR:User Info Is Null,Please Excute:bash $0 -h" && exit 1 for info in $@ do local info_array=(${info//:/ }) if [[ ${#info_array[*]} -ne 2 ]];then echo "ERROR:${info} Format Error,Please Excute:bash $0 -h" && exit 1 fi local mysql_user=${info_array[0]} local mysql_pwd=${info_array[1]} echo "INFO:Begin Create Mysql User ${mysql_user}..." ${MYSQL_CMD} -e "create USER if NOT EXISTS '${mysql_user}'@'%' identified by '${mysql_pwd}';" done}#创建schemaCreate_Schema(){ [[ $# -eq 0 ]] && echo "ERROR:Schema Is Null,Please Excute:bash $0 -h" && exit 1 for mysql_schema in $@ do echo "INFO:Begin Create Mysql Schema ${mysql_schema}..." ${MYSQL_CMD} -e "create SCHEMA if NOT EXISTS ${mysql_schema} default character set utf8mb4 collate utf8mb4_bin;" done}#给用户授权Grant_User(){ [[ $# -eq 0 ]] && echo "ERROR:User Info Is Null,Please Excute:bash $0 -h" && exit 1 for info in $@ do local info_array=(${info//:/ }) if [[ ${#info_array[*]} -ne 2 ]];then echo "ERROR:${info} Format Error,Please Excute:bash $0 -h" && exit 1 fi local schema_name=${info_array[0]} local mysql_user=${info_array[1]} echo "INFO:Begin grant schema ${schema_name} to Mysql User ${mysql_user}..." ${MYSQL_CMD} -e "grant ALL on ${schema_name}.* to '${mysql_user}'@'%';" done}#导入sqlImport_Sql(){ [[ $# -ne 5 ]] && echo "ERROR:Invalid Param!!!,Please Excute:bash $0 -h" && exit 1 local mysql_user=$1 local mysql_pwd=$2 local mysql_schema=$3 local jfrog_user_info=$4 local sql_url=$5 curl -u ${jfrog_user_info} -O ${sql_url} local sql_name=$(echo ${sql_url##*/}) echo "INFO:Begin Import ${sql_name} Sql To Schema ${mysql_schema} On Mysql User ${mysql_user}..." mysql -h${MYSQL_IP} -P${MYSQL_PORT} -u${mysql_user} -p${mysql_pwd} ${mysql_schema} < ${sql_name}}#帮助文档Help(){ cat << EOFUsage: bash $0 IP 端口 Mysql管理员用户名 密码 -cWARNING: 当传入的位置参数使用冒号(:)分隔时,冒号两边的值不能包含冒号(:)=======================================================================optional arguments: -h 提供帮助信息 -c 进行创建的相关操作 user 创建Mysql用户,格式:-c user 用户1:密码1 用户2:密码2,如-c user user1:pwd1 user2:pwd2 schema 创建schema,格式:-c schema schema1 schema2 grant 授权,格式:-c grant schema名字1:mysql用户名1 schema名字1:mysql用户名1 import 导入sql,格式:-c import mysql用户名 密码 schema名字 jfrog用户名:密码 sql的urlEXAMPLE: bash $0 192.168.2.1 3306 root 123456-c user user1:pwd1 user2:pwd2EOF}######################主程序######################[[ "x$1" == "x-h" ]] && Help && exit 0[[ $# -le 5 ]] && echo "ERROR:Invalid Param!!!,Please Excute:bash $0 -h" && exit 1Check_Envif [[ "x$5" == "x-c" ]];then shift 5 case $1 in user) shift 1 Create_User $@;; schema) shift 1 Create_Schema $@;; grant) shift 1 Grant_User $@;; import) shift 1 Import_Sql $@;; *) echo "ERROR:Invalid Param!!!,Please Excute:bash $0 -h" && exit 1 esacelse echo "Help" && exit 1fi

-h" && exit 1 fi local schema_name=${info_array[0]} local mysql_user=${info_array[1]} echo "INFO:Begin grant schema ${schema_name} to Mysql User ${mysql_user}..." ${MYSQL_CMD} -e "grant ALL on ${schema_name}.* to '${mysql_user}'@'%';" done}#导入sqlImport_Sql(){ [[ $# -ne 5 ]] && echo "ERROR:Invalid Param!!!,Please Excute:bash

#!/bin/bash< /dev/null;then echo "ERROR:Please Install Mysql Client" && exit 1 fi if ! echo ${MYSQL_IP} | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' &> /dev/null;then echo "ERROR:${MYSQL_IP}不合法!!!" && exit 1 fi if ! ${MYSQL_CMD} -e 'show databases;' &> /dev/null;then echo "ERROR:${MYSQL_IP} Database Access Fail" && exit 1 fi}#创建Mysql用户Create_User(){ [[ $# -eq 0 ]] && echo "ERROR:User Info Is Null,Please Excute:bash $0 -h" && exit 1 for info in $@ do local info_array=(${info//:/ }) if [[ ${#info_array[*]} -ne 2 ]];then echo "ERROR:${info} Format Error,Please Excute:bash $0 -h" && exit 1 fi local mysql_user=${info_array[0]} local mysql_pwd=${info_array[1]} echo "INFO:Begin Create Mysql User ${mysql_user}..." ${MYSQL_CMD} -e "create USER if NOT EXISTS '${mysql_user}'@'%' identified by '${mysql_pwd}';" done}#创建schemaCreate_Schema(){ [[ $# -eq 0 ]] && echo "ERROR:Schema Is Null,Please Excute:bash $0 -h" && exit 1 for mysql_schema in $@ do echo "INFO:Begin Create Mysql Schema ${mysql_schema}..." ${MYSQL_CMD} -e "create SCHEMA if NOT EXISTS ${mysql_schema} default character set utf8mb4 collate utf8mb4_bin;" done}#给用户授权Grant_User(){ [[ $# -eq 0 ]] && echo "ERROR:User Info Is Null,Please Excute:bash $0 -h" && exit 1 for info in $@ do local info_array=(${info//:/ }) if [[ ${#info_array[*]} -ne 2 ]];then echo "ERROR:${info} Format Error,Please Excute:bash $0 -h" && exit 1 fi local schema_name=${info_array[0]} local mysql_user=${info_array[1]} echo "INFO:Begin grant schema ${schema_name} to Mysql User ${mysql_user}..." ${MYSQL_CMD} -e "grant ALL on ${schema_name}.* to '${mysql_user}'@'%';" done}#导入sqlImport_Sql(){ [[ $# -ne 5 ]] && echo "ERROR:Invalid Param!!!,Please Excute:bash $0 -h" && exit 1 local mysql_user=$1 local mysql_pwd=$2 local mysql_schema=$3 local jfrog_user_info=$4 local sql_url=$5 curl -u ${jfrog_user_info} -O ${sql_url} local sql_name=$(echo ${sql_url##*/}) echo "INFO:Begin Import ${sql_name} Sql To Schema ${mysql_schema} On Mysql User ${mysql_user}..." mysql -h${MYSQL_IP} -P${MYSQL_PORT} -u${mysql_user} -p${mysql_pwd} ${mysql_schema} < ${sql_name}}#帮助文档Help(){ cat << EOFUsage: bash $0 IP 端口 Mysql管理员用户名 密码 -cWARNING: 当传入的位置参数使用冒号(:)分隔时,冒号两边的值不能包含冒号(:)=======================================================================optional arguments: -h 提供帮助信息 -c 进行创建的相关操作 user 创建Mysql用户,格式:-c user 用户1:密码1 用户2:密码2,如-c user user1:pwd1 user2:pwd2 schema 创建schema,格式:-c schema schema1 schema2 grant 授权,格式:-c grant schema名字1:mysql用户名1 schema名字1:mysql用户名1 import 导入sql,格式:-c import mysql用户名 密码 schema名字 jfrog用户名:密码 sql的urlEXAMPLE: bash $0 192.168.2.1 3306 root 123456-c user user1:pwd1 user2:pwd2EOF}######################主程序######################[[ "x$1" == "x-h" ]] && Help && exit 0[[ $# -le 5 ]] && echo "ERROR:Invalid Param!!!,Please Excute:bash $0 -h" && exit 1Check_Envif [[ "x$5" == "x-c" ]];then shift 5 case $1 in user) shift 1 Create_User $@;; schema) shift 1 Create_Schema $@;; grant) shift 1 Grant_User $@;; import) shift 1 Import_Sql $@;; *) echo "ERROR:Invalid Param!!!,Please Excute:bash $0 -h" && exit 1 esacelse echo "Help" && exit 1fi

-h" && exit 1 local mysql_user= local mysql_pwd= local mysql_schema= local jfrog_user_info= local sql_url= curl -u ${jfrog_user_info} -O ${sql_url} local sql_name=$(echo ${sql_url##*/}) echo "INFO:Begin Import ${sql_name} Sql To Schema ${mysql_schema} On Mysql User ${mysql_user}..." mysql -h${MYSQL_IP} -P${MYSQL_PORT} -u${mysql_user} -p${mysql_pwd} ${mysql_schema} < ${sql_name}}#帮助文档Help(){ cat << EOFUsage: bash

#!/bin/bash< /dev/null;then echo "ERROR:Please Install Mysql Client" && exit 1 fi if ! echo ${MYSQL_IP} | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' &> /dev/null;then echo "ERROR:${MYSQL_IP}不合法!!!" && exit 1 fi if ! ${MYSQL_CMD} -e 'show databases;' &> /dev/null;then echo "ERROR:${MYSQL_IP} Database Access Fail" && exit 1 fi}#创建Mysql用户Create_User(){ [[ $# -eq 0 ]] && echo "ERROR:User Info Is Null,Please Excute:bash $0 -h" && exit 1 for info in $@ do local info_array=(${info//:/ }) if [[ ${#info_array[*]} -ne 2 ]];then echo "ERROR:${info} Format Error,Please Excute:bash $0 -h" && exit 1 fi local mysql_user=${info_array[0]} local mysql_pwd=${info_array[1]} echo "INFO:Begin Create Mysql User ${mysql_user}..." ${MYSQL_CMD} -e "create USER if NOT EXISTS '${mysql_user}'@'%' identified by '${mysql_pwd}';" done}#创建schemaCreate_Schema(){ [[ $# -eq 0 ]] && echo "ERROR:Schema Is Null,Please Excute:bash $0 -h" && exit 1 for mysql_schema in $@ do echo "INFO:Begin Create Mysql Schema ${mysql_schema}..." ${MYSQL_CMD} -e "create SCHEMA if NOT EXISTS ${mysql_schema} default character set utf8mb4 collate utf8mb4_bin;" done}#给用户授权Grant_User(){ [[ $# -eq 0 ]] && echo "ERROR:User Info Is Null,Please Excute:bash $0 -h" && exit 1 for info in $@ do local info_array=(${info//:/ }) if [[ ${#info_array[*]} -ne 2 ]];then echo "ERROR:${info} Format Error,Please Excute:bash $0 -h" && exit 1 fi local schema_name=${info_array[0]} local mysql_user=${info_array[1]} echo "INFO:Begin grant schema ${schema_name} to Mysql User ${mysql_user}..." ${MYSQL_CMD} -e "grant ALL on ${schema_name}.* to '${mysql_user}'@'%';" done}#导入sqlImport_Sql(){ [[ $# -ne 5 ]] && echo "ERROR:Invalid Param!!!,Please Excute:bash $0 -h" && exit 1 local mysql_user=$1 local mysql_pwd=$2 local mysql_schema=$3 local jfrog_user_info=$4 local sql_url=$5 curl -u ${jfrog_user_info} -O ${sql_url} local sql_name=$(echo ${sql_url##*/}) echo "INFO:Begin Import ${sql_name} Sql To Schema ${mysql_schema} On Mysql User ${mysql_user}..." mysql -h${MYSQL_IP} -P${MYSQL_PORT} -u${mysql_user} -p${mysql_pwd} ${mysql_schema} < ${sql_name}}#帮助文档Help(){ cat << EOFUsage: bash $0 IP 端口 Mysql管理员用户名 密码 -cWARNING: 当传入的位置参数使用冒号(:)分隔时,冒号两边的值不能包含冒号(:)=======================================================================optional arguments: -h 提供帮助信息 -c 进行创建的相关操作 user 创建Mysql用户,格式:-c user 用户1:密码1 用户2:密码2,如-c user user1:pwd1 user2:pwd2 schema 创建schema,格式:-c schema schema1 schema2 grant 授权,格式:-c grant schema名字1:mysql用户名1 schema名字1:mysql用户名1 import 导入sql,格式:-c import mysql用户名 密码 schema名字 jfrog用户名:密码 sql的urlEXAMPLE: bash $0 192.168.2.1 3306 root 123456-c user user1:pwd1 user2:pwd2EOF}######################主程序######################[[ "x$1" == "x-h" ]] && Help && exit 0[[ $# -le 5 ]] && echo "ERROR:Invalid Param!!!,Please Excute:bash $0 -h" && exit 1Check_Envif [[ "x$5" == "x-c" ]];then shift 5 case $1 in user) shift 1 Create_User $@;; schema) shift 1 Create_Schema $@;; grant) shift 1 Grant_User $@;; import) shift 1 Import_Sql $@;; *) echo "ERROR:Invalid Param!!!,Please Excute:bash $0 -h" && exit 1 esacelse echo "Help" && exit 1fi

IP 端口 Mysql管理员用户名 密码 -cWARNING: 当传入的位置参数使用冒号(:)分隔时,冒号两边的值不能包含冒号(:)=======================================================================optional arguments: -h 提供帮助信息 -c 进行创建的相关操作 user 创建Mysql用户,格式:-c user 用户1:密码1 用户2:密码2,如-c user user1:pwd1 user2:pwd2 schema 创建schema,格式:-c schema schema1 schema2 grant 授权,格式:-c grant schema名字1:mysql用户名1 schema名字1:mysql用户名1 import 导入sql,格式:-c import mysql用户名 密码 schema名字 jfrog用户名:密码 sql的urlEXAMPLE: bash

#!/bin/bash< /dev/null;then echo "ERROR:Please Install Mysql Client" && exit 1 fi if ! echo ${MYSQL_IP} | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' &> /dev/null;then echo "ERROR:${MYSQL_IP}不合法!!!" && exit 1 fi if ! ${MYSQL_CMD} -e 'show databases;' &> /dev/null;then echo "ERROR:${MYSQL_IP} Database Access Fail" && exit 1 fi}#创建Mysql用户Create_User(){ [[ $# -eq 0 ]] && echo "ERROR:User Info Is Null,Please Excute:bash $0 -h" && exit 1 for info in $@ do local info_array=(${info//:/ }) if [[ ${#info_array[*]} -ne 2 ]];then echo "ERROR:${info} Format Error,Please Excute:bash $0 -h" && exit 1 fi local mysql_user=${info_array[0]} local mysql_pwd=${info_array[1]} echo "INFO:Begin Create Mysql User ${mysql_user}..." ${MYSQL_CMD} -e "create USER if NOT EXISTS '${mysql_user}'@'%' identified by '${mysql_pwd}';" done}#创建schemaCreate_Schema(){ [[ $# -eq 0 ]] && echo "ERROR:Schema Is Null,Please Excute:bash $0 -h" && exit 1 for mysql_schema in $@ do echo "INFO:Begin Create Mysql Schema ${mysql_schema}..." ${MYSQL_CMD} -e "create SCHEMA if NOT EXISTS ${mysql_schema} default character set utf8mb4 collate utf8mb4_bin;" done}#给用户授权Grant_User(){ [[ $# -eq 0 ]] && echo "ERROR:User Info Is Null,Please Excute:bash $0 -h" && exit 1 for info in $@ do local info_array=(${info//:/ }) if [[ ${#info_array[*]} -ne 2 ]];then echo "ERROR:${info} Format Error,Please Excute:bash $0 -h" && exit 1 fi local schema_name=${info_array[0]} local mysql_user=${info_array[1]} echo "INFO:Begin grant schema ${schema_name} to Mysql User ${mysql_user}..." ${MYSQL_CMD} -e "grant ALL on ${schema_name}.* to '${mysql_user}'@'%';" done}#导入sqlImport_Sql(){ [[ $# -ne 5 ]] && echo "ERROR:Invalid Param!!!,Please Excute:bash $0 -h" && exit 1 local mysql_user=$1 local mysql_pwd=$2 local mysql_schema=$3 local jfrog_user_info=$4 local sql_url=$5 curl -u ${jfrog_user_info} -O ${sql_url} local sql_name=$(echo ${sql_url##*/}) echo "INFO:Begin Import ${sql_name} Sql To Schema ${mysql_schema} On Mysql User ${mysql_user}..." mysql -h${MYSQL_IP} -P${MYSQL_PORT} -u${mysql_user} -p${mysql_pwd} ${mysql_schema} < ${sql_name}}#帮助文档Help(){ cat << EOFUsage: bash $0 IP 端口 Mysql管理员用户名 密码 -cWARNING: 当传入的位置参数使用冒号(:)分隔时,冒号两边的值不能包含冒号(:)=======================================================================optional arguments: -h 提供帮助信息 -c 进行创建的相关操作 user 创建Mysql用户,格式:-c user 用户1:密码1 用户2:密码2,如-c user user1:pwd1 user2:pwd2 schema 创建schema,格式:-c schema schema1 schema2 grant 授权,格式:-c grant schema名字1:mysql用户名1 schema名字1:mysql用户名1 import 导入sql,格式:-c import mysql用户名 密码 schema名字 jfrog用户名:密码 sql的urlEXAMPLE: bash $0 192.168.2.1 3306 root 123456-c user user1:pwd1 user2:pwd2EOF}######################主程序######################[[ "x$1" == "x-h" ]] && Help && exit 0[[ $# -le 5 ]] && echo "ERROR:Invalid Param!!!,Please Excute:bash $0 -h" && exit 1Check_Envif [[ "x$5" == "x-c" ]];then shift 5 case $1 in user) shift 1 Create_User $@;; schema) shift 1 Create_Schema $@;; grant) shift 1 Grant_User $@;; import) shift 1 Import_Sql $@;; *) echo "ERROR:Invalid Param!!!,Please Excute:bash $0 -h" && exit 1 esacelse echo "Help" && exit 1fi

192.168.2.1 3306 root 123456-c user user1:pwd1 user2:pwd2EOF}######################主程序######################[[ "x" == "x-h" ]] && Help && exit 0[[ $# -le 5 ]] && echo "ERROR:Invalid Param!!!,Please Excute:bash

#!/bin/bash< /dev/null;then echo "ERROR:Please Install Mysql Client" && exit 1 fi if ! echo ${MYSQL_IP} | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' &> /dev/null;then echo "ERROR:${MYSQL_IP}不合法!!!" && exit 1 fi if ! ${MYSQL_CMD} -e 'show databases;' &> /dev/null;then echo "ERROR:${MYSQL_IP} Database Access Fail" && exit 1 fi}#创建Mysql用户Create_User(){ [[ $# -eq 0 ]] && echo "ERROR:User Info Is Null,Please Excute:bash $0 -h" && exit 1 for info in $@ do local info_array=(${info//:/ }) if [[ ${#info_array[*]} -ne 2 ]];then echo "ERROR:${info} Format Error,Please Excute:bash $0 -h" && exit 1 fi local mysql_user=${info_array[0]} local mysql_pwd=${info_array[1]} echo "INFO:Begin Create Mysql User ${mysql_user}..." ${MYSQL_CMD} -e "create USER if NOT EXISTS '${mysql_user}'@'%' identified by '${mysql_pwd}';" done}#创建schemaCreate_Schema(){ [[ $# -eq 0 ]] && echo "ERROR:Schema Is Null,Please Excute:bash $0 -h" && exit 1 for mysql_schema in $@ do echo "INFO:Begin Create Mysql Schema ${mysql_schema}..." ${MYSQL_CMD} -e "create SCHEMA if NOT EXISTS ${mysql_schema} default character set utf8mb4 collate utf8mb4_bin;" done}#给用户授权Grant_User(){ [[ $# -eq 0 ]] && echo "ERROR:User Info Is Null,Please Excute:bash $0 -h" && exit 1 for info in $@ do local info_array=(${info//:/ }) if [[ ${#info_array[*]} -ne 2 ]];then echo "ERROR:${info} Format Error,Please Excute:bash $0 -h" && exit 1 fi local schema_name=${info_array[0]} local mysql_user=${info_array[1]} echo "INFO:Begin grant schema ${schema_name} to Mysql User ${mysql_user}..." ${MYSQL_CMD} -e "grant ALL on ${schema_name}.* to '${mysql_user}'@'%';" done}#导入sqlImport_Sql(){ [[ $# -ne 5 ]] && echo "ERROR:Invalid Param!!!,Please Excute:bash $0 -h" && exit 1 local mysql_user=$1 local mysql_pwd=$2 local mysql_schema=$3 local jfrog_user_info=$4 local sql_url=$5 curl -u ${jfrog_user_info} -O ${sql_url} local sql_name=$(echo ${sql_url##*/}) echo "INFO:Begin Import ${sql_name} Sql To Schema ${mysql_schema} On Mysql User ${mysql_user}..." mysql -h${MYSQL_IP} -P${MYSQL_PORT} -u${mysql_user} -p${mysql_pwd} ${mysql_schema} < ${sql_name}}#帮助文档Help(){ cat << EOFUsage: bash $0 IP 端口 Mysql管理员用户名 密码 -cWARNING: 当传入的位置参数使用冒号(:)分隔时,冒号两边的值不能包含冒号(:)=======================================================================optional arguments: -h 提供帮助信息 -c 进行创建的相关操作 user 创建Mysql用户,格式:-c user 用户1:密码1 用户2:密码2,如-c user user1:pwd1 user2:pwd2 schema 创建schema,格式:-c schema schema1 schema2 grant 授权,格式:-c grant schema名字1:mysql用户名1 schema名字1:mysql用户名1 import 导入sql,格式:-c import mysql用户名 密码 schema名字 jfrog用户名:密码 sql的urlEXAMPLE: bash $0 192.168.2.1 3306 root 123456-c user user1:pwd1 user2:pwd2EOF}######################主程序######################[[ "x$1" == "x-h" ]] && Help && exit 0[[ $# -le 5 ]] && echo "ERROR:Invalid Param!!!,Please Excute:bash $0 -h" && exit 1Check_Envif [[ "x$5" == "x-c" ]];then shift 5 case $1 in user) shift 1 Create_User $@;; schema) shift 1 Create_Schema $@;; grant) shift 1 Grant_User $@;; import) shift 1 Import_Sql $@;; *) echo "ERROR:Invalid Param!!!,Please Excute:bash $0 -h" && exit 1 esacelse echo "Help" && exit 1fi

-h" && exit 1Check_Envif [[ "x" == "x-c" ]];then shift 5 case in user) shift 1 Create_User $@;; schema) shift 1 Create_Schema $@;; grant) shift 1 Grant_User $@;; import) shift 1 Import_Sql $@;; *) echo "ERROR:Invalid Param!!!,Please Excute:bash

#!/bin/bash< /dev/null;then echo "ERROR:Please Install Mysql Client" && exit 1 fi if ! echo ${MYSQL_IP} | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' &> /dev/null;then echo "ERROR:${MYSQL_IP}不合法!!!" && exit 1 fi if ! ${MYSQL_CMD} -e 'show databases;' &> /dev/null;then echo "ERROR:${MYSQL_IP} Database Access Fail" && exit 1 fi}#创建Mysql用户Create_User(){ [[ $# -eq 0 ]] && echo "ERROR:User Info Is Null,Please Excute:bash $0 -h" && exit 1 for info in $@ do local info_array=(${info//:/ }) if [[ ${#info_array[*]} -ne 2 ]];then echo "ERROR:${info} Format Error,Please Excute:bash $0 -h" && exit 1 fi local mysql_user=${info_array[0]} local mysql_pwd=${info_array[1]} echo "INFO:Begin Create Mysql User ${mysql_user}..." ${MYSQL_CMD} -e "create USER if NOT EXISTS '${mysql_user}'@'%' identified by '${mysql_pwd}';" done}#创建schemaCreate_Schema(){ [[ $# -eq 0 ]] && echo "ERROR:Schema Is Null,Please Excute:bash $0 -h" && exit 1 for mysql_schema in $@ do echo "INFO:Begin Create Mysql Schema ${mysql_schema}..." ${MYSQL_CMD} -e "create SCHEMA if NOT EXISTS ${mysql_schema} default character set utf8mb4 collate utf8mb4_bin;" done}#给用户授权Grant_User(){ [[ $# -eq 0 ]] && echo "ERROR:User Info Is Null,Please Excute:bash $0 -h" && exit 1 for info in $@ do local info_array=(${info//:/ }) if [[ ${#info_array[*]} -ne 2 ]];then echo "ERROR:${info} Format Error,Please Excute:bash $0 -h" && exit 1 fi local schema_name=${info_array[0]} local mysql_user=${info_array[1]} echo "INFO:Begin grant schema ${schema_name} to Mysql User ${mysql_user}..." ${MYSQL_CMD} -e "grant ALL on ${schema_name}.* to '${mysql_user}'@'%';" done}#导入sqlImport_Sql(){ [[ $# -ne 5 ]] && echo "ERROR:Invalid Param!!!,Please Excute:bash $0 -h" && exit 1 local mysql_user=$1 local mysql_pwd=$2 local mysql_schema=$3 local jfrog_user_info=$4 local sql_url=$5 curl -u ${jfrog_user_info} -O ${sql_url} local sql_name=$(echo ${sql_url##*/}) echo "INFO:Begin Import ${sql_name} Sql To Schema ${mysql_schema} On Mysql User ${mysql_user}..." mysql -h${MYSQL_IP} -P${MYSQL_PORT} -u${mysql_user} -p${mysql_pwd} ${mysql_schema} < ${sql_name}}#帮助文档Help(){ cat << EOFUsage: bash $0 IP 端口 Mysql管理员用户名 密码 -cWARNING: 当传入的位置参数使用冒号(:)分隔时,冒号两边的值不能包含冒号(:)=======================================================================optional arguments: -h 提供帮助信息 -c 进行创建的相关操作 user 创建Mysql用户,格式:-c user 用户1:密码1 用户2:密码2,如-c user user1:pwd1 user2:pwd2 schema 创建schema,格式:-c schema schema1 schema2 grant 授权,格式:-c grant schema名字1:mysql用户名1 schema名字1:mysql用户名1 import 导入sql,格式:-c import mysql用户名 密码 schema名字 jfrog用户名:密码 sql的urlEXAMPLE: bash $0 192.168.2.1 3306 root 123456-c user user1:pwd1 user2:pwd2EOF}######################主程序######################[[ "x$1" == "x-h" ]] && Help && exit 0[[ $# -le 5 ]] && echo "ERROR:Invalid Param!!!,Please Excute:bash $0 -h" && exit 1Check_Envif [[ "x$5" == "x-c" ]];then shift 5 case $1 in user) shift 1 Create_User $@;; schema) shift 1 Create_Schema $@;; grant) shift 1 Grant_User $@;; import) shift 1 Import_Sql $@;; *) echo "ERROR:Invalid Param!!!,Please Excute:bash $0 -h" && exit 1 esacelse echo "Help" && exit 1fi

-h" && exit 1 esacelse echo "Help" && exit 1fi

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:printf命令详解
下一篇:Mysql命令大全
相关文章

 发表评论

暂时没有评论,来抢沙发吧~