This guide shows you how to connect to a MySQL server directly from your terminal or command prompt, run SQL queries, and manage databases without needing a graphical tool. You’ll learn the exact commands to log in, check what’s there, and execute common operations—all from the command line.
What You’ll Need
- A MySQL server installed and running (locally or on a remote host).
- The MySQL command-line client (
mysql) installed on your computer. It comes with the MySQL server package, or you can install it separately. - Valid credentials: a username (usually
rootfor local development) and a password. - Access to a terminal (Linux/macOS) or Command Prompt/PowerShell (Windows).
Step-by-Step
- Open your terminal – On Linux/macOS, open Terminal. On Windows, open Command Prompt or PowerShell.
- Check if the MySQL client is installed – Type
mysql --versionand press Enter. If you see something likemysql Ver 8.0.36 for Linux on x86_64, you’re good. If not, install it using your package manager (e.g.,sudo apt install mysql-clienton Ubuntu,brew install mysql-clienton macOS, or download from the official MySQL site on Windows). - Connect to MySQL – Use the basic syntax:
mysql -u username -p. Replaceusernamewith your MySQL username (e.g.,root). The-pflag tells MySQL to prompt for a password. Example:mysql -u root -p. Press Enter, then type your password when prompted. You’ll see themysql>prompt, meaning you’re in. - Connect to a specific database (optional) – If you know which database you want to use, add the database name at the end:
mysql -u username -p database_name. For example:mysql -u root -p mydb. - Connect to a remote MySQL server – Add the
-hflag with the hostname or IP address:mysql -h 192.168.1.100 -u username -p. You can also specify a port with-P(uppercase) if it’s not the default 3306, e.g.,mysql -h host -P 3307 -u user -p. - Run your first query – Once connected, type
SHOW DATABASES;and press Enter. This lists all databases on the server. End every SQL statement with a semicolon;. - Select a database to work with – Use
USE database_name;(e.g.,USE mysql;). - View tables in the selected database – Type
SHOW TABLES;to see all tables. - Run a simple SELECT query – For example,
SELECT * FROM your_table_name LIMIT 10;will show the first 10 rows of that table. - Exit the MySQL prompt – Type
EXIT;or pressCtrl+D(Linux/macOS) orCtrl+Zthen Enter (Windows).
Common Mistakes
- Forgetting the semicolon – SQL statements won’t execute until you end them with a semicolon. If you forget, the prompt shows a continuation
->instead ofmysql>. - Using the wrong password or username – Double-check with your system administrator or the MySQL installation logs.
- Typing
-pwith a space –-p passwordis incorrect; you should use-pand then enter the password when prompted, or-ppassword(no space) if you must include it in the command (but this is insecure). - Using the wrong host – If connecting remotely, ensure the host is reachable and MySQL is configured to allow remote connections (bind-address).
- Case sensitivity – On Linux, database and table names are case-sensitive. Use the exact case.
Tips
- Use
-pwithout a password in the command – This avoids exposing the password in your shell history. - Create a configuration file – For frequent connections, create a
~/.my.cnffile with your credentials (chmod 600) to avoid typing them each time. - Use
--ssl-mode=REQUIRED– When connecting remotely over untrusted networks, add this flag to encrypt the connection. - Learn the help – Type
help;or\hat the mysql prompt to see available commands. - Use batch mode for scripts – For automation, use
mysql -u user -p database < script.sqlto run a file of SQL commands.
Troubleshooting
- Error: “Can’t connect to local MySQL server through socket” – The MySQL server may not be running. Start it with
sudo systemctl start mysql(Linux) orbrew services start mysql(macOS). - Error: “Access denied for user ‘root’@’localhost'” – Check the username and password. If you forgot the root password, you may need to reset it (search for “reset MySQL root password” for your OS).
- Error: “Command not found” – The MySQL client isn’t in your PATH. On Windows, add the MySQL bin directory to PATH, or use the full path like
C:\Program Files\MySQL\MySQL Server 8.0\bin\mysql.exe. - Error: “Unknown database” – The database name you specified doesn’t exist. Use
SHOW DATABASES;to see available ones. - Connection hangs – If connecting to a remote host, check firewall rules, network connectivity, and that MySQL is listening on the expected port (
netstat -tlnp | grep 3306on Linux).
Frequently Asked Questions
How do I exit the MySQL command line?
Type EXIT; or QUIT; and press Enter, or use the shortcut Ctrl+D (Linux/macOS) or Ctrl+Z then Enter (Windows).
What is the default MySQL port?
The default port is 3306. If your server uses a different port, specify it with the -P flag (uppercase) when connecting.
How do I connect as a different user?
Use the -u flag followed by the username, e.g., mysql -u admin -p.
Can I run a SQL file from the command line?
Yes, use input redirection: mysql -u user -p database_name < file.sql. This executes all commands in the file.
How do I see all MySQL commands available?
At the mysql prompt, type help; or \h to list client commands. For SQL syntax help, type help contents;.
Is it safe to type my password in the command line?
No, it’s safer to use the -p flag without a password so you’re prompted interactively. If you must include it, use -pYourPassword with no space, but be aware it may be visible in shell history.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Marvel Rivals Magneto Guide: Abilities, Combos & Counters8.12 dmg/s
- PS2 Remakes & Remasters: What to Know
- WordPress Multisite with Nginx Setup Guide
- AI vs Enigma: How It Could Have Cracked WWII Codes
- Logitech Cloud Gaming Images: What They Are
- HDMI Cable for Gaming: What to Look For