SoFunction
Updated on 2025-03-04

How to check the username and password of local mysql and how to update password

To find the username and password for the local MySQL database, you can do it in the following ways:

1. Find MySQL current user

You can log in to MySQL through the following command and view the username of the currently connected.

step:

Open the command line (terminal/command prompt).

Log in to MySQL: If you know the password of MySQL root user or other user, enter the following command to log in:

mysql -u root -p

or:

mysql -u your_username -p

View the current user: After logging in, run the following SQL command to view the current user:

SELECT USER();

This command returns the MySQL username of the currently connected.

2. Find MySQL user list

To view all users in the MySQL database, you can execute the following command in the MySQL terminal:

SELECT user, host FROM ;

This lists all MySQL users and their hostnames (such as localhost). The user in this list is the username you can use in the local MySQL database.

3. Find MySQL user password

MySQL user passwords are usually not stored directly in configuration files, but are stored in encryption fields in the database. You cannot view your password directly, but you can reset or update your password by:

Reset MySQL user password

Log in MySQL: Log in with a user (usually root) with administrator privileges:

mysql -u root -p

Update user password: If you need to reset your password for a user, you can use the following command:

ALTER USER 'your_username'@'localhost' IDENTIFIED BY 'new_password';

For example, reset the password for the root user:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';

Refresh permissions: After modifying the password, refresh the permissions to ensure they take effect:

FLUSH PRIVILEGES;

4. Check the MySQL configuration file

MySQL's username and password are sometimes saved in the application's configuration files or environment variables (such as files of Spring Boot projects), but MySQL's own passwords are usually not stored directly in these places. If you are deploying MySQL using some automation scripts or configuration management tools, you can sometimes find the default MySQL username and password in the configuration file.

  • The configuration file for MySQL on Linux/MacOS may be located at:

/etc/mysql/
/usr/local/mysql/
/etc/

  • The configuration file for MySQL on Windows may be located at:
    C:\ProgramData\MySQL\MySQL Server \
    Note: The password is usually not stored directly in the configuration file, only the database connection configuration will be configured. If you cannot find your password, it is recommended to reset it.

5. Forgot the MySQL root user's password? How to reset

If you forget the MySQL root user's password, you can reset it as follows:

1. Stop MySQL service

Linux/MacOS:

sudo systemctl stop mysql

Windows: Open Service Manager, locate the MySQL service and stop.

2. Start MySQL to Safe Mode

Start MySQL without loading:

sudo mysqld_safe --skip-grant-tables &

3. Log in to MySQL (no password required)

mysql -u root

4. Reset password

Enter the following command in the MySQL command line to reset the root password:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
FLUSH PRIVILEGES;

5. Restart MySQL service

Linux/MacOS:

sudo systemctl restart mysql

Windows: Restart the MySQL service in Service Manager.

Summarize

You can view the current username through the MySQL command or list all users.

If you forget your password, you can reset the user's password through the command line.

If the root user password is lost, you can restart MySQL in safe mode to reset the password.

This is the article about how to check the username and password of local mysql and how to update passwords. For more information about checking the username password of local mysql, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!