How do I reset the MariaDB root password on an Amazon Elastic Compute Cloud (Amazon EC2) instance that's running Amazon Linux 2?
Short description
By default, MariaDB 5.5 on Amazon Linux 2 doesn't have a root password. If you create a root password for MariaDB and then lock yourself out of your database, you must reset the root password.
Note: You can't query your database while you’re resetting the root password.
Resolution
1. Verify that you have a recent snapshot of the volume where the MariaDB data directory resides. You can recreate the volume from this snapshot, if needed.
2. Stop the MariaDB service:
sudo systemctl stop mariadb
3. Start MariaDB in safe mode:
sudo mysqld_safe --skip-grant-tables --skip-networking &
4. Run the following command to set the MariaDB command history to /dev/null and log in to the MariaDB monitor:
Important: Setting the command history /dev/null prevents your password information from appearing in plaintext in the history file. You can reset this value after completing the full procedure.
export MYSQL_HISTFILE=/dev/null
mysql -u root
5. Update your MariaDB root password:
UPDATE mysql.user SET Password = PASSWORD('new_password_here') WHERE User = 'root';
The output looks like this:
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
6. Flush the privileges to reload the grant tables. Your new password takes effect after the reload:
FLUSH PRIVILEGES;
7. Exit the MariaDB monitor:
exit
8. Stop the MariaDB process:
sudo kill `sudo cat /var/run/mariadb/mariadb.pid`
Note: The process might take a few seconds to terminate.
9. Start the MariaDB service:
sudo systemctl start mariadb
10. Test your new MariaDB root password:
mysql -u root -p