Command to import MySQL database

To import a MySQL or MariaDB database that you have in a backup file, with SQL statements, you need to run the mysql command.

You use the mysql command as if you were going to connect to the database, with the typical connection options, host, user… and at the end you put the database to which you want to send the content of your .sql file , along with the less than sign and the name of the file to import.

mysql -h localhost -u username -p your_key database_backup-file-mysql.sql

Of course, the file “backup-file-mysql.sql” should be in the same folder that you are located in the terminal. If not, you’ll have to type the full path to the file.

Other command alternatives

You can omit the password to avoid having to write it in the command, a recommendation for security, so that nobody sees the password that is written or is stored in the history of commands carried out in the console sessions:

mysql -h localhost -u username -p database < backup-file-mysql.sql

Then it will ask you for the password before restoring the backup.

You can connect with other hosts, to import the MySQL database on a remote server, placing its IP:

mysql -h 1.2.3.4 -u username -p database < backup-file-mysql.sql

You can even use the localhost IP, since sometimes connecting to “localhost” gives problems on some computers.

mysql -h 127.0.0.1 -u username -p database < backup-file-mysql.sql

Loading Facebook Comments ...
Loading Disqus Comments ...