如何在 bash 脚本中授予 MySQL 权限?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7528967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to grant MySQL privileges in a bash script?
提问by Dunaril
I need to grant privileges to a database from within a bash script. Some parameters are in the form of variables, say, the username and the password. The command in a mysql shell would look like this:
我需要从 bash 脚本中向数据库授予权限。一些参数采用变量的形式,例如用户名和密码。mysql shell 中的命令如下所示:
GRANT ALL ON *.* TO "$user"@localhost IDENTIFIED BY "$password";
...Except that $userand $passwordwould be replaced by their values.
......除了$user和$password将它们的值来代替。
Is there a way to perform such a command from within a bash script?
有没有办法从 bash 脚本中执行这样的命令?
Thank you!
谢谢!
回答by Jauzsika
There you go :)
你去吧:)
#!/bin/bash
MYSQL=`which mysql`
EXPECTED_ARGS=3
Q1="USE ;"
Q2="GRANT ALL ON *.* TO ''@'localhost' IDENTIFIED BY '';"
Q3="FLUSH PRIVILEGES;"
SQL="${Q1}${Q2}${Q3}"
if [ $# -ne $EXPECTED_ARGS ]
then
echo "Usage: cat /etc/psa/.psa.shadow
dbname dbuser dbpass"
exit $E_BADARGS
fi
$MYSQL -uroot -p -e "$SQL"
回答by Agusjar
If we don′t know the password we can get it with:
如果我们不知道密码,我们可以通过以下方式获取:
mysql -uadmin -p`cat /etc/psa/.psa.shadow`
So we can get into mysql without prompt password like:
所以我们可以在没有提示密码的情况下进入mysql,例如:
##代码##
