Home  >  Article  >  Database  >  How to create username and password in mysql

How to create username and password in mysql

下次还敢
下次还敢Original
2024-04-14 19:42:13520browse

Steps to create a username and password for the MySQL database: Create user: CREATE USER 'username'@'hostname' IDENTIFIED BY 'password'; Grant permissions: GRANT ALL PRIVILEGES ON database_name.* TO 'username'@' hostname'; Refresh privilege table: FLUSH PRIVILEGES.

How to create username and password in mysql

MySQL Create Username and Password

To create a username and password for the MySQL database, follow these steps:

1. Create a new user

<code class="sql">CREATE USER 'username'@'hostname' IDENTIFIED BY 'password';</code>

where:

  • username is the new username to be created.
  • hostname Specifies the hosts from which users can connect to the database. You can use the % wildcard to allow connections from any host.
  • password is the password of the new user.

2. Grant user permissions

To grant a new user access to the database, use the GRANT statement. For example:

<code class="sql">GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'hostname';</code>

Where:

  • database_name is the name of the database to which access is to be granted.
  • ALL PRIVILEGES Grants the user all permissions on the database. Specific permissions can also be specified.

3. Refresh the permissions table

After granting permissions, refresh the permissions table for the changes to take effect:

<code class="sql">FLUSH PRIVILEGES;</code>

Example

To create a new user with password secretpassword for the user newuser with hostname localhost, and grant it mydb For all permissions on the database, please use the following statement:

<code class="sql">CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'secretpassword';
GRANT ALL PRIVILEGES ON mydb.* TO 'newuser'@'localhost';
FLUSH PRIVILEGES;</code>

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

Statement:
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