MySQL user management

Source: Internet
Author: User
Tags ip number net domain

The MySQL administrator should know how to set up the MySQL user account and specify which user can connect to the server, where to connect, and what to do after the connection. MySQL 3.22.11 introduces two statements to make this work easier: GRANT statements create MySQL users and specify their permissions, while REVOKE statements delete permissions. The two statements assume the front-end role of the mysql database and provide a different method than directly operating the contents of these tables. The CREATE and REVOKE statements affect four tables:

Authorization Table

Content

User Users who can connect to the server and any global permissions they have
Db Database-level Permissions
Tables_priv Table-level Permissions
Columns_priv Column-level permission

There are also 5th authorization tables host), but it is not affected by GRANT and REVOKE.

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 permission management 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:

Permission specifier

Permitted operations

ALTER Modify tables and Indexes
CREATE Create databases and tables
DELETE Delete existing records in the table
DROP Discard and delete) databases and tables
INDEX Create or discard an index
INSERT Insert a new row to the table
REFERENCE Unused
SELECT Retrieve records in a table
UPDATE Modify existing table records
FILE Read or write files on the server
PROCESS View the thread information executed on the server or kill the thread
RELOAD Reload the authorization table or clear logs, host caches, or table caches.
SHUTDOWN Disable the server
ALL ALL; all privileges Synonyms
USAGE Special "no permission" permission

The table above shows that the permission specifiers in the first group apply to databases, tables, and columns, and the second group manages permissions. Generally, these are relatively strictly authorized because they allow users to affect server operations. The third group has special permissions. "ALL" means "ALL Permissions", and "UASGE" means "no Permissions", that is, creating users, but not granting 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 apply globally to all databases and tables), specific databases apply 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 identified by clause for a new user, it is insecure if the user is not assigned a password ). 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 identified by, 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 SET password.

The with grant option 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@.snake.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 MySQL 3.23, you can also specify an IP number with a network mask indicating 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 to separate the user name and host name from each other ).

GRANT ALL ON samp_db.president TO "my friend"@"boa.snake.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 ethel@localhost IDENTIFIED BY "coffee" WITH GRANT 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, such as FILE, PROCESS, RELOAD, and SHUTDOWN, are administrative permissions and can only be authorized with a "ON *. *" Global permission identifier. 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 read-only user ro_user who strictly restricts access). Only all tables in the samp_db database can be accessed, but only tables can be 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.net INDETIFIED 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 TO bill@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 TO assistant @ 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 and use tmp.Tbl_nameCreate an anonymous user by referencing the table in tmp in the user-specified character, and any user matches 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 with grant option. 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 with grant option 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 syntax of REVOKE is very similar TO the GRANT statement, except that it is replaced by from without the indetifed by and with grant option clauses:

REVOKEPrivileges(Columns) ONWhatFROMUser

UserSome must match those of the user you want to revoke permissions from the original GRANT statement.User.PrivilegesSome do not need to be matched. You can use the GRANT statement to authorize, 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 mysqlmysql>DELETE FROM user     ->WHERE User="user_name" and Host="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, but you do not modify the authorization table directly .)


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.