Running SQL commands in the MySQL command line allows you to interact directly with your MySQL database to execute queries, manage data, and administer the server using text-based input. This method is ideal for automation, remote administration, and precise control without relying on graphical tools.

What You'll Need

  • MySQL Server installed and running
  • MySQL Command-Line Client (typically included with MySQL Server installation)
  • Valid MySQL username and password
  • Access to a terminal or command prompt (Windows CMD, PowerShell, macOS Terminal, or Linux shell)
  • (Optional) A specific database to work with

Step-by-Step

  1. Open your terminal or command prompt.
  2. Type the command to launch the MySQL client: mysql -u username -p, replacing "username" with your MySQL user (e.g., root).
  3. Press Enter. You will be prompted to enter your password. Type it carefully (no characters will appear as you type) and press Enter again.
  4. If the credentials are correct, you will see the MySQL prompt: mysql>.
  5. To list available databases, type SHOW DATABASES; and press Enter.
  6. To select a database to work with, use USE database_name;, replacing "database_name" with the name of your target database (e.g., USE wordpress;).
  7. Now you can run SQL commands. For example:
    • To create a table: CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50), email VARCHAR(100));
    • To insert data: INSERT INTO users (name, email) VALUES ('John Doe', 'john@example.com');
    • To query data: SELECT * FROM users;
    • To update data: UPDATE users SET email = 'new@example.com' WHERE name = 'John Doe';
    • To delete data: DELETE FROM users WHERE name = 'John Doe';
  8. Each command must end with a semicolon (;) to execute. Without it, MySQL will wait for more input.
  9. To exit the MySQL client, type EXIT or QUIT and press Enter.

Common Mistakes

  • Forgetting the semicolon at the end of a command, causing the client to hang waiting for input.
  • Typing the password incorrectly due to no visual feedback; ensure caps lock is off and keyboard layout is correct.
  • Using the wrong username or host; if connecting remotely, you may need -h hostname.
  • Running commands without selecting a database first, leading to "No database selected" errors.
  • Using reserved words (like order or group) as column or table names without backticks, causing syntax errors.

Tips

  • Use the up and down arrow keys to recall previous commands, saving time when repeating or modifying queries.
  • Backtick identifiers (e.g., `order`) if they match MySQL reserved words to avoid syntax errors.
  • For long queries, use a text editor to write and test them first, then paste into the client.
  • Use \c to clear the current command if you make a mistake and want to start over without exiting.
  • Add -h localhost explicitly if you're unsure whether the client is connecting via socket or TCP.
  • Check your MySQL version with SELECT VERSION(); to confirm compatibility with certain SQL syntax.

Troubleshooting

  • Can't connect to MySQL server:
    • Ensure the MySQL service is running (use sudo systemctl status mysql on Linux or Services.msc on Windows).
    • Verify the server is accepting connections on the expected port (default 3306).
    • Check if a firewall is blocking the port.
    • Confirm you're using the correct host (-h), username, and password.
  • Access denied for user:
    • Double-check the username and password.
    • Ensure the user has permission to connect from your host (localhost vs remote).
    • If you forgot the password, you may need to reset it using MySQL's safe mode (requires server restart).
  • Command not found:
    • Make sure MySQL is installed and the mysql binary is in your system's PATH.
    • On Windows, you may need to navigate to the MySQL bin directory (e.g., C:\Program Files\MySQL\MySQL Server 8.0\bin\) or add it to PATH.
    • On macOS with Homebrew, try /usr/local/mysql/bin/mysql or install via brew install mysql.
  • MySQL server has gone away:
    • The connection timed out; try reconnecting.
    • Increase wait_timeout or interactive_timeout in MySQL configuration if this happens frequently during long sessions.

Frequently Asked Questions

Do I need to install MySQL separately to use the command line client?

Yes, the MySQL command-line client is part of the MySQL Server package. Installing MySQL Server (via official installer, package manager, or Docker) includes the client tools.

Can I run SQL commands from a file using the command line?

Yes. Use mysql -u username -p database_name < script.sql to execute all commands in a file. You’ll be prompted for the password.

Is it safe to run the MySQL command line as root?

It works, but it's not recommended for routine tasks. Create a dedicated user with only the necessary privileges for better security.

Why does my query return no results even though data exists?

Check that you’ve selected the correct database with USE, and verify table and column names are spelled correctly (case sensitivity depends on your operating system).

Can I change my password from the MySQL command line?

Yes. After logging in, run: ALTER USER 'username'@'host' IDENTIFIED BY 'new_password'; then execute FLUSH PRIVILEGES;.

What if I make a mistake in a DELETE or UPDATE statement?

Always run a SELECT version first to see what rows will be affected. Once executed, changes are permanent unless you have a backup or enabled binary logging for point-in-time recovery.