This article will discuss the following content:
- Import Database to the Local MySQL Server Using Command Line
- Import Database to the Remote MySQL Server Using Command Line
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:
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”:
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:
Provide the parameters according to you and run the command:
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:
After the successful login, run these commands, navigate to the database, and see all the available tables:
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:
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:
Use these command to see the names of tables in the database you imported:
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.