How to connect to the MySQL using the command line

March 30, 2022 / MySQL

This guide is going to help you in knowing how to connect to MySQL using a command line.

Steps to Connect to MySQL from the command line:

  1. Log in to your hosting account via SSH.
    (SSH is a command-line interface that allows you to access your hosting account.)
    Type the command at the command prompt, substituting username with your username:
    mysql -u username -p
  2. Enter your password on the Enter Password screen. If the password is correct, the mysql> prompt will appear.
    To view a list of databases, type the following command at the mysql> prompt (remember to include the semicolon):
    show databases;
  3. At the mysql> prompt, type the following command, substituting dbname with the name of the database you want to access:
    (Don’t forget to include the semicolon at the end of the statement.)
    use dbname;
  4. You can perform SQL queries, list tables, and so on after you’ve accessed a database. Furthermore:
    1. Type help at the mysql> prompt to see a list of MySQL commands.
    2. Type q at the mysql> prompt to exit the programme.

This way you can easily connect to your database using the command line.

Spread the love