Setting up a backup routine with Select Into OUTFILE or alternative?


I am trying to create a php backup routine for my MySQL databases that I can run as CRON jobs a few times a day.
Here is what I have so far:

The error I get is:

Access denied for user 'database'@'%' (using password: YES)

I have set the directorypath folder permissions to 777 and created the file name in advance.

Can you provide assistance with this method or suggest an alternate script? Thank you very much.

connect stuff...
$tableName  = 'SampleTable';
$backupFile = $_SERVER['DOCUMENT_ROOT'].'/directorypath/Sampletable.sql';
$query      = "SELECT * INTO OUTFILE '$backupFile' FROM $tableName";
$result = mysql_query($query) or die(mysql_error());

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi there,

INTO OUTFILE is a MySQL file query and therefore it is not limited by the OS permissions (CHMOD). I would say that your user hasn't got the proper GRANT privileges to enable you to perform this operation.

If you're on shared hosting, I doubt you're going to be able to get the permissions from the provider.

If it's your server then you may want to look at the following:

GRANT FILE is the command you're looking for to give file permissions.

Hope that helps,

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
abernsAuthor Commented:
Okay, thanks. It is a shared server so you're right...I will not be able to get this permission. I am looking at another solution....
You may want to check this out:

It's a MySQL tool to run backups and it can be scheduled as a cron job.
abernsAuthor Commented:
For the link you sent, it looks like I need shell access? The only access I have is via PHPMyAdmin or by writing a PHP script.

I was actually looking into this solution:

That is basically a PHP shell using exec() to use the mysqldump command.

If you have permission to exec() it looks like a reasonable solution. Give it a go :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.