MySQL online course

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

MethodInterface TypeBest ForTypical Connection DetailsKey StrengthsLimitations
mysql command-line clientTerminalFast administration, scripting, remote work, and minimal systemsHostname, port, username, password, and optional databaseLightweight, scriptable, and available over SSHRequires familiarity with commands and SQL
MySQL WorkbenchDesktop graphical applicationQuery editing, visual design, saved connections, and administrationHostname, port, username, password, and optional default schemaVisual object browsing and an integrated SQL editorMust be installed and is less convenient on headless servers
phpMyAdminBrowser-based PHP applicationConvenient database work in web-hosting environmentsConfigured web address and authorized MySQL credentialsBrowse, edit, import, and export through a browserRequires 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 DetailExamplePurposeWhen It Is Required
Hostnamelocalhost, a DNS name, or an IP addressIdentifies the computer running MySQLAlways, although local clients often default to a local host
Port3306Identifies the network endpoint used by MySQLRequired when the server uses a nondefault port or when specifying it explicitly
Usernameapp_userIdentifies the MySQL accountAlways
PasswordEntered interactively or provided through an approved authentication methodAuthenticates the accountDepends on the account's authentication configuration
Database nameinventorySelects the initial database, also called a schema in MySQL contextsOptional at login, but needed before using database-specific objects
TLS or SSL settingsRequired certificates or encryption optionsProtects traffic between client and serverWhen 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

  1. Open Workbench and choose the option to create a new MySQL connection.
  2. Use Standard TCP/IP as the connection method when connecting through a normal hostname and port.
  3. Enter the server Hostname, such as a local host name or remote DNS name.
  4. Enter the Port, usually 3306 unless the server administrator specified another value.
  5. Enter the MySQL Username.
  6. Choose whether the password should be prompted for or stored using the approved local credential-storage option. Avoid insecure plaintext storage.
  7. Optionally enter a Default Schema. This is the database Workbench selects when the connection opens.
  8. 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

  1. Open the configured phpMyAdmin web address supplied by the administrator or hosting provider.
  2. Authenticate with an authorized MySQL username and its configured authentication method.
  3. Choose a database from the navigation pane.
  4. 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 -p without 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 with USE database_name;, and run SHOW 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 mysql client, 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.