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

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.

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

sql oracle introduction after every command

I'm new to command line sql. But after a lot of work i finally understodo how to log into the xampp server using:
mysql -u myusername -p mypassword mydbname;
This is using the shell in xampp.
But now that i want to run a few different commands, (source was in my mind), im having trouble because after every command nothing happens except the introductory paragraph of oracle mysql.
Please help. I want to add a table to my database using a .sql file. This doesnt happen in phpmyadmin because the file size is too big.
You can use:
mysql -u myusername -p mypassword mydbname <mysqlfile.sql;
This will pipe the commands in your SQL file to the mysql shell.

Execute mysqldump to back up database in sql format

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

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.

Python's os.execvp equivalent for 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.

Categories