Solved

Use System Function to  execute a mysqldump

Posted on 2008-10-30
9
328 Views
Last Modified: 2013-12-13
I am trying to create a mysqldump in PHP:

----------
$command = "mysqldump -h localhost --user=me --password=mypass --no-create-info --opt --where=\"Id<12\" MyDatabase MyTable |sed -e \"s|INSERT INTO|REPLACE INTO|\" > ".$path."replacedump.sql";
system($command,$error);
-------------

The dump file is not being executed.

I can take the string for $command and put in straight to the command line and it works. Then I thought maybe it was the system() function, so I create system("ls",$error); in PHP and it worked, I got the file list in my browser.

Is there a reason why system() is not working with mysqldump. I should have permission to run it because I can do it on the command line. Does PHP read permissions differently? Or is there something wrong with my mysqldump?

I appreciate any help!
0
Comment
Question by:SaltyDawg
  • 5
  • 4
9 Comments
 
LVL 1

Author Comment

by:SaltyDawg
ID: 22841444
I've also used exec() and passthru() and none worked.
0
 
LVL 26

Expert Comment

by:ushastry
ID: 22841631
This may help you..

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/Q_23025392.html

Also, can you tell me whether safe_mode is enabled in php.ini?

Thanks,
Umesh
0
 
LVL 1

Author Comment

by:SaltyDawg
ID: 22841860
Hello Umesh
thanks again for your help

safe_mode and safe_mode_gid are off
and
safe_mode_exec_dir and safe_mode_include_dir have no value
and
sql.safe_mode is off


my error always returns 1
0
Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 26

Expert Comment

by:ushastry
ID: 22842288
Can you please try this way... lets start one-by-one.. first will see whether it dumps or writes any error...

Once you execute below code.. then from command line check whether the file /tmp/mydump.sql is created...


<?php
 
$command = "mysqldump -h localhost --user=root --password=123 --no-create-info --opt --where=\"Id<12\" MyDatabase MyTable 2>&1 > /tmp/mydump.sql";
system($command,$error);
 
?>

Open in new window

0
 
LVL 26

Expert Comment

by:ushastry
ID: 22842411
Just checked below code and it works for me..

PHP details..

$ php -v
PHP 5.0.4 (cli) (built: Mar 17 2006 15:37:24)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v2.0.4-dev, Copyright (c) 1998-2004 Zend Technologies


Pls change required info from below...script.. hostname, user,pass, DBname, table etc
<?php
 
$command = "mysqldump -h xx.xx.xx.xx --user=me --password=secret --no-create-info --opt --where=\"id <12\" myDB myTable |sed -e \"s|INSERT INTO|REPLACE INTO|\" 2>&1 > /tmp/mydump.sql";
system($command,$error);
 
?>

Open in new window

0
 
LVL 1

Author Comment

by:SaltyDawg
ID: 22843139
Yes it worked for me to. Apparently from PHP that's only directory I can write to. I can't even write to my my Home dir. I'm guessing from PHP I'm not logged in as myself. Is there a command I can run to log in?
0
 
LVL 26

Accepted Solution

by:
ushastry earned 500 total points
ID: 22843271
Command...not sure will check and let you know...

BTW, you can do onething create a directory under your home or anywahere you want and chmod it to 777

chmod -R 777
0
 
LVL 1

Author Comment

by:SaltyDawg
ID: 22843420
Yea I figured out the chmod, thanks
0
 
LVL 1

Author Closing Comment

by:SaltyDawg
ID: 31511676
Thanks again.

But keep an eye out for me again I may have one more thing to do regarding scp to another host with php
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating and Managing Databases with phpMyAdmin in cPanel.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question