SQL is the language used to query, add, update, etc. for SQL database servers. SQL is also used to store database structure or data in a MySQL server. We can export the MySQL server data and structure in SQL format with the
*.sql extension. We can use this SQL file via a command-line interface in order to import it into MySQL.
Export SQL via Command Line Interface
We can export a database via the command line interface. This export puts all structure and data into an SQL file. The
mysqldump command is used to dump the MySQL server. In the following example, we run the mysqldump command to export a database named
customers into the SQL file named
$ mysqldump customers> customers.sql
Import SQL via Command Line Interface
mysql command is used to import an SQL file via the command line interface. The username and database name is provided as a parameter and then the password is provided interactively. In the following example, we import the SQL file named
customers.sql into the database named
$ mysql -u root -p customer < customer.sql
We can also import the SQL file into the remote MySQL server via the command line interface.
$ mysql 192.168.1.10 -u root -p customer < customer.sql
Import SQL via MySQL Interactive Shell
MySQL also provides an interactive shell that can be also used to import SQL files. First, we should log in to the MySQL interactive shell using the mysql command. We provide the password and MySQL database server hostname or IP address. We use the
source command in the MySQL command providing the SQL file name.
$ mysql 192.168.1.10 -u root -p mysql> source customer.sql