After setting up a fresh Ubuntu install in production with Digital Ocean, it is important to make your new environment extra secure and resilient. The goal is to optimize your installation for scale. In addition to staying up to date, you can follow a few steps that will make your entire server rock on! We are starting with MySQL and what to do when you don't remember your root password, especially when you use the pre-made Ghost image.

You are no exception to the rule, remembering passwords can be complicated. It is often said that memory is fleeting, so you might as well be equipped when you need to find or change a password.

During an automated installation like Ghost on a Digital Ocean server, you probably missed the step concerning MySQL. At least, you may have missed the choice of a password. Here's how to reset it on your Ubuntu 20.04 environment in just a few steps.

Production Library Item #SW-1.002 | RSDB™
Geometric Collection VOL.01
Photo by Resource Database™ / Unsplash

First of all, we assume that you are using Ubuntu. This is the Linux distribution with which the Ghost pre-installation works on Digital Ocean.

Step 1: Verify MySQL Version

This article contains the solution to change your password under Ubuntu 20.04. It is important to make sure that your server is running version 8 of MySQL (or higher). To be sure, here is the command :

mysql --version

You should have an output similar to the following :

mysql Ver 8.0.30-0ubuntu0.20.04.2 for Linux on x86_64 ((Ubuntu))
Production Library Item #OB-1.002 | RSDB™
Geometric Collection VOL.01
Photo by Resource Database™ / Unsplash

Step 2: Skip Grant Tables

Normally, if you try to log in with MySQL but you don't know the root password, you will get an error :

mysql -uroot -p

The outpout of this command should look like something similar as follow :

💡
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

Thus, the system will have to start without granting the tables and this is only possible if you edit the mysqld.cnf in consequence.

Find mysqld section and add skip-grant-table to the file and then save it. We usually like to use the nano command but if you'd rather go with the vi command, we won't blame you at all!

nano /etc/mysql/mysql.conf.d/mysqld.cnf

Ready for the next step? It's now time to start the process that will help you resetting the password.

Production Library Item #SB-1.006 | RSDB™ Geometric Collection VOL.01
Photo by Resource Database™ / Unsplash

Step 3: Restart MySQL Server

To apply the changes, the service must be restarted using the command below :

service mysql restart

Then, you can enter to the database with password using the command as follow :

mysql -uroot -p
Production Library Item #MP-1.005 | RSDB™
Photo by Resource Database™ / Unsplash

Step 4: Reset Root Password

Now, flush the privileges first. Run the following command:

mysql> flush privileges;

Select the MySQL database. Run the following command:

mysql> use mysql;

And set the new password for the root user, run the following command:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '(YOUR NEW PASSWORD)';

At the place of (YOUR NEW PASSWORD) provide your new password. After successfully changing the MySQL root password, log out from the MySQL shell.

mysql> quit;
Production Library Item #OB-1.001 | RSDB™
Geometric Collection VOL.01
Photo by Resource Database™ / Unsplash

Step 5: MySQL Processes & Service

Thus, you will have to kill all the processes of MySQL before even restarting the MySQL server.

sudo killall -u mysql

And the restart the MySQL service, run the following command:

service mysql restart

Finally, the root can connect to MySQL again using the new password. It is time to improve your database and how your server is dealing with data. If you want to start by fixing Memory and CPU issues with Swap Space, follow this link.

Don't forget to keep your ghost installation up to date. Whenever running a public-facing production web server it’s critically important to keep all software up to date. If you don’t keep everything up to date, you place your site and your server at risk of numerous potential exploits and hacks.

Share this post