Share via:

Solution to SOURCE Error 2 with MySQL

The error happens when importing database backups using the SOURCE command, either because you got the path wrong, or because you used the command incorrectly.


Edited: 2019-03-05 12:16

This is just a quick reminder to those having problems backing up- and importing MySQL databases. Often when I try to import a database using the SOURCE command I will get a SOURCE error 2 message.

The error simply means that we have gotten the file path wrong. However, in my case, it was because I used a less than sign between the SOURCE and the file name.

The reason for my mistake is that I use a greater than sign when backing up a database using mysqldump, and sometimes I mess up because the same logic does not apply when importing. Hopefully I will remember it now.

mysql> SOURCE < /home/username/database_backup.sql

Failed to open file '< /home/username/database_backup.sql', error: 2

If you have made the same mistake, simply remove the less than sign from your statement. The SQL statement should look like this:

SOURCE /home/UserName/some_sql_file.sql

The problem can also be that you miss typed the path for the .sql file, though I suppose this is unlikely if you know what you are doing, but be sure to double check this as well.

Alternatively, you can also import databases using mysql in a terminal, in which case you will be using the less than sign:

mysql database_name < database_name.sql

To create a .sql backup of a database, use mysqldump:

mysqldump database_name > database_name.sql