Good day. The following snag occurred:

I use windows with WAMP installed. The hosting service provider allows you to connect to the database only via SSH2 (it is correct), but you cannot connect to the database. Do the following

$ssh_server='93.125.xx.xx'; //адрес сервера $ssh_port='22'; //порт сервера $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); 

Here everything goes with a bang. Then I check and try to connect to the database. But I did not understand how using the tunnel created to connect

 if ($ssh_connection and $ssh_auth and $ssh_tunnel) { $db_hostname = '127.0.0.1'; $db_database = ''; //имя БД mysql $db_username = ''; //имя пользоватля БД $db_password = ''; //пароль пользователя БД $db_port = '3306'; $connection_mysql = new mysqli($db_hostname, $db_username, $db_password, $db_database, $db_port); if ($connection_mysql->connect_error) exit($connection_mysql->connect_error); mysqli_set_charset($connection_mysql, "utf8"); } 

It gives a standard connection error

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

But apparently it just connects to my local database, but I don’t have one on my machine.

If you try to substitute ssh variables as $db_hostname , then nothing will come out of the way. For variables $ ssh_connection, $ ssh_auth, $ ssh_tunnel produces, respectively:

mysqli :: __ construct () expects parameter 1 to be string, resource given

mysqli :: __ construct (): php_network_getaddresses: getaddrinfo failed

mysqli :: __ construct () expects parameter 1 to be string, resource given

var_dump three ssh variables

 var_dump($ssh_connection); var_dump($ssh_auth); var_dump($ssh_tunnel); 

gives the following

resource (2) of type (SSH2 Session)

bool (true)

resource (3) of type (stream)

If you specify the server address itself as $db_hostname , as when connecting via ssh ($ ssh_server = '93 .125.xx.xx '; and also $db_hostname='93.125.xx.xx'; ), there will be no sense (mysqli :: __ construct (): (HY000 / 2002): and a bunch of diamonds with questions - something with an encoding)

How to be, tell me, please.

    2 answers 2

    If I understand correctly, it should be as follows:

    1. Create a tunnel.
    2. Make port forwarding from remote to local.
    3. Requests to do to the local port.

    Simple option (for Linux):

     <?php shell_exec(“ssh -f -L 3307:127.0.0.1:3306 user@remote_server sleep 60 >> logfile”); $db = mysqli_connect('127.0.0.1', 'sqluser', 'sqlpassword', 'database', 3307); 

    Taken from here https://blog.rjmetrics.com/2009/01/06/php-mysql-and-ssh-tunneling-port-forwarding/

    Not sure how it will work with ssh2 And if you believe https://stackoverflow.com/questions/309615/connect-to-a-mysql-server-over-ssh-in-php, then neither will.

    Well, the project page states that the library is not stable ...

    As an option for the windows user. Create a tunnel in putty.

    http://www.akadia.com/services/ssh_putty.html

    Tie to local port 3037

    And your code will be simple:

     <?php $db = mysqli_connect('127.0.0.1', 'sqluser', 'sqlpassword', 'database', 3307); 

    putty has an interface via the command line so that you can use shell_exec .

    I have one question, why do you need it? Why from the local computer to the database to connect?

    • Thanks for the answer. Tomorrow I will try. The fact is that we have a database running on a local machine with a user interface through a browser over a local network. There is also a website where you can check the readiness of orders online. Therefore, it is necessary to unload readiness status from the local database to the database on the site. This is how it is. Now everything works (I use some kind of free sooo buggy base for this without problems with SSH). But since we have normal hosting, I want to use its base. However, it is possible with it only on SSH. Like this. - n.osennij
    • Ubuntu is on that machine. I'll be right there and try. I wanted to debug everything and test everything at home, but it’s obvious that you need to do everything on Linux right away. - n.osennij
    • @ n.osennij sleep 60 -> the tunnel will live a minute. If you need more, increase the value. - E_p

    try $ connection_mysql = new mysqli ($ db_hostname, $ db_username, $ db_password, $ db_database, $ db_port, $ ssh_tunnel);

    • Error mysqli :: __ construct () expects parameter 6 to be string, resource given - n.osennij