Search The Hostwinds Guides Knowledge Base

How To Use MySQL / MariaDB From Command Line

Share This Article

While tools like phpMyAdmin make interacting with MySQL / MariaDB databases very easy, sometimes one must access the database directly from the command line. This article will touch on getting into a database and some common tasks, but will not provide a full education on SQL syntax, database management, or other high-level topics. The examples in this guide are for CentOS 7 and MariaDB as included in our WordPress VPS image but should work on our cPanel VPSes, LAMP stack, and others. This page assumes you have Connected to Your Server with SSH.

#  prompts indicate something that should be entered from the Bash command line, > prompts are inside MySQL itself.

Common MySQL Tasks Done Through Command Line

Login To MySQL Database

To log in to the database as the root user use the following command:

Enter the root password.

Reset Your MySQL password

On CentOS 7:

(Other systemd-based Linux distros may have similar commands depending whether they run actual MySQL or MariaDB; other init systems will be different)

Once you run mysql -u root -p  and enter your password you will be presented with a prompt that tells you the program really running (MariaDB) and the database being used:

 

List Your Databases

Issue the show databases;  command as seen below to see all databases. An example is shown below:

Switch Database With The ‘ use ‘ command:

The ‘ show ‘ command is also used to list the tables in a database:

Always Take a Backup Before Making ANY Changes

Use ‘ mysqldump databasename > databasebackup.sql ‘ to make a backup of your database before continuing with this guide is strongly recommended. Replace databasename  with your actual database name and databasebackup  with the name of the file you would like to create and end it append it with .sql  as the type of file to save your database to. This will allow you to restore MySQL Databases Using mysqldump from this backup file at any time.

We recommend that you run this command from a directory that is not publicly accessible so that your database cannot be downloaded from your account without logging into the command line, or FTP. Make sure to change your directory to /root  or /home or another location in your file system that requires proper credentials. 

Example: Resetting the WordPress Admin password

See the above instructions on how to take a backup of your database before proceeding.

Step One: You must know what database, username, and password are being used by the WordPress installation. These are in wp-config.php in the root directory of your WordPress installation as DB_NAME, DB_USER, and DB_PASSWORD:

Step Two: With this information, you can adapt the instructions from How to Reset WordPress Admin Password and do the same from the command line:

Step Three: Switch to the appdb database:

Step Four: and show the tables:

Step Five: Then we can SELECT user_login  and user_pass  from the  wp_users table to see what row we will be updating:

Step Six: Which lets us set the new password with

Step Seven: And we can see the new password hash with the same SELECT again

That’s it! Your new admin password is set.

Related Articles: