in Web and Tech, Work

Loading an SQL file via SSH CLI

Loading an SQL file to a remote MySQL server has been made easy nowadays by web-based interfaces made available by web applications like PHPMyAdmin and the compact Adminer utility. Another approach is remotely connecting to the server via a desktop client utility like the MySQL WorkBench and SQLYog. Unfortunately, remote host access is often disabled on many servers for reasons citing good security practice, so going this route is not always a good option.

There could be a number of reasons for loading an SQL file, the more common of which are restoring a backup or installing a web application.

There are times though when the limits imposed by web-based transactions, particularly the maximum allowable file size (50MB by default), necessitate managing the MySQL server via SSH command line. This can happen when you’re trying to restore a large backup or are migrating a large SQL file.

To do this, load the SQL file to an accessible directory on the MySQL server via FTP or Rsync. Afterwards, connect via SSH. Navigate to the directory you placed the SQL file.

Then run MySQL like so:

mysql -p -u username database_name < file.sql

Enter your password when prompted. If there are no errors messages and you are brought back to the prompt, then you have successfully loaded the file.

Write a Comment

Comment