Tool Logo
Database

How to Import a MySQL Database or SQL File

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

To import an SQL file into MySQL, create the database, then run one command in your terminal. You'll be asked for the MySQL password:

mysql -u root -p database_name < /path/to/file.sql

Already inside the MySQL shell? Select the database with USE database_name; and run source /path/to/file.sql. The steps below cover both methods, plus how to export a database with mysqldump.

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