Home > Database > Mysql Tutorial > How to create users and grant authorization in Mysql8

How to create users and grant authorization in Mysql8

PHPz
Release: 2023-05-30 16:55:06
forward
6376 people have browsed it

1. Enter mysql

mysql -uroot -p
Copy after login

2. Create user

create user 'testuser1'@'%' identified by '123456';
Copy after login

This means creating a user testuser1 that does not restrict IP login.

The user’s password is 123456

% means no restriction on ip login

Refresh the permissions, refresh them after each permission change

flush privileges;
Copy after login

You can log in to the user by creating a new local connection

How to create users and grant authorization in Mysql8

When you open it, you will find that there is only one database information_schema

How to create users and grant authorization in Mysql8

3. Empower users

grant all privileges on test_grant.* to 'testuser1'@'%' with grant option;
Copy after login

Here it means giving User testuser1 grants all permissions to all tables in the database test_grant (this is the database I created before)

with grant option means that the user can grant permissions to other users, but cannot exceed the user's permissions

Looking at this time, user testuser1 has an additional test_grant database

How to create users and grant authorization in Mysql8

all privileges here can be replaced with select, insert, update, delete, drop, create, etc.

4. View user permissions

show grants for 'testuser1'@'%';
Copy after login

How to create users and grant authorization in Mysql8

5. Revoke user permissions

revoke all privileges on test_grant.* from 'testuser1'@'%';
Copy after login

This means revoking all operating permissions of user testuser1 on database test_grant

Note: If you write like this here, you will find that you still have the database test_grant when you open it (but you cannot operate the database). This is because I used with grant option when I created it before, because all privileges are except All permissions with grant option

Execute the following statement to reclaim all user permissions

revoke all privileges,grant option from 'testuser1'@'%';
Copy after login

6. Delete users

drop user 'testuser1'@'%';
Copy after login

7. Query all users and their permissions

SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;
Copy after login

You can also do this

SELECT User, Host FROM mysql.user;
Copy after login

Attachment: View user permissions

show grants for ‘#userName'@'#host';
Copy after login

#userName represents the user name

#host represents the access permissions, as follows

  • % represents wildcard all host address permissions (remote access is available)

  • localhost is local permission (remote access is not possible)

  • Specify special IP access rights such as 10.138.106.102

##????What this dog wants to check is testUser

show grants for 'testUser'@'%';
Copy after login

How to create users and grant authorization in Mysql8

The above is the detailed content of How to create users and grant authorization in Mysql8. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template