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

--

--

Export (add dump) >

$ mysqldump -u username -p databasename > /home/file.sql
$ mysqldump -u username -p databasename | gzip > /home/file.sql.gz

Import <

$ mysql -u username -p databasename < /home/file.sql

or with source

mysql> use database
mysql> source /home/file.sql

--

--

Natan Cabral

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