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:
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
| Option | Purpose | Example |
|---|---|---|
-u or --user | Specifies the MySQL username | -u appuser |
-p or --password | Prompts for the password | -p |
-h or --host | Specifies the MySQL server host | -h 127.0.0.1 |
-P or --port | Specifies the TCP port; uppercase P is required | -P 3306 |
-D or a final database name | Selects 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.

MySQL Server x.0\bin contains mysql.exe. From that directory, run the following command. Replace yourusername with the MySQL account you were given.
C:\Program Files\MySQL\MySQL Server 8.0\bin> mysql -u yourusername -p

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.

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:
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:
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:
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.
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:
mysql -h localhost -u yourusername -p school
You can also select a database after logging in:
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:
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, orNULLif none is selected.
To leave the MySQL client, run:
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.

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.
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:
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:
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:
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.
- Open MySQL Workbench.
- Select the plus button beside MySQL Connections, or edit an existing connection.
- Enter the hostname, port, and MySQL username.
- Select Test Connection.
- Enter the password when prompted, then save the connection if the test succeeds.
MySQL Login Command Examples
| Task | Command |
|---|---|
| Local server with password prompt | mysql -u username -p |
| Remote server | mysql -h db.example.com -u username -p |
| Custom port | mysql -h db.example.com -P 3307 -u username -p |
| Open a database immediately | mysql -u username -p database_name |
| Force a local TCP connection | mysql -h 127.0.0.1 -u username -p |
| Show the installed client version | mysql --version |
MySQL Login Verification Checklist
- The
mysqlclient 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.
TutorialKart.com