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.
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
- Open MySQL Workbench and select the plus icon beside MySQL Connections.
- Choose Standard TCP/IP for a network connection. Enter the MySQL server host, port (usually
3306), and account name. - Select Store in Keychain or the available secure password-storage option if you want Workbench to remember the password. Otherwise, enter it when prompted.
- Select Test Connection. If it succeeds, save the connection and open it from the home screen.
- 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
- Error 2002: cannot connect through the local MySQL socket: check that the server is running and confirm whether the client should use a socket or TCP.
- Error 2003: cannot connect to the MySQL server: check the host, port, server listener, firewall, and network route.
- Error 2005: unknown MySQL server host: verify DNS resolution and the host name.
- Error 2026: SSL connection error: check the client TLS mode, certificate trust, host-name verification, and compatible TLS settings.
- Error 1045: access denied for a MySQL account: check the user name, password, authentication method, and account host.
- Error 1044: access denied to a database: confirm that the account has privileges on the selected database.
- Error 1049: unknown database: verify the database name or connect without selecting a database first.
For all connection fields and options, see the MySQL manual on connecting to the server, connection options, and encrypted connections.