Solved

Use System Function to  execute a mysqldump

Posted on 2008-10-30
9
331 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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:Umesh
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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 26

Expert Comment

by:Umesh
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:Umesh
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:
Umesh 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

623 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