How to connect to MySQL from the command line

This article describes how to connect to MySQL from the command line using the mysql program. You can use the mysql program as a quick and easy way to access your databases directly.

Connecting to MySQL from the Command Line

To connect to MySQL from the command line, follow these steps:

  1. Log in to your A2 Hosting account using SSH.
  2. At the command line, type the following command, replacing username with your username:
    mysql -u username -p
  3. At the Enter Password prompt, type your password. When you type the correct password, the mysql> prompt appears.
  4. To display a list of databases, type the following command at the mysql> prompt:

    show databases;
    Make sure you do not forget the semicolon at the end of the statement.
  5. To access a specific database, type the following command at the mysql> prompt, replacing dbname with the name of the database that you want to access:

    use dbname;
    Make sure you do not forget the semicolon at the end of the statement.
  6. After you access a database, you can run SQL queries, list tables, and so on. Additionally:

    • To view a list of MySQL commands, type help at the mysql> prompt.
    • To exit the mysql program, type \q at the mysql> prompt.

    When you run a command at the mysql> prompt, you may receive a warning message if MySQL encounters a problem. For example, you may run a query and receive a message that resembles the following:

    Query OK, 0 rows affected, 1 warning (0.04 sec).

    To view the complete warning message, type the following command:

    SHOW WARNINGS;

More Information

Host Your MySQL Database @ A2

Article Details

  • Level: Intermediate

Did you find this article helpful? Then you'll love our support. Experience the A2 Hosting difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.

We use cookies to personalize the website for you and to analyze the use of our website. You consent to this by clicking on "I consent" or by continuing your use of this website. Further information about cookies can be found in our Privacy Policy.