As a Linux server administrator, you must have encountered the situation of forgetting the MySQL database password more than once. When you need to manage or modify the MySQL database, you suddenly find that you cannot log in, or the password you set previously no longer applies. At this time, don’t panic, this article will introduce you to several methods to retrieve the MySQL database password from the Linux system.
Method 1: Use troubleshooting mode to reset password
MySQL provides a special troubleshooting mode (Troubleshooting Mode), which can reset the MySQL root user when the password is forgotten. password.
The steps are as follows:
- Close the MySQL service:
$ systemctl stop mysqld
- Start the MySQL service and enter debugging mode:
$ mysqld_safe --skip-grant-tables &
- Connect to the MySQL service:
$ mysql -u root
- Execute the following command to reset the root user password:
> use mysql; > update user set password=PASSWORD("new_password") where User='root'; > flush privileges; > quit
- Close the MySQL service, Then restart:
$ systemctl stop mysqld $ systemctl start mysqld
After that, try to log in to MySQL using the new password.
Method 2: Use the Linux system account to reset the password
If you used the system user when creating the MySQL user and you have remembered the user's password, then you can use this user to log in and change the MySQL user's password.
The steps are as follows:
- Use the system account to log in to the Linux system;
- Open the command line terminal and enter the following command:
$ mysql -u user_name -p
Among them, user_name is the MySQL user name for which you need to reset the password.
- Enter the password of the MySQL user and press Enter;
- Use the following statement to change the password (replace new_password with your own password):
> SET PASSWORD FOR 'user_name'@'localhost' = PASSWORD('new_password');
- Enter the following command to make the settings take effect:
> FLUSH PRIVILEGES;
- Exit the MySQL terminal:
> quit
Method 3: Change the password through the configuration file
If you know the password of the MySQL root user, then you can directly modify the configuration file and then restart the MySQL service.
The steps are as follows:
- Use vi to edit the MySQL configuration file, which is usually in /etc/my.cnf or /etc/mysql/my.cnf:
$ sudo vi /etc/my.cnf
- Find a paragraph in the file that looks similar to the following:
[mysqld]
- Add the following to the end of the paragraph:
skip-grant-tables
- Save and exit the vi editor;
- Restart the MySQL service:
$ sudo systemctl restart mysqld
- Use the following statement to update the password (replace new_password with your own password ):
$ mysql -u root > use mysql; > update user set password=PASSWORD("new_password") where User='root'; > flush privileges; > quit
- Delete the skip-grant-tables line added in the /etc/my.cnf file;
- Restart the MySQL service:
$ sudo systemctl restart mysqld
Summary
Whether the MySQL database is inaccessible due to forgotten passwords, incorrect operations, or other reasons, don't panic, the above three methods can help you solve the problem. If you encounter a similar situation, please try to follow the above steps to retrieve the MySQL database password.
The above is the detailed content of Forgot mysql password linux. For more information, please follow other related articles on the PHP Chinese website!

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

MySQloffersechar, Varchar, text, Anddenumforstringdata.usecharforfixed-Lengthstrings, VarcharerForvariable-Length, text forlarger text, AndenumforenforcingdataAntegritywithaetofvalues.

Optimizing MySQLBLOB requests can be done through the following strategies: 1. Reduce the frequency of BLOB query, use independent requests or delay loading; 2. Select the appropriate BLOB type (such as TINYBLOB); 3. Separate the BLOB data into separate tables; 4. Compress the BLOB data at the application layer; 5. Index the BLOB metadata. These methods can effectively improve performance by combining monitoring, caching and data sharding in actual applications.

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6
Visual web development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1
Easy-to-use and free code editor
