#!/bin/bash
<<INFO
AUTHOR:anqixiang
DATE:2021-06-24
DESCRIBE:初始化Mysql,如创建用户、schema、授权、导入sql
SYSTEM:CentOS7/RedHat7
WARNING:
MODIFY:
INFO
set -e
MYSQL_IP=$1
MYSQL_PORT=$2
MYSQL_ADMIN_USER=$3
MYSQL_ADMIN_PWD=$4
MYSQL_CMD="mysql -h -P -u -p"
Check_Env(){
echo "INFO:Begin Check Env..."
if ! command -v mysql &> /dev/null;then
echo "ERROR:Please Install Mysql Client" && exit 1
fi
if ! echo | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' &> /dev/null;then
echo "ERROR:不合法!!!" && exit 1
fi
if ! -e 'show databases;' &> /dev/null;then
echo "ERROR: Database Access Fail" && exit 1
fi
}
Create_User(){
[[ $
for info in $@
do
local info_array=()
if [[ -ne 2 ]];then
echo "ERROR: Format Error,Please Excute:bash $0 -h" && exit 1
fi
local mysql_user=
local mysql_pwd=
echo "INFO:Begin Create Mysql User ..."
-e "create USER if NOT EXISTS ''@'%' identified by '';"
done
}
Create_Schema(){
[[ $
for mysql_schema in $@
do
echo "INFO:Begin Create Mysql Schema ..."
-e "create SCHEMA if NOT EXISTS default character set utf8mb4 collate utf8mb4_bin;"
done
}
Grant_User(){
[[ $
for info in $@
do
local info_array=()
if [[ -ne 2 ]];then
echo "ERROR: Format Error,Please Excute:bash $0 -h" && exit 1
fi
local schema_name=
local mysql_user=
echo "INFO:Begin grant schema to Mysql User ..."
-e "grant ALL on .* to ''@'%';"
done
}
Import_Sql(){
[[ $
local mysql_user=$1
local mysql_pwd=$2
local mysql_schema=$3
local jfrog_user_info=$4
local sql_url=$5
curl -u -O
local sql_name=$(echo ${sql_url##*/})
echo "INFO:Begin Import Sql To Schema On Mysql User ..."
mysql -h -P -u -p <
}
Help(){
cat << EOF
Usage:
bash $0 IP 端口 Mysql管理员用户名 密码 -c
WARNING:
当传入的位置参数使用冒号(:)分隔时,冒号两边的值不能包含冒号(:)
=======================================================================
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的url
EXAMPLE:
bash $0 192.168.2.1 3306 root 123456-c user user1:pwd1 user2:pwd2
EOF
}
[[ "x$1" == "x-h" ]] && Help && exit 0
[[ $
Check_Env
if [[ "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
esac
else
echo "Help" && exit 1
fi