通过 linux 命令行创建 mysql 用户?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9263570/
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
Creating mysql users via linux command line?
提问by Lostsoul
I'm working on a python script to setup servers quickly and it basically has a list of commands I want to execute on the linux commandline.
我正在编写一个 python 脚本来快速设置服务器,它基本上有一个我想在 linux 命令行上执行的命令列表。
I can install all the software I need but not sure how to create a mysql user solely via command line? I can I can go into the mysql shell and do it but is there a way to do it solely from the linux shell(once the user is created I can do all the database setup remotely with a python script)?
我可以安装我需要的所有软件,但不确定如何仅通过命令行创建 mysql 用户?我可以进入 mysql shell 并执行此操作,但是有没有办法仅从 linux shell 执行此操作(创建用户后,我可以使用 python 脚本远程执行所有数据库设置)?
采纳答案by Michael Berkowski
You do it with the same query you'd use in the client, but execute it on the command line with the -e
flag:
您使用在客户端中使用的相同查询来执行此操作,但在带有-e
标志的命令行上执行它:
mysql -uroot -prootpw -e "GRANT ALL PRIVILEGES ON dbname.* TO username@hostname IDENTIFIED BY 'userpassword'"
Or pipe in the command
或者在命令中使用管道
echo "GRANT ALL PRIVILEGES ON dbname.* TO username@hostname IDENTIFIED BY 'userpassword'" | mysql -uroot -prootpw