如何在 MySQL 上切换用户
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22811829/
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 switch users on MySQL
提问by macavity
what is the windows command to switch to users on MySQL Im having a little problem in using mysql on windows. I know how to create a new user and giving grants to the new user and all. But i don't know how to switch to new user from mysql console!
在 MySQL 上切换到用户的 windows 命令是什么 我在 windows 上使用 mysql 时遇到了一点问题。我知道如何创建一个新用户并授予新用户和所有用户。但我不知道如何从 mysql 控制台切换到新用户!
回答by shivakumara K Madegowda
I figured out solution. There is system command, where we can execute shell command like 'ls, cd, mv, mkdir etc.. Here we can login as different user. first I logged in as regular user.
我想出了解决办法。有系统命令,我们可以在其中执行shell命令,如'ls、cd、mv、mkdir等。在这里我们可以以不同的用户身份登录。首先我以普通用户身份登录。
[madegow@fedora20 ~]$ mysql -u root -p
Enter password:
...
MariaDB [(none)]> select user()
-> ;
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
MariaDB [(none)]> system mysql -u madegow -p
Enter password:
...
MariaDB [(none)]> select user();
+-------------------+
| user() |
+-------------------+
| madegow@localhost|
Now user got changed to madegow user. but when you run quit/exit it will not go back to root. we need to bare with that.
现在用户已更改为 madegow 用户。但是当你运行退出/退出时,它不会回到 root。我们需要忍受这一点。
回答by bmfloyd
I was looking for an equivalence of the CONNECT command of Oracle Database and just found it with the command \CONNECT available on MySQL Shell (since version 5.7.12), but that's another story.
我一直在寻找与 Oracle 数据库的 CONNECT 命令等效的命令,并在 MySQL Shell(自版本 5.7.12 起)上找到了命令 \CONNECT 的等效项,但那是另一回事了。
So, as said years ago by our friend Shivakumara... the answer is:
所以,正如我们的朋友 Shivakumara 多年前所说的……答案是:
MySQL> SYSTEM mysql -u user -p
Which is nothing but calling an OS shell command from MySQL prompt.
这只不过是从 MySQL 提示符调用 OS shell 命令。
回答by avinash mangal
Actually I understand the issue.
其实我明白这个问题。
- go to windows command prompt Microsoft Windows [Version 10.0.10586] (c) 2015 Microsoft Corporation. All rights reserved.
- 转到 windows 命令提示符 Microsoft Windows [版本 10.0.10586] (c) 2015 Microsoft Corporation。版权所有。
C:\Users\avinash>cd/
C:>cd "Program Files\MySQL\MySQL Server 5.7\bin"
C:\Program Files\MySQL\MySQL Server 5.7\bin>mysql -u xyzzzz -p Enter password: ***** ERROR 1045 (28000): Access denied for user 'xyzzzz'@'localhost' (using password: YES)
C:\Program Files\MySQL\MySQL Server 5.7\bin>mysql -u xyzzzz -p Enter password:
C:\Users\avinash>cd/
C:>cd "Program Files\MySQL\MySQL Server 5.7\bin"
C:\Program Files\MySQL\MySQL Server 5.7\bin>mysql -u xyzzzz -p Enter password: ***** ERROR 1045 (28000): Access denied for user 'xyzzzz'@'localhost' (using password: YES )
C:\Program Files\MySQL\MySQL Server 5.7\bin>mysql -u xyzzzz -p 输入密码:
回答by Alma Do
MySQL CLI session is bound to the user which started it. Thus, you'll have to open new session with using desired user after you've created it & set needed grants. You can open new session (i.e. keep current one) or end current session and then start new:
MySQL CLI 会话绑定到启动它的用户。因此,在创建并设置所需的授权后,您必须使用所需的用户打开新会话。您可以打开新会话(即保持当前会话)或结束当前会话,然后开始新会话:
mysql -uUSER -p DATABASE
where USER
is name of new user and DATABASE
is database to switch when log on (-p
points that you'll prompted to enter password). To exit session, just use exit
.
其中USER
是新用户的名称,DATABASE
是登录时要切换的数据库(-p
提示您输入密码的点)。要退出会话,只需使用exit
.
回答by Sulthan Allaudeen
Creating user syntax : http://dev.mysql.com/doc/refman/5.1/en/create-user.html
创建用户语法:http: //dev.mysql.com/doc/refman/5.1/en/create-user.html
Code :
代码 :
CREATE USER 'jeffrey'@'localhost' IDENTIFIED BY 'mypass';
To switch the only way in command line is :
在命令行中切换的唯一方法是:
mysql -u user -p