When will it be needed

  • When connecting to an external system, you need to grant access rights to other systems
  • In this system, manage data by permissions to prevent root permission from deleting database and running šŸ˜‚

Mysql version

MySql8.0 +

Specific steps

1. Run the MySql command

Mysql -u#UserName -p#PassWord

#UserName indicates your MySql UserName. #PassWord indicates your MySql PassWord

šŸ¶ The user name and password of the dog are root

mysqlĀ -urootĀ -proot

Copy the code
The mysql.png command is displayed

2. Access the database

If no database is created, run the command first. If a database already exists, skip this step

create database #databaseName;

#databaseName represents the database you are working on

šŸ¶ this dog to create a B2B database, remember to add a semicolon;

createĀ databaseĀ b2b;

Copy the code

use databaseName;

#databaseName represents the database you are working on

šŸ¶ this dog to operate b2b database, remember to add semicolon;

useĀ b2b;

Copy the code
The. PNG database is displayed

3. Create a user

create user ‘#userName’@’#host’ identified by ‘#passWord’;

#userName represents the new user account you want to create for the database. #host represents the access permission

  • % indicates all host address permissions (remote access)
  • Localhost: local permission (not remotely accessible)
  • Specify a special Ip access permission such as 10.138.106.102

#passWord represents the new passWord you want to create for this database

šŸ¶ The user to be created is testUser, and the password is Haier… The password strength must be lowercase, lowercase, and alphanumerical. Otherwise, the password strength does not match āš ļø. ERROR 1396 (HY000) is displayed if the user name is the same: Operation CREATE USER failed for ‘testUser’@’%’

create user 'testUser'@'%' identified by 'Haier... 123 ';

Copy the code
Create user.png

4. View users

The mysql database is displayed

user mysql;

View user information

select host, user, authentication_string, plugin from user;

userĀ mysql;Ā 

selectĀ host,Ā user,Ā authentication_string,Ā pluginĀ fromĀ user;

Copy the code

šŸ¶ If the displayed information contains the newly added user testUser, the user is successfully added. Remember to check to switch back to the operation of the database, the dog needs to operate b2b

useĀ b2b;Ā 

Copy the code
View user.png

5. User authorization

grant #auth on #databaseName.#table to ‘#userName’@’#host’;

#auth stands for permission, as follows

  • All PRIVILEGES All privileges
  • Select query permission
  • The select, insert, update, delete bowdlerize check permissions
  • The select, […]. Increase… Such as permissions

#databaseName indicates the name of the database. #table indicates the specific table

  • * represents all tables
  • A and B are concrete A and B tables

#userName indicates the userName

#host stands for access, as follows

  • % indicates all host address permissions (remote access)
  • Localhost: local permission (not remotely accessible)
  • Specify a special Ip access permission such as 10.138.106.102

šŸ¶ This dog has the permission to add, delete, and change the difference in the B2B database areA_code table

grantĀ select,insert,update,deleteĀ onĀ b2b.area_codeĀ toĀ 'testUser'@'%';

Copy the code
User authorization.png

6. Refresh

šŸ”„ be sure to refresh the authorization for it to take effect

flush privileges;

Refresh. PNG

7. View user rights

show grants for ‘#userName’@’#host’;

#userName indicates the userName

#host stands for access, as follows

  • % indicates all host address permissions (remote access)
  • Localhost: local permission (not remotely accessible)
  • Specify a special Ip access permission such as 10.138.106.102

šŸ¶ This dog wants to view testUser

showĀ grantsĀ forĀ 'testUser'@'%';

Copy the code
PNG to view user permissions

Validation 8.

Verify using visual tools such as NavICat

Validation. The PNG

9. Revoke the permission

revoke #auth on #databaseName.#table from ‘#userName’@’#host’;

#auth stands for permission, as follows

  • All PRIVILEGES All privileges
  • Select query permission
  • The select, insert, update, delete bowdlerize check permissions
  • The select, […]. Increase… Such as permissions

#databaseName indicates the name of the database. #table indicates the specific table

  • * represents all tables
  • A and B are concrete A and B tables

#userName indicates the userName

#host stands for access, as follows

  • % indicates all host address permissions (remote access)
  • Localhost: local permission (not remotely accessible)
  • Specify a special Ip access permission such as 10.138.106.102

šŸ¶ alter table areA_code alter table areA_code alter table area_code alter table area_code alter table area_code alter table area_code

revokeĀ select,insert,update,deleteĀ onĀ b2b.area_codeĀ fromĀ 'testUser'@'%';

Copy the code

šŸ¶ this dog again check user permissions

showĀ grantsĀ forĀ 'testUser'@'%';

Copy the code
Revoking permission. PNG

10. Delete the user

drop user ‘#userName’@’#host’;

#userName indicates the userName

#host stands for access, as follows

  • % indicates all host address permissions (remote access)
  • Localhost: local permission (not remotely accessible)
  • Specify a special Ip access permission such as 10.138.106.102

šŸ¶ The user to be deleted is testUser

dropĀ userĀ 'testUser'@'%';

Copy the code
Delete user.png

šŸ˜‚ This article is by JavaDog original, please indicate the source.