B4J Tutorial [GCE] For Noobs, Part 7 - Changing MariaDB root Password

After installing the MariaDB database we need to change the password for the root user. If the password is not changed then anyone could log into your database do what they like to it.

We’ll do that now while we are in the client. First, we need to get into the MySQL Client by typing in the following at the console

sudo mysql

You should see the ‘mysql>’ prompt after the Welcome details. Follow the next steps to change the root password.
  • type the following at the prompt to see current users

    SELECT user, host, password FROM mysql.user;
    +------+-----------+----------+
    | user | host | password |
    +------+-----------+----------+
    | root | localhost | |
    +------+-----------+----------+
    1 row in set (0.00 sec)


    As you see above the root user does not have a password (assuming that you have not already changed the password for root)
  • type the following to change the password for the root user

    SET PASSWORD FOR 'root'@'localhost' = PASSWORD('new_password');

    Query OK, 0 rows affected, 1 warning (0.00 sec)
  • Now do the first step again to confirm that the password has been set

    SELECT user, host, password FROM mysql.user;

    +------+-----------+-------------------------------------------+
    | user | host | password |
    +------+-----------+-------------------------------------------+
    | root | localhost | *0913BF2E2CE20CE21BFB1961AF124D4920458E5F |
    +------+-----------+-------------------------------------------+
    1 row in set (0.00 sec)


    This time the password is not empty. Note that it’s also a ‘hash’ of the password you used
  • Next, we need to tell MariaDB to clear any cached credentials. Type the following to clear/flush credentials

    FLUSH PRIVILEGES;

    Query OK, 0 rows affected (0.01 sec)
  • To quit the MySQL Client type the following and then press Enter

    quit;
That’s it! You’re all done. If you’re connecting to this database remotely then you will need to provide the password you added here. Using a tool like MySQL Workbench you will be able to create additional users with their own passwords. Creating a different user for accessing the database via a web app is much better and I would highly recommend not using the ‘root’ user for accessing the database via a web app or any other external app. Create specific user account to do this.
 
Top