473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem using 'OUTFILE' to produce a backup of table

196 100+
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`";
  2.  
  3. $result = mysql_query($query);
  4.  
  5.  
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-

user_name (Privileges: SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, REFERENCES, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES, CREATE ROUTINE)
Nov 17 '09 #1
6 4488
Atli
5,058 Expert 4TB
Hey.

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
chazzy69
196 100+
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
Atli
5,058 Expert 4TB
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);
  9.  
  10.  
  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"');
  16.  
  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.             }
  38.  
  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.         }
  46.  
  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
chazzy69
196 100+
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
Atli
5,058 Expert 4TB
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
chazzy69
196 100+
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

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

Similar topics

3
by: James | last post by:
HI, I'm looking for a script that will allow users/admins to have a one click backup solution for a MYSQL Database.. 'BACK DATABASE' button, click and its done... The a restore option, that...
1
by: juhu | last post by:
Hi there! I've got a problem using ADO to execute a stored procedure (sp) on sql server, which gets two dates and fills a table with some data on that interval. Typical execution time is about a...
11
by: Ebi | last post by:
It's a main function of club program in Borland c++ 5; There is a film class in my club program... But I have a problem with it: whenever I add a film by addfilm function to film.dat file,...
0
by: tim | last post by:
I wrote this very simple program that checks a directory and makes a list of files that surpass a given size. I tried to compile it using py2exe. I used py2exe before with more complex programs...
7
by: Serge Rielau | last post by:
Hi all, Following Ian's passionate postings on problems with ALTOBJ and the alter table wizard in the control center I'll try to explain how to use ALTOBJ with this thread. I'm not going to get...
2
by: varusnyc | last post by:
Hello, Im having really hard time writing an Employee Payroll program that uses functions to read data from file then send all data to another file. I managed to construct some pieces of the code,...
3
by: MariyaGel | last post by:
I have wrote the program and it worked fine until I had to include one more array into it. As you can see below the two arrays of volume and mass are working properly now I need to include a third...
3
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0...
4
cassbiz
by: cassbiz | last post by:
Could use some help here. This script is carrying over an image just fine but the text isn't coming over. can you see why it is not working???? from the form I want to carry over two lines of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.