Table of Contents
1. Use the MySQL secure initialization tool
2. Change the password directly in MySQL
3. Add the root password in the MySQL configuration file
Summary
Home Database Mysql Tutorial How to add MySQL root password?

How to add MySQL root password?

Mar 14, 2024 pm 05:12 PM
mysql Add to root password

MySQL root密码应该如何添加?

MySQL is a popular open source relational database management system commonly used to store and manage data. After installing MySQL, we need to set a root user password to protect the database security. This article will introduce how to add the MySQL root password, as well as specific code examples.

1. Use the MySQL secure initialization tool

MySQL provides a secure initialization toolmysql_secure_installation, which can help us set the root password and other security configurations.

  1. Open the terminal and enter the following command to log in to the MySQL database:
mysql -u root -p
Copy after login
Copy after login
  1. Then enter the default password (if this is the first time you log in, you may not have a password).
  2. Next, execute the following command to start the security initialization tool:
sudo mysql_secure_installation
Copy after login
  1. Follow the prompts, including setting the root password, deleting anonymous users, disabling remote root login, etc.

2. Change the password directly in MySQL

If you do not use the security initialization tool, you can also change the root password directly in MySQL.

  1. Log in to MySQL through the terminal:
mysql -u root -p
Copy after login
Copy after login
  1. Use the following command to modify the password of the root user:
ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';
Copy after login
Copy after login
  1. Modify After completing the password, refresh the permissions to make the changes take effect:
FLUSH PRIVILEGES;
Copy after login

3. Add the root password in the MySQL configuration file

You can also add the root password directly in the MySQL configuration file, This way MySQL will automatically load the password when it starts.

  1. Open the MySQL configuration file (usually /etc/mysql/my.cnf or /etc/my.cnf):
sudo nano /etc/mysql/my.cnf
Copy after login
  1. Add the following under the [mysqld] section:
[mysqld]
skip-grant-tables
Copy after login
  1. Save and close the file, then restart the MySQL service:
sudo systemctl restart mysql
Copy after login
  1. Use the following command to log in to MySQL. At this time, you can directly access it as the root user without a password:
mysql -u root
Copy after login
  1. Change the root password:
ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';
Copy after login
Copy after login
  1. Delete the content added in the configuration file, save and close the file, and restart the MySQL service.

Summary

With the above method, we can easily add the MySQL root password to improve the security of the database. Whether you use a secure initialization tool, change the password directly in MySQL, or add a password in the configuration file, you can effectively protect the database from unauthorized access. Of course, when setting a password, you must ensure that the password is secure and complex to avoid being cracked.

The above is the detailed content of How to add MySQL root 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 Article Tags

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)

PHP's big data structure processing skills PHP's big data structure processing skills May 08, 2024 am 10:24 AM

PHP's big data structure processing skills

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

How to optimize MySQL query performance in PHP?

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

How to use MySQL backup and restore in PHP?

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into a MySQL table using PHP?

What are the application scenarios of Java enumeration types in databases? What are the application scenarios of Java enumeration types in databases? May 05, 2024 am 09:06 AM

What are the application scenarios of Java enumeration types in databases?

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

How to fix mysql_native_password not loaded errors on MySQL 8.4

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

How to use MySQL stored procedures in PHP?

Performance optimization strategies for PHP array paging Performance optimization strategies for PHP array paging May 02, 2024 am 09:27 AM

Performance optimization strategies for PHP array paging

See all articles