日期:2014-05-16  浏览次数:20694 次

MYSQL中限制资源的使用
自己查看MYSQL.USER 表就会发现里面最后几个字段:
mysql> select version();
+------------------------------------+
| version()                          |
+------------------------------------+
| 5.1.17-beta-community-nt-debug-log |
+------------------------------------+
1 row in set (0.00 sec)

*************************** 36. row ***************************
Field: max_questions
   Type: int(11) unsigned
   Null: NO
    Key:
Default: 0
Extra:
*************************** 37. row ***************************
Field: max_updates
   Type: int(11) unsigned
   Null: NO
    Key:
Default: 0
Extra:
*************************** 38. row ***************************
Field: max_connections
   Type: int(11) unsigned
   Null: NO
    Key:
Default: 0
Extra:
*************************** 39. row ***************************
Field: max_user_connections
   Type: int(11) unsigned
   Null: NO
    Key:
Default: 0
Extra:
39 rows in set (0.00 sec)

这三个字段可以用GRANT语句来生成。
1、MAX_QUERIES_PER_HOUR 用来限制用户每小时运行的查询数量
mysql> grant select on *.* to 'cu_blog'@'localhost' identified by '123456' with
max_queries_per_hour 5;
Query OK, 0 rows affected (0.00 sec)
...
mysql> select user();
+-------------------+
| user()            |
+-------------------+
| cu_blog@localhost |
+-------------------+
1 row in set (0.00 sec)
当到了指定的次数时就会报错
mysql> select user();
ERROR 1226 (42000): User 'cu_blog' has exceeded the 'max_questions' resource (cu
rrent value: 5)
2、MAX_UPDATES_PER_HOUR 用来限制用户每小时的修改数据库数据的数量。
mysql> grant select on *.* to 'cu_blog'@'localhost' with max_updates_per_hour 5;
Query OK, 0 rows affected (0.00 sec)
3、MAX_CONNECTIONS_PER_HOUR用来控制用户每小时打开新连接的数量。
mysql> grant select on *.* to 'cu_blog'@'localhost' with max_connections_per_hou
r 5;
Query OK, 0 rows affected (0.00 sec)
4、MAX_USER_CONNECTIONS 限制有多少用户连接MYSQL服务器。
mysql> grant select on *.* to 'cu_blog'@'localhost' with max_user_connections 2;

Query OK, 0 rows affected (0.00 sec)

5、要想将所有账户当前的记数重设为零,可以执行FLUSH USER_RESOURCES语句。还可以通过重载授权表来重设记数。
mysql> flush user_resources;
Query OK, 0 rows affected (0.00 sec)