Create a user and authorize a user in MySQL

Source: Internet
Author: User
Tags ip number net domain


When you issue a grant statement to a user, create a record for the user in the User table. If the statement specifies any global permissions (administrative permissions or permissions applicable to all databases), these are also recorded in the User table. If you specify database, table, and column-level permissions, they are recorded in dB, tables_priv, and columns_priv tables respectively.
      Using Grant and revoke is easier than directly modifying the authorization table. However, read MySQL security guide. These tables are exceptionally important, and as an administrator, you should understand how they go beyond the functional level of grant and revoke statements.

In the following sections, we will introduce how to set up and authorize a MySQL user account. We also involve how to revoke permissions and delete users from the authorization table.

You may also want to use mysqlaccess and mysql_setpermission scripts, which are part of MySQL distribution. They are Perl scripts and provide another option to set user accounts for grant statements. DBI support is required for mysql_setpermission.

1. Create and authorize a user

The syntax of the grant statement looks like this.
  
Grant privileges (columns)
On what
To user identified by "password"
With grant option
To use this statement, you must enter the following parts:
  
Privileges
The following table lists the permissions that can be used for grant statements:
   All means "All Permissions". uasge means that you have no permissions, that is, you create a user, but do not grant permissions.

Columns
The permission column is optional, and you can only set specific permissions for the column. If the command has more than one column, separate them with commas.
  
What
Permission usage level. Permissions Can be global (applicable to all databases and tables), specific databases (applicable to all tables in a database), or specific tables. You can specify a columns statement to indicate that the permission is column-specific.
  
User
The user authorized by the permission, which consists of a user name and host name. In MySQL, you not only specify who can connect, but also where to connect. This allows two users with the same name to connect from different places. MySQL allows you to differentiate them and grant them permissions independently.
  
A user name in MySQL is the user name specified when you connect to the server. It does not need to be associated with your UNIX or Windows Name. By default, if you do not specify a specific name, the customer program uses your login name as the MySQL user name. This is just an agreement. You can change the name to nobody in the authorization table, and then use the nobody connection to perform operations that require superuser permissions.
  
Password
The password assigned to the user. It is optional. If you do not specify the identifiedby clause for a new user, the user is not assigned a password (Insecure ). For existing users, any password you specify will replace the old password. If you do not specify a password, the old password remains unchanged. When you use identifiedby, the password string uses the literal meaning of the password, and grant will encode the password for you, do not use the password () function as you use setpassword.
  
The with grantoption clause is optional. If you include it, you can grant permissions to other users through the grant statement. You can use this clause to grant permissions to other users.
  
The username, password, database, and table name are case sensitive in the authorization table record, and the host name and column name are not.
  
Generally, you can identify the types of grant statements by asking a few simple questions:
  
Who can connect from there?
What level of permissions should users have and what do they apply?
Should the user be allowed to manage permissions?
The following are some examples.
  

1.1 who can connect and connect from there?
You can allow a user to connect from a specific host or a series of hosts. There is one extreme: If you know that a demotion is connected from a host, you can limit the permissions to a single host:
  
Grant all on samp_db. * to Boris @ localhost identified by "Ruby"
Grant all on samp_db. * To fred@res.mars.com identified by "quartz"
(Samp_db. * indicates "all tables in the samp_db database.) Another extreme is that you may have a user max that is frequently traveling and needs to be connected from hosts around the world. In this case, you can allow him to connect from anywhere:
  
Grant all on samp_db. * To Max @ % identified by "diamond"
The "%" character acts as a wildcard and matches the like pattern. In the preceding statement, it means "any host ". So Max and Max @ % are equivalent. This is the easiest way to build a user, but it is also the least secure.
  
You can allow a user to access from a restricted host set. For example, to allow Mary to be connected from any host in the snake.net domain, use the following identifier:
  
Grant all on samp_db. * to Mary @ .snke.net identified by "quartz ";
If you like, the host part of the User Identifier can be specified by an IP address instead of a host name. You can specify an IP address or an address that contains a pattern character. In addition, from mysql3.23, you can also specify an IP number with a network mask pointing to the number of digits used for the network number:
  
Grant all on samp_db. * To boris@192.168.128.3 identified by "Ruby"
Grant all on samp_db. * To fred@192.168.128. % identified by "quartz"
Grant all on samp_db. * To rex@192.168.128.0/17 identified by "Ruby"
The first example indicates that the user can connect to a specific host, and the second specifies the IP Mode for the class C subnet 192.168.128. In the third statement, 192.168.128.0/17 specify a 17-bit network number and match the IP address with the header 192.168.128.

If MySQL complains about the user value you specified, you may need to use quotation marks (only separate the user name and host name with quotation marks ).
  
Grant all on samp_db.president to "myfriend" @ "boa.snke.net"

1.2 What level of permissions should users have and what should they apply?
You can grant different levels of permissions. Global permissions are the most powerful because they apply to any database. To make Etel a Super User who can do anything, including authorizing other users, issue the following statement:
  
Grant all on *. * To Etel @ localhost identified by "coffee" withgrant Option
*. * In the on Clause indicates "all databases and all tables ". For security considerations, we specify that Etel can only be connected locally. It is usually wise to restrict the host that a Super User can connect to because it limits the host that tries to crack the password.
Some permissions (file, process, reload, and shutdown) are administrative permissions and can only be authorized with the "on *. *" Global permissions. If you want to, you can grant these permissions without authorizing database permissions. For example, if the following statement sets a flush user, it can only issue flush statements. This may be useful when you need to execute management scripts such as clearing logs:
  
Grant reload on *. * To flushl @ localhost identified by "flushpass"
Generally, you want to authorize management permissions, because users with these permissions can affect the operations on your servers.
  
Database-level permissions apply to all tables in a specific database. They can be granted by using the on db_name. * clause:
  
Grant all on samp_db to bill@racer.snake.net indetified by "rock"
Grant select on samp_db to ro_user @ % indetified by "rock"
The first statement grants permissions to all tables in the samp_db database to Bill, and the second statement creates a user ro_user (read-only user) that strictly restricts access, which can only access all tables in the samp_db database, but only read, that is, you can only issue select statements.
  
You can list a series of permissions granted at the same time. For example, if you want to allow users to read and modify the content of an existing database but cannot create or delete a new table, grant the following permissions:
  
Grant select, insert, delete, update on samp_db to bill@snake.netINDETIFIED by "rock"
For more refined access control, you can grant permissions on each table or even on each column of the table. When you want to hide a part of a table from a user, or you want a user to modify only specific columns, column-specific permissions are very useful. For example:
  
Grant select on samp_db.member to Bill @ localhost indetified by "rock"
Grant Update (expiration) on samp_db. Member tobill @ localhost
The first statement grants the read permission to the entire Member table and sets a password. The second statement adds the update permission when only the expiration column is applied. You do not need to specify a password because the first statement has already been specified.
  
If you want to grant permissions to multiple columns, specify a list separated by commas. For example, to add the update permission for the address field of the member table to the assistant user, use the following statement to add the new permission to the user's existing permissions:
  
Grant Update (street, city, state, zip) on samp_db toassistant @ localhost
  
Generally, you do not want to grant any permissions that are wider than what the user really needs. However, when you want users to create a temporary table to save intermediate results, but you do not want them to do so in a database that contains the content they should not modify, A relatively loose permission is granted to a database. You can create a separate database (such as TMP) and grant all permissions to the database. For example, if you want any user from a host in the mars.net domain to use the TMP database, you can issue the following grant statement:
  
 Grant all on TMP. * to "" @ mars.net
  
After you finish, you can create a table in TMP. tbl_name and reference it in the form of TMP (create an anonymous user in "" specified by the user, and all users match the blank user name ).
  
1.3 Should users be permitted to manage permissions?
You can allow a database owner to control database access by granting all database owner permissions. During authorization, specify withgrantoption. For example, if you want Alicia to connect to any host in the big.corp.com domain and have the Administrator permission for all tables in the sales database, you can use the following grant statement:
  
Grant all on sales. * to Alicia @ % .big.corp.com indetified by "AppleJuice" with grant option
  
In effect, the withgrantoption clause allows you to grant the Access Authorization right to another user. Note that two users with the grant permission can authorize each other. If you only grant select permission to the first user and grant select permission to the other user, the second user can be the first user more powerful ".
  
2. revoke permissions and delete users

To cancel a user's permissions, use the revoke statement. The revoke syntax is very similar to the grant statement, except that the from clause is replaced with the indetifedby and with grant option clauses:
  
  Revoke privileges (columns) on whatfrom user
  
The user part must match the user part of the user you want to revoke permission from the original grant statement. Privileges does not need to be matched. You can use the grant statement to grant permissions, and then use the revoke statement to revoke only some permissions.
  
The revoke statement only deletes permissions, but does not delete users. Even if you revoke all permissions, the user records in the User table are retained, which means that the user can still connect to the server. To completely delete a user, you must use a delete statement to explicitly delete user records from the user table:
  
% Mysql-u root MySQL
Mysql> Delete from user
-> Where user = "user_name" andhost = "host_name ";
Mysql> flush privileges;
  The delete statement deletes user records, while the flush statement tells the server to overload the authorization table. (When you use the grant and revoke statements, the table is automatically reloaded. When you directly modify the authorization table

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.