Forums

Can't connect the database with an external php script

My account is paid. I created a connection from an external server by DBBl.php file the class (tested with other db in localhost and works fine):

class DBBl  {

        private $host = 'username.mysql.eu.pythonanywhere-services.com';
        private $user  = 'username';
        private $password = 'pw';
        private $database = 'mydb';
        private $conn;
....

I wanted to connect to the mydb database on the PythonAntwhere server but error occurred: Warning: mysqli_connect(): (HY000/2002)

Some help? Thanks ! Giuse

[edit by admin: formatting]

You'll need to use an SSH tunnel to connect to the MySQL database from outside PythonAnywhere -- see this help page for the details. We don't have an example there for PHP specifically, but if you google for "PHP ssh tunnel mysql" you'll be likely to find appropriate modules for it -- and if not, you can check out the "Manual SSH tunneling" section at the end for details on how to do it without specific support for the language you're using.

Has anyone ever tried to make this type of script? could you help me solve it? Thank you very much! Giuseppe

//*SSH connection information*/
$sshHost = 'ssh.eu.pythonanywhere.com';
$sshUser = 'myID login';
$sshPassword = 'myPwd Login';

/* Remote MySQL server information/*
$dbHost = 'lovalhost'; // Because the SSH tunnel will forward the connection locally
$dbPort = 3306; // Default MySQL port
$dbuser = 'my user mysql db';



<?php



$command = "ssh -L {$dbPort}:{$dbuser}{$dbHost}:{$dbPort} {$sshUser}@{$sshHost} -N -f -L";
shell_exec($command);

$dbHost =     'usernamemysql.eu.pythonanywhere-services.com';
$dbUser  =    'user Db';
$dbPassword = 'PW DB';
$dbName = 'DB name';

$mysqli = new mysqli($dbHost, $dbUser, $dbPassword, $dbName);


?>

Error:"Warning: mysqli::__construct(): (HY000/2002): Unable to establish connection. Incorrect response from the connected party after the time interval or no response from the connected host.."

[edit by admin: formatting]

I haven't done anything like that in PHP, but one thing that looks wrong to me in your code there is that you're changing the value of dbHost, so your new mysqli is using the remote hostname. I would expect something more like this:

<?php

//*SSH connection information*/
$sshHost = 'ssh.eu.pythonanywhere.com';
$sshUser = 'myID login';
$sshPassword = 'myPwd Login';

/* Remote MySQL server information/*
$dbHost = 'usernamemysql.eu.pythonanywhere-services.com'; 
$dbPort = 3306; // Default MySQL port
$dbuser = 'my user mysql';
$dbPassword = 'PW DB';
$dbName = 'DB name';

$command = "ssh -L {$dbPort}:{$dbHost}:{$dbPort} {$sshUser}@{$sshHost} -N -f -L";
shell_exec($command);

$mysqli = new mysqli("localhost", $dbUser, $dbPassword, $dbName);

?>

Access denied for user ''user'@'localhost' (using password: YES) in...

Already tried. It does not work. Thank you for the attempt. It's strange that no one has ever attempted to access externally via PHP script. I'm sorry that the online guide doesn't dedicate even one line to this topic. PHP remains one of the most used languages ​​and greatly enhances the use of Python on the web. Thank you very much.

Is your ssh tunnel still present when you try to connect to MySQL?