In MySQL you can use the GRANT statement to create users and assign privileges to them at the same time.在MySQL中,您可以使用贈款的聲明,創建用戶和分配的特權,他們在同一時間內。 It is extremely convenient and beats create user or directly hacking the mysql.user table.這是非常方便和節拍創建用戶或直接入侵mysql.user表。 I learned few things about GRANT the hard way.我學到幾件事有關贈款的努力方式。

MySQL allows you to create users associated with a particular host name or a set of host names. MySQL的讓您建立用戶與特定的主機名稱或一套主機名稱。 This allows you to easily restrict your user account to a particular host or a small set of allowed hosts.這可以讓您輕易地限制您的用戶帳戶到某個特定的主機或一個小的一套允許主機。
A mysql user is identified not only by the user name but also by the host name. 1 MySQL用戶是確定的不僅是由使用者名稱,但也由主機名稱。 So, for example, you can have separate privileges for user foo connecting from host bar and for user foo connecting from localhost.因此,舉例來說,您可以有單獨的特權,為用戶foo從主機連接的酒吧和為用戶foo從本地連接。

It is a good policy to allow the least amount of security as you can do with.這是一個很好的政策,容許的最低金額的安全,你可以做的。 It is better to add privileges as needed as opposed to providing blanket privileges upfront.最好是添加的特權,視需要作為反對提供毛毯特權的前期。

A typical GRANT statement to GRANT all privileges to all tables on database db to user tim with password pass connecting from machine jaguar can be written as follows:一個典型的贈款聲明給予所有特權,所有表格上的數據庫分貝用戶與密碼, 通過連接從機美洲虎可以寫如下:
grant all on db.* to ‘ tim ‘@’ jaguar ‘ identified by ‘ pass ‘; 給予所有關於分貝.* '添' @ '美洲虎'所確定的'通行證' ;

To allows tim to connect only from localhost (same machine as the database) change it as follows:要允許添連接,只有從本地(同一台機器作為數據庫)改變如下:
grant all on db.* to ‘ tim ‘@’ localhost ‘ identified by ‘ pass ‘; 給予所有關於分貝.* '添' @ '機'所確定的'通行證' ;

To allows tim to connect only from any machine change it as follows:要允許添連接,只有從任何機器,改變它如下:
grant all on db.* to ‘ tim ‘@’ % ‘ identified by ‘ pass ‘; 給予所有關於分貝.* '添' @ ' % '所確定的'通行證' ;

To restrict tim connecting from localhost to only select & insert privilege in db database use:限制添連接從本地只選擇與插入的特權,在DB數據庫的使用:
grant select, insert on db.* to ‘ tim ‘@’ localhost ‘ identified by ‘ pass ‘; 給予選擇,插入就分貝.* '添' @ '機'所確定的'通行證' ;

Reference:參考: MySQL Manual MySQL的手冊

The full syntax for GRANT is:充分的語法贈款是:

GRANT priv_type [(column_list)] [, priv_type [(column_list)]] …金priv_type [ ( column_list ) ] [ , priv_type [ ( column_list ) ] …
ON [object_type] {tbl_name | * | *.* | db_name.*}對[ object_type ] ( tbl_name | * | *.* | db_name .* )
TO user [IDENTIFIED BY [PASSWORD] ‘password’]用戶[確定[密碼] '密碼' ]
[, user [IDENTIFIED BY [PASSWORD] ‘password’]] … [ ,用戶[確定[密碼] '密碼' ] …
[REQUIRE [要求
NONE |無|
[{SSL| X509}] [ (的SSL | x509 ) ]
[CIPHER 'cipher' [AND]] [密碼'密碼' [和]
[ISSUER 'issuer' [AND]] [發行'發行人' [和]
[SUBJECT 'subject']] [主題'受' ]
[WITH with_option [with_option] …] [ with_option [ with_option ] … … ]

object_type = object_type =
TABLE桌子
| FUNCTION |功能
| PROCEDURE |程序

with_option = with_option =
GRANT OPTION補助金的選擇
| MAX_QUERIES_PER_HOUR count | max_queries_per_hour計數
| MAX_UPDATES_PER_HOUR count | max_updates_per_hour計數
| MAX_CONNECTIONS_PER_HOUR count | max_connections_per_hour計數
| MAX_USER_CONNECTIONS count | max_user_connections計數