Skip to content

Instantly share code, notes, and snippets.

@webmechanicx
Last active January 28, 2026 17:22
Show Gist options
  • Select an option

  • Save webmechanicx/c9ecca3c168a3482fbd264bc33f89c06 to your computer and use it in GitHub Desktop.

Select an option

Save webmechanicx/c9ecca3c168a3482fbd264bc33f89c06 to your computer and use it in GitHub Desktop.
How to Reset MySQL Password on Amazon AWS EC2

How to Reset MySQL Password on Amazon AWS EC2

Follow these steps (can be helpful if you really forget your password and you can try it anytime, even if you're not in the situation at the moment 😉):

1. Stop mysql

sudo /etc/init.d/mysql stop

Or for other distribution versions:

sudo /etc/init.d/mysqld stop

2. Start MySQL in safe mode

sudo mysqld_safe --skip-grant-tables &

3. Log into MySQL using root

mysql -u root

4. Select the MySQL database to use

use mysql;

5. Reset the password

-- MySQL version < 5.7
update user set password=PASSWORD("mynewpassword") where User='root';

-- MySQL 5.7, mysql.user table "password" field -> "authentication_string"

update user set authentication_string=password('mynewpassword') where user='root';

6. Flush the privileges

flush privileges;

7. Restart the server

quit

8. Stop and start the server again

Ubuntu and Debian:

sudo /etc/init.d/mysql stop
...
sudo /etc/init.d/mysql start

On CentOS, Fedora, and RHEL:

sudo /etc/init.d/mysqld stop
...
sudo /etc/init.d/mysqld start

9. Login with a new password

mysql -u root -p

10. Type the new password and enjoy your server again like nothing happened

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment