Login to MySQL Server with a Username and Password

To log in to a MySQL server from a terminal or Command Prompt, run the mysql client with the -u option for the account name and -p to request the password securely:

</>
Copy
mysql -u username -p

Replace username with your MySQL account name. Press Enter, type the password at the prompt, and press Enter again. The password is not displayed while you type it. This is normal.

MySQL Login Command Options

OptionPurposeExample
-u or --userSpecifies the MySQL username-u appuser
-p or --passwordPrompts for the password-p
-h or --hostSpecifies the MySQL server host-h 127.0.0.1
-P or --portSpecifies the TCP port; uppercase P is required-P 3306
-D or a final database nameSelects a database after login-D school

Log in to MySQL from Windows Command Prompt

If the MySQL client directory is not included in the Windows PATH, open Command Prompt and navigate to the MySQL Server bin directory. The exact installation folder can vary by MySQL version and installation method.

Windows - Navigate to MySQL Server bin location in Command Prompt

MySQL Server x.0\bin contains mysql.exe. From that directory, run the following command. Replace yourusername with the MySQL account you were given.

</>
Copy
  C:\Program Files\MySQL\MySQL Server 8.0\bin> mysql -u yourusername -p
MySQL Server Login - Prompt for password

Type the password and press Enter. Most terminals do not display characters, dots, or asterisks while a password is being entered. The input is still being received.

If the login succeeds, the MySQL client displays a mysql> prompt.

MySQL Command Line Interface

Add the MySQL Client to the Windows PATH

Adding the MySQL bin directory to the Windows PATH lets you run mysql from any folder. After updating PATH, close and reopen Command Prompt, then verify the client:

</>
Copy
mysql --version

If Windows reports that mysql is not recognized, use the full path to mysql.exe or correct the PATH entry.

Log in to MySQL from Linux or macOS

Open a terminal and run:

</>
Copy
mysql -u yourusername -p

On some Linux installations, the local MySQL administrative account is configured to authenticate through the operating system rather than with a MySQL password. In that case, an administrator may connect locally with:

</>
Copy
sudo mysql

This behavior depends on how MySQL was installed and how the account is configured. It should not be treated as a universal replacement for mysql -u root -p.

Connect to a Remote MySQL Server

Use -h when the MySQL server is on another computer. Add -P when it listens on a non-default port.

</>
Copy
mysql -h db.example.com -P 3306 -u yourusername -p

The server must accept network connections, the port must be reachable, and the MySQL account must be permitted to connect from your client host. MySQL accounts are identified by both an account name and a host value, so an account allowed from localhost may not be allowed from another computer.

Connect Directly to a MySQL Database

Place the database name at the end of the login command:

</>
Copy
mysql -h localhost -u yourusername -p school

You can also select a database after logging in:

</>
Copy
USE school;

Verify the MySQL Account and Current Connection

After the mysql> prompt appears, run the following queries to confirm the server and account in use:

</>
Copy
SELECT VERSION();
SELECT USER();
SELECT CURRENT_USER();
SELECT DATABASE();
  • USER() shows the username and client host supplied for the connection.
  • CURRENT_USER() shows the MySQL account used for privilege checking.
  • DATABASE() shows the selected database, or NULL if none is selected.

To leave the MySQL client, run:

</>
Copy
EXIT;

Fix “Access Denied for User” During MySQL Login

If the username, password, account host, or authentication method does not match the server configuration, MySQL can return an error similar to ERROR 1045 (28000): Access denied for user.

MySQL Access denied for user with wrong password

Check the MySQL Username and Host

Confirm the account name, letter case, server host, and port. Connecting with -h localhost can use a local socket on Unix-like systems, while -h 127.0.0.1 requests a TCP connection. These connection methods can match different MySQL account entries or server settings.

</>
Copy
mysql -h localhost -u yourusername -p
mysql -h 127.0.0.1 -u yourusername -p

Confirm That the MySQL Server Is Running

An access-denied error means the server responded but rejected authentication. A connection-refused or socket error usually indicates a different problem, such as a stopped service, incorrect host, incorrect port, or incorrect socket path.

On systems that use systemd, check the MySQL service with:

</>
Copy
sudo systemctl status mysql

The service name may be mysqld on some distributions.

Do Not Put the MySQL Password in the Command

A command such as mysql -u username -pMyPassword can expose the password through shell history, process information, logs, or scripts. Use -p without the password and enter it only when prompted.

Find a MySQL Username or Reset a Forgotten Password

MySQL passwords cannot normally be retrieved in readable form because the server stores authentication data rather than the original plaintext password. If a password is forgotten, an authorized administrator must reset it.

An administrator who can already log in may list configured MySQL account names and allowed hosts:

</>
Copy
SELECT User, Host
FROM mysql.user
ORDER BY User, Host;

Access to the mysql.user system table requires suitable privileges. Application credentials may also be stored in the application’s environment variables, secret manager, deployment configuration, or hosting control panel. Do not publish or share those values.

If an administrator needs to change a known account password, the standard SQL form is:

</>
Copy
ALTER USER 'username'@'host' IDENTIFIED BY 'new_password';

Replace both username and host with the exact account entry. Resetting an administrative password when no privileged login is available requires a server-specific recovery procedure and operating-system access.

Log in with MySQL Workbench

MySQL Workbench provides a graphical connection form. On the Workbench home screen, create or edit a connection and enter the hostname, port, username, and optional default schema. Use the password prompt or the operating system’s credential storage instead of placing the password in a connection name or note.

  1. Open MySQL Workbench.
  2. Select the plus button beside MySQL Connections, or edit an existing connection.
  3. Enter the hostname, port, and MySQL username.
  4. Select Test Connection.
  5. Enter the password when prompted, then save the connection if the test succeeds.

MySQL Login Command Examples

TaskCommand
Local server with password promptmysql -u username -p
Remote servermysql -h db.example.com -u username -p
Custom portmysql -h db.example.com -P 3307 -u username -p
Open a database immediatelymysql -u username -p database_name
Force a local TCP connectionmysql -h 127.0.0.1 -u username -p
Show the installed client versionmysql --version

MySQL Login Verification Checklist

  • The mysql client is installed and can be found from the terminal.
  • The command uses the correct account name after -u.
  • The password is entered at the prompt rather than embedded in the command.
  • The hostname and TCP port identify the intended MySQL server.
  • The account is allowed to connect from the client host.
  • The selected database exists and the account has permission to use it.
  • The tutorial distinguishes authentication errors from connection or service errors.

MySQL Username and Password Login FAQs

What is the command to log in to MySQL with a username and password?

Run mysql -u username -p. Replace username, press Enter, and type the password when MySQL prompts for it.

Why does nothing appear when I type my MySQL password?

Terminal password prompts normally disable visual echo. No letters, dots, or asterisks may appear, but the terminal is still receiving the password.

Can I retrieve a forgotten MySQL password?

You cannot normally recover the original plaintext password from MySQL. An authorized administrator can reset the password for the relevant 'user'@'host' account.

How do I connect to a MySQL database on another server?

Use mysql -h hostname -P port -u username -p. The server must accept network connections, the port must be reachable, and the account must permit connections from your host.

Why can I log in with localhost but not 127.0.0.1?

On Unix-like systems, localhost commonly uses a local socket while 127.0.0.1 uses TCP. The server configuration and matching MySQL account can therefore differ between the two connection methods.