500 Internal Server when importing database via PHP

500-errortimeout

I get a 500 Internal Server Error in my web browser immediately after I view a php page that runs the following script:

exec("mysql -h db.server.com -u myuser -pmypass db < db.sql");

The mysql command takes a long time to execute because db.sql is a very big file and it takes time to send it to an external database server. While the command executes, all other php pages on my shared hosting services yields a 500 Internal Server error.

Is there a way to get rid of the 500 internal server error and have my other php pages running? Is there a better way to import my sql file?

Best Answer

Have the php program save the SQL file to a temporary location on the remote machine and have a cronjob that runs and looks for files in that location and sucks them up into MySQL.

Also set up the php script to create a file in the same directory when the file is beginning to be uploaded so you can write your cronjob to ignore to not attempt to read it while its being uploaded.