skip to Main Content

I have installed Ubuntu 18.04.3 LTS on my virtual box:

enter image description here

I have also created an ssh tunnel the following way to connect to my remote db:

admin@admin-VirtualBox:~$ ssh -v -v -v [email protected] -CNL 13306:localhost:3306
OpenSSH_7.6p1 Ubuntu-4ubuntu0.3, OpenSSL 1.0.2n  7 Dec 2017
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug2: resolving "xx.xx.xxx.xxx" port 22
debug2: ssh_connect_direct: needpriv 0
debug1: Connecting to xx.xx.xxx.xxx [xx.xx.xxx.xxx] port 22.
debug1: Connection established.
debug1: identity file /home/admin/.ssh/id_rsa type 0
debug1: key_load_public: No such file or directory
debug1: identity file /home/admin/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/admin/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/admin/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/admin/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/admin/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/admin/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/admin/.ssh/id_ed25519-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.3
debug1: Remote protocol version 2.0, remote software version OpenSSH_8.2p1 Ubuntu-4ubuntu0.1
debug1: match: OpenSSH_8.2p1 Ubuntu-4ubuntu0.1 pat OpenSSH* compat 0x04000000
debug2: fd 3 setting O_NONBLOCK
debug1: Authenticating to xx.xx.xxx.xxx:22 as 'root'
debug3: hostkeys_foreach: reading file "/home/admin/.ssh/known_hosts"
debug3: record_hostkey: found key type ECDSA in file /home/admin/.ssh/known_hosts:1
debug3: load_hostkeys: loaded 1 keys from xx.xx.xxx.xxx
debug3: order_hostkeyalgs: prefer hostkeyalgs: [email protected],[email protected],[email protected],ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
debug3: send packet: type 20
debug1: SSH2_MSG_KEXINIT sent
debug3: receive packet: type 20
debug1: SSH2_MSG_KEXINIT received
debug2: local client KEXINIT proposal
debug2: KEX algorithms: curve25519-sha256,[email protected],ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha256,diffie-hellman-group14-sha1,ext-info-c
debug2: host key algorithms: [email protected],[email protected],[email protected],ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,[email protected],[email protected],ssh-ed25519,rsa-sha2-512,rsa-sha2-256,ssh-rsa
debug2: ciphers ctos: [email protected],aes128-ctr,aes192-ctr,aes256-ctr,[email protected],[email protected]
debug2: ciphers stoc: [email protected],aes128-ctr,aes192-ctr,aes256-ctr,[email protected],[email protected]
debug2: MACs ctos: [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: MACs stoc: [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: compression ctos: [email protected],zlib,none
debug2: compression stoc: [email protected],zlib,none
debug2: languages ctos: 
debug2: languages stoc: 
debug2: first_kex_follows 0 
debug2: reserved 0 
debug2: peer server KEXINIT proposal
debug2: KEX algorithms: curve25519-sha256,[email protected],ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha256
debug2: host key algorithms: rsa-sha2-512,rsa-sha2-256,ssh-rsa,ecdsa-sha2-nistp256,ssh-ed25519
debug2: ciphers ctos: [email protected],aes128-ctr,aes192-ctr,aes256-ctr,[email protected],[email protected]
debug2: ciphers stoc: [email protected],aes128-ctr,aes192-ctr,aes256-ctr,[email protected],[email protected]
debug2: MACs ctos: [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: MACs stoc: [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: compression ctos: none,[email protected]
debug2: compression stoc: none,[email protected]
debug2: languages ctos: 
debug2: languages stoc: 
debug2: first_kex_follows 0 
debug2: reserved 0 
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: [email protected] MAC: <implicit> compression: [email protected]
debug1: kex: client->server cipher: [email protected] MAC: <implicit> compression: [email protected]
debug3: send packet: type 30
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug3: receive packet: type 31
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:aaaaaaaaaaaaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaaaaaaaaaaaaaa
debug3: hostkeys_foreach: reading file "/home/admin/.ssh/known_hosts"
debug3: record_hostkey: found key type ECDSA in file /home/admin/.ssh/known_hosts:1
debug3: load_hostkeys: loaded 1 keys from xx.xx.xxx.xxx
debug1: Host 'xx.xx.xxx.xxx' is known and matches the ECDSA host key.
debug1: Found key in /home/admin/.ssh/known_hosts:1
debug3: send packet: type 21
debug2: set_newkeys: mode 1
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug3: receive packet: type 21
debug1: SSH2_MSG_NEWKEYS received
debug2: set_newkeys: mode 0
debug1: rekey after 134217728 blocks
debug2: key: /home/admin/.ssh/id_rsa (0x55ba671d36b0), agent
debug2: key: /home/admin/.ssh/id_dsa ((nil))
debug2: key: /home/admin/.ssh/id_ecdsa ((nil))
debug2: key: /home/admin/.ssh/id_ed25519 ((nil))
debug3: send packet: type 5
debug3: receive packet: type 7
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,[email protected],ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,[email protected]>
debug3: receive packet: type 6
debug2: service_accept: ssh-userauth
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug3: send packet: type 50
debug3: receive packet: type 51
debug1: Authentications that can continue: publickey,password
debug3: start over, passed a different list publickey,password
debug3: preferred gssapi-keyex,gssapi-with-mic,publickey,keyboard-interactive,password
debug3: authmethod_lookup publickey
debug3: remaining preferred: keyboard-interactive,password
debug3: authmethod_is_enabled publickey
debug1: Next authentication method: publickey
debug1: Offering public key: RSA SHA256:aaaaaaaaaaaaaaaaaaaaaaaaaaa /home/admin/.ssh/id_rsa
debug3: send_pubkey_test
debug3: send packet: type 50
debug2: we sent a publickey packet, wait for reply
debug3: receive packet: type 60
debug1: Server accepts key: pkalg rsa-sha2-512 blen 279
debug2: input_userauth_pk_ok: fp SHA256:aaaaaaaaaaaaaaaaaaaaaaaaaaa
debug3: sign_and_send_pubkey: RSA SHA256:aaaaaaaaaaaaaaaaaaaaaaaaaaa
debug3: send packet: type 50
debug3: receive packet: type 52
debug1: Enabling compression at level 6.
debug1: Authentication succeeded (publickey).
Authenticated to xx.xx.xxx.xxx ([xx.xx.xxx.xxx]:22).
debug1: Local connections to LOCALHOST:13306 forwarded to remote address localhost:3306
debug3: channel_setup_fwd_listener_tcpip: type 2 wildcard 0 addr NULL
debug3: sock_set_v6only: set socket 5 IPV6_V6ONLY
debug1: Local forwarding listening on ::1 port 13306.
debug2: fd 5 setting O_NONBLOCK
debug3: fd 5 is O_NONBLOCK
debug1: channel 0: new [port listener]
debug1: Local forwarding listening on 127.0.0.1 port 13306.
debug2: fd 6 setting O_NONBLOCK
debug3: fd 6 is O_NONBLOCK
debug1: channel 1: new [port listener]
debug2: fd 3 setting TCP_NODELAY
debug3: ssh_packet_set_tos: set IP_TOS 0x10
debug1: Requesting [email protected]
debug3: send packet: type 80
debug1: Entering interactive session.
debug1: pledge: network
debug3: receive packet: type 80
debug1: client_input_global_request: rtype [email protected] want_reply 0
debug3: receive packet: type 4
debug1: Remote: /root/.ssh/authorized_keys:1: key options: agent-forwarding port-forwarding pty user-rc x11-forwarding
debug3: receive packet: type 4
debug1: Remote: /root/.ssh/authorized_keys:1: key options: agent-forwarding port-forwarding pty user-rc x11-forwarding
debug1: Connection to port 13306 forwarding to localhost port 3306 requested.
debug2: fd 7 setting TCP_NODELAY
debug2: fd 7 setting O_NONBLOCK
debug3: fd 7 is O_NONBLOCK
debug1: channel 2: new [direct-tcpip]
debug3: send packet: type 90
debug3: receive packet: type 91
debug2: channel 2: open confirm rwindow 2097152 rmax 32768
debug1: Connection to port 13306 forwarding to localhost port 3306 requested.
debug2: fd 8 setting TCP_NODELAY
debug2: fd 8 setting O_NONBLOCK
debug3: fd 8 is O_NONBLOCK
debug1: channel 3: new [direct-tcpip]
debug3: send packet: type 90
debug3: receive packet: type 91
debug2: channel 3: open confirm rwindow 2097152 rmax 32768
debug3: receive packet: type 96
debug2: channel 3: rcvd eof
debug2: channel 3: output open -> drain
debug2: channel 3: obuf empty
debug2: channel 3: close_write
debug2: channel 3: output drain -> closed
debug2: channel 3: read<=0 rfd 8 len 0
debug2: channel 3: read failed
debug2: channel 3: close_read
debug2: channel 3: input open -> drain
debug2: channel 3: ibuf empty
debug2: channel 3: send eof
debug3: send packet: type 96
debug2: channel 3: input drain -> closed
debug2: channel 3: send close
debug3: send packet: type 97
debug3: channel 3: will not send data after close
debug3: receive packet: type 97
debug2: channel 3: rcvd close
debug3: channel 3: will not send data after close
debug2: channel 3: is dead
debug2: channel 3: garbage collecting
debug1: channel 3: free: direct-tcpip: listening port 13306 for localhost port 3306, connect from 127.0.0.1 port 59650 to 127.0.0.1 port 13306, nchannels 4
debug3: channel 3: status: The following connections are open:
  #2 direct-tcpip: listening port 13306 for localhost port 3306, connect from 127.0.0.1 port 59644 to 127.0.0.1 port 13306 (t4 r0 i0/0 o0/0 fd 7/7 cc -1)
  #3 direct-tcpip: listening port 13306 for localhost port 3306, connect from 127.0.0.1 port 59650 to 127.0.0.1 port 13306 (t4 r1 i3/0 o3/0 fd 8/8 cc -1)

Locally I am using the following mysql-version:

admin@admin-VirtualBox:~$ mysql --version
mysql  Ver 14.14 Distrib 5.7.33, for Linux (x86_64) using  EditLine wrapper

When trying to connect via my local mysql-client it works fine:

admin@admin-VirtualBox:~$ mysql --host=127.0.0.1 --port=13306 mysql -u root -p
Enter password: 
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 136
Server version: 8.0.23-0ubuntu0.20.04.1 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql> show databases;
+-----------------------------+
| Database                    |
+-----------------------------+
| information_schema          |
| mysql                       |
| performance_schema          |
| phpmyadmin                  |
| sys                         |
| test_db                     |
+-----------------------------+
7 rows in set (0.03 sec)

mysql> 

Also when using dbeaver 7.3.5 works out fine:

enter image description here

However, when trying to connect via the following php script I get an error in the log of my mysql-db /etc/mysql/mysql.conf.d

I am using locally PHP 7.4.1 and on the remote host PHP 7.4.3.

<?php
$mysqli = new mysqli("127.0.0.1", "root", "myPassword", "test_db", 13306);

/* check connection */
if ($mysqli->connect_errno) {
    mysqli_debug("/home/marcus/Desktop/client.trace");
    printf("Connect failed: %sn", $mysqli->connect_error);
    exit();
}

/* check if server is alive */
if ($mysqli->ping()) {
    mysqli_debug("/home/marcus/Desktop/client.trace");
    printf ("Our connection is ok!n");
} else {
    printf ("Error: %sn", $mysqli->error);
}

/* close connection */
$mysqli->close();
?>

The error that I get remotely is:

2021-03-08T05:26:49.885317Z 132 [Note] [MY-010914] [Server] Got timeout reading communication packets

I also tried changing $mysqli = new mysqli("localhost", "root", "myPassword", "test_db", 13306); but this does not work as it seems to me that mysqli confuses this with my localhost on my local machine. I only get the above error when using 127.0.0.1.

Any suggestions what I am doing wrong? Why can I connect via my local mysql client and also with dbeaver, BUT not via php?

Is there a way to see how mysqli() builds the connection?

I think it might be an issue with either my virtualbox (less likely as dbever etc is working) or my local php settings.

The ssh connection is working on another computer. (that is not my dev computer)

I really appreciate your replies!

UPDATE

I tried it now using python and it works fine:

import mysql.connector

database = mysql.connector.connect(
    host='127.0.0.1',
    user='root',
    password='myPassword',
    database='test_db',
    autocommit=True,
    port=13306
)
cursor = database.cursor()

operation = 'SELECT * FROM companies limit 10;'
for result in cursor.execute(operation, multi=True):
  if result.with_rows:
    print("Rows produced by statement '{}':".format(
      result.statement))
    print(result.fetchall())
  else:
    print("Number of rows affected by statement '{}': {}".format(
      result.statement, result.rowcount))

My result:

admin@admin-VirtualBox:~$ python testMysql.py 
Rows produced by statement 'SELECT * FROM companies limit 10':
[(1, u'LDP', u'Cohen &amp; Steers Limited Duration Preferred and Inco', datetime.datetime(2020, 10, 6, 21, 0, 5), datetime.datetime(2021, 3, 7, 12, 2, 32)), (2, u'WIW', u'Western Asset Inflation-Linked Opportunities &amp; Inc', datetime.datetime(2020, 10, 6, 21, 0, 5), datetime.datetime(2021, 3, 9, 12, 0, 47)), (3, u'ATOAX', u'Aberdeen Ultra Short Municipal Income Fund Class A', datetime.datetime(2020, 10, 6, 21, 0, 5), datetime.datetime(2020, 10, 6, 21, 0, 5)), (4, u'BA', u'The Boeing Company', datetime.datetime(2020, 10, 6, 21, 0, 5), datetime.datetime(2021, 3, 11, 13, 23, 13)), (5, u'WMT', u'Wal-Mart Stores Inc.', datetime.datetime(2020, 10, 6, 21, 0, 5), datetime.datetime(2021, 3, 11, 14, 48, 8)), (6, u'AAPL', u'Apple Inc. (NASDAQ)', datetime.datetime(2020, 10, 6, 21, 0, 5), datetime.datetime(2021, 3, 11, 14, 48, 49)), (7, u'NVDA', u'NVIDIA Corporation', datetime.datetime(2020, 10, 6, 21, 0, 5), datetime.datetime(2021, 3, 11, 13, 24, 28)), (8, u'NFLX', u'Netflix, Inc.', datetime.datetime(2020, 10, 6, 21, 0, 5), datetime.datetime(2021, 3, 11, 13, 35, 19)), (9, u'BYND', u'Beyond Meat, Inc.', datetime.datetime(2020, 10, 6, 21, 0, 5), datetime.datetime(2021, 2, 23, 0, 4, 2)), (10, u'TSLA', u'Tesla, Inc.', datetime.datetime(2020, 10, 6, 21, 0, 5), datetime.datetime(2021, 3, 11, 13, 54, 40))]

4

Answers


  1. Not sure what’s the root cause of the issue, but one way that has been reported to work well is the following:

    <?php
    shell_exec(“ssh -f -L 13306:127.0.0.1:3306 [email protected] sleep 60 >> logfile”);
    $db = mysqli_connect('127.0.0.1', 'root', 'myPassword', 'test_db', 13306);
    

    You will need to make sure that you have the ssh extension installed in PHP.

    In a nutshell, use the PHP script to create the SSH port forwarding before connecting to the DB.

    Read more about it at https://blog.rjmetrics.com/2009/01/06/php-mysql-and-ssh-tunneling-port-forwarding/

    Login or Signup to reply.
  2. for me is sounds like php is running inside another machine, maybe a docker on the same computer or something else and "localhost" is not the same as the one on your computer.

    Login or Signup to reply.
  3. I can think of two reasons:

    1. Firewall, but it doesnt make any sense cause python is good which shouldn’t be the case
    2. Please note localhost $mysqli = new mysqli("localhost", "root", "myPassword", "test_db", 13306); You need to go into mysql and move into the table "mysql" and provide perimisson on user host to be both accessible via whatever ip’s you would like.

    See if 2 works and it should be fine.

    Login or Signup to reply.
  4. localhost and 127.0.0.1 both are the same because both are loopback addresses, Which will point to the host itself.

    One of the more common problems that users run into when trying to set up a remote MySQL database is that their MySQL instance is only configured to listen for local connections. This is MySQL’s default setting, but it won’t work for a remote database setup since MySQL must be able to listen for an external IP address where the server can be reached.

    To enable this, open up your mysqld.cnf file:

    sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
    

    Find bind-address directive.

    # Instead of skip-networking the default is now to listen only on
    # localhost which is more compatible and is not less secure.
    bind-address            = 127.0.0.1
    

    Change bind-address directive value to 0.0.0.0

    bind-address            = 0.0.0.0
    

    After changing this line, save and close the file.

    Then restart the MySQL service to put the changes you made to mysqld.cnf into effect:

    sudo systemctl restart mysql
    

    Now, you can use mysql connection from a remote client. Please make sure, You’ll use mysql-server’s Network IP. ( not loopback address)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search