Ruachost

Mastering MySQL: Command Line Connection Guide

Learn to connect to MySQL with the command line and execute basic SQL commands. Perfect for beginners seeking to improve database skills.

Ruachost Editorial31 October 2026 10 min read
Mastering MySQL: Command Line Connection Guide

Connecting to MySQL from the command line is a practical skill for website owners, developers, and server administrators. The MySQL client lets you inspect databases, run queries, import scripts, and diagnose problems without relying on a graphical interface. This guide walks through the process from environment setup to your first SQL statements, including guidance for connecting within a Ruachost hosting account and troubleshooting failed connections.

Introduction to MySQL and CLI#

MySQL is a relational database management system used to store structured information in tables. Content management systems, online stores, membership platforms, and custom applications commonly use it for content, user accounts, settings, orders, and other application data.

The command-line interface, or CLI, is a text-based way to communicate with a MySQL server. Instead of opening a database management application, you enter the mysql command in a terminal and then submit SQL statements at the mysql> prompt.

Learning the CLI is useful because it:

  • Works well over an SSH connection.
  • Provides direct access without requiring a browser-based interface.
  • Makes it easy to run SQL script files.
  • Helps isolate database, network, and authentication problems.
  • Can execute one-off commands from shell scripts and scheduled tasks.

Three components are involved in a typical connection:

  1. MySQL server: The service that stores databases and processes queries.
  2. MySQL client: The command-line program that sends requests to the server.
  3. MySQL account: A username, password, and host combination with assigned privileges.

The operating-system account used to log in through SSH is not necessarily the same as the MySQL account. On shared hosting, their names may be related, but they remain separate credentials with separate permissions.

Another important distinction is the difference between local and remote connections. A local connection originates from the server running MySQL. A remote connection comes from another computer or server over a network. Remote access requires additional account, firewall, and hosting configuration.

Setting Up Your Environment#

Before attempting to connect, confirm that you have the necessary client software and connection details.

If you manage your own server, the MySQL server must be installed and running. If the database is hosted for you, the provider normally manages the server process, while you manage databases, users, and permissions through the hosting control panel.

Open Terminal on macOS or Linux. On modern Windows systems, you can use PowerShell, Windows Terminal, or an SSH client. Ruachost customers connecting within their hosting environment should first sign in to the server using their SSH credentials.

After logging in, verify that the MySQL command-line client is available by entering mysql --version. A successful result displays the installed client version. If the shell reports that mysql is not recognized or cannot be found, the client may not be installed or its executable directory may not be included in your system’s PATH.

Gather these connection details before continuing:

  • MySQL username
  • MySQL password
  • Database name
  • Database host or server IP address
  • MySQL port, if it is not the default
  • SSL/TLS requirements for a remote connection

For a connection made from within a Ruachost hosting account, the database host is normally localhost. Database and account names may also include the cPanel username as a prefix. For example, a database created as store might have a full name resembling account_store. Always use the complete name shown in the control panel.

For remote access, do not assume that knowing the username and password is sufficient. The MySQL account must be permitted to connect from your public IP address, and the server must accept network connections. On Ruachost, add the connecting IP address through the Remote MySQL area in cPanel and request inbound port access through support if required.

Security should be part of setup rather than an afterthought. Create a dedicated MySQL user for the relevant application or task and grant only the permissions it needs. Avoid using a database administrator account for routine website operations.

Using the Command Line to Access MySQL#

For a local connection, enter mysql -u username -p, replacing username with the actual MySQL user. The -p option tells the client to request the password securely. Type the password at the prompt and press Enter. The terminal normally does not display dots, asterisks, or other characters while you type it.

Do not append the password directly to -p. A command such as mysql -u username -pYourPassword can expose the credential through shell history or process information. The MySQL connection documentation recommends using the password prompt instead.

A successful login displays information about the server followed by the mysql> prompt. You are now inside the interactive MySQL client.

The main connection options are:

Option

Purpose

Typical value

-u

MySQL account

database_user

-p

Prompt for password

No value after flag

-h

Database host

localhost or server IP

-P

TCP port

3306

--protocol

Connection method

TCP or SOCKET

Database name

Select database at login

account_store

The basic local command needs only a username and password prompt, while remote connections generally require an explicit host and may need a port or TLS settings.

To select a database during login, use mysql -u username -p database_name. For a remote server, use mysql -h hostname_or_ip -P 3306 -u username -p database_name. Note that the port option uses an uppercase P; lowercase -p refers to the password.

The word localhost can behave differently from 127.0.0.1 on Unix-like systems. localhost often makes the client use a local socket file, while 127.0.0.1 requests a TCP connection. If a local socket connection fails even though the server is running, try mysql -h 127.0.0.1 -u username -p.

Remote database traffic should be protected with TLS whenever supported. MySQL clients provide SSL options such as --ssl-mode, although exact availability depends on the client version. Also restrict access to known IP addresses rather than permitting connections from every host.

When you are finished, enter exit or quit and press Enter. You can also press Ctrl+D on many Unix-like systems.

Executing Basic SQL Commands#

SQL statements entered in the interactive client should normally end with a semicolon. If you press Enter without one, MySQL assumes the statement is incomplete and displays a continuation prompt. Add the semicolon and press Enter to execute it.

Start by listing databases with SHOW DATABASES;. MySQL displays only the databases your account is allowed to see. If an expected database is missing, verify the full name and the user’s assigned privileges.

Choose a database with USE database_name;. MySQL should respond that the database has changed. To confirm the active database, run SELECT DATABASE();.

Once a database is selected, these commands help you explore it safely:

  • SHOW TABLES; lists the tables in the active database.
  • DESCRIBE table_name; displays a table’s columns and data types.
  • SELECT VERSION(); reports the MySQL server version.
  • SELECT USER(); shows the authenticated account and host context.
  • SELECT * FROM table_name LIMIT 10; retrieves a small sample of rows.

Using LIMIT is a good habit when inspecting unfamiliar or large tables. An unrestricted query may return thousands of records and make the terminal difficult to use.

SQL keywords are generally case-insensitive, so show databases; and SHOW DATABASES; have the same effect. Uppercase keywords are nevertheless common because they make statements easier to read. Database and table name sensitivity can vary by operating system and server configuration, so reproduce object names exactly.

Be careful with statements that change data. UPDATE, DELETE, DROP, and TRUNCATE can have extensive or irreversible effects. Before changing production data, confirm the selected database, take an appropriate backup, and test the filtering condition with a SELECT statement.

You do not always need to enter the interactive client. To execute one statement from the shell, use the -e option, as in mysql -u username -p -e "SHOW DATABASES;". To apply an existing SQL file to a database, use shell input redirection: mysql -u username -p database_name < script.sql. Review scripts and back up important information before importing them.

For repeated administrative work, a protected option file or mysql_config_editor can avoid placing credentials in commands. Make sure any credential file has restrictive filesystem permissions and is not stored in a public website directory or source-code repository.

Common Connection Issues and How to Fix Them#

Connection errors usually fall into one of three categories: authentication, local server or socket problems, and remote networking problems. Read the complete error message because its numeric code often identifies the category.

Error

Likely cause

First checks

ERROR 1045: Access denied

Invalid credentials or host permission

Check user, password, host, and grants

ERROR 1049: Unknown database

Incorrect or incomplete name

Run SHOW DATABASES;

ERROR 2002: Can't connect through socket

Service stopped or wrong socket

Check service; try 127.0.0.1

ERROR 2003: Can't connect to server

Port, firewall, or bind issue

Check host, port, firewall, and listener

Authentication plugin error

Client-server incompatibility

Compare versions and update client

Command not found

Client missing from PATH

Install client or correct PATH

The error number provides a useful starting point, but the surrounding configuration determines the final fix.

Access denied: Re-enter the username carefully and remember that a MySQL account includes a host identity. MySQL can treat user connecting from localhost differently from the same username connecting from another IP. In cPanel, confirm that the user has been added to the intended database and has the required privileges. If a password contains shell-sensitive characters, using the interactive -p prompt prevents the shell from interpreting them.

Unknown database: Check the complete database name. On hosted accounts, the control-panel username is often added as a prefix. Connect without specifying a database, run SHOW DATABASES;, and copy the exact permitted name.

Local socket failure: This often means the MySQL service is not running or the client expects a different socket-file location. On a self-managed server, check the service status and configuration. Trying -h 127.0.0.1 can reveal whether the problem is limited to the socket, because it forces a TCP connection. The official MySQL guide provides additional connection troubleshooting steps.

Remote connection failure: Confirm that the hostname resolves to the correct server and that the configured port is reachable. The server must listen on an appropriate network interface rather than only 127.0.0.1. Its firewall must permit the connection, and the MySQL user must be authorized for the client’s IP. Avoid changing bind-address to expose MySQL on every interface unless access is tightly controlled elsewhere.

Authentication plugin incompatibility: Modern MySQL versions commonly use caching_sha2_password, which older clients may not understand. Compare the output of mysql --version with the server version and update the client before weakening the account’s authentication method. MySQL’s pluggable authentication documentation explains how server and client authentication components interact.

When troubleshooting, change one variable at a time. First verify the client, then the host and port, followed by credentials, account host permissions, database grants, and TLS requirements. This sequence makes it easier to identify the real cause instead of introducing several configuration changes at once.

Conclusion and Next Steps#

The essential workflow is straightforward: confirm that the client is available, gather the correct connection details, connect with mysql -u username -p, select the database, and terminate every SQL statement with a semicolon. From there, commands such as SHOW DATABASES;, USE database_name;, and SHOW TABLES; provide a safe introduction to database navigation.

As you become more comfortable, learn about backups, transactions, indexes, account privileges, and secure script execution. Practice first on a development database rather than production data, and always use narrowly scoped users for applications and routine tasks.

Ready to manage your database in a supported hosting environment? Visit Ruachost to take the next step.

Found this useful? Share it.

Keep reading

More from the journal.

Developer31 October 2026

How to Connect to psql from Terminal

Learn how to connect to PostgreSQL from the terminal with step-by-step instructions, plus discover the benefits of Ruachost for database needs.

Ruachost Editorial10 min read