How to import a .sql file into MySQL or MariaDB using the Linux Terminal

Home > Search > How-to
  by

It is important to know if the .sql file has code that will create the database if the database does not exit. The .sql file can be opened in Notepad++. If the file contains the commands CREATE DATABASE IF NOT EXISTS, the .sql file does include the commands to create the databases.

CREATE DATABASE /*!32312 IF NOT EXISTS*/ `example` /*!40100 DEFAULT CHARACTER SET latin1 */;

 

Use this command to import the SQL file.

mysql -u root -p < the_file.sql

 

On the other hand, if the .sql file appends data to a database that already exists, you need to list the name of the database in Terminal.

mysql -u root -p databasename < the_file.sql

 

To verify that the data was imported, connect to your MySQL or MariaDB, and use the show databases, use database, show tables and select * from tablename commands.



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments