Python's os.execvp equivalent for PHP - php

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.

Related

Php shell_exec unable to run mysql commands?

Referencing this post as well(How to create database using php shell_exec and sql command line)
I've followed the accepted answer in that post, but I am curious as to why the shell_exec is unable to run mysql code. It merely returns NULL.
The user already has sudo access, when i copy the following command line into the console log manually it works.
$cmd = escapeshellcmd('sudo mysql -u root -e "create database somedb"');
$test = shell_exec($cmd);
var_dump($test);
Edit 1:
Updated [username], if root may cause some issues
You will need to use the full path to mysql
Type whereis mysql to find it.
It will most likely be /usr/bin/mysql
Change your command to:
$cmd = escapeshellcmd('sudo /usr/bin/mysql -u [username] -e "create database somedb"');
You may need full path to sudo .. same principles will apply.

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

Change rsync port with php exec

With PHP, I am trying to connect to a remote server and exec an rsync command :
$rsync_cmd = 'rsync -P -azv -e "ssh -p 1234" /dir/file.jpg root#remoteserver2:/dir/file.jpg';
exec('sudo ssh -tt root#remoteserver1 '.$rsync_cmd.' 2>&1', $output);
The remote connection is working fine but then when I am including my rsync command I got a port error "ssh: connect to host remoteserver2 port 22: Connection refused"
The correct port to use is 1234 and this command is working fine on the Terminal (shell) but php "exec" function dont want to take it ("ssh -p 1234"), any idea ?
Did you test it in Terminal by sshing into remoteserver1, and then executing the rsync command, or by executing the entire ssh-to-rsync command on one line? Because I'm pretty sure doing it in two steps will work, but one step won't. This is because when it's done as one step, the command string is parsed by the local shell (including doing quote and escape interpretation and removal) before it's passed over ssh to the remote shell (which then does another pass of quote and escape interpretation and removal). Those double-quotes in "ssh -p 1234" get parsed and removed by the local shell, so they don't have the intended effect of being parsed and applied by the remote shell.
If I'm right about the problem, the solution is pretty simple: escape the double-quotes. That way the local shell will parse and remove the escapes, and pass the double-quotes through unmolested so the remote shell can see and apply them:
$rsync_cmd = 'rsync -P -azv -e \"ssh -p 1234\" /dir/file.jpg root#remoteserver2:/dir/file.jpg';
exec('sudo ssh -tt root#remoteserver1 '.$rsync_cmd.' 2>&1', $output);
The Solution is pretty mush simple try skipping -P flag from the command.
$rsync_cmd = 'rsync -azv -e \"ssh -p 1234\" /dir/file.jpg root#remoteserver2:/dir/file.jpg';
exec('sudo ssh -tt root#remoteserver1 '.$rsync_cmd.' 2>&1', $output);
It will work fine with that -P flag it took default port for the rsync.

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');

is the code given in description is executable in Windows system?

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.

Categories