mysql create users, assign permissions, delete users

To create a user through the create user command, there are only two ways :( introduced by create user command, the user table to insert data in a straightforward manner, can not say here)

Create a user at the same time, the user can specify the login and password of the host

create user 'test_user'@'%' identified by "123"; 
create user 'test_user'@'localhost' identified by "123"; 
create user 'test_user'@'127.0.0.1' identified by "123"; 


grant all privileges on test_db.* to 'test_user'@'%';
grant select,update,delete,insert,drop on test_db.* to 'test_user'@'%';
grant all privileges on test_db.* to 'test_user'@'%' identified by '1112';
grant all privileges on test_db.* to 'test_user'@'%' with grant option;

First, explain to create a user command parameters:

  • 'test_user'Is the user name
  • @Followed by the specified login host, 'localhost'and '127.0.0.1'said we could only log on locally; '%' represents only log on the remote host
  • identified byFollowed password
  • Tips: The above command, the user name, login host, password, recommends that you use quotation marks to wrap , to prevent unnecessary trouble

Command to assign permissions parameters:

  • grant Is the command to assign permissions
  • all privileges Refers to all rights, it can be like the second as specify certain permissions to users
  • test_db.*Test_db library represents all the tables, you can specify a table test_db.table1or all tables in all databases,*.*
  • identified by Specify a password, if not specified, the default password used when creating the user, or may be different permissions to different passwords, or login host to various different passwords are possible
  • with grant option If you take this parameter, indicating that the user is assigned privileges, you can also assign their rights to others
  • Permission to create a grant statement is no need to manually refresh the authorization form, as it has been automatically refreshed.

On the front of a three commands can only be used simultaneously as create user can not create a user with the same name

So the question becomes, suppose that you create a user through the first command, then the user can only log in on the remote host, and can not log on locally, if you log on locally, the error will be reported:ERROR 1045 (28000): Access denied for user 'card_test1'@'localhost' (using password: YES)

This error appears there are many circumstances, as far as I know, it might be:

  1. Users do not have permission to log on locally, that is, 'card_test1'@'localhost'the user does not exist, because there is only 'card_test1'@'%'this user
  2. Wrong password

To solve this problem, by the second approach, that is, create user when these parameters are not assigned login host

Create a user only, do not assign a host

1. create user test_user;
2. create user test_user identified by "123";

3. grant all privileges on test_db.* to 'test_user'@'%' identified by "1234";
4. grant all privileges on test_db.* to 'test_user'@'localhost'identified by "1235";

The above command, performs a 1,2, simply create a user, or both assigned password;

Then, 3,4 two can be performed, so that the user can make test_user, both to log on locally, but also in remote login, if the grant when different passwords, but also the same in different host login a user, use a different password

Cancel user rights

Use revokestatement

revoke all on test_db.* from 'test_user'@'localhost';

This command will cancel the test_user the user, all privileges of the logged on locally

delete users

drop user 'test_user'@'%';
drop user 'test_user'@'localhost';

Modify the specified user's password

update mysql.user set password=password('new_passwd') where User='test_user' and Host='%';

Create a user permission to create or modify permissions to users, are available through direct manipulation mysql.user table; direct attention to the operating table, then, need flush privileges;to refresh the authority to order

Guess you like

Origin www.cnblogs.com/zhang-can/p/12053815.html