[MySQL] account and permission management _ MySQL

Source: Internet
Author: User
[MySQL] account and permission management bitsCN.com

[MySQL] account and permission management

MySQL initial account management

The initial MySQL account is as follows:

[sql] [root@lx16 ~]# mysql -u root  mysql> select host,user,password from mysql.user;  +-----------+------+----------+  | host      | user | password |  +-----------+------+----------+  | lx16      | root |          |  | 127.0.0.1 | root |          |  | ::1       | root |          |  | localhost |      |          |  | lx16      |      |          |  | localhost | root |          |  +-----------+------+----------+  

MySQL has two types of initial users:

Root super account: has all permissions and can do anything.

Anonymous account: anyone can connect to the server through it, but it has little permissions.

By default, these accounts do not have any passwords. therefore, to ensure security, we must first set a password for all root accounts.

The first way to SET a PASSWORD is to use the set password statement. if we want to SET a PASSWORD for 'root' @ 'localhost', run the following command:

[sql] mysql> set password for 'root'@'localhost'=password('*****');  

The second method to set a password is to directly update the user permission table. the advantage of this method is that you can set a password for multiple accounts at the same time. the following statement can modify the password of all root accounts at a time:

[sql] mysql> update mysql.user set password=password('***') where user='root';  mysql> flush privileges;  

If you use the update method, you must explicitly tell the server to reload the permission table (flush privileges)

We strongly recommend that you delete anonymous accounts. The DELETE statement is as follows:

[sql] mysql> drop user ''@'localhost';  mysql> drop user ''@'lx16'; 

After the preceding operations are performed, the data in the user permission table is as follows:

[sql] mysql> select host,user,password from mysql.user;  +-----------+------+-------------------------------------------+  | host      | user | password                                  |  +-----------+------+-------------------------------------------+  | lx16      | root | *578EC7851088AC1F2A67B100540344B03BD2BA99 |  | 127.0.0.1 | root | *578EC7851088AC1F2A67B100540344B03BD2BA99 |  | ::1       | root | *578EC7851088AC1F2A67B100540344B03BD2BA99 |  | localhost | root | *578EC7851088AC1F2A67B100540344B03BD2BA99 |  +-----------+------+-------------------------------------------+  

Create a new account

MySQL requires that you not only specify who (user_name) can connect, but also specify where (host_name) to connect, that is, even if the two accounts have the same name, if they will be connected from different clients, you also need to create an account for them.

You can use the following two wildcards to flexibly configure host name restrictions:

'%'-Match any multiple characters

'-Configure a character

[SQL] The test account can connect to the create user 'test' @ '% 'identified by' *** 'from any IP address '***'; the test account can only connect to the create user 'test' @ 'localhost' identified by '***' locally. 168.2.% 'network segment connection create user 'test' @ '192. 168.2.% 'identified by '***'; you can also use the IP mask create user 'test' @ '192. 168.2.2

Permission management

To authorize an account, you must use the Grant statement. if the account already exists, Grant the statement to authorize it. if the account does not exist, create the Grant statement first, and then authorize it.

You can use the show grants statement to obtain your own permissions:

[sql] mysql> show grants;  +----------------------------------------------------------------------------------------------------------------------------------------+  | Grants for root@localhost                                                                                                              |  +----------------------------------------------------------------------------------------------------------------------------------------+  | GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*578EC7851088AC1F2A67B100540344B03BD2BA99' WITH GRANT OPTION |  +----------------------------------------------------------------------------------------------------------------------------------------+  

You can also use show grants for to obtain permissions of other users:

[sql] mysql>show grants for ''@'localhost';  +--------------------------------------+  | Grants for @localhost                |  +--------------------------------------+  | GRANT USAGE ON *.* TO ''@'localhost' |  +--------------------------------------+  

The above shows two types of special permissions. one is ALL (the PRIVILEGES keyword can be omitted later), indicating the permissions for ALL operations (but not Grant permissions, grant permissions are granted by with grant option), USAGE, and non-permission.

In some rare cases, we may need more detailed permission control. MySQL can authorize permissions on columns. the following statement indicates that the select permission for the entire table is given to test, however, only the update permission for the two columns (street, city) is granted to it:

[sql] grant select, update (street,city) on sampdb.member to 'test'@'localhost';  


BitsCN.com

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.