import sql file in mysql

How to Import SQL File in MySQL Using Command Line

SQL file is a collection of SQL queries used to create and populate databases and tables in MySQL. When you take a backup of MySQL database using mysqldump command, it generates a SQL file. Often you may need to import SQL file in MySQL using command line. In this short article, we will learn how to do this.

How to Import SQL File in MySQL Using Command Line

Let us say you have a database named sample_db. Here is the command to easily take a backup of this database as a .sql file named sample.sql.

$ mysqldump -u<username> -p sample_db > /home/ubuntu/sample.sql

You will be prompted for MySQL password, enter it to run the above command.

Let us say you want to import this .sql file in MySQL to a database called sample2.

First we create this database and exit MySQL.

$ create database sample2

In most cases, you need to create an empty database unless your .sql file also contains create database statements.

Once you have created the new database, run the following command to import the sample.sql file.

$ mysql -u username -p sample2 < /home/ubuntu/sample_db.sql

You will be prompted for password, enter it to run the command.

Please note, it is advisable to mention the full path to .sql file while importing from SQL file to MySQL database, as well as while using mysqldump to create .sql file from database.

Also, if the above command gives you ‘permission denied’ error, try using sudo keyword at the beginning of the command.

In this article, we have learnt how to import SQL file in MySQL.

Also read:

How to Change Date Format in jQuery UI Date Picker
How to Fix NGINX Bind to 0.0.0.0:80 Failed
How to Fix NGINX Unable to Load SSL Certificate
How to Detect Touchscreen Device in JavaScript
How to Fix Server Quit Without Updating PID File MySQL Error

Leave a Reply

Your email address will not be published. Required fields are marked *