The MySQL Command Line Client is a text-based tool that lets you interact directly with a MySQL database server using SQL commands. It’s ideal for database administration, scripting, and development tasks where graphical tools aren’t available or practical. To use it, you need MySQL installed on your system and a working server instance running—either locally or remote—with valid login credentials.
What You'll Need
- A computer with MySQL Server installed (MySQL Community Server or MySQL Cloud Service)
- Valid username and password for a MySQL account (e.g., 'root' or a custom user with appropriate privileges)
- Network access if connecting to a remote MySQL server (port 3306 must be open and reachable)
- Command-line access: Terminal on macOS/Linux or Command Prompt/PowerShell on Windows
Step-by-Step
- Open the Command Line Interface
Launch your terminal: on macOS or Linux, open Terminal; on Windows, open Command Prompt or PowerShell. Ensure the MySQL Client tools are installed—on most Linux systems, they’re included with the MySQL server package; on Windows, they’re installed by default with MySQL Server unless deselected during setup. - Start the MySQL Client
Enter the command:mysql -u username -p(replaceusernamewith your MySQL user, such asroot). You’ll be prompted to enter the password—type it carefully (no characters appear as you type, for security), then press Enter. If the login succeeds, you’ll see themysql>prompt, indicating you’re now in the MySQL client shell. - Connect to a Remote Server (if needed)
To connect to a remote MySQL server, include the host:mysql -h hostname_or_ip -u username -p. For example:mysql -h 192.168.1.100 -u admin -p. If the server uses a nonstandard port (not 3306), add-P port_number, such as-P 3307. - Verify Your Connection
RunSELECT VERSION();and press Enter. You should see output showing the MySQL version (e.g.,8.0.36). Then runSELECT USER();to confirm you’re logged in as the expected user. Both queries confirm connectivity and correct authentication. - Create or Select a Database
To list existing databases, typeSHOW DATABASES;and press Enter. To create a new database (e.g.,testdb), useCREATE DATABASE testdb;. To start using it, runUSE testdb;. All subsequent commands will apply to this database until you switch. - Create a Table and Insert Data
Define a table withCREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50), email VARCHAR(100));. Then insert a record:INSERT INTO users (name, email) VALUES ('Alice Smith', 'alice@example.com');. Confirm insertion withSELECT * FROM users;. - Exit the Client
When done, typeEXIT;or\qand press Enter to return to your system shell. Alternatively, pressing Ctrl+D (on macOS/Linux) or Ctrl+Z then Enter (on Windows) also exits the client.
Common Mistakes
- Missing semicolons: SQL statements in the client require a trailing semicolon (
;) to execute. Forgetting it causes the prompt to change to>, waiting for more input—just add;and press Enter. - Typing the password at the command line: Using
mysql -pMyPassword(no space) is insecure and may fail on newer versions due to security changes. Always use-pwithout a value and enter the password interactively when prompted. - Case sensitivity confusion: Database and table names are case-sensitive on Linux/Unix (but not Windows). If
USE TestDB;fails, tryUSE testdb;with exact casing. - Assuming localhost vs. 127.0.0.1: On some systems,
localhostuses a Unix socket, while127.0.0.1forces TCP/IP. If remote access fails, try-h 127.0.0.1instead of-h localhost. - Overlooking user permissions: If you get
Access denied for user 'user'@'host', the user may lack privileges for the requested operation or host. UseGRANTstatements or checkSELECT host, user FROM mysql.user;as root.
Tips
- Use
\cto cancel a partially typed or stuck query (e.g., if you forgot the semicolon) without exiting. - Enable command history with
\H(persistent history across sessions) or rely on your terminal’s up/down arrow keys for previous commands. - Run SQL from a file:
source /path/to/script.sql;or\. /path/to/script.sql;to execute batches of commands. - Format output for readability:
SELECT * FROM users\Gdisplays rows vertically (one column per line), useful for wide tables. - Use
\Wto show the full host:port in the prompt, helping avoid connection mix-ups in multi-server setups. - For safer testing, start the client with
mysql --safe-updatesto block dangerous operations (e.g.,DELETEwithout a key condition).
Troubleshooting
- "Can't connect to MySQL server on 'hostname' (10061)": This means the server isn’t reachable. Check if the MySQL service is running: on Linux,
sudo systemctl status mysql; on Windows, open Services and verify “MySQL” is running. Also confirm the host IP/port is correct and firewalls allow port 3306. - "Access denied for user 'root'@'localhost'": On fresh installs, the root password may be blank or auto-generated. Try logging in with
sudo mysqlon Linux (uses system auth), then set a password viaALTER USER 'root'@'localhost' IDENTIFIED BY 'newpass';. On Windows, reinstall MySQL or reset the password using the --skip-grant-tables method (see MySQL docs). - Command hangs after typing
mysql -u root -p: Press Enter after the password prompt. If it still hangs, the password may be wrong or the server is overloaded. Trymysqladmin -u root pingto test server responsiveness. - “Unknown command '\c'”: This often happens if you’re in the middle of a multi-line query and type
\cwithout a newline first. Press Enter once, then type\cand press Enter again. - Command not found:
mysql(Linux/macOS): The client isn’t in your PATH. Find it withwhich mysqlorfind /usr -name mysql 2>/dev/null, then use the full path (e.g.,/usr/local/mysql/bin/mysql) or add it to PATH. On Windows, ensure “MySQL Command Line Client” is installed via the MySQL Installer or add the bin directory to PATH.
Frequently Asked Questions
Is the MySQL Command Line Client the same as MySQL Workbench?
No. Workbench is a graphical tool with visual query building and database design features; the command-line client is a lightweight, text-only interface focused on SQL execution and administration. They can be used together—e.g., design in Workbench, run scripts in the CLI.Can I use the MySQL CLI without installing the full server?
Yes, on most platforms you can install just the MySQL Client tools (e.g.,mysql-client package on Debian/Ubuntu) without the server. This is common for remote administration or CI/CD pipelines.
How do I save query results to a file?
UseSELECT * FROM users INTO OUTFILE '/tmp/users.csv' FIELDS TERMINATED BY ','; (requires FILE privilege and write access to the directory). Alternatively, pipe output: mysql -u user -p -e "SELECT * FROM users;" > output.txt.
Why do some commands show “Query OK, 0 rows affected”?
This is normal for non-data-returning statements likeCREATE TABLE, ALTER TABLE, or UPDATE with no matching rows. It confirms the command ran successfully—even if no rows changed.
Does the CLI support autocomplete?
Yes, but only for keywords and object names (tables, columns) after typing a partial word and pressing Tab. Database/table names autocomplete only after you’ve usedUSE database; or run SHOW DATABASES;.
Can I run the CLI on macOS without Homebrew or MySQL Server?
Yes—download the MySQL Community Server DMG from Oracle, which includes the command-line client. Alternatively, usebrew install mysql-client (Homebrew) to install just the client tools.