How to export and import a .sql file from command line

Natan Cabral
Aug 30, 2021

How to import mysql database with terminal

Type the following command to import sql data file:

$ mysql -u root -p -h localhost databasename < file.sql

In this example, import ‘data.sql’ file into ‘blog’ database using vivek as username:

$ mysql -u root -p -h localhost databasename < file.sql

If you have a dedicated database server, replace localhost hostname with actual server name or IP address as follows:

$ mysql -u root -p -h 202.54.1.10 databasename < file.sql

How to export

To export a database, use the following:

$ mysqldump -u username -p databasename > file.sql

Note the < and > symbols in each case.

$ mysqldump --add-drop-table -u admin -p databasename > file.sql

--

--

Natan Cabral

Full Stack Developer | Dev Java, Node.js, TypeScript, React.js, Vue.js, Express.js, Next.js, Rest API, Laravel, Databases, MongoDB, Unix distro and Open Source