Execute mysqldump to back up database in sql format - php

I'm trying to use PHP's exec() function to run mysqldump to back up a database named projectdata from Amazon Web Service. But I can only create an empty sql file.
I'm running the php file with xampp, under Windows 7 where mysqldump is in C:\xampp\mysql\mysqldump
exec('C:\xampp\mysql\bin\mysqldump --user=user --password=password --host=cannotTellyou.amazonaws.com:3306 projectdata > backup.sql');

What you should do is: do a ssh login to your AWS machine. Run the mysqldump in command line and start debugging from there.
ssh <your remote AWS using your private_key>
then run
mysqldump -u <username> -p<password> yourDB | gzip > backupfilename.sql.tar.gz
use gzip if you want to zip your backup file, otherwise, it's not necessary.
Then refer to this post:
how to mysqldump remote db from local machine

I would try to explicitly specify the file name instead of redirecting the output. Like this:
exec('C:\xampp\mysql\mysqldump --user=user --password=password --host=cannotTellyou.amazonaws.com:3306 projectdata -r backup.sql');
The -r option should be used also because:
Direct output to a given file. This option should be used in MSDOS, because it prevents new line '\n' from being converted to '\r\n' (carriage return + line feed).

It works after removing the port number
C:\xampp\mysql\bin\mysqldump --user=user --password=password --host=cannotTellyou.amazonaws.com projectdata > backup.sql

Related

mysqldump don't work with PHP, just into command line

I am trying to execute this command with php:
system ('mysqldump -u myUser myDbname | mysql -u myUser -A myDbBackupName');
This does not return a error, but does nothing.
The same command executed in server by command line works perfectly.
I am using .my.cnf and i configured the user to mysql, mysqldump and client.
I don't know what is happening. Can somebody help me?
I solved this issue. Put this 2>&1 in the end of the command to force return the output of method exec() or system() to facilit the debug.
The correct mysql command is without space between -p[password]. In fact the password is necessery, independent if you using the archive .my.cnf
Like this:
mysqldump -u user -ppassword myDbName | mysql -u user -ppassword myDbBackupName

Phpmyadmin "Error in processing request" when export table - Error code 500

I'm trying to use PhpMyAdmin v. 4.5.3.1 to access a DB on a localhost and export a table but it is not working.
I can access the DB, insert, search, etc. but when I click on "Export" tab it gives me this message:
I don't have this issue with PhpMyAdmin 4.2.6 using the same WAMP....
Does anyone knows how to fix it?
Thank you!
I think you should use mysqldump instead, when exporting data. From the command line:
mysqldump -uMYSQL-USER -h server -pMYSQL-USER database_name > /path-to-export
Or from a script:
$command = "mysqldump -uMYSQL-USER -h server -pMYSQL-USER database_name > /path-to-export/file.sql";
exec($command, $output, $return_var);
This can easily be automated.
You could fix this error by increasing memory limit to your requirement and restart httpd/apache service. I fixed it sometimes by increasing memory_limit. But now i prefer to use terminal commands only to handle it. Its better to always get habitual using terminal commands for doing such big operations in mysql. You get speed and more control over it as you are not dependent upon GUI based systems.
Use mysqldump in terminal to export data:
mysqldump -u root -p db_name > /home/dump.sql
Use mysqldump in terminal to export only schema without data:
mysqldump -u root -p db_name --no-data > /home/dump.sql

getting "db: SQLSTATE[HY000] [2002] Connection refused" error on Mac with MAMP

I am trying to run the CLI version of this PHP databse Search and Replace Script, but I think this a more general MySQL problem relating to Mac OS X and MAMP. I receive the following error whenever I attempt to run the CLI script locally:
db: SQLSTATE[HY000] [2002] Connection refused
Here is the command I'm running:
./srdb.cli.php -h 127.0.0.1 -u root -n mydbname -proot -c utf\-8 -s mywebsite.com -r dev.mywebsite.com
What I've tried
I am able to connect to mysql using these settings, no problem, using mysql -u root -proot etc...
Swapping 127.0.0.1 for localhost gives the same error.
All my my.cnf files are blank.
Apache and MySQL are running fine.
I have succeeded in replicating this problem on another Mac running MAMP
I am using this mysql: /Applications/MAMP/Library/bin/mysql
And this php: /Applications/MAMP/bin/php/php5.3.28/bin/php
Anybody any ideas? Thanks!
Edit
Here is the source code showing how the script connects to MySQL:
https://github.com/interconnectit/Search-Replace-DB/blob/master/srdb.cli.php
which in turn imports this:
https://github.com/interconnectit/Search-Replace-DB/blob/master/srdb.class.php
As stated in my comment already, chances are that you're not running the PHP binary you thought you were running. Even if the MAMP php binary is in your path, the shebang line in srdb.cli.php reads #!/usr/bin/php and that points to the Apple-provided php binary.
So if you invoke the script with the full path to your MAMP php binary, the problem should be avoided:
/Applications/MAMP/bin/php/php5.3.28/bin/php srdb.cli.php -h 127.0.0.1 -u root -n mydbname -proot -c utf\-8 -s mywebsite.com -r dev.mywebsite.com
Another solution might be to replace the shebang line with:
#!/usr/bin/env php
This works only if the MAMP binary is in your $PATH in front of /usr/bin. Using #!/usr/bin/env phpensures however, that you're always using the same binary no matter if you're invoking the script via ./srdb.cli.php or with php srdb.cli.php.
Stop mysql :
sudo service mysql stop
And then start it :
sudo service mysql start
It resolved the problem
To add onto z80crew's brilliant solution, for anyone else unfamiliar/uncomfortable with altering path variables, specifying the full location paths for both the MAMP php binary and the search-replace-db script in the cli script provided by interconnect solved the problem for me. I put the strings to search for and replace with in quotes. I also increased the php timeout limit in wp-config.php with: set_time_limit(3000);
I was consistent with the server name between the options passed to the script and what's in my wp-config.php file (using localhost in wp-config, using localhost in the script as well)
/Applications/MAMP/bin/php/php7.4.2/bin/php /Applications/MAMP/htdocs/test/Search-Replace-DB-master/srdb.cli.php -h localhost -u root -proot --port 8889 -n test -s "http://olddomain.com" -r "http://localhost:8888/test" -v true

an alternative to mysqldump?

First of all, I am having serious problems with MYSQLDump, We have a dedicated server here for our main domain and I am running the following command:
mysqldump --opt -h localhost -u root -p ***** --all-databases > ~/var/www/vhosts/mydomain/httpdocs/db.sql
and I get nothing :(
But more importantly, I don't have root access to every server I have access to. But I do have database username and passwords. Surely there is a PHP only way of dumping the entire contents of a SQL database?
then why don't you use your user/password for the databases to do a per database dump as described i.e. here:
http://dev.mysql.com/doc/refman/5.1/de/mysqldump.html
mysqldump [options] --databases db_name1 [db_name2 db_name3...]
i just know two options to backup mysql-databases. One is to use mysqldump, the other one is to stop the mysql-server and backup the databasefiles. Doing dumps using PHP or whatever will last longer and cause much more trouble then just using mysqldump!
I was not aware there was a MySQL root.
Well, then that's the most likely cause of your problems, since you have this:
mysqldump --opt -h localhost -u root -p *****
^^^^^^^
The -u parameter expects a MySQL user and you are probably feeding it with systems' root user, which is something entirely different.
If you have a separate user for each database, I'm afraid you'll have to issue separate dumps.
Additionally, try to fetch error messages. You can redirect stderr to stdout by appending the 2>&1 operator to your command and you can grab output from shell_exec()'s return.
In the mysqldump command there is no space after -p and the password so your line should look like:
mysqldump --opt -h localhost -u root -p***** --all-databases > /var/www/vhosts/mydomain/httpdocs/db.sql

Generating a dump file from PHP

I'm trying to generate a mysql dump file from PHP using the following:
exec('mysqldump -u root -ppassword maindb > c:\DB_Dump.sql');
However the file being generated is blank. Anyone knows what's wrong?
From the cmd, this is working:
cd C:\Program Files\MySQL\MySQL Server 5.5\bin to change the path and then
mysqldump -u root -ppassword maindb > c:\DB_Dump.sql
But I'm trying to do it within 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')

Categories