To access a MySQL 5.5 database via the command line, use the command mysql -u username -p in your terminal or command prompt, then enter your password when prompted. This allows you to interact directly with the database engine to create tables, manage data, and execute complex queries without needing a graphical user interface. This method is essential for server administration, automating tasks via scripts, and performing deep database maintenance.
What You'll Need
- A computer with MySQL 5.5 installed locally or access to a remote server running MySQL 5.5.
- A terminal emulator (Command Prompt or PowerShell on Windows; Terminal on macOS or Linux).
- Administrative or user-level credentials (a username and password) with the necessary permissions to access the database.
- The MySQL binary folder added to your system's PATH environment variable so the
mysqlcommand is recognized globally.
Step-by-Step
- Open your Command Line Interface: Launch your preferred terminal. On Windows, press the Windows key, type "cmd", and press Enter. On macOS, press Command + Space, type "Terminal", and press Enter. On Linux, use your distribution's default terminal emulator.
- Navigate to the MySQL Bin Directory (If not in PATH): If you have not added MySQL to your system environment variables, you must navigate to the folder where the executable resides. For example, on Windows, this is often
cd "C:\Program Files\MySQL\MySQL Server 5.5\bin". - Initiate the Connection Command: Type the following command and press Enter:
mysql -u root -p. Replace "root" with your specific username if you are not using the administrative account. The-uflag specifies the user, and the-pflag tells the system to prompt you for a password. - Enter Your Password: After pressing Enter, the terminal will display
Enter password:. Type your password carefully. Note that for security reasons, the characters will not appear on the screen as you type. Press Enter once the password is complete. - Verify the Connection: Once successfully logged in, your prompt will change to
mysql>. To confirm you are connected and see the current server version, typeSELECT VERSION();and press Enter. You should see a result confirming version 5.5.x. - Select a Database: You cannot run queries against tables until you specify which database to use. Type
SHOW DATABASES;to see a list of available schemas. Once you identify the one you need, typeUSE database_name;(replacing "database_name" with your actual database name) and press Enter. - Execute Basic SQL Commands: Now that a database is selected, you can run commands. To see the tables within that database, type
SHOW TABLES;. To view data from a specific table, useSELECT * FROM table_name LIMIT 10;. - Exit the Command Line: When you have finished your work, type
exitorquitand press Enter to return to your standard system prompt.
Common Mistakes
One of the most frequent errors is forgetting the semicolon (;) at the end of an SQL statement. The MySQL command line is an interactive shell; it will not execute a command until it sees the terminating semicolon. If you press Enter and see a -> prompt, it means MySQL is waiting for you to finish the statement. Simply type the semicolon and press Enter again.
Another common mistake is entering the password directly in the command line (e.g., mysql -u root -pMyPassword). While this technically works, it is a significant security risk because your password remains in the terminal's command history in plain text, where other users or malicious software can find it. Always use the -p flag without a trailing password to trigger the secure prompt.
Users often confuse the system command line with the MySQL prompt. Commands like cd or dir are operating system commands and will result in a syntax error if typed after the mysql> prompt. Conversely, SELECT or DESCRIBE are SQL commands and will not work if typed directly into the Windows Command Prompt or Bash shell without first connecting to MySQL.
Tips
To make your data more readable, use the
or modifiers, or use the vertical format for wide tables. For example, if a table has too many columns to fit on one screen, you can end your query with instead of a semicolon to display the results vertically.
Utilize the DESCRIBE table_name; command frequently. This provides the schema of the table, including column names, data types, and whether a column allows NULL values. This prevents errors when writing INSERT or UPDATE statements by ensuring you are using the correct data formats.
If you are performing a repetitive task, consider creating a .sql script file. You can execute an entire file of commands from the system command line using the command mysql -u username -p database_name < script.sql. This is far more efficient than typing dozens of commands manually into the interactive shell.
Troubleshooting
If you receive the error 'mysql' is not recognized as an internal or external command, the MySQL bin directory is not in your system's PATH. You can fix this by either navigating directly to the bin folder using the cd command or by adding the path to your system environment variables in the Control Panel (Windows) or .bashrc/.zshrc file (Linux/macOS).
If you encounter Access denied for user 'root'@'localhost' (using password: YES), it means the password you entered is incorrect or the user does not have permissions to connect from your current host. Double-check your Caps Lock key. If you have lost the root password, you will need to restart the MySQL server with the --skip-grant-tables option to reset the credentials, which requires administrative access to the server hardware or OS.
If the connection times out or you get a Can't connect to MySQL server on 'localhost' error, the MySQL service may not be running. On Windows, open the Services manager (services.msc), find the MySQL service, and ensure its status is "Running". On Linux, use sudo service mysql start or sudo systemctl start mysql.
Frequently Asked Questions
How do I change the password for a user in MySQL 5.5?
Use the SET PASSWORD command. For example: SET PASSWORD FOR 'username'@'localhost' = PASSWORD('new_password');. Ensure you have the necessary privileges to modify user accounts.
Can I connect to a remote MySQL 5.5 server?
Yes, use the -h flag to specify the host address: mysql -h 192.168.1.100 -u username -p. The remote server must be configured to allow external connections in its my.cnf or my.ini file, and the firewall must allow traffic on port 3306.
How do I import a large SQL dump file?
Avoid using the source command inside the MySQL prompt for very large files. Instead, use the system command line: mysql -u username -p database_name < backup.sql. This is generally faster and more stable for multi-gigabyte files.
What is the difference between MySQL 5.5 and newer versions in the command line?
While basic SQL syntax remains similar, newer versions have different default authentication plugins and stricter SQL modes. MySQL 5.5 is an older version and may lack some of the advanced optimization and security features found in 5.7 or 8.0, but the core command line interaction remains largely the same.
How do I clear the screen in the MySQL command line?
The MySQL shell does not have a built-in clear command like Bash. On Windows, you generally have to exit and clear the CMD window. On Linux/macOS, you can sometimes use Ctrl + L to clear the terminal screen without exiting the session.