Home  >  Article  >  Database  >  mysql change root password

mysql change root password

WBOY
WBOYOriginal
2023-05-08 11:37:075995browse

MySQL is a popular relational database management system commonly used in fields such as website development and data storage. After understanding the basic knowledge of MySQL, many administrators will need to change the MySQL root password. This article will explore how to change the root password in MySQL.

Before you start, you need to make sure that MySQL is installed. If it is not installed, you can install it on Ubuntu with the following command:

sudo apt update
sudo apt install mysql-server

After completing the installation, you can use the following command to enter the command line of MySQL:

sudo mysql -u root -p

You need to enter the root user on the system The password is required to enter the MySQL command line.

Next, we will introduce two methods to change the root password of MySQL: using mysqladmin and using the command line.

Method 1: Use mysqladmin

Mysqladmin is an official MySQL command line tool that can be used to manage MySQL servers. Here are the steps to change the root password using mysqladmin:

Step 1: Enter the MySQL command line and use the following command to change the root password:

mysqladmin -u root password 新密码

"New password" should be replaced with the new one you want to set password.

Step 2: Check whether the password has been changed successfully by running the following command:

mysqladmin -u root -p ping

Enter your newly set root password. If "mysqld is alive" is successfully output, the password has been changed successfully.

Method 2: Using the command line

Another way to change the MySQL root password is through the command line.

Step 1: Use the following command to enter the MySQL command line interface:

sudo mysql -u root -p

After entering the root user password on the system, you will enter the MySQL command line interface.

Step 2: Change the root password using the following command:

USE mysql;
UPDATE user SET authentication_string=PASSWORD('新密码') WHERE User='root';
FLUSH PRIVILEGES;

Please make sure to replace "new password" with the actual password you want to set.

Step 3: Use the following command to check whether the password was changed successfully:

mysqladmin -u root -p ping

Enter your newly set root password again. If "mysqld is alive" is successfully output, the password has been changed successfully.

The above is how to use mysqladmin and the command line to change the MySQL root password. Either way, it's simple and easy to do. After logging into MySQL using the new password, the administrator can now manage the database through the root user.

The above is the detailed content of mysql change root password. 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
Previous article:Install mysql under linuxNext article:Install mysql under linux