Home > Database > Mysql Tutorial > body text

How to create username and password in mysql

下次还敢
Release: 2024-04-14 19:42:13
Original
529 people have browsed it

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>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

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!

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!