an alternative to mysqldump? - php

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

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

How to backup database entirely from local database to host database in laravel?

I'm using laravel and mysql as database, and I want to make a backup database in hosting with one click button in html, but I wonder what is the best query for them instead of query all data in all table with loop, then insert it one by one to host with loop. Thanks before
Use mysqldump
$ mysqldump -h <host> -u <user> -p<password> <database_name> > backup.sql
This will create a sql file backup.sql with all your data.
Note the missing space beween -p and <password>.
It's okay to give the password interactively (in which case, you'll have to skip the <password> part), but since you mentioned laravel in the question, I am assuming you want to code this and want it to be non-interactive.
EDIT:
Using code
Execute the command with exec()
$cmd = "mysqldump -h <host> -u <user> -p<password> <database_name> > backup.sql";
exec($cmd, $output, $return_value);

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

shell_exec with MySQL not working even though safe mode is off

I have a string of script which working in terminal but does not work when I use it in PHP with shell_exec().I know a lot of questions similar to this question has been asked already but in my case the problem I am facing is that I have already tried the proposed solutions I found. Below is my simple code.
<?php
$output = shell_exec('mysql -u root -pmypass -h 127.0.0.1 mydatabase< db.sql');
echo "<pre>$output</pre>";
?>
So far this is the best solution I have found.
Does anyone knows what might be wrong?
Your shell_exec is this:
$output = shell_exec('mysql -u root -pmypass -h 127.0.0.1 mydatabase< db.sql');
And your command is this:
mysql -u root -pmypass -h 127.0.0.1 mydatabase< db.sql
The reason that command works when you are in the shell is the binary path to mysql is part of your user login profile.
To see what I mean, login to the shell as yourself and then type echo $PATH and what you will see is a list of search paths the shell uses to figure out where binaries you are attempting to run are located.
But when you attempt to run a script via shell_exec() the Apache server user running PHP is making the sell call. And that user typically does not have binary paths set. So you need to provide the full path to mysql which might be:
/usr/bin/mysql
Or:
/usr/local/bin/mysql
The best solution is from the shell use the which command like so:
which mysql
And then take the full path provided and adjust your shell_exec() command as follows; using /usr/bin/ for example:
$output = shell_exec('/usr/bin/mysql -u root -pmypass -h 127.0.0.1 mydatabase < db.sql');
Also, where is db.sql actually located? You would have to prepend the full path to that MySQL script like this as well; using /full/path/to/this/ for example:
$output = shell_exec('/usr/bin/mysql -u root -pmypass -h 127.0.0.1 my database < /full/path/to/this/db.sql');

MySQL to MySQL clone with PHP

Anyone know of a PHP script that will clone an entire MySQL database to another on another server? As a sort of backup?
You'd have your PHP script run (with e.g. the exec() or system() call) something like
mysqldump -q -C --databases mydatabase | mysql -C -h othermachine
Add the appropriate flags to these commands for specifying credentials, etc.
phpMyAdmin does this very well. Of course, if you had shell access, try this instead:
mysqldump -u [username] -p [password] [database] > [filename]
You can refer this link. -
http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html
This page is titled
MYSQL DUMP — A Database Backup Program
It contains all the details.
Hope this helps you.

Categories