Translated 20150122 Linux FAQs with Answers--How to create and configure a MySQL user from the command line.md

This commit is contained in:
Ping 2015-01-26 10:47:15 +08:00
parent b591981031
commit a69c9e40e0
2 changed files with 107 additions and 108 deletions

View File

@ -1,108 +0,0 @@
Ping Translating
Linux FAQs with Answers--How to create and configure a MySQL user from the command line
================================================================================
> **Question**: I would like to create a new user account on MySQL server, and apply appropriate permissions and resource limits to the account. How can I create and configure a MySQL user from the command line?
To access a MySQL server, you need to log in to the server using a user account. Each MySQL user account has a number of attributes associated with it, such as user name, password, as well as privileges and resource limits. Privileges are user-specific permissions defining what you can do inside a MySQL server, while resource limits set the limitations on the amount of server resource allowed for for the user. Creating or updating a MySQL user involves managing all these attributes of the user account.
Here is how to create and configure a MySQL user on Linux.
You first log in to MySQL server as the root.
$ mysql -u root -p
When prompted for authentication, enter the MySQL root password.
![](https://farm8.staticflickr.com/7482/16024190060_fff53d8840_b.jpg)
### Create a MySQL User ###
To create a new user with username 'myuser' and password 'mypassword', use the following command.
mysql> CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword';
Once a user is created, all its account details including an encrypted password, privileges and resource limits are stored in a table called **user** in a special database named **mysql**.
To verify that the account is created successfully, run:
mysql> SELECT host, user, password FROM mysql.user WHERE user='myuser';
### Grant Privileges to MySQL User ###
A newly created MySQL user comes with zero access privilege, which means that you cannot do anything inside MySQL server. You need to grant necessary privileges to the user. Some of available privileges are the following.
- **ALL**: all privileges available.
- **CREATE**: create databases, tables or indices.
- **LOCK_TABLES**: lock databases.
- **ALTER**: alter tables.
- **DELETE**: delete tables.
- **INSERT**: insert tables or columns.
- **SELECT**: select tables or columns.
- **CREATE_VIEW**: create views.
- **SHOW_DATABASES**: show databases.
- **DROP**: drop daabases, tables or views.
To grant a particular privilege to user 'myuser', use the following command.
mysql> GRANT <privileges> ON <database>.<table> TO 'myuser'@'localhost';
In the above, <privileges> is expressed as a comma-separated list of privileges. If you want to grant privileges for any database (or table), place an asterisk (*) in the database (or table) name.
For example, to grant CREATE and INSERT privileges for all databases/tables:
mysql> GRANT CREATE, INSERT ON *.* TO 'myuser'@'localhost';
To verify the granted privileges of the user:
mysql> SHOW GRANTS FOR 'myuser'@'localhost';
![](https://farm8.staticflickr.com/7556/16209665261_923282bddd_c.jpg)
To grant all privileges to all databases/tables:
mysql> GRANT ALL ON *.* TO 'myuser'@'localhost';
You can also remove existing privileges from a user. To revoke existing privileges from the account 'myuser', use the following command.
mysql> REVOKE <privileges> ON <database>.<table> FROM 'myuser'@'localhost';
### Add Resource Limits to MySQL User ###
In MySQL, you can place limits on MySQL resource usage for individual users. The available resource limits are the following.
- **MAX_QUERIES_PER_HOUR**: number of allowed queries per hour.
- **MAX_UPDATES_PER_HOUR**: number of allowed updates per hour.
- **MAX_CONNECTIONS_PER_HOUR**: number of allowed logins per hour.
- **MAX_USER_CONNECTIONS**: number of simultaneous connections to the server.
To add a resource limit to the account 'myuser', use the following command.
mysql> GRANT USAGE ON <database>.<table> TO 'myuser'@'localhost' WITH <resource-limits>;
In <resource-limits>, you can specify multiple resource limits separated by space.
For example, to add MAX_QUERIES_PER_HOUR and MAX_CONNECTIONS_PER_HOUR resource limits:
mysql> GRANT USAGE ON *.* TO 'myuser'@'localhost' WITH MAX_QUERIES_PER_HOUR 30 MAX_CONNECTIONS_PER_HOUR 6;
To verify the resource limits of the user:
mysql> SHOW GRANTS FOR 'myuser'@'localhost;
![](https://farm8.staticflickr.com/7537/16025443759_5cb4177bc6_c.jpg)
The last important step after creating and configuring a MySQL user is to run:
mysql> FLUSH PRIVILEGES;
so that the changes take effect. Now the MySQL user account is good to go!
--------------------------------------------------------------------------------
via: http://ask.xmodulo.com/create-configure-mysql-user-command-line.html
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出

View File

@ -0,0 +1,107 @@
Linux有问必答 如何通过命令行创建和设置一个MySQL用户
================================================================================
> **问题**我想要在MySQL服务器上创建一个新的用户帐号并且赋予他适当的权限和资源限制。如何通过命令行的方式来创建并且设置一个MySQL用户呢
要访问一个MySQL服务器你需要使用一个用户帐号登录其中方可进行。每个MySQL用户帐号都有许多与之相关连的属性例如用户名、密码以及权限和资源限制。"权限"定义了特定用户能够在MySQL服务器中做什么而"资源限制"为用户设置了一系列服务器资源的使用许可。创建或更新一个用户涉及到了对用户帐号所有属性的管理。
下面展示了如何在Linux中创建和设置一个MySQL用户。
首先以root身份登录到MySQL服务器中。
$ mysql -u root -p
当验证提示出现的时候输入MySQL的root帐号的密码。
![](https://farm8.staticflickr.com/7482/16024190060_fff53d8840_b.jpg)
### 创建一个MySQL用户 ###
使用如下命令创建一个用户名和密码分别为"myuser"和"mypassword"的用户。
mysql> CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword';
一旦用户被创建后,包括加密的密码、权限和资源限制在内的所有帐号细节都会被存储在一个名为**user**的表中,这个表则存在与**mysql**这个特殊的数据库里。
运行下列命令,验证帐号是否创建成功
mysql> SELECT host, user, password FROM mysql.user WHERE user='myuser';
### 赋予MySQL用户权限 ###
一个新建的MySQL用户没有任何访问权限这就意味着你不能在MySQL数据库中进行任何操作。你得赋予用户必要的权限。以下是一些可用的权限
- **ALL**: 所有可用的权限
- **CREATE**: 创建库、表和索引
- **LOCK_TABLES**: 锁定表.
- **ALTER**: 修改表.
- **DELETE**: 删除表.
- **INSERT**: 插入表或列.
- **SELECT**: 选择表或列.
- **CREATE_VIEW**: 创建视图.
- **SHOW_DATABASES**: 展示数据库.
- **DROP**: 删除库、表和视图.
运行以下命令赋予"myuser"用户特定权限。
mysql> GRANT <privileges> ON <database>.<table> TO 'myuser'@'localhost';
以上命令中,`<privileges>` 代表着用逗号分隔的权限列表。如果你想要将权限赋予任意数据库(或表),那么使用星号(*)来代替数据库(或表)的名字。
例如,为所有数据库/表赋予 CREATE 和 INSERT 权限:
mysql> GRANT CREATE, INSERT ON *.* TO 'myuser'@'localhost';
验证给用户赋予的全权限:
mysql> SHOW GRANTS FOR 'myuser'@'localhost';
![](https://farm8.staticflickr.com/7556/16209665261_923282bddd_c.jpg)
将全部的权限赋予所有数据库/表:
mysql> GRANT ALL ON *.* TO 'myuser'@'localhost';
你也可以将用户现有的权限删除。使用以下命令废除"myuser"帐号的现有权限:
mysql> REVOKE <privileges> ON <database>.<table> FROM 'myuser'@'localhost';
### 为用户添加资源限制 ###
在MySQL中你可以为单独的用户设置MySQL的资源使用限制。可用的资源限制如下
- **MAX_QUERIES_PER_HOUR**: 允许的每小时最大请求数量.
- **MAX_UPDATES_PER_HOUR**: 允许的每小时最大更新数量.
- **MAX_CONNECTIONS_PER_HOUR**: 允许的每小时最大连接(译者注:[其与 MySQL全局变量 max_user_connections 共同决定用户到数据库的同时连接数量](http://dev.mysql.com/doc/refman/5.0/en/user-resources.html))数量.
- **MAX_USER_CONNECTIONS**: 对服务器的同时连接量.
使用以下命令为"myuser"帐号增加一个资源限制:
mysql> GRANT USAGE ON <database>.<table> TO 'myuser'@'localhost' WITH <resource-limits>;
`<resource-limits>` 中你可以指定多个使用空格分隔开的资源限制。
例如,增加 MAX_QUERIES_PER_HOUR 和 MAX_CONNECTIONS_PER_HOUR 资源限制:
mysql> GRANT USAGE ON *.* TO 'myuser'@'localhost' WITH MAX_QUERIES_PER_HOUR 30 MAX_CONNECTIONS_PER_HOUR 6;
验证用户的资源限制:
mysql> SHOW GRANTS FOR 'myuser'@'localhost;
![](https://farm8.staticflickr.com/7537/16025443759_5cb4177bc6_c.jpg)
创建和设置一个MySQL用户最后的一个重要步骤
mysql> FLUSH PRIVILEGES;
如此一来更改便生效了。现在MySQL用户帐号就可以使用了。
--------------------------------------------------------------------------------
via: http://ask.xmodulo.com/create-configure-mysql-user-command-line.html
译者:[Ping](http://weibo.com/370321376)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出