linux 创建mysql用户以及同名数据库并授权的shell脚本

2023-08-12 20:50:25
744
#!/bin/bash

username="wp_$1"
password=$2

mysql -uroot -e "CREATE USER '${username}'@'%' IDENTIFIED BY '${password}'"

mysql -uroot -e "CREATE DATABASE IF NOT EXISTS ${username}"

mysql -uroot -e "GRANT ALL PRIVILEGES ON ${username}.* TO ${username}@'%'"

mysql -uroot -e "FLUSH PRIVILEGES"

 

以上命令中省略了 -p部分。为了安全在mysql配置文件中[client]下配置user和password即可。