How to Access a MySQL Database Server
Learn how to connect to MySQL using the mysql command-line client, MySQL Workbench, or phpMyAdmin, with connection requirements, SQL examples, security guidance, and troubleshooting.
A MySQL server is the database service that stores data and accepts client connections. A MySQL client is a program or interface used to connect to that server and send requests.
You can access MySQL from a terminal, a desktop application, or a web browser. The three common choices are the mysql command-line client, MySQL Workbench, and phpMyAdmin. These tools change the workflow and interface, but they send SQL to the same type of database server. The SQL language does not fundamentally change because you use a different client.
MySQL access methods at a glance
| Method | Interface Type | Best For | Typical Connection Details | Key Strengths | Limitations |
|---|---|---|---|---|---|
| mysql command-line client | Terminal | Fast administration, scripting, remote work, and minimal systems | Hostname, port, username, password, and optional database | Lightweight, scriptable, and available over SSH | Requires familiarity with commands and SQL |
| MySQL Workbench | Desktop graphical application | Query editing, visual design, saved connections, and administration | Hostname, port, username, password, and optional default schema | Visual object browsing and an integrated SQL editor | Must be installed and is less convenient on headless servers |
| phpMyAdmin | Browser-based PHP application | Convenient database work in web-hosting environments | Configured web address and authorized MySQL credentials | Browse, edit, import, and export through a browser | Requires a web server and careful access protection |
Prerequisites for connecting
Before opening a client, collect the information required by the server and your account. A local connection and a remote connection have the same general requirements, but remote connections also depend on network and server policy.
| Connection Detail | Example | Purpose | When It Is Required |
|---|---|---|---|
| Hostname | localhost, a DNS name, or an IP address | Identifies the computer running MySQL | Always, although local clients often default to a local host |
| Port | 3306 | Identifies the network endpoint used by MySQL | Required when the server uses a nondefault port or when specifying it explicitly |
| Username | app_user | Identifies the MySQL account | Always |
| Password | Entered interactively or provided through an approved authentication method | Authenticates the account | Depends on the account's authentication configuration |
| Database name | inventory | Selects the initial database, also called a schema in MySQL contexts | Optional at login, but needed before using database-specific objects |
| TLS or SSL settings | Required certificates or encryption options | Protects traffic between client and server | When the server or organization requires encrypted connections |
The server must be running, and your account must be valid. For a remote server, you also need network access to the hostname and port. The server may restrict which hosts an account can connect from, and the account must have suitable privileges. Privileges are permissions such as selecting, inserting, creating, or administering objects.
For installation and service basics, see Install MySQL on Linux. You can also review database terms before continuing.
Access MySQL with the command-line client
The mysql program is a terminal-based client for interactive access and automation. It is useful on Linux servers, over SSH, and in environments where a graphical application is unavailable.
Connect to a local server
Use -u to specify the username and -p to request the password interactively:
mysql -u username -p
After running the command, enter the password when prompted. Do not put the password directly after -p in a shared shell or script, because command arguments can be exposed through shell history or process inspection.
To connect and select a database immediately, place the database name at the end:
mysql -u username -p database_name
Connect to a remote server
Use -h for the hostname and -P for the port. The capital -P is the port option:
mysql -h db.example.com -P 3306 -u username -p
Supply a database name if you want it selected during login:
mysql -h db.example.com -P 3306 -u username -p database_name
The hostname and port in these examples are placeholders. Replace them with the values supplied by the database administrator or hosting provider. Remote access must be enabled by the server, allowed by firewalls and routing, and permitted for the account's source host.
Use the interactive prompt
After a successful login, the client displays a prompt such as mysql>. Most SQL statements end with a semicolon. You can select a database and inspect its objects with:
SHOW DATABASES;
USE database_name;
SHOW TABLES;
DESCRIBE table_name;
USE changes the current database for the session. In MySQL, the terms database and schema are commonly used interchangeably.
Query, create, and modify objects
A basic query retrieves rows from a table:
SELECT * FROM table_name LIMIT 10;
You can also create a database and table when your account has the required privileges:
CREATE DATABASE example_db;
USE example_db;
CREATE TABLE tasks (
task_id INT PRIMARY KEY,
description VARCHAR(200),
completed BOOLEAN
);
Data-changing statements should be written carefully, especially UPDATE and DELETE. A missing WHERE clause can affect every row:
INSERT INTO tasks (task_id, description, completed)
VALUES (1, 'Review connection settings', FALSE);
UPDATE tasks
SET completed = TRUE
WHERE task_id = 1;
For more SQL practice, see Create a Database, Create a Table, and Query a Database.
Exit safely
End the interactive session with either command:
exit;
\q
Logging out releases the session and is especially important when using a shared computer or administrative account.
Access MySQL with MySQL Workbench
MySQL Workbench is a graphical desktop application for SQL development, database design, object browsing, and MySQL administration. It can store connections to multiple servers, so you can switch between development, testing, and production environments.
Create and test a connection
- Open Workbench and choose the option to create a new MySQL connection.
- Use Standard TCP/IP as the connection method when connecting through a normal hostname and port.
- Enter the server Hostname, such as a local host name or remote DNS name.
- Enter the Port, usually
3306unless the server administrator specified another value. - Enter the MySQL Username.
- Choose whether the password should be prompted for or stored using the approved local credential-storage option. Avoid insecure plaintext storage.
- Optionally enter a Default Schema. This is the database Workbench selects when the connection opens.
- Use Test Connection. Save the connection only after confirming that its settings and authentication method are correct.
After connecting, open an SQL editor or query tab. Enter statements such as SHOW DATABASES;, USE database_name;, and SELECT * FROM table_name LIMIT 10;, then run the selected statement or script. Workbench also provides panels for browsing schemas and tables, viewing object definitions, and performing supported server-administration tasks.
Access MySQL with phpMyAdmin
phpMyAdmin is a browser-based PHP application that administers MySQL through a web interface. It is common in web-hosting environments where a provider supplies a configured phpMyAdmin installation.
Log in and navigate
- Open the configured phpMyAdmin web address supplied by the administrator or hosting provider.
- Authenticate with an authorized MySQL username and its configured authentication method.
- Choose a database from the navigation pane.
- Choose a table to browse its rows, view its structure, or open related actions.
The SQL tab or editor lets you send SQL directly to the selected server. For example, you can run:
SHOW TABLES;
SELECT * FROM table_name LIMIT 10;
phpMyAdmin also commonly provides graphical actions for browsing data, editing individual rows, creating or changing objects, and importing or exporting data. Buttons and forms may generate SQL behind the scenes. Understand the database action before confirming it, particularly for changes or deletions.
Use the same SQL across clients
Standard SQL statements are sent to MySQL in the same form whether they originate in the terminal, Workbench, or phpMyAdmin. The following sequence works conceptually in each interface:
USE shop;
SHOW TABLES;
SELECT * FROM products LIMIT 10;
exit;
The final exit; is appropriate for the command-line session; graphical tools normally use a disconnect or close action instead. The database-selection, table-listing, and data-retrieval statements remain the same.
Graphical interfaces may provide buttons for selecting a schema, browsing a table, or editing a row. Those controls are conveniences, not a replacement for understanding SQL. Check which database is selected, which rows are affected, and which privileges the operation requires.
See SQL Commands Syntax for statement structure and LIMIT Clause for restricting returned rows.
Choosing an access method
- Choose the mysql client for speed, repeatable commands, scripting, SSH-based remote administration, and minimal systems without a desktop or web server.
- Choose MySQL Workbench for visual database design, saved server connections, query editing, schema browsing, and guided administration on a desktop.
- Choose phpMyAdmin for convenient browser-based tasks, especially when a web-hosting provider already supplies and secures it.
Your organization may require a particular tool, authentication system, audit process, or network route. Security requirements and available tools should be considered alongside personal preference.
Connection safety and credential handling
- Use
-pwithout placing the password in the command line. Shell history, process listings, copied commands, and logs can expose inline passwords. - Do not store passwords in world-readable configuration files. Use approved credential stores, protected client configuration, or interactive prompts.
- Create separate, least-privilege accounts for applications and routine administration. Avoid using a full administrative account for everyday queries.
- Use encrypted TLS connections for remote access when supported and required. Confirm the server's certificate and client settings rather than disabling verification to bypass an error.
- Restrict remote network access with firewalls, private networks, VPNs, or other approved controls.
- Log out and close clients when finished, especially on shared systems.
Troubleshooting connection problems
Access denied during login
- Confirm the username and enter the password when prompted.
- Ask an administrator to verify that the account is allowed to connect from the current host and has the required privileges.
- Check whether the client's authentication support is compatible with the server's configured authentication method. A current client may be required.
Cannot connect to a remote server
- Confirm the hostname and port, including whether the server uses a nondefault port.
- Verify that the MySQL service is running.
- Check routing, firewall rules, security groups, and other network controls.
- Confirm that MySQL listens on an interface reachable from the client rather than only on local interfaces.
- Verify that the account has permission for remote connections.
A database or table is not visible
- Run
SHOW DATABASES;, select the intended database withUSE database_name;, and runSHOW TABLES;. - Check the spelling and capitalization of the object name. Case-sensitivity behavior can differ by server platform and configuration.
- Ask an administrator to confirm that the account has privileges on the database or table.
Workbench cannot test a connection
- Compare the saved hostname, port, username, and default schema with known working details.
- Test the same server with the command-line client if it is available. This helps distinguish a Workbench setting from a server or network problem.
- Review required TLS settings and certificates for a configuration mismatch.
phpMyAdmin is unavailable or its login fails
- Confirm the configured web address and check whether the web server and PHP services are running.
- Review phpMyAdmin's authentication configuration and whether it supports the MySQL server's authentication method.
- Use a secured administrative path and avoid making phpMyAdmin publicly reachable without access controls.
Key points to remember
- The MySQL server stores data and accepts connections; the client is the tool that connects to it.
- The
mysqlclient, Workbench, and phpMyAdmin use different interfaces but can execute the same SQL. - A connection generally requires a host, port, username, authentication method, and suitable privileges; a database can be selected during or after login.
- Remote access requires both network reachability and server-side permission.
- Protect credentials, use least privilege, encrypt remote traffic, and close sessions when finished.