Home > Database > Mysql Tutorial > A brief discussion on how to add, delete users and authorize in MySQL

A brief discussion on how to add, delete users and authorize in MySQL

青灯夜游
Release: 2021-10-08 18:49:53
forward
2317 people have browsed it

This article will introduce you to the user management in MySQL and introduce the methods of adding users, authorizing and deleting users. I hope it will be helpful to you!

A brief discussion on how to add, delete users and authorize in MySQL

Do not directly use the root user to manage application data. [Related recommendations: mysql video tutorial]

Add user

Log in to the database as root user and run the following command:

create user zhangsan identified by 'zhangsan';
Copy after login

The above command creates user zhangsan, the password is zhangsan. You can view the new user information in the mysql.user table:

select User, Host, Password from mysql.user where User = 'zhangsan';
Copy after login

Authorization

Command format: grant privilegesCode on dbName.tableName to username@host identified by "password";

grant all privileges on zhangsanDb.* to zhangsan@'%' identified by 'zhangsan';
flush privileges;
Copy after login

The above The statement authorizes all operation permissions of the zhangsanDb database to the user zhangsan.

You can view the new additions in the mysql.db table Database permission information:

select User, Db, Host, Select_priv, Insert_priv, Update_priv, Delete_priv from mysql.db where User = 'zhangsan';
Copy after login

You can also view the command executed by permission grant through the show grants command:

show grants for 'zhangsan';
Copy after login

privilegesCode indicates the type of permission granted. , the following types are commonly used [1]

  • ##all privileges: All privileges
  • select: Read privileges
  • delete: Delete permission
  • update: Update permission
  • create: Create permission
  • drop: Delete database and data table permissions

dbName.tableName indicates the specific library or table to which permissions are granted. Commonly used ones include the following Several options

  • .: Grant permissions to all databases on this database server
  • dbName.*: Grant dbName database Permissions on all tables
  • dbName.dbTable: Grant permissions to the dbTable table in database dbName

username@host means The granted user and the IP address that allows the user to log in. The Host has the following types

  • localhost: The user is only allowed to log in locally, not remotely.
  • %: Allow remote login from any machine except this machine
  • 192.168.52.32: Specific IP Indicates that the user is only allowed to log in from a specific IP.

password Specifies the password for the user to log in

flush privileges Indicates refresh permission changes

Modify password

You can modify the user password by running the following command:

update mysql.user set password = password('zhangsannew') where user = 'zhangsan' and host = '%';
flush privileges;
Copy after login

Delete user

Run the following command to delete the user:

drop user zhangsan@'%';
Copy after login

drop user The command will delete the user and the corresponding permissions. After executing the command, you You will find that the corresponding records of the mysql.user table and mysql.db table have disappeared.

Common command group

Create a user and grant all permissions to the specified database

Suitable for web applications to create a MySQL user

create user zhangsan identified by 'zhangsan';
grant all privileges on zhangsanDb.* to zhangsan@'%' identified by 'zhangsan';
flush privileges;
Copy after login

Create the user

zhangsan and assign the database# All permissions of ##zhangsanDB are granted to zhangsan. If you want zhangsan to be able to log in from this machine, you can give localhost additional permissions:

grant all privileges on zhangsanDb.* to zhangsan@'localhost' identified by 'zhangsan';
Copy after login
[Related recommendations: mysql video tutorial

]

The above is the detailed content of A brief discussion on how to add, delete users and authorize in MySQL. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.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