I'd like to do something like this:
ssh root#host "mysql database -e 'query to run on table_name; more queries to run;'"
However, I'd like to run an entire .sql file not just few statements. Is it possible ?
If the SQL file is on the remote host, put redirection in the remote command:
ssh root#host "mysql database < filename.sql"
If it's on the local host, redirect the input of SSH:
ssh root#host "mysql database" < filename.sql
Establish a ssh connection followed by the following command:
mysql -u USERNAME -p DATABASE_NAME < scripts.sql
The script should be stored in scripts.sql file located at your working directory
Sure, but you should upload this .sql file to the remove server before...
You can use scp to do that (if you can connect with ssh, you can use scp too)
Related
I am trying to ssh to a remote server to check to see if a specific file exists.
I am able to ssh in the command line but whenever I try to with my script it does not return anything / I have to type "exit" and hit enter to get back to the command line.
Steps:
ssh root#website.com
cd ..
ls ATMEXTRACT
I put all of these commands into ouputs so they look like this:
$output = shell_exec("ssh root#website.com");
$ouput1 = shell_exec("cd ..");
$ouput2 = shell_exec("ls *ATMEXTRACT*");
echo($output2);
I am confused as to why this works directly in the command line but is failing in the script. Any help is much appreciated
Here's what you do interactively:
Run ssh root#website.com in the current shell
Input cd .. in ssh
Input ls *ATMEXTRACT* in ssh
Input exit in ssh, which now exits
Find yourself back in your original shell
Here's what you do in your script:
Run ssh root#website.com in a new shell and exit it
Run cd .. in a second shell and exit it
Run ls *ATMEXTRACT* in a third shell and exit it
You could try to open and interact with an ssh command, but you can also just save yourself the trouble and use ssh's command line feature for specifying the commands to run:
$output = shell_exec("ssh root#website.com 'cd .. && ls *ATMEXTRACT*'");
Be aware that this is likely to fail from a PHP website script because you need to set up an authentication mechanism. This true even if ssh root#website.com connects without a password when you manually log in to the web server and try it.
I would recommend you to use ssh2 module of PHP. This will help you to connect any remote server which is reachable through appropriate SSH PORT.
You will need to check, if few modules like OpenSSL and ssh2 are installed on your host server.
if not please check this https://ehikioya.com/install-ssh2-php/ and install above modules.
once these modules are installed and enabled.
follow this code.
$server="website.com";
$server_pwd="Password";
//creating connection using server credentials
$connection = ssh2_connect($server, 22);
//authenticating username and password
if(ssh2_auth_password($connection, 'root', $server_pwd)){
echo "connected"
}else{
echo "could not connect to server";
}
ssh2_exec($connection, "ls /FULL_PATH/ATMEXTRACT"); //run your command here
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.
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
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
I want to back up and roll back the back up on a database, and I'm trying to see what's wrong:
'mysqldump' is not recognized as an internal or external command, operable program or batch file.
The above is what I get in the file that is exported. And this is what I run:
$database = 'logindb';
$backup = $location.'/'.$database.'_backup_'.date('Y').'_'.date('m').'_'.date('d').'.sql';
exec("mysqldump --opt -h localhost -u root logindb > $backup 2>&1", $output);
print_r($output);
As $location being my backup folder. So why do I get that error ?
And as additional info, I'm testing on localhost with XAAMP, the Apache server is running and MySQL is running as wel.
Because mysqldump is not in httpd user path.
I asume you work in Linux (or UNIX).
To know witch directory contains mysqldump you can execute whereis mysqldump.
Then you have two options:
Check for mysqldump directory is in PATH environment variable of httpd user (usualy www-data user for apache server). And include it in PATH is it not present.
Also you can execute mysqldump with full path:
Like this:
exec("/path_to_mysql_bins/mysqldump ...
The error is not a mysqldump error, it's a Windows error that says that it does not understand what mysqldump relates to. To fix that you can either include the path to mysqldump executable to the environment variables or run the exec command with a full path to the mysqldump executable (which you can find within XAMPP mysql folder).