本文共 3212 字,大约阅读时间需要 10 分钟。
Right after you install MySQL, you will have the root
user available.
在安装MySQL之后,您将具有root
用户可用。
This is the user you might use to try and see if MySQL is working, but it should not be the user for other kinds of usages.
这是您可能用来尝试查看MySQL是否正在运行的用户,但不应该是其他用途的用户。
Why? Because it’s too powerful.
为什么? 因为它太强大了 。
With great power comes great responsibility (said Spider-Man’s uncle). And in particular, great danger if you make mistakes.
强大的力量带来巨大的责任(蜘蛛侠的叔叔说)。 特别是如果您犯了错误,将会带来极大的危险。
Instead, you should create ad-hoc users that only have permissions to perform their job, and no more. Same for using Linux or any Unix system, for example: you don’t want to be using the root
user, but your own user account.
相反,您应该创建临时用户,这些用户仅具有执行其工作的权限,而没有其他权限。 例如,与使用Linux或任何Unix系统的情况相同:您不想使用root
用户,而要使用自己的用户帐户。
To create a new user, connect to MySQL using the root
user:
要创建新用户,请使用root
用户连接到MySQL:
mysql -u root -p
then use the command
然后使用命令
CREATE USER ''@'localhost' IDENTIFIED BY ' ';
For example, to create a user named test_user
with password test_password12A
run the command:
例如,要创建一个名为test_user
且密码为test_password12A
的用户,请运行以下命令:
CREATE USER 'test_user'@'localhost' IDENTIFIED BY 'test_password12A';
The command should return a line saying Query OK, 0 rows affected (X.XX sec)
:
该命令应返回一行Query OK, 0 rows affected (X.XX sec)
:
If you used an invalid password, the system would tell you something like ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
. In this case the error says that the password is not complex enough, because when I installed MySQL I told it to use a certain policy on passwords.
如果您使用了无效的密码,系统将告诉您类似ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
。 在这种情况下,该错误表明密码不够复杂,因为在安装MySQL时,我告诉它对密码使用某种策略。
That’s great! Now the user has been created, and we can connect using that user to MySQL. From the command line you can exit by typing QUIT
, and typing:
那很棒! 现在已经创建了用户,我们可以使用该用户连接到MySQL。 在命令行中,您可以输入QUIT
并QUIT
:
mysql -u test_user -p
Now if I try to create a database, I’d get an error saying ERROR 1044 (42000): Access denied for user 'test_user'@'localhost' to database 'testing'
:
现在,如果我尝试创建数据库,则会收到一条错误消息,提示ERROR 1044 (42000): Access denied for user 'test_user'@'localhost' to database 'testing'
:
Why? Because the user does not have the permission to create a new database.
为什么? 因为用户没有创建新数据库的权限 。
We’ll see how to work with permissions in another tutorial.
在另一个教程中,我们将介绍如何使用权限。
It’s worth mentioning the @'localhost'
string we used to create the user. This tells MySQL that the user can only connect from localhost. Which is fine when testing things out and when any application that will connect to MySQL is running on the same computer that runs the DBMS.
值得一提的是我们用来创建用户的@'localhost'
字符串。 这告诉MySQL用户只能从本地主机连接。 当进行测试以及将要连接到MySQL的任何应用程序在运行DBMS的同一台计算机上运行时,这很好。
When this is not true, you either need to manually enter the IP the user will connect from, or use the %
wildcard:
如果不正确,则需要手动输入用户将要连接的IP,或者使用%
通配符:
CREATE USER 'test_user'@'%' IDENTIFIED BY 'test_password12A';
I want to close this tutorial by showing how to remove the user we created:
我想通过显示如何删除我们创建的用户来结束本教程:
DROP USER 'test_user'@'localhost';
翻译自:
转载地址:http://xtqgb.baihongyu.com/