Using mysql via SSH (php) - php

Try to connect to remote mysql via ssh2 and php.
$ssh_server='';
$ssh_port='';
$ssh_user='';
$ssh_password='';
$ssh_connection=ssh2_connect($ssh_server, $ssh_port);
$ssh_auth=ssh2_auth_password($ssh_connection, $ssh_user, $ssh_password);
$ssh_tunnel = ssh2_tunnel($ssh_connection, $ssh_server, $ssh_port);
$db_hostname = 'localhost';
$db_database = '';
$db_username = '';
$db_password = '';
$db_port = '3306';
$connection_mysql = new mysqli($db_hostname, $db_username, $db_password, $db_database, $db_port);
Tunnel is ok, but I see, that mysql use my local mysql DB, not remote via SSH. So, how can i use mysql via SSH?
OS windows with WAMP on it.

create private/public key and setup key authentication with ssh(aka passwordless authentication).
then run this code in your php script
shell_exec("ssh -f -L 3307:localhost:3306 myuser#10.0.0.1 sleep 10 >> logfile");
then create database connection like:
$pdo = new PDO("mysql:host=$servername;dbname=$dbname;port=3307", $username, $password);

$ssh_connection = ssh2_connect($ssh_server, $ssh_port);
ssh2_auth_password($ssh_connection, $ssh_user, $ssh_password);
$query="SET CHARACTER SET utf8; use ${db_database}; ${query};";
$query=str_replace('"', '\'', stripslashes($query));
$ssh_query="ssh -L 3307:${ssh_server}:${ssh_port}; echo \"${query}\" | mysql -u ${db_username} -h ${db_hostname} --password=${db_password}";
$result=ssh2_exec($ssh_connection, $ssh_query);

Related

How can I convert this pdo connection to use socket | PHP

Essentially I need to convert the following pdo connection method to reference my mysql socket connection file - this is for my localhost environment. Connection to the localhost mysql database works fine in mysql workbench, just not sure how to reference the socket instead of the host address in PDO:
Current:
$host = 'localhost:8889';
$user = 'root';
$pass = 'root';
$db = 'mydb';
$dsn = "mysql:host=$host;dbname=$db;charset=$this->charset";
$this->pdoConnect = new \PDO($dsn, $user, $pass, $this->options);
Need to use the following socket file:
/Applications/MAMP/tmp/mysql/mysql.sock

Unable to run php mysqli script from ubuntuserver

I got hosted ubuntu server where I got apache2 and phpmyadmin. I wrote small script to connect to my mysql database. I placed that script in /var/www/html/ . When I enter to 193.219.91.103:5858/init.php I just see blank page. I use php 7. I found that I could check if mysqli enabled with php -m | grep mysql it did showed me mysqli.
<?php
$db_name = "AndroidDatabase";
$mysql_user = "dbreader";
$mysql_pass = "dbreader";
$server_name = "localhost";
$port = 3306
$con = mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name,$port);
if(!$con)
{
echo "Connection error".mysqli_connect_error();
}
else
{
echo "<h3>Database connection success</h3>";
}
?>
EDIT:
Fixed all i missed was extra argument $port

Connect to MySQL using PHP on Ubuntu 14

I am trying to connect to a MySQL database using PHP using the following code:
<?php
$servername = "localhost";
$username = "root";
$password = "pass";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
When using localhost, I get a message
Connection failed: No such file or directory
When using 127.0.0.1 for the host, I get
Connection failed: Connection refused
I have confirmed that I can connect using the command line, and the output is below
mysql -u root -p pass -h localhost
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7282
Server version: 5.5.47-0ubuntu0.14.04.1-log (Ubuntu)
Connecting using TCP
mysql -u root -p pass -h 127.0.0.1
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7318
Server version: 5.5.47-0ubuntu0.14.04.1-log (Ubuntu)
The following is the relevant mysqli section of the php.ini file in /etc/php5/apache2
mysqli.default_port = 3306
mysqli.default_socket = /var/run/mysqld/mysqld.sock
mysqli.default_host = 127.0.0.1
And the settings from /etc/mysql/my.cnf
[mysqld]
#
# * Basic Settings
#
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
.
.
.
bind-address = 127.0.0.1
I have tried using PDO to connect to the database with the same results. Can anyone see a configuration setting that is not correct, or have another idea of what could be wrong?
Edit: I installed phpmyadmin and it connects to MySQL with the same settings.
Try to do this?
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>

How to restore a mysql database in php

I am trying to restore a mysql database using php. I googled and found out this piece of code :-
<?php
$restore_file = "backup.bkp";
$server_name = "localhost";
$username = "root";
$password = "pass";
$database_name = "db";
$cmd = "mysql -h {$server_name} -u {$username} -p{$password} {$database_name}
< $restore_file";
exec($cmd);
?>
I am using a shared server (linux) and the file was restored using phpmyadmin successfully. But I want to use php to restore. The code does not work. Where am I being wrong?
Thanks to #Scoopy, I found that exec() was disabled my by hosting provider. However, I have solved my problem using the following code I found here :-
$db = mysql_connect ( 'localhost', 'username', 'pass' ) or die('not connected');
mysql_select_db( 'test', $db) or die('Not found');
$fp = fopen ( 'backup-file', 'r' );
$fetchData = fread ( $fp, filesize ( 'backup-file') );
$sqlInfo = explode ( ";\n", $fetchData); // explode dump sql as a array data
foreach ($sqlInfo AS $sqlData )
{
mysql_query ( $sqlData ) or die('Query not executed');
}
BUT, THE CODE IS DEPRECATED. So, I made up this code :-
$host = 'localhost';
$user = 'root';
$password = 'root';
$database = 'test';
$conn = new mysqli($host, $user, $password, $database);
$conn->autocommit(FALSE);
$fp = fopen('bkp-file', 'r');
$fetchData = fread($fp, filesize('bkp-file'));
$sqlInfo = explode(";\n", $fetchData); // explode dump sql as a array data
foreach ($sqlInfo AS $sqlData) {
$conn->query($sqlData);
}
$conn->commit();
$conn->close();
This code is perfect to use. It uses transactions to prevent multiple writes.
Generally a shared hosting server will disable access to system functions as it introduces a security risk for them. You can confirm this if your server provider allows you to use the phpinfo command (docs).
Create a page that dumps the PHP info and then look on that page for a setting called disable_functions - it's under the "Core" section. If exec appears there then you will know that exec is a disabled function and you will not be able to use it. Unfortunately one of the drawbacks of using some shared hosting services as it is impossible/very difficult to implement the sorts of things you are talking about.
I don't see why you need PHP, you can do this directly in the terminal.
Backup
mysqldump -u root -p db> backup.bkp
Restore
mysql -u root -p db < backup.bkp

Connecting from VM to MySQL

I'm having trouble connecting to a MySQL database on my host OS (Win 7) from my Oracle VirtualBox running Ubuntu. I wrote this php code in order to connect but I keep receiving an error connecting:
<?php
$dsn = 'mysql:host=192.168.1.9;dbname=finance';
$db_username = 'sdb_user';
$db_password = 'password';
try {
$db = new PDO($dsn, $db_username, $db_password);
}catch (PDOException $e) {
$error_message = $e->getMessage();
include('../errors/database_error.php');
exit();
}
$query = 'SELECT * FROM books';
$books = $db->query($query);
foreach ($books as $book) {
echo $book['title'];
}
?>
I know the code works, because I can get it to run from my Windows 7 host OS using localhost instead of the IP address, and I know that both Linux on VirtualBox and Windows 7 are on the same subnet because I can successfully ping the other from each side.
I'm fairly certain that I opened port 3306 correctly on the windows side also, but I have no idea how to check if that's the problem or not.
What else should I be doing to get the code to connect to the MySQL db?
EDIT: The problem was that the port was not open for remote connections. After I opened the port I got the error message stating I was not allowed to connect to the sql server. From there I created a new user on the Windows MySQL with the username "home" and password "home" and was able to connect to it using:
$ mysql -h 192.168.1.9 -u home -p
However, when I run my file, which now reads:
$dsn = 'mysql:host=192.168.1.9;dbname=finance';
$db_username = 'home';
$db_password = 'home';
I still get the 'database error'
Any suggestions?

Categories