Table of Contents
MySQL changes root password
MySQL root changes the password of ordinary users
Home Database Mysql Tutorial what is mysql root

what is mysql root

Dec 27, 2021 pm 06:13 PM
mysql root

Root refers to the super administrator; when MySQL is installed, a user named root will be created by default. This user has super permissions and can control the entire MySQL server. The root user has very high permissions and can not only change his own password, but also change the passwords of other users.

what is mysql root

The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.

In mysql, root refers to the super administrator, and the system will default to a super administrator.

When MySQL is installed, a user named root will be created by default. 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.

Generally, the root super administrator has much greater permissions than ordinary users, so some operations require root permissions to run.

The root user has very high permissions and can not only change his own password, but also change the passwords of other users.

MySQL changes root password

In MySQL, the root user has very high permissions, so the security of the root user password must be ensured.

Modify the user table of the MySQL database

Because all account information is stored in the user table, you can change the root user's password directly by modifying the user table.

After the root user logs in to the MySQL server, you can use the UPDATE statement to modify the authentication_string field of the user table of the MySQL database to modify the user's password.

The syntax format for using the UPDATA statement to modify the root user password is as follows:

UPDATE mysql.user set authentication_string = PASSWORD ("rootpwd) WHERE User = "root" and Host="localhost";
Copy after login

The new password must be encrypted using the PASSWORD() function. After executing the UPDATE statement, you need to execute the FLUSH PRIVILEGES statement to reload user permissions.

Example

The following uses the UPDATE statement to change the password of the root user to "rootpwd2".

After logging in to the MySQL server as the root user, the SQL statement and running results are as follows:

mysql> UPDATE mysql.user set authentication_string = password ("rootpwd2")
    -> WHERE User = "root" and Host = "localhost";
Query OK, 1 row affected, 0 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings:0
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.06 sec)
Copy after login

The results show that the password has been changed successfully. And the FLUSH PRIVILEGES; statement is used to load permissions. After logging out, you must log in with a new password.

Use the SET statement to modify the password of the root user

The SET PASSWORD statement can be used to reset the login password of other users or the password of the account you use. The syntax structure of using the SET statement to change the password is as follows:

SET PASSWORD = PASSWORD ("rootpwd");
Copy after login

Example

The following uses the SET statement to change the password of the root user to "rootpwd3".

After logging in to the MySQL server as the root user, the SQL statement and running results are as follows:

MySQL> SET PASSWORD = password ("rootpwd3");
Query OK, 0 rows affected (0.00 sec)
Copy after login

The results show that the SET statement was successfully executed and the root user's password was successfully set to "rootpwd3" .

MySQL root changes the password of ordinary users

Use the SET statement to change the password of ordinary users

In MySQL, only the root user can update the MySQL database to change your password. After logging in to the MySQL server as the root user, you can use the SET statement to modify the ordinary user password. The syntax format is as follows:

SET PASSWORD FOR 'username'@'hostname' = PASSWORD ('newpwd');
Copy after login

Among them, the username parameter is the user name of an ordinary user, the hostname parameter is the host name of an ordinary user, and newpwd is the new password to be changed.

Note: The new password must be encrypted using the PASSWORD() function. If it is not encrypted using PASSWORD(), it will be executed successfully, but the user will not be able to log in.

If an ordinary user changes the password, you can omit the FOR clause to change your own password. The syntax format is as follows:

SET PASSWORD = PASSWORD('newpwd');
Copy after login

Example 1

First create a testuser user without a password. The SQL statement and running results are as follows:

mysql> CREATE USER 'testuser'@'localhost';
Query OK, 0 rows affected (0.14 sec)
Copy after login

After the root user logs in to the MySQL server, then Use the SET statement to change the password of the testuser user to "newpwd". The SQL statement and running results are as follows:

mysql> SET PASSWORD FOR 'testuser'@'localhost' = PASSWORD("newpwd");
Query OK, 0 rows affected, 1 warning (0.01 sec)
Copy after login

It can be seen from the running results that the SET statement is executed successfully and the password of the testuser user is successfully set to "newpwd" ".

The following verifies whether the password of the testuser user has been modified successfully. Exit the MySQL server, log in as the testuser user, and enter the password "newpwd". The SQL statement and running results are as follows:

C:\Users\leovo>mysql -utestuser -p
Enter password: ******
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.7.29-log MySQL Community Server (GPL)
 
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
 
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
Copy after login

It can be seen from the running results that the testuser user logged in successfully and changed the password successfully.

Example 2

Use the testuser user to log in to the MySQL server, and then use the SET statement to change the password to "newpwd1". The SQL statement and running results are as follows:

mysql> SET PASSWORD = PASSWORD('newpwd1');
Query OK, 0 rows affected, 1 warning (0.00 sec)
Copy after login

By It can be seen from the running results that the password was changed successfully.

Use the UPDATE statement to modify the password of an ordinary user

After logging in to the MySQL server as the root user, you can use the UPDATE statement to modify the authentication_string field of the user table of the MySQL database, thereby modifying Password for normal users. The syntax of the UPDATA statement is as follows:

UPDATE MySQL.user SET authentication_string = PASSWORD("newpwd") WHERE User = "username" AND Host = "hostname";
Copy after login

Among them, the username parameter is the user name of an ordinary user, the hostname parameter is the host name of an ordinary user, and newpwd is the new password to be changed.

Note that after executing the UPDATE statement, you need to execute the FLUSH PRIVILEGES statement to reload user permissions.

Example 3

使用 root 用户登录 MySQL 服务器,再使用 UPDATE 语句将 testuser 用户的密码修改为“newpwd2”的 SQL 语句和运行结果如下:

mysql> UPDATE MySQL.user SET authentication_string = PASSWORD ("newpwd2")
    -> WHERE User = "testuser" AND Host = "localhost";
Query OK, 1 row affected, 1 warning (0.07 sec)
Rows matched: 1  Changed: 1  Warnings: 1
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.03 sec)
Copy after login

由运行结果可以看出,密码修改成功。testuser 的密码被修改成了 newpwd2。使用 FLUSH PRIVILEGES 重新加载权限后,就可以使用新的密码登录 testuser 用户了。

使用 GRANT 语句修改普通用户密码

除了前面介绍的方法,还可以在全局级别使用 GRANT USAGE 语句指定某个账户的密码而不影响账户当前的权限。需要注意的是,使用 GRANT 语句修改密码,必须拥有 GRANT 权限。一般情况下最好使用该方法来指定或修改密码。语法格式如下:

GRANT USAGE ON *.* TO 'user'@’hostname’ IDENTIFIED BY 'newpwd';
Copy after login

其中,username 参数是普通用户的用户名,hostname 参数是普通用户的主机名,newpwd 是要更改的新密码。

示例 4

使用 root 用户登录 MySQL 服务器,再使用 GRANT 语句将 testuser 用户的密码修改为“newpwd3”,SQL 语句和运行结果如下:

mysql> GRANT USAGE ON *.* TO 'testuser'@'localhost' IDENTIFIED BY 'newpwd3';
Query OK, 0 rows affected, 1 warning (0.05 sec)
Copy after login

由运行结果可以看出,密码修改成功。

【相关推荐:mysql视频教程

The above is the detailed content of what is mysql root. 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)

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.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

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.

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.

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

Centos install mysql Centos install mysql Apr 14, 2025 pm 08:09 PM

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

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 vs. Other Databases: Comparing the Options MySQL vs. Other Databases: Comparing the Options Apr 15, 2025 am 12:08 AM

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

See all articles