简体   繁体   English

创建一个bash脚本以自动创建数据库和用户

[英]creating a bash script to create database and user automatically

Hi I am trying to create a bash script, which take username and database name and password as in arguments and create a database. 嗨,我正在尝试创建一个bash脚本,该脚本将用户名,数据库名称和密码作为参数,并创建一个数据库。

mysql -u xxx -pxxxxxx  << EOF
CREATE DATABASE '$2';
CREATE USER '$2'@'localhost' IDENTIFIED BY '$3';
GRANT ALL PRIVILEGES ON '$2'.* TO '$2'@'localhost';
EOF

I am getting mysql 1064 error while executing this command. 执行此命令时出现mysql 1064错误。

Any help will be appreciated. 任何帮助将不胜感激。

Thanks 谢谢

 mysql -u $user -p$pass  << EOF
CREATE DATABASE $2;
GRANT ALL PRIVILEGES ON $2.* TO $2@'localhost' IDENTIFIED BY '$3';
 EOF

The answer here did not work for me (got a number of syntax errors, including the same 1064 error as the OP). 这里的答案对我不起作用(得到了许多语法错误,包括与OP相同的1064错误)。 Posting this in case it helps someone else: 发布此信息以防其他人:

#!/bin/bash

echo "mysql root password:"
read rootpass

if [[ $rootpass ]]; then
    # create db
    # create user
    mysql -uroot -p$rootpass <<EOF
CREATE DATABASE \`${1}\`;
CREATE USER $1@localhost IDENTIFIED BY '$1';
GRANT ALL PRIVILEGES ON \`${1}\`.* TO \`${1}\`@'localhost';
EOF

fi

Seems like some statements required quotes, some escaped, some none at all! 好像有些语句需要引号,有些是转义的,有些则根本没有! :D :d

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM