Linux-新建用户名还有数据库脚本

export LANG=zh_CN.UTF-8

#判断数据库账号密码是否正确
password()
{
    database_password=""
    database_username=root

    while [ "${database_password}" = "" ]
    do
    read -p "请输入数据库root密码:" database_password
    done

    error
}

error()
{
    info=(`mysql -u $database_username -p$database_password -e "select now() as time;"`)
    if [ "$info" = "" ];then
    echo "密码错误"
    password
    fi
}

password

while [ "${database_name}" = "" ]
do
read -p "请输入数据库名:(例如:testbase)" database_name
done

#IP
#获取ip,设置全局ip,IP
while [[ "$IP" = '' ]]
do

IP=127.0.0.1

if [ "$IP" = "" ];then
read -p "获取服务器ip失败,请输入:" IP
IP=${IP}
fi
#判断ip
read -p "服务器的ip是:$IP,如果不对请重新输入:" new_ip
if [ ! "${new_ip}" ];then
IP=${IP}
else
IP=${new_ip}
fi

done


#添加用户、数据库、设置密码
database_pwd=`head -c 500 /dev/urandom | tr -dc a-z0-9A-Z | head -c 10`
echo "创建数据库..."
#mysql -u root -p$database_password -e "insert into mysql.user(Host,User,Password) values('localhost','${database_name}',password('$database_pwd')); flush privileges; create database ${database_name} DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ; grant all privileges on ${database_name}.* to ${database_name}@localhost identified by '$database_pwd'; flush privileges;"

mysql -u root -p$database_password -e "CREATE USER '${database_name}'@'localhost' IDENTIFIED BY '$database_pwd';GRANT USAGE ON *.* TO '${database_name}'@'localhost' WITH MAX_USER_CONNECTIONS 0 MAX_QUERIES_PER_HOUR 10000;CREATE DATABASE IF NOT EXISTS \`${database_name}\`;GRANT ALL PRIVILEGES ON \`${database_name}\`.* TO '${database_name}'@'localhost';GRANT ALL PRIVILEGES ON ${database_name}.* TO '${database_name}'@'localhost';GRANT ALL PRIVILEGES ON \`${database_name}\_%\`.* TO '${database_name}'@'localhost';flush privileges;"

mkdir -p ./db
touch ./db/$database_name
cat >>./db/$database_name<<EOF
"##################################################################"
"phpMyAdmin:http://$IP/phpMyAdmin-4.4.15.5-all-languages/"
"数据库地址:localhost"
"数据库名:"${database_name}
"数据库用户名称:"${database_name}
"数据库密码:$database_pwd"
"##################################################################"
EOF

tail -n 7 ./db/$database_name