How to Connect to MySQL from Command Line in Windows

Introduction
MySQL is the most extensively employed relational database management systems (RDBMS) worldwide. It does not matter if you are a database administrator, or IT expert, knowing how to connect to MySQL using the Windows command line is essential. The command-line interface (CLI) allows you to execute queries, manage databases, and troubleshoot issues efficiently—all without needing a graphical user interface (GUI).
Using the CLI to interact with MySQL provides numerous advantages. It offers faster performance compared to GUI-based tools, as commands are executed directly without the overhead of visual rendering. Additionally, it allows automation through scripts, making database management more efficient, especially when handling large datasets or repetitive tasks.
For those working in cloud computing, DevOps, or backend development, mastering MySQL via the command line is a valuable skill. It ensures seamless integration with various applications and platforms, allowing for quick access to data, better security management, and improved performance monitoring. Whether you’re setting up a new database, optimizing queries, or troubleshooting connection issues, the command line provides precise control over MySQL operations.
This guide will walk you through the step-by-step process of connecting to MySQL from the Windows command line, ensuring you can perform essential database tasks efficiently.
Starting MySQL from Windows Command Line

The Windows Command Prompt (CMD) is a built-in tool that allows users to execute system-level commands and manage applications like MySQL without a graphical interface. Using CMD to access MySQL provides greater control, improved speed, and automation capabilities that are essential for database administrators and developers.
To open Command Prompt, follow these steps:
- Press Windows + R to launch the Run dialog box.
- Type cmd in the text field & press Enter.
- A black window with a blinking cursor will show up, indicating that CMD is ready to accept commands.
Alternatively, you can open it with administrative privileges for better control:
- Click the Start Menu, type cmd, and right-click on “Command Prompt.”
- Select Run as administrator to gain elevated access.
This method is particularly useful when executing commands that require higher-level permissions, such as starting or stopping MySQL services.
Step 1: Verify MySQL Is Operating on Windows
Before you can connect to MySQL, you must guarantee that the MySQL service is functioning. If the service is not active, connection attempts will fail.
Checking Active Services via Command Prompt
1. Open Command Prompt (cmd) as described earlier.
2. Type the given below command & press Enter:
net start
3. This command will show up a list of all active services on your Windows system.
4. Look for an entry labeled MySQL or MySQL80 (depending on your version).
5. If MySQL appears in the list, it means the service is already running, and you can proceed to connect.
Starting MySQL Manually (If Not Running)
If MySQL is not listed in the active services, you will need to start it manually:
- Press Windows + R, type services.msc, and press Enter. This displays the Windows Services Manager.
- In this opened window, scroll down to site MySQL or MySQL80.
- If the Status column says “Stopped,” right-click on MySQL and select Start.
- Wait for some time for the service to start. Once running, you can proceed to link to MySQL from the command line.
Verifying MySQL Status with PowerShell
Another way to check MySQL’s status is by using Windows PowerShell:
1. Open PowerShell via typing powershell in the Start Menu and selecting it.
2. Run the following command:
Get-Service -Name MySQL*
3. If the Status is “Running,” MySQL is active. If not, start the service using:
Start-Service -Name MySQL80
Following these steps ensures that MySQL is running before trying to construct a connection. If the service repeatedly fails to start, checking the MySQL error logs or Windows Event Viewer can help diagnose the issue.
Step 2: Run MySQL Server Command-Line Client
The MySQL Server Command-Line Client is a powerful tool that permits direct interaction with your database. It enables you to execute SQL queries, create and modify databases, manage users, and troubleshoot issues efficiently—all without the need for a graphical interface.
Using the command-line client offers several advantages:
Faster execution of SQL commands compared to GUI-based tools.
Direct access to advanced MySQL features and configurations.
Better control over database management, ideal for developers and administrators
How to Open MySQL Server Command-Line Client
Follow these steps to launch the MySQL command-line interface on Windows:
- Press the Windows key on your keyboard.
- Type MySQL 8.0 Command Line Client in the search bar. (Replace “8.0” with your installed version if different.)
- Click on the application to open it.
- A black terminal window will appear, prompting you to enter a password.
- Enter the MySQL root password that you set while installing and press Enter.
Once authenticated, the MySQL prompt (mysql>) will appear, indicating a successful connection to the server. You can now begin executing SQL commands to manage your databases.
Troubleshooting MySQL Client Issues
If you encounter any errors while opening the MySQL command-line client, follow these steps to diagnose and fix the issue:
Check if MySQL Service is Running
- Press Windows + R, type services.msc, and press Enter.
- In the Services window, scroll down and find MySQL.
- If the Status is Stopped, right-click & select Start to activate the service.
Verify MySQL Installation Path
- If the client does not launch, check if MySQL is installed correctly.
- Navigate to C:\Program Files\MySQL\MySQL Server 8.0\bin\ and look for mysql.exe.
- If the file is missing, reinstall MySQL to restore necessary components.
Run as Administrator
- Sometimes, permission issues prevent MySQL from launching.
- Right-click MySQL Command Line Client and select Run as Administrator.
Step 3: Connect to Local MySQL Server
Besides using the MySQL Command-Line Client, you can also connect to MySQL through the standard Windows Command Prompt (CMD). This method is particularly useful if you need to run MySQL commands from different directories or automate tasks using scripts.
Accessing MySQL via Command Prompt
1. Open Command Prompt by pressing Windows + R, typing cmd, and pressing Enter.
2. Navigate to the MySQL bin directory where the MySQL executables are stored. Use the following command (adjust based on your installation path):
cd C:\Program Files\MySQL\MySQL Server 8.0\bin
3. Once inside the bin folder, link to MySQL by entering:
mysql -u root -p
4. Press Enter. You will be asked to enter your MySQL root password.
5. After entering the correct password, you will see the MySQL prompt (mysql>), indicating a successful connection.
Alternative Connection Methods
1. If your MySQL installation is configured with a different username, replace root with the correct username:
mysql -u your_username -p
2. To connect to a remote MySQL server, use:
mysql -h your_server_ip -u your_username -p
3. Replace your_server_ip with the actual IP address of the MySQL server.
4. If you frequently access MySQL, consider adding MySQL’s bin directory to the Windows system PATH. This allows you to run MySQL commands from any directory without navigating to the bin folder every time.
Step 4: Get a List of Databases on MySQL Server
After successfully connecting to MySQL, you can check which databases are available on the server. This is specifically helpful for database administrators who manage multiple databases and require quick access to their names.
Listing All Databases
1. Ensure you are connected to MySQL and see the mysql> prompt.
2. Type the following command and press Enter:
SHOW DATABASES;
3. The server will return a list of all existing databases in tabular format.
Understanding the Output
The output will display system databases along with any user-created databases. Some common system databases include:
- information_schema – Stores metadata about all databases.
- mysql – Contains MySQL system configurations and user privileges.
- performance_schema – Provides insights into server performance metrics.
- sys – Simplifies database administration with performance views.
If you have created custom databases, they will also appear in this list.
Filtering the List of Databases
If the server contains many databases, you may want to narrow down the list:
- To search for databases with specific names, use:
SHOW DATABASES LIKE 'test%';
This will display only databases whose names begin with “test”.
- To display databases accessible by the current user, execute:
SELECT schema_name FROM information_schema.schemata;
Common Errors and Troubleshooting
- Error: “Access denied for user” – Ensure you have the necessary privileges to view databases.
- No databases displayed? – Check if MySQL is running with the right user permissions.
Step 5: How to Select and Edit Database
Once you have a list of available databases, the next step is selecting the one you want to work with. This step is essential because MySQL commands must be executed within a specific database context. Selecting a database allows you to:
Create and modify tables
Insert, update, and delete records
Manage database objects like indexes, views, and stored procedures
How to Select a Database
To start working inside a particular database, run the following command:
USE database_name;
Replace database_name with the real name of the database you want to access.
After implementing this command, MySQL will confirm the selection with:
Database changed
This means that any SQL queries you run afterward will apply to the selected database.
Verifying the Currently Selected Database
If you are unsure which database is currently active, check it with:
SELECT DATABASE();
This command returns the name of the database you are currently working in, ensuring you execute queries in the correct environment.
Performing Essential Database Operations
Once inside a database, you can start managing its data and structure. Below are some common operations you might need:
1. Creating a New Table
To define a new table within your database, use:
CREATE TABLE employees (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
age INT,
department VARCHAR(50),
hire_date DATE DEFAULT CURRENT_DATE
);
This command constructs a table for employees with columns for an ID, name, age, department, and hire date.
2. Inserting Data into a Table
Once a table is created, you can add records using:
INSERT INTO employees (name, age, department)
VALUES ('John Doe', 30, 'IT');
This inserts a new employee named John Doe into the IT department.
3. Retrieving Records from a Table
To view the stored data, use:
SELECT * FROM employees;
This retrieves all records from the employees table.
4. Updating Existing Records
Modify existing records using an UPDATE statement:
UPDATE employees SET age = 31 WHERE name = 'John Doe';
This updates John Doe’s age to 31.
5. Deleting Records from a Table
Remove specific records using:
DELETE FROM employees WHERE name = 'John Doe';
This deletes John Doe’s record from the employees table.
Troubleshooting Common Database Selection Issues
Error: “Unknown database ‘database_name'”
- Ensure the database name is spelled correctly.
- Use SHOW DATABASES; to confirm its existence.
Error: “Access denied for user”
- You may not have permission to access the database.
- Check user privileges using:
SHOW GRANTS FOR 'your_username'@'localhost';
Database not found?
- If the database is not existing, you can build it employing:
CREATE DATABASE database_name;
Step 6: Create Windows Shortcut to Log in to MySQL
Manually opening the Command Prompt, navigating to MySQL’s installation directory, and entering login credentials every time can be repetitive and time-consuming. Creating a dedicated shortcut on your desktop allows you to access MySQL instantly with just a double-click, improving workflow efficiency.
How to Create a MySQL Shortcut on Windows
Take up the below steps to build a desktop shortcut for quick access to MySQL:
1. Right-click on an empty space on your desktop & choose New > Shortcut from the context menu.
2. In the “Type the location of the item” field, enter the following command:
"C:\Program Files\MySQL\MySQL Server 8.0\bin\mysql.exe" -u root -p
- Ensure the path correctly points to the real site of your MySQL installation.
- If you installed MySQL in a different directory or use a different version, adjust the path accordingly.
- The -u root flag specifies the root user for login, and -p ensures you are prompted for the password when opening MySQL.
3. Click Next and give the shortcut a meaningful name, like “MySQL Command Line Client”.
4. Click Finish to build the shortcut.
Using the MySQL Shortcut
After setting up the shortcut, follow these steps to connect quickly:
- Double-click the shortcut on your desktop.
- A command window will open, prompting you to enter your MySQL root password.
- Once authenticated, you can implement MySQL commands directly.
This method eliminates the need to navigate to MySQL’s directory manually each time you want to connect.
Enhancing the Shortcut for Auto-Login (Optional)
For an even faster login, you can modify the shortcut so that it logs in automatically without requiring a password each time. However, this approach stores your password in plaintext, which poses a security risk.
- Right-click the newly built shortcut & choose Properties.
- In the Target field, modify the command as follows:
"C:\Program Files\MySQL\MySQL Server 8.0\bin\mysql.exe" -u root -pYourPassword
Replace YourPassword with your real MySQL root password.
Warning: This method is not recommended for production environments or shared computers, as anyone with access to your system can see and use your credentials.
Additional Customizations
To further optimize your MySQL shortcut, consider the following modifications:
Changing the Icon
- Right-click the shortcut, select Properties, then click Change Icon.
- Choose an appropriate icon to make it easily recognizable.
Pinning the Shortcut to the Taskbar
- Drag and drop the shortcut onto the Windows taskbar for even quicker access.
Running as Administrator (if needed)
- If you face permission issues, right-click the shortcut and select Properties > Advanced.
- Check Run as administrator and click OK.
Why Use a MySQL Shortcut?
Creating a shortcut saves time and effort, especially for frequent MySQL users. It eliminates the need to manually open the Command Prompt, navigate directories, and type login commands. By following these steps, you ensure faster, more convenient access to MySQL whenever you need it.
Also Read: How To Show a List of All Databases in MySQL
Conclusion
Gaining proficiency in MySQL through the Windows command line equips you with the ability to manage databases with precision and speed. Unlike graphical tools, the command line gives you full control over database operations, from creating and modifying tables to executing complex queries. This method intensifies effectiveness as well as reduces reliance on third-party applications.
Database administrators and developers often rely on the CLI for critical tasks such as database backups, performance tuning, and security configuration. Since MySQL command-line interactions can be automated through batch scripts, they are especially useful for enterprise environments where efficiency and accuracy are crucial. Additionally, learning to work with MySQL in this manner improves your troubleshooting skills, helping you diagnose and resolve connectivity, query performance, and user access issues more effectively.
By mastering MySQL via the command line, you acquire a comprehensive apprehension of database management foundation. This knowledge is valuable for optimizing databases, ensuring data integrity, and maintaining high-performance applications. Whether you are a beginner or an experienced database professional, the ability to interact with MySQL through the command line is a powerful skill that enhances your technical expertise.