Menu

How to Connect to a MySQL Database

Connect to MySQL locally or remotely from the command line or Workbench, select a database, verify the session, and troubleshoot common errors.

Updated on

A MySQL client connects to a running MySQL Server using a MySQL account. You need the server host, the account name, its password, and (optionally) the database to open after connecting. Use an account that has only the privileges your task requires; do not use root for routine application access.

Connect with the MySQL command-line client

To connect to a local server and open the sakila database, run:

mysql --host=127.0.0.1 --port=3306 --user=appuser --password sakila

The client prompts for the password. Do not put a real password directly in the command, where it may be saved in shell history or visible to other processes. Replace appuser and sakila with an account and database that exist on your server.

On Unix-like systems, --host=localhost normally uses a local socket. Use 127.0.0.1 when you specifically want a TCP connection to the local server. To connect without selecting a database, omit sakila and choose one after login with USE database_name;.

For a remote server, provide its DNS name or IP address and the TCP port:

mysql --host=db.example.com --port=3306 --user=appuser --password sakila

For production connections, follow your server’s TLS requirements. If the server provides a trusted CA certificate, verify both the certificate and host name:

mysql --host=db.example.com --port=3306 --user=appuser --password \
  --ssl-mode=VERIFY_IDENTITY --ssl-ca=/path/to/ca.pem sakila

The MySQL client must be installed on the computer where you run it. Check that it is available with mysql --version. If the shell reports that mysql is not found, add the MySQL client bin directory to PATH or run the client by its full path.

Verify the connection and selected database

At the mysql> prompt, check the connected server, authenticated account, and current database:

SELECT VERSION(), @@hostname, @@port, CURRENT_USER(), DATABASE();

DATABASE() returns NULL if you connected without selecting a database. To switch after connecting, use:

USE sakila;
SHOW TABLES;

Connect with MySQL Workbench

  1. Open MySQL Workbench and select the plus icon beside MySQL Connections.
  2. Choose Standard TCP/IP for a network connection. Enter the MySQL server host, port (usually 3306), and account name.
  3. Select Store in Keychain or the available secure password-storage option if you want Workbench to remember the password. Otherwise, enter it when prompted.
  4. Select Test Connection. If it succeeds, save the connection and open it from the home screen.
  5. Select a database from the Schemas panel, or run USE database_name; in the SQL editor.

For a remote server, confirm that its firewall and network configuration allow your client host to reach the MySQL port. The MySQL account must also be allowed to connect from that client host. Avoid exposing port 3306 to the public internet without an explicit access-control and TLS plan.

Troubleshoot common connection errors

For all connection fields and options, see the MySQL manual on connecting to the server, connection options, and encrypted connections.