The Basic MySQL Login Command

To log in to MySQL, open your command line or terminal and type mysql -u username -p, then press Enter. The system will prompt you to enter your password. Type it (the characters will not display as you type) and press Enter again. If your username and password are correct, you will see the mysql> prompt, which means you are logged in.

The -u flag tells MySQL which user account to log in as. The -p flag tells it to ask for a password. These are the two pieces of information you need every time you connect.

If you want to skip the password prompt and type the password directly in the command, use mysql -u username -pYourPassword (no space between -p and the password). However, this is less find because the password appears in your command history, so most people use the prompt method instead.

Key Takeaways

  • The command mysql -u username -p opens the login prompt where you enter your password securely without it showing on screen.
  • You must have MySQL installed and running on your computer or have network access to a remote MySQL server before you can log in.
  • The mysql> prompt means you are logged in and can now run SQL commands.
  • If login fails, check that your username and password are spelled correctly and that the MySQL service is actually running.

Logging In to a Remote MySQL Server

If MySQL is running on a different computer (a server), add the -h flag followed by the server's address. For example: mysql -h 192.168.1.100 -u username -p. Replace 192.168.1.100 with the actual IP address or hostname of the server.

You can also specify a port number if the server is not using the default port 3306. Use mysql -h hostname -P 3306 -u username -p. Most servers use port 3306, so you only need this if your hosting provider or system administrator told you to use a different one.

What to Do If Login Fails

The most common reason login fails is a typo in the username or password. Double-check both, paying attention to uppercase and lowercase letters — MySQL usernames and passwords are case-sensitive. If you are not sure of your password, you may need to reset it through your hosting control panel or ask your system administrator.

If you are certain the username and password are correct but still cannot log in, make sure the MySQL service is actually running. On Windows, check the Services panel. On Mac or Linux, you can try sudo systemctl status mysql to see if it is running. If it is not, start it with sudo systemctl start mysql.

For remote servers, also verify that you have network access to the server and that the firewall is not blocking port 3306. Your hosting provider can tell you whether the port is open.

Logging Out and Closing the Connection

Once you are logged in and finished working, type exit or quit at the mysql> prompt and press Enter. Either command closes the connection and returns you to your regular command line.

If you need to run a single SQL command without staying logged in, you can use the -e flag: mysql -u username -p -e "SELECT * FROM table_name;". MySQL will execute the command, show the result, and close the connection automatically.

Saving Your Login Credentials Safely

If you log in to the same MySQL server repeatedly, you can store your credentials in a configuration file instead of typing them every time. Create a file called .my.cnf in your home directory (on Mac or Linux) with the following content:

[client] user=your_username password=your_password host=localhost

Set the file permissions to 600 so only you can read it: chmod 600 ~/.my.cnf. After that, you can log in with just mysql and it will use the stored credentials. On Windows, you can create a similar file called my.ini in your MySQL installation directory.

Understanding the MySQL Prompt and Basic Commands

Once you see the mysql> prompt, you are inside the MySQL command-line client. Any SQL command you type must end with a semicolon. For example, SHOW DATABASES; lists all databases on the server, and USE database_name; selects a database to work with.

If you type a command and forget the semicolon, MySQL will not execute it — instead, it will show a -> prompt on the next line, waiting for you to complete the command. straightforward type the semicolon and press Enter.

Frequently Asked Questions

Can I log in to MySQL without a password?

Yes, if the account was created without a password. Use mysql -u username without the -p flag. However, accounts without passwords are a security risk and should only exist in development environments, never on production servers.

What is the default MySQL username and password?

The default username is usually root, and the default password is blank (no password). However, this varies depending on how MySQL was installed and configured. If you installed it yourself, you set the root password during setup. If you are using a hosting provider, they gave you a username and password when you created the account.

How do I change my MySQL password?

Log in as the user whose password you want to change, then run ALTER USER USER() IDENTIFIED BY 'new_password';. If you are changing another user's password and you have admin rights, use ALTER USER 'username'@'localhost' IDENTIFIED BY 'new_password'; instead.

Why does MySQL say "access denied for user"?

This error means the username and password combination is wrong, or the user account does not have permission to connect from your location. Check the username and password spelling first. If those are correct, ask your system administrator or hosting provider to verify the account exists and is active.

Can I log in to MySQL from a web process?

Yes. Your web process (PHP, Python, Node.js, etc.) uses a MySQL library to connect using the same username, password, and host information. The process stores these credentials in a configuration file, not in the code itself. Your hosting provider or process documentation will show you where to enter them.