How to Run MySQL Queries From the Command Line
- Accessing the MySQL Command Line
- Running Basic SQL Queries
- Updating and Deleting Data
- Conclusion
- FAQ

Running MySQL queries from the command line can seem daunting at first, but it’s an invaluable skill for any developer or database administrator. Whether you’re managing a local database or connecting to a remote server, mastering command line operations can significantly enhance your productivity and efficiency.
In this tutorial, we will walk you through the steps to execute MySQL queries directly from the command line. You will learn how to access the MySQL shell, execute basic commands, and manipulate data with ease. By the end of this guide, you’ll feel confident in using MySQL queries via the command line, making your database interactions smoother and more effective.
Accessing the MySQL Command Line
To start running MySQL queries from the command line, you need to access the MySQL shell. This is typically done using the terminal on Linux or macOS, or the Command Prompt on Windows. Here’s how to get started.
First, open your terminal or command prompt. To connect to your MySQL server, use the following command:
mysql -u username -p
Replace username
with your actual MySQL username. After executing this command, you’ll be prompted to enter your password. Once you’ve entered it, you’ll be inside the MySQL shell, where you can start executing queries.
Output:
Enter password: ********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 123
Server version: 8.0.23 MySQL Community Server - GPL
When you see the welcome message, it means you are successfully connected to the MySQL server. Now, you can start running your SQL commands.
Accessing the MySQL command line is the first step in executing your queries. This method is straightforward and provides you with a direct interface to interact with your databases. Knowing how to connect to the MySQL shell is essential for executing any SQL commands effectively.
Running Basic SQL Queries
Once you’re in the MySQL shell, you can start running basic SQL queries. Let’s take a look at how to create a database, create a table, and insert some data.
First, you might want to create a new database. Use the following command:
CREATE DATABASE mydatabase;
USE mydatabase;
Next, create a table called users
:
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100)
);
Now, let’s insert some data into the users
table:
INSERT INTO users (name, email) VALUES ('John Doe', 'john@example.com');
Output:
Query OK, 1 row affected
After inserting the data, you can retrieve it using a simple SELECT query:
SELECT * FROM users;
Output:
+----+----------+------------------+
| id | name | email |
+----+----------+------------------+
| 1 | John Doe | john@example.com |
+----+----------+------------------+
In this section, we covered the basics of creating a database and a table, as well as inserting and retrieving data. These fundamental operations form the backbone of most database interactions. By mastering these commands, you can efficiently manage your data and perform necessary operations directly from the command line.
Updating and Deleting Data
After inserting data, you may need to update or delete records. This section will guide you through those operations using SQL commands in the MySQL shell.
To update an existing record in the users
table, use the following command:
UPDATE users SET email = 'john.doe@example.com' WHERE id = 1;
Output:
Query OK, 1 row affected
Now, if you want to see the updated data, run the SELECT query again:
SELECT * FROM users;
Output:
+----+----------+---------------------+
| id | name | email |
+----+----------+---------------------+
| 1 | John Doe | john.doe@example.com |
+----+----------+---------------------+
To delete a record, you can use the DELETE command:
DELETE FROM users WHERE id = 1;
Output:
Query OK, 1 row affected
After deletion, you can check the table again:
SELECT * FROM users;
Output:
Empty set (0.00 sec)
In this section, we explored how to update and delete records in your MySQL database. These actions are crucial for maintaining accurate and relevant data. Knowing how to manipulate your records effectively will empower you to manage your database with confidence.
Conclusion
Running MySQL queries from the command line is a fundamental skill that can greatly enhance your database management capabilities. By understanding how to access the MySQL shell, execute basic queries, and manipulate data, you can streamline your workflow and improve your productivity. Whether you’re a beginner or an experienced developer, mastering these command line operations will serve you well in your database endeavors. With practice, you’ll find that working with MySQL from the command line becomes second nature.
FAQ
-
What is the MySQL command line?
The MySQL command line is a text-based interface that allows users to interact with the MySQL database server, execute queries, and manage databases. -
How do I connect to MySQL from the command line?
You can connect to MySQL by using the commandmysql -u username -p
, replacing “username” with your MySQL username. -
Can I run complex queries from the command line?
Yes, you can run complex SQL queries, including joins, subqueries, and aggregations, directly from the MySQL command line. -
What should I do if I forget my MySQL password?
If you forget your MySQL password, you can reset it by stopping the MySQL server and starting it with the--skip-grant-tables
option, then updating your password. -
Is it possible to run MySQL commands in a script?
Yes, you can create a SQL script file containing your commands and run it using the commandmysql -u username -p < script.sql
.