Using a .php file to generate a MySQL dump
You can use the exec()
function to execute an external command.
Note: between shell_exec()
and exec()
, I would choose the second one, which doesn't return the output to the PHP script -- no need for the PHP script to get the whole SQL dump as a string : you only need it written to a file, and this can be done by the command itself.
That external command will :
- be a call to
mysqldump
, with the right parameters, - and redirect the output to a file.
For example :
mysqldump --user=... --password=... --host=... DB_NAME > /path/to/output/file.sql
Which means your PHP code would look like this :
exec('mysqldump --user=... --password=... --host=... DB_NAME > /path/to/output/file.sql');
Of course, up to you to use the right connection information, replacing the ...
with those.
mysqldump via PHP
You should check the third parameter of exec function: &$return_var
.
$return_var = NULL;
$output = NULL;
$command = "/usr/bin/mysqldump -u mysql-user -h 123.145.167.189 -pmysql-pass database_name > /path-to-export/file.sql";
exec($command, $output, $return_var);
By convention in Unix a process returns anything other than 0 when something goes wrong.
And so you can:
if($return_var) { /* there was an error code: $return_var, see the $output */ }
Create MySQL DB export via PHP
$dump = shell_exec('mysqldump --user=user --password=asdasd --host=localhost db_name');
alternatively you can use php implementation of mysqldump, available on https://github.com/clouddueling/mysqldump-php
Using PHP script: why mysqldump does not dump sql file?
Okay, actually I already solve this issues a few days later after I post the question. Unfortunately, I didn't have enough reputation to post an answer.So here we go.
Here are a few things that need to TAKE NOTE:-
- Compulsory to use absolute path for MySQL dump.
- Try using --opt (default option for MySQL dump). Read more here.
- For the option, if use short form, no need to have (--). eg: --p. Use (--) when use full form. eg: --password. So use '-p' instead of '--p' (applied for others option too).
- try 'shell_exec' or 'system' if mysqldump doesnt work on 'exec'.
- try avoid have space between option and variable. Eg: -p$dbpass
*Also bear with mind, it involves with permission whether system command can be executed from php or not.
Generating a dump file from PHP
To discard most common issues:
- Use full paths
- Send output to a publicly writable directory
The path to mysqldump in your computer appears to have white spaces. Make sure you quote it properly:
exec('"C:\\Program Files\\MySQL\\MySQL Server 5.5\\bin\\mysqldump" -u root -ppassword maindb > c:\\DB_Dump.sql 2> c:\file.err.txt')
Export data from database to sql file with PHP
You can run a mysqldump
command using PHP exec
function to only export data. Ex:
exec('mysqldump -u [user] -p[pass] --no-create-info mydb > mydb.sql');
More info:
- mysqldump data only
- Using a .php file to generate a MySQL dump
Related Topics
What's the Difference Between Echo, Print, and Print_R in PHP
PHP Reading Shell_Exec Live Output
How to Convert a Number to a Word in PHP
Glob() - Sort Array of Files by Last Modified Datetime Stamp
I Have an Array of Integers, How to Use Each One in a MySQL Query (In PHP)
A Numeric String as Array Key in PHP
Pdo::_Construct(): Server Sent Charset (255) Unknown to the Client. Please, Report to the Developers
Codeigniter: How to Connect to Your Database Server Using the Provided Settings Error Message
What's the Best Way to Get the Last Element of an Array Without Deleting It
Show Values from a MySQL Database Table Inside a HTML Table on a Webpage
PHP: Convert Any String to Utf-8 Without Knowing the Original Character Set, or At Least Try
Json_Encode PHP Array as Json Array Not Json Object
MySQL Prepared Statements With a Variable Size Variable List
Where Does PHP'S Error Log Reside in Xampp