By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,971 Members | 1,868 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,971 IT Pros & Developers. It's quick & easy.

Problem using 'OUTFILE' to produce a backup of table

P: 196
Im currently using a php script to access a database and use the following lines to produce a backup of a table-

Expand|Select|Wrap|Line Numbers
  1. $query = "SELECT * INTO OUTFILE 'test.sql' FROM `default_en_listingsdbelements`";
  3. $result = mysql_query($query);
Which seems to execute fine and produces no errors except it doesn't actually produce any file. I figured it would produce the file in the same directory as the script or if not in the root directory but neither contains the file.

I then tested the same line in phpmyadmin/mysql which produced user does not have permission to create file.

So im a little unsure as what to do from here, do i need to change permission for the user and if so what access do i need to allow or am i using the 'OUTFILE' function incorrectly?

Any help would be greatly appreciated, note i think the username im using currently has all access permissions-

Nov 17 '09 #1
Share this Question
Share on Google+
6 Replies

Expert 5K+
P: 5,058

I think this is not a problem with internal MySQL user privileges, but rather the access the user running the MySQL service has to the actual file-system.

If you want to use the SELECT INTO OUTFILE command, the user running MySQL (typically; mysql) needs to have access to the directory you want the file saved into.

On a Linux system, try running the following command as root:
Expand|Select|Wrap|Line Numbers
  1. $ chown -R mysql:mysql /path/to/where/you/save/the/file/
If that doesn't work, you can try altering the permission of the file, giving everybody access to it. Note! this is a very unsafe thing to do, as it gives everybody access to everything under the directory.
Expand|Select|Wrap|Line Numbers
  1. $ chmod -R 777 /path/to/where/you/save/the/file/
Nov 17 '09 #2

P: 196
First up thx for the reply, secondly the database, files and everything are on a remote server i.e. hosted under website so i don't think i could run the linux command.

The idea of giving everyone access to the folder might work, ill check it out.

Thanks again for the reply,

Ok tried changing the permission for the whole folder to chmod 777 and interestingly produces a internal server 500 error whenever i try to run the script. So where should i go from here? talk to the people hosting the account?

Thanks again,
Nov 17 '09 #3

Expert 5K+
P: 5,058
As it turns out, your MySQL user does need the FILE privilege to use this.
The file is created on the server host, so you must have the FILE privilege to use this syntax.
So to use this at all you would need to contact your host and have him grant you this privilege.

Why do you want to use the OUTFILE clause at all? Is there any specific to want the file saved on the server?

You could replicate the file created by the command using PHP, and then just download it to your computer via the PHP script.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // Query the database
  3. $dbLink = new mysqli('host', 'usr', 'pwd', 'dbname');
  4. if(mysqli_connect_errno()) {
  5.     trigger_error(mysqli_connect_error(), U_USER_ERROR);
  6. }
  7. $sql = "SELECT * FROM `tblName`";
  8. $result = $dbLink->query($sql);
  11. if($result)
  12. {
  13.     // Set it up so the data will be downloaded as a CSV file.
  14.     header('content-type: text/csv; charset=utf-8');
  15.     header('content-disposition: attachment; filename="test.csv"');
  17.     // Create the file.
  18.     while($row = $result->fetch_row())
  19.     {
  20.         // Counts down the number of rows left.
  21.         // Used to skip the \t after the last column.
  22.         $col_countdown = count($row) - 1;
  23.         foreach($row as $_column)
  24.         {
  25.             // In case the column is NULL, write the
  26.             // NULL escape char: \N (Note the upper case N)
  27.             if($_column === null)
  28.             {
  29.                 echo "\N";
  30.             }
  31.             else
  32.             {
  33.                 // By default, only \n, \t and  \ are escaped.
  34.                 // Escaping \ alone will esacpe the other two as well
  35.                 // so we don't have to explicitly escape them.
  36.                 echo str_replace("\\", "\\\\", $_column);
  37.             }
  39.             // Print the trailing \t, unless this is the last column.
  40.             if($col_countdown > 0)
  41.             {
  42.                 echo "\t";
  43.             }
  44.             --$col_countdown;
  45.         }
  47.         // Line breaks are a single \n by default. (Unix style)
  48.         echo "\n";
  49.     }
  50. }
  51. else
  52. {
  53.     trigger_error($dbLink->error, E_USER_ERROR);
  54. }
  55. ?>
That should create the file in the default format used by the SELECT INTO OUTFILE command, and send it to you as a CSV file.
Nov 18 '09 #4

P: 196
That would work great but i need it to be saved onto the server not my computer, the reason for this is basically -

attached to one website is the main database which contains a whole bunch of elements, now im setting up satellite sites that can link to the other database, previously it was directly linked to the main database but this was cause to many problems and wasn't working. Therefore decided that i could use php to backup certain tables within the database then use another php script on the satellite sites to retrieve and input the tables into their own databases.

More specifically i wanted to use this method "outfile" because it has a simple input method -

$query = "LOAD DATA INFILE 'backupFile' INTO TABLE $tableName";
but in any case i should be able to change the privileges of the mysql user as i have access to it, hopefully this will fix the problem.

Thanks again for the help :)
Nov 18 '09 #5

Expert 5K+
P: 5,058
Ok. I see.

In case you are unable to alter the privileges to get this to work, you could alter the script I posted so that, instead of sending the file to you, it saves it onto the server's file system.
This would bypass the MySQL privilege problem, and would still allow you to use the LOAD DATA INFILE command to update your other databases.
Nov 18 '09 #6

P: 196
Oh really? sweet thanks heaps then that will definitely help in the case in can't get the appropriate privileges.

Didn't realize that you could use the LOAD DATA INFILE with a .csv file thought it had to be it had its own structure,

But thanks agains, cheers
Nov 18 '09 #7

Post your reply

Sign in to post your reply or Sign up for a free account.