Ubuntu 18.04 Change the Root Password of MySQL

The latest MySQL 5.7.1 install without prompting for root password on the Ubuntu 18.04 server. If you use the mysql_secure_intallation it will ask you for setting a new root password, which is perfectly fine. The issue is it will not change the root password, and will leave MySQL unsecured.

Changing the root password manually is the right solution. However, it will not be straightforward as some files and folders required by MySQL are not created during the installation. You may need to go through several installation runs. Make sure you clear up the system by purging, autoremoving, and autocleaning every time you start a new MySQL installation attempts.

1. Clean Up the System of MySQL Traces and Update, Upgrade

sudo apt purge mysql*
sudo apt autoremove
sudo apt autoclean
sudo apt upgrade
sudo apt update
sudo apt dist-update

2. Install MySQL

sudo apt install mysql-server mysql-client

3. Create Missing Directory

mkdir -p /var/run/mysqld
chown mysql:mysql /var/run/mysqld

3. Stop and Kill MySQL Service

/etc/init.d/mysql stop
sudo kill (mysql process id)

4. Start MySQL in Elevated Mode and Go in MySQL console

sudo mysqld_safe --skip-grant-tables --skip-networking
sudo mysql

5. Type in the MySQL Console

use mysql;
describe user;
UPDATE mysql.user SET authentication_string=PASSWORD('NEWPASSWORDHERE'), plugin='mysql_native_password' WHERE User='root';
flush privileges;

6. Kill and Start the MySQL Service

kill (mysql process id)
/etc/init.d/mysql start


If you are lucky now you should have a secured MySQL database ready to work with.