PDO via SSH2 in PHP? - php

I'm trying to access a remote MySQL Database which is only reachable locally (localhost).
My Code looks like this:
$connection = ssh2_connect('IP to Server', 22);
if (ssh2_auth_password($connection, 'User', 'Password')) {
echo "Authentication Successful!\n";
} else {
die('Authentication Failed...');
}
$tunnel = ssh2_tunnel($connection, '127.0.0.1', 3306);
try {
$this->_connection = new PDO($dsn, $config['login'], $config['password'], $flags);
$this->connected = true;
if (! empty($config['settings'])) {
foreach ($config['settings'] as $key => $value) {
$this->_execute("SET $key=$value");
}
}
} catch (PDOException $e) {
throw new MissingConnectionException(array(
'class' => get_class($this),
'message' => $e->getMessage()
));
}
The script is successful till $tunnel. The PDO connection fails with "Connection refused". I think PDO is trying to connect to MAMP? The $dsn variable looks like "mysql:host=127.0.0.1;port=3306;dbname=mydb".
How can I tell MAMP, that 127.0.0.1 is on the remote server connected through SSH?
Thank you!

As you already noted, your script is attempting to connect to your local MySQL server instead of the remote one. In order for PDO to establish a connection with the remote server, you could establish an SSH tunnel and forward a local port to the server port. As shown here https://brettrawlins.com/blog/mysql-ssh-tunnel.
To run the ssh command in php: shell_exec(ssh code here)
You might want to see: Connect to a MySQL server over SSH in PHP
However as noted in there as well, this method is relatively slow as you will have to create a new tunnel each time you query the database, making your queries take quite a bit longer. I suppose for a development environment it's a valid option since you don't have a static IP, but I don't see this working in production.
If you are trying to do this for the sake of encryption, I would recommend connecting to your database with SSL. However note that SSL for PDO might not be supported by all PHP versions.

Related

How to access mysql server within a server?

I am running mysql on a linux server. I have the linux server IP and mysql runs on localhost on that linux server. Now how do I access mysql running on localhost server remotely?
I can do this by ssh into linux server then running mysql, but how would I do it with in a php script? For example...
<?php
$servername = "localhost";
$username = "username";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername;dbname=myDB", $username,
$password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
(this script only connects to mysql directly which is not possible remotely)
In my script I would need to access linux server then mysql server unless there is a way to access mysql directly (remotely). How is this achievable?
You need change bind address in my.cnf and restart your mysql service.
Official documentation:
https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_bind_address
If the address is 0.0.0.0, the server accepts TCP/IP connections on all server host IPv4 interfaces.
By default all connections restricted localhost.
In order to access your mysql instance from your server; you need to install workbench (or some other mySql explorer) on your development machine.
Create a connection to mysql instance using server ip address as host name and provide username and password to open connection. If every thing goes well you will see your databases in explorer.

PDO can't connect to database

Our school let us sue a small database that I am trying to access through the school network.
Connecting using
mysql -D ddddd -h hhhh -u uuuu
works fine (Note that contrary to multiple questions on this site, hhhh is not localhost). However, using
new PDO('mysql:host=hhhh;dbname=dddd, uuuu, pppp)
does not, and brings up an error:
error: 'Can't connect to local MySQL server through socket '/tmp/mysql.sock''
How do I locate this socket? (NB: In case it is relevant, the schools computers run on FreeBSD and I only have a very limited permissions).
the error:
error: 'Can't connect to local MySQL server through socket '/tmp/mysql.sock''
means that the PDO is trying to connect to MySQL via local socket (linux socket is file).
But my guess is that you want to connect via hostname/ip. Your code Has to be wrong. From the example it looks like you are using php. Try double check everything.
Here is an example from php.net:
<?php
/* Connect to an ODBC database using driver invocation */
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
Use correct IP address to connect.
If u r using localhost then by default socket will be used on Linux so
Use 127.0.0.1 as host instead of localhost.

Connection to the database fails

I'm using PDO's as PHP extension. My project works fine in local server, but when I upload it on a live server... it throws a connection error..
Here goes my connection.php file
<?php
try {
$pdo = new PDO('mysql:host=localhost;dbname=cms','root','');
}
catch(PDOException $e) {
exit('Database Error');
}
?>
Check if the MySQL database ist hosted on the same server as the script is. Otherwise you cannot use localhost.
In addition you may add a password for the root user, which you should have configured already.
$pdo = new PDO('mysql:host=localhost;dbname=cms', 'root', 'yourpwhere');

Php Remote access to Ec2 Mysql server

Im trying to acess an EC2 mysql server from another EC2 instance using PHP.
$con = mysqli_connect('elastic_ip_host','user','pass','database');
if(!$con){
echo mysqli_connect_error(); echo mysql_error();
}
//Also trying PDO
try
{
$dbcon = new PDO('mysql:host=elastic_ip_host;dbname=database','user','pass');
$dbcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo 'Connected!';
}
catch(PDOException $e)
{
echo 'ERROR! '.$e->getMessage();
die();
}
Can't connect to MySQL server on 'elastic_ip_host' (13)ERROR! SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'elastic_ip_host' (13)
Using MySQLWorkbench app in my computer, Im connecting pretty fine using Elastic IP (number). Just PHP is not connecting. Checking phpinfo I see it has pdo and mysqli all ok.
Do you have any idea?
(Also Im using percona and I run that command first install that ask if I will not be able to connect remotly with root. But I created a new user as %. Thats what I am using in MySqlWorkbench program from my computer to access the database fine)
Running:
setsebool -P httpd_can_network_connect=1
As root in ssh fixed my problem :)

Can't access database server with remote connection

I'm a beginner and I'm trying to connect remotely to my MySQL database server, but still don't succeed.
I have a dedicated server and my provider doesn't want to help me.
-CentOS 6 with Parallels Plesk 12 (64-bit)-
I'm accessing my server trough Parallels Plesk. I set up a administrator and database user with "allow remote connection from any host".
I'm able to access my server with FTP and the MySQL database server locally, but not from my computer.
I have this error displaying when trying to connect with php:
Warning: PDO::__construct(): MySQL server has gone away in /Users/X/Sites/connexionBDD.php on line 31
Here is the way I connect:
define("DB_SERVER","mywebsite.com:8443");
define("DB_NAME","databasName");
define("DB_USER","MyUser");
define("DB_PWD","MyPassword");
try {
//line 31
$bdd = new PDO('mysql:host='.DB_SERVER.';dbname='.DB_NAME, DB_USER, DB_PWD);
}
catch (Exception $e) {
die($e->getMessage());
}
echo 'You made it';
For DB_SERVER, if using IP address, do I need to give V4 + port?
The host and port should not be specified in the same variable, split them up and define your DSN like below. Additionally, check the port is the correct one, MySQL by default uses port 3306.
define("DB_SERVER","mywebsite.com");
define("DB_PORT","8443");
define("DB_NAME","databasName");
define("DB_USER","MyUser");
define("DB_PWD","MyPassword");
try {
//line 31
$bdd = new PDO('mysql:host='.DB_SERVER.';port='.DB_PORT.';dbname='.DB_NAME, DB_USER, DB_PWD);
}
catch (Exception $e) {
die($e->getMessage());
}
echo 'You made it';

Categories