MySQL MariaDB

MySQL Import Database Using Command Line

MySQL is an open-source relational database management system that helps users to create, manage and store huge volumes of data. Moreover, MySQL also provides the facility of importing databases using SQL files. The import helps to back up and transfer the data.

This article will discuss the following content:

Import Database to the Local MySQL Server Using Command Line

To import databases using a MySQL file to the local MySQL server, first login into your local server using this syntax:

mysql -u [username] -p

Provide the username of the Local server in the command:

The next step is to create a new database in which you want to import a database. Provide the name for your database in the command. For this post, the name is “newlinuxhint”:

create database newlinuxhint;

The success message will display as “Query OK, 1 row affected”:

The SQL file is a backup file containing instructions to create a database with its data. Now to import the database use this syntax:

mysql -u [username] -p [db-name] < [file-name].sql

Provide the parameters according to you and run the command:

mysql -u root -p newlinuxhint < linuxhint_dump.sql

The error-free output indicates that the command is executed successfully:

To confirm it, login to the local server. Use this command and make sure to provide your username:

mysql -u root -p

After the successful login, run these commands, navigate to the database, and see all the available tables:

use newlinuxhint;

show tables;

The output will display the names of tables if the import was successful:

You have successfully imported a database using a SQL file in your local MySQL server.

Import Database to the Remote MySQL Server Using Command Line

To import the database on the Remote server using SQL file through the command line, use this syntax:

mysql -h [host-name] -P [port-number] -u [username] -p [db-name] < [file-name].sql

Provide the port number, username, hostname(endpoint) of the Remote MySQL server. Also, provide the database name and the name of the MySQL file:

To confirm if the database is imported successfully or not, log in to your remote MySQL server by using this syntax:

mysql -h [host-name] -P [port-number] -u [username] -p

Use these command to see the names of tables in the database you imported:

use newlinuxhint;

show tables;

The output is displaying all the tables from the imported database:

The database is successfully imported from a SQL file to your remote MySQL server.

Conclusion

To import the database in your MySQL server using a SQL file. For the local server, run the “mysql -u [username] -p [db-name] < [file-name].sql” command, and for the remote server, run “mysql -h [host-name] -P [port-number] -u [username] -p” syntax. This post discussed how you could import the database using the command line to your remote as well as local MySQL server using SQL files.

About the author

Abdul Mannan

I am curious about technology and writing and exploring it is my passion. I am interested in learning new skills and improving my knowledge and I hold a bachelor's degree in computer science.