How to Run SQL Query from File on MySQL Command Prompt

In this tutorial, we will learn How to Run SQL Query from File on MySQL Command Prompt?

If you are on MySQL command prompt and need to execute all SQL queries contained in SQL file. You can simply do this by using source option to load any .sql file on your MySQL database. This executes all SQL queries available in a text file on a selected database.

For example, you have a database schema file schema.sql to load on the database. Now login to MySQL server and select database.

mysql -u root -p

After that, run the following command from the MySQL command prompt.

USE mydb;
source /home/user/Downloads/sql-query.sql

The above command will run all the available SQL queries on the selected database [mydb in this case].

Leave a Comment