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
Related
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
I am trying to execute the following command through PHP using shell_exec() function:
sudo -u USERNAME -pPASSWORD echo "SOME-STRING" > /dev/tcp/127.0.0.1/64444
but nothing happens (if I copy/paste it manually to the terminal it works). There is also nothing in the $output of the shell_exec() function.
So with that command I am trying to send a string over TCP through a different user with sudo privileges.
EDIT #1: The full PHP command looks like this:
shell_exec('sudo -u USERNAME -pPASSWORD echo "RLSET|79-192.168.1.33-0-1" > /dev/tcp/127.0.0.1/64444');
EDIT #2: I've also checked the phpinfo() function and safe_mode is off also the shell_exec() function is not on the disallowed functions list.
EDIT #4: My PHP PATH variable contains the following locations:
/usr/local/bin:/usr/bin:/bin
I'm lost here, any and all help would be appreciated.
Scape your quotes put your full command path:
shell_exec("sudo -u USERNAME -pPASSWORD echo \"FULL-PATH-COMMAND\" > /dev/tcp/127.0.0.1/64444")
maybe your binary (sudo, echo, ...) is not in the PATH env variable :
putenv('PATH', '/bin:/sbin:/usr/bin:/usr/sbin:') // etc...
Have you tried putting a space after -p?
So that should be
shell_exec('sudo -u USERNAME -p PASSWORD echo "RLSET|79-192.168.1.33-0-1" > /dev/tcp/127.0.0.1/64444');
I just tried some commands here on my own machine.
I tried
sudo -u USERNAME -p PASSWORD ls
and it works but when I tried
sudo -u USERNAME -pPASSWORD ls
It give me the usage information.
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
is the code given is executable in Windows system? as it seems to be Linux commands
echo 'create database foo2' | mysql -uroot
mysqldump --skip-triggers -uroot foo | mysql -uroot foo2
EDITED
I am executing the following code but I am not getting what I expect..
<?php
mysql_connect('localhost','root','********');
echo 'create database pranav_chk'. | mysql -u root
mysqldump --skip-triggers -u root pranav_test | mysql -u root pranav_chk;
?>
The code uses three command-line features:
An echo command
Connecting commands via pipes (the | symbol)
The mysql command line client
All three are available and work identically (to the degree they're used here) on Windows and on Linux, so the code is in fact portable.
Edit: The code needs to be run in the shell, you can't just put it into a PHP script. You have to use exec(), something like this:
<?php
exec("echo 'create database foo2' | mysql -uroot");
exec("mysqldump --skip-triggers -uroot foo | mysql -uroot foo2");
?>
However, the mysql_connect() call before won't do you any good, because it's only valid for the PHP script. You'll have to do authentication via the command line directly. Alternatively, you could use it and mysql_create_db() to replace the first exec() line.
It should work yes, assuming mysql and mysqldump are in the windows PATH variable. Although you should add a space between the -u and the root.
3 notes.
First, the database connection in the PHP script is not shared with the outside system command.
Second, you need to supply password to the command line.
Three, use the exec function to run your command.
For example:
<?php
$PASSWORD="YOURPASSWORD";
exec("echo 'create database foo2' | mysql -u root -p $PASSWORD");
?>
Not tested but thats the general idea.
I've got a PHP command line program running. And I want to connect to a mysql shell straight from PHP. I've done this before in Python using os.execvp But I can't get the same thing to work in PHP.
I've tried the following functions:
system
passthru
exec
shell_exec
example:
system('mysql -u root -pxxxx db_name');
But they all seem to wait for mysql to exit and return something. What I really want is for PHP to launch the mysql shell and then exit it self. any ideas?
If you want shell commands to be interactive, use:
system("mysql -uroot -p db_name > `tty`");
That will work for most cases, but will break if you aren't in a terminal.
Give MySQL a script to run that's separate from the PHP script:
system('mysql -u root -pxxxx db_name < script.mysql');
In addition to what acrosman said, you can also use the -e switch to pass SQL from the command line.
$sql = ".....";
system("mysql -u root -pxxxx db_name -e \"$sql\"");
Also, I hope your not actually connecting to the database as root from a PHP application.