Home > Database > Mysql Tutorial > body text

How to create a database user in mysql

下次还敢
Release: 2024-04-22 18:51:34
Original
928 people have browsed it

Create a MySQL database user through the following steps: 1. Use the CREATE USER statement to create a new user; 2. Grant permissions (optional); 3. Refresh permissions so that the permissions take effect immediately.

How to create a database user in mysql

Create MySQL database user

Question: How to create a MySQL database user?

Answer:

Create a MySQL database user in the following steps:

1. Use the CREATE USER statement

CREATE USER statement is used to create a new user. The syntax is as follows:

<code class="sql">CREATE USER [IF NOT EXISTS] username IDENTIFIED BY 'password';</code>
Copy after login
  • username: The username of the new user.
  • password: The password of the new user.

For example, create a user named newuser:

<code class="sql">CREATE USER newuser IDENTIFIED BY 'password123';</code>
Copy after login

2. Grant permissions (optional)

New users have no permissions by default. You can grant permissions using the GRANT statement, for example:

<code class="sql">GRANT SELECT, INSERT, UPDATE, DELETE ON database_name.* TO newuser;</code>
Copy after login
  • database_name: The name of the database to which permissions are granted.
  • newuser: The user to whom permissions are to be granted.

3. Refresh permissions

In order for the permissions to take effect immediately, you need to refresh the permissions:

<code class="sql">FLUSH PRIVILEGES;</code>
Copy after login

Other notes:

  • IF NOT EXISTS: This clause prevents the creation of duplicate users if the user already exists.
  • Password security: Choose a safe and reliable password for the user.
  • Permission management: Grant users appropriate permissions as needed to avoid over-authorization.
  • Restrict user access: You can restrict users to only access specific databases or tables as needed.

The above is the detailed content of How to create a database user in mysql. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!