Resetting MySQL Password in a Docker Container

Are you having trouble remembering your MySQL password? Do you need to reset it for some reason? In this article, we’ll show you how to reset the MySQL password in a Docker container.

Before We Begin

To start, you’ll need to have Docker installed on your system. If you don’t already have it, you can download and install it from the official Docker website. You can follow our guides in Installing Docker on Windows and Install Docker on MacOS.

Step 1: Stop the MySQL Container

The first step is to stop the MySQL container if it’s currently running. You can do this using the following command:

docker-compose stop mysql

This will bring down the MySQL container, stopping any active connections and shutting down the database server.

Step 2: Enter the MySQL Container

Next, you’ll need to enter the Docker container where MySQL is installed. To do this, use the following command:

docker-compose run mysql bash

This will start a new container from the existing mysql image and open an interactive shell inside it.

Step 3: Start the MySQL Server with Skipping Password Verification

Now that you’re inside the MySQL container, you can start the database server. To do this without verifying the password, use the following command:

mysqld --skip-grant-tables &

This will start the MySQL server in non-interactive mode, skipping password verification and allowing you to reset your password.

Waiting for the Server to Start

You’ll need to wait about 10 seconds for the MySQL server to finish starting up. This may take a little longer depending on your system’s performance.

Step 4: Resetting the MySQL Password

Once the server is running, you can use the following commands to reset your password:

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

Replace 'NEW_PASSWORD' with the new password you want to set for the root user.

Step 5: Testing the New Password

Finally, use the following command to test your new password:

mysql -u root -p

This will prompt you for the new password. If everything went smoothly during the reset process, you should be able to log in successfully with your new password.

Conclusion

Resetting a MySQL password in a Docker container is relatively straightforward. By following these steps, you should be able to recover access to your database server and get back to work on your project or application. Remember to replace 'NEW_PASSWORD' with your desired password whenever you run the reset commands.

Note: It’s generally not recommended to use the --skip-grant-tables option in production environments as it can leave your database vulnerable to security risks.