Shubham S Nimje logo
Shubham Nimje
Database

How to Import MySQL Database or SQL File

How to Import MySQL Database or SQL File
2 min read
#Database

How to Import MySQL Database or SQL File

Manage your MySQL databases with ease! This guide provides clear instructions for importing existing SQL files and exporting databases as backups. Learn the essential commands and gain control over your data.

Access MySQL with Root User

To access your MySQL server with the root user, use the following command:

mysql -u root -p

Create a New Database

To create a new database, use the following syntax:

CREATE DATABASE Database_name;

For example, to create a database named osmsdb, you would use:

CREATE DATABASE osmsdb;

Access the Newly Created Database

To switch to the newly created database, use:

USE Database_name;

For example:

USE osmsdb;

Import a Database or SQL File

To import a database from an SQL file, use the source command:

source sql_file_path

For example, to import a file located at /var/myimporteddb/osmsdb.sql, you would use:

source /var/myimporteddb/osmsdb.sql

Export MySQL Database as SQL File

To export a MySQL database to an SQL file, use the mysqldump command:

mysqldump -u db_user_name -p Database_Name > File_Path/File_Name.sql

For example, to export the dps database to /var/myexporteddb/dps.sql, use:

mysqldump -u root -p dps > /var/myexporteddb/dps.sql