什么时候真正需要 MySQL 中的 Flush Privileges?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36463966/
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
When is Flush Privileges in MySQL really needed?
提问by kojow7
When creating new tables and a user to go along with it, I usually just invoke the following commands:
在创建新表和一个用户时,我通常只调用以下命令:
CREATE DATABASE mydb;
GRANT ALL PRIVILEGES ON mydb.* TO myuser@localhost IDENTIFIED BY "mypassword";
I have never ever needed to utilize the FLUSH PRIVILEGES
command after issuing the previous two commands. Users can log in and use their database and run PHP scripts which connect to the database just fine. Yet I see this command used in almost every tutorial I look at.
FLUSH PRIVILEGES
在发出前两个命令后,我从未需要使用该命令。用户可以登录并使用他们的数据库并运行连接到数据库的 PHP 脚本就好了。然而,我几乎在我看过的每个教程中都看到了这个命令。
When is the FLUSH PRIVILEGES
command really needed and when is it unnecessary?
什么时候FLUSH PRIVILEGES
真正需要命令,什么时候不需要?
回答by Sanj
Privileges assigned through GRANT option do not need FLUSH PRIVILEGES to take effect - MySQL server will notice these changes and reload the grant tables immediately.
通过 GRANT 选项分配的权限不需要 FLUSH PRIVILEGES 才能生效 - MySQL 服务器会注意到这些更改并立即重新加载授权表。
If you modify the grant tables directly using statements such as INSERT, UPDATE, or DELETE, your changes have no effect on privilege checking until you either restart the server or tell it to reload the tables. If you change the grant tables directly but forget to reload them, your changes have no effect until you restart the server. This may leave you wondering why your changes seem to make no difference!
To tell the server to reload the grant tables, perform a flush-privileges operation. This can be done by issuing a FLUSH PRIVILEGES statement or by executing a mysqladmin flush-privileges or mysqladmin reload command.
If you modify the grant tables indirectly using account-management statements such as GRANT, REVOKE, SET PASSWORD, or RENAME USER, the server notices these changes and loads the grant tables into memory again immediately.
如果您直接使用 INSERT、UPDATE 或 DELETE 等语句修改授权表,则您的更改不会影响权限检查,直到您重新启动服务器或告诉它重新加载表。如果您直接更改授权表但忘记重新加载它们,则您的更改在重新启动服务器之前无效。这可能会让您想知道为什么您的更改似乎没有任何区别!
要告诉服务器重新加载授权表,请执行刷新权限操作。这可以通过发出 FLUSH PRIVILEGES 语句或通过执行 mysqladmin flush-privileges 或 mysqladmin reload 命令来完成。
如果您使用帐户管理语句(例如 GRANT、REVOKE、SET PASSWORD 或 RENAME USER)间接修改授权表,则服务器会注意到这些更改并立即将授权表再次加载到内存中。
回答by simhumileco
TL;DR
TL; 博士
You should use FLUSH PRIVILEGES;
only if you modify the grant tables directly using statements such as INSERT
, UPDATE
, or DELETE
.
您应该使用FLUSH PRIVILEGES;
,如果您修改直接使用语句,如授权表只INSERT
,UPDATE
或DELETE
。
回答by cristi148
Just to give some examples. Let's say you modify the password for an user called 'alex'. You can modify this password in several ways. For instance:
只是举一些例子。假设您修改了名为“alex”的用户的密码。您可以通过多种方式修改此密码。例如:
mysql> update* user set password=PASSWORD('test!23') where user='alex';
mysql> flush privileges;
Here you used UPDATE. If you use INSERT, UPDATE or DELETE on grant tables directly you need use FLUSH PRIVILEGES in order to reload the grant tables.
在这里,您使用了 UPDATE。如果直接在授权表上使用 INSERT、UPDATE 或 DELETE,则需要使用 FLUSH PRIVILEGES 以重新加载授权表。
Or you can modify the password like this:
或者您可以像这样修改密码:
mysql> set password for 'alex'@'localhost'= password('test!24');
Here it's not necesary to use "FLUSH PRIVILEGES;" If you modify the grant tables indirectly using account-management statements such as GRANT, REVOKE, SET PASSWORD, or RENAME USER, the server notices these changes and loads the grant tables into memory again immediately.
这里没有必要使用“FLUSH PRIVILEGES”;如果您使用帐户管理语句(例如 GRANT、REVOKE、SET PASSWORD 或 RENAME USER)间接修改授权表,则服务器会注意到这些更改并立即将授权表再次加载到内存中。
回答by ashkan nasirzadeh
2 points in addition to all other good answers:
除了所有其他好的答案外,还有 2 点:
1:
1:
what are the Grant Tables?
什么是拨款表?
The MySQL system database includes several grant tables that contain information about user accounts and the privileges held by them.
MySQL 系统数据库包括几个授权表,其中包含有关用户帐户及其拥有的权限的信息。
clari?cation: in MySQL, there are some inbuilt databases , one of them is "mysql" , all the tables on "mysql" database have been called as grant tables
澄清:在MySQL中,有一些内置数据库,其中之一是“mysql”,“mysql”数据库上的所有表都被称为授权表
2:
2:
note that if you perform:
请注意,如果您执行:
UPDATE a_grant_table SET password=PASSWORD('1234') WHERE test_col = 'test_val';
and refresh phpMyAdmin , you'll realize that your password has been changed on that table but even now if you perform:
并刷新 phpMyAdmin ,您会意识到您的密码已在该表上更改,但即使现在执行:
mysql -u someuser -p
your access will be denied by your new password until you perform :
您的访问将被您的新密码拒绝,直到您执行以下操作:
FLUSH PRIVILEGES;