Home Database Mysql Tutorial What is the statement to add mysql username and password?

What is the statement to add mysql username and password?

Oct 15, 2020 am 11:04 AM
mysql password username

Statements for adding user names and passwords in mysql: 1. CREATE USER statement, you can create an ordinary user and set the corresponding password; 2. INSERT statement, you can create a new ordinary user and set the corresponding password; 3. GRANT statement, create a new user, and set the corresponding password and user permissions.

What is the statement to add mysql username and password?

(Recommended tutorial: mysql video tutorial)

When MySQL is installed, a file named root will be created by default. User, this user has super privileges and can control the entire MySQL server.

In the daily management and operation of MySQL, in order to prevent someone from maliciously using the root user to control the database, we usually create some users with appropriate permissions and use the root user as little or as little as possible to log in to the system. to ensure secure access to data.

MySQL provides the following 3 methods to create users, add user names and passwords.

  • Use the CREATE USER statement to create a user and add a username and password.

  • Add a user in the mysql.user table, add user name and password.

  • Use the GRANT statement to create a user and add a username and password.

The following will explain these three methods in detail based on examples.

1. Use the CREATE USER statement to create a user

You can use the CREATE USER statement to create a MySQL user and set the corresponding password. The basic syntax format is as follows:

CREATE USER <用户> [ IDENTIFIED BY [ PASSWORD ] &#39;password&#39; ] [ ,用户 [ IDENTIFIED BY [ PASSWORD ] &#39;password&#39; ]]
Copy after login

Parameter description is as follows:

1) User

specifies the creation of a user account, the format is user_name'@'host_name . Here user_name is the user name, and host_name is the host name, which is the name of the host used by the user to connect to MySQL. If only the user name is given without specifying the host name during the creation process, the host name defaults to "%", which represents a group of hosts, that is, permissions are open to all hosts.

2) IDENTIFIED BY clause

is used to specify the user password. New users do not need to have an initial password. If the user does not have a password, this clause can be omitted.

3) PASSWORD 'password'

PASSWORD means using a hash value to set the password. This parameter is optional. If the password is a plain string, there is no need to use the PASSWORD keyword. 'password' represents the password used by the user to log in and needs to be enclosed in single quotes.

You should pay attention to the following points when using the CREATE USER statement:

  • The CREATE USER statement does not need to specify an initial password. However, from a security perspective, this approach is not recommended.

  • To use the CREATE USER statement, you must have the INSERT permission of the mysql database or the global CREATE USER permission.

  • After using the CREATE USER statement to create a user, MySQL will add a new record in the user table of the mysql database.

  • The CREATE USER statement can create multiple users at the same time. Multiple users are separated by commas.

Newly created users have very few permissions and they can only perform operations that do not require permissions. For example, log in to MySQL, use the SHOW statement to query the list of all storage engines and character sets, etc. If two users have the same username but different hostnames, MySQL treats them as two users and allows the two users to be assigned different sets of permissions.

Example 1

Use CREATE USER to create a user, the username is test1, the password is test1, and the host name is localhost. The SQL statement and execution process are as follows.

mysql> CREATE USER &#39;test1&#39;@&#39;localhost&#39; IDENTIFIED BY &#39;test1&#39;;
Query OK, 1 rows affected (0.06 sec)
Copy after login

The results show that the test1 user was successfully created.

In practical applications, we should avoid specifying passwords in clear text. We can use the PASSWORD keyword to set the password using the hash value of the password.

Example 2

In MySQL, you can use the password() function to obtain the hash value of the password. The SQL statement and execution process for viewing the test1 hash value are as follows:

mysql> SELECT password(&#39;test1&#39;);
+-------------------------------------------+
| password(&#39;test1&#39;)                         |
+-------------------------------------------+
| *06C0BF5B64ECE2F648B5F048A71903906BA08E5C |
+-------------------------------------------+
1 row in set, 1 warning (0.00 sec)
“*06C0BF5B64ECE2F648B5F048A71903906BA08E5C”就是 test1 的哈希值。下面创建用户 test1,SQL 语句和执行过程如下:
mysql> CREATE USER &#39;test1&#39;@&#39;localhost&#39;IDENTIFIED BY PASSWORD &#39;*06C0BF5B64ECE2F648B5F048A71903906BA08E5C&#39;;
Query OK, 0 rows affected, 1 warning (0.00 sec)
Copy after login

After successful execution, you can log in using the password "test1".

2. Use the INSERT statement to create a new user

You can use the INSERT statement to add user information to the mysql.user table, but you must have INSERT permission on the mysql.user table. Usually the INSERT statement only adds the values ​​of the three fields Host, User and authentication_string.

The password field in the user table of MySQL 5.7 has changed from Password to authentication_string. If you are using a version before MySQL 5.7, just replace the authentication_string field with Password.

The code to create a user using the INSERT statement is as follows:

INSERT INTO mysql.user(Host, User,  authentication_string, ssl_cipher, x509_issuer, x509_subject) VALUES (&#39;hostname&#39;, &#39;username&#39;, PASSWORD(&#39;password&#39;), &#39;&#39;, &#39;&#39;, &#39;&#39;);
Copy after login

Since the three fields ssl_cipher, x509_issuer and x509_subject in the user table of the mysql database do not have default values, new records are inserted into the user table. , be sure to set the values ​​of these three fields, otherwise the INSERT statement will not be executed.

Example 3

The following uses the INSERT statement to create a user named test2. The host name is localhost and the password is also test2. The SQL statement and execution process are as follows:

mysql> INSERT INTO mysql.user(Host, User, authentication_string, ssl_cipher, x509_issuer, x509_subject) VALUES (&#39;localhost&#39;, &#39;test2&#39;, PASSWORD(&#39;test2&#39;), &#39;&#39;, &#39;&#39;, &#39;&#39;);
Query OK, 1 row affected, 1 warning (0.02 sec)
Copy after login

结果显示,新建用户成功。但是这时如果通过该账户登录 MySQL 服务器,不会登录成功,因为 test2 用户还没有生效。

可以使用 FLUSH 命令让用户生效,命令如下:

FLUSH PRIVILEGES;
Copy after login

使用以上命令可以让 MySQL 刷新系统权限相关表。执行 FLUSH 命令需要 RELOAD 权限。

注意:user 表中的 User 和 Host 字段区分大小写,创建用户时要指定正确的用户名称或主机名。

3. 使用GRANT语句新建用户

虽然 CREATE USER 和 INSERT INTO 语句都可以创建普通用户,但是这两种方式不便授予用户权限。于是 MySQL 提供了 GRANT 语句。

使用 GRANT 语句创建用户的基本语法形式如下:

GRANT priv_type ON database.table TO user [IDENTIFIED BY [PASSWORD] &#39;password&#39;]
Copy after login

其中:

  • priv_type 参数表示新用户的权限;

  • database.table 参数表示新用户的权限范围,即只能在指定的数据库和表上使用自己的权限;

  • user 参数指定新用户的账号,由用户名和主机名构成;

  • IDENTIFIED BY 关键字用来设置密码;

  • password 参数表示新用户的密码。

例 4

下面使用 GRANT 语句创建名为 test3 的用户,主机名为 localhost,密码为 test3。该用户对所有数据库的所有表都有 SELECT 权限。SQL 语句和执行过程如下:

mysql> GRANT SELECT ON*.* TO &#39;test3&#39;@localhost IDENTIFIED BY &#39;test3&#39;;
Query OK, 0 rows affected, 1 warning (0.01 sec)
Copy after login

其中,“*.*” 表示所有数据库下的所有表。结果显示创建用户成功,且 test3 用户对所有表都有查询(SELECT)权限。

技巧:GRANT 语句是 MySQL 中一个非常重要的语句,它可以用来创建用户、修改用户密码和设置用户权限。教程后面会详细介绍如何使用 GRANT 语句修改密码、更改权限。

The above is the detailed content of What is the statement to add mysql username and password?. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1264
29
C# Tutorial
1237
24
MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL for Beginners: Getting Started with Database Management MySQL for Beginners: Getting Started with Database Management Apr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

Solve MySQL mode problem: The experience of using the TheliaMySQLModesChecker module Solve MySQL mode problem: The experience of using the TheliaMySQLModesChecker module Apr 18, 2025 am 08:42 AM

When developing an e-commerce website using Thelia, I encountered a tricky problem: MySQL mode is not set properly, causing some features to not function properly. After some exploration, I found a module called TheliaMySQLModesChecker, which is able to automatically fix the MySQL pattern required by Thelia, completely solving my troubles.

See all articles