I have Ubuntu 21.04 with the following setup: MySQL 8.0.25 Apache2: 2.4.46 PHP: 7.4

My PHP scripts are working fine when I try to connect to the MySQL server as a ‘localhost’ (i.e. everything on the same servers).

Now, with a slight change in the setup, I am running my PHP scripts (and apache) on server A, with the MySQL server running on B. Both the systems are Ubuntu 21.04, running on the same intranet network (172.16.10.XX series). From the command-line of B, I can run the following command successfully:

mysql --verbose --ssl-mode=DISABLE -u user -h -p

However, the connection through PHP fails and interestingly, there is no error. More precisely, the command

returns a non-NULL object whereas
fails. Here is the PHP code:

con=mysqli_connect( "","user","XXXXXX","myDB" ); // Check connection if(con == false)
        die( 'Could not connect'.mysqli_error() );
var_dump(con); if (mysqli_connect_errno()) {   echo "Failed to connect to MySQL: " . mysqli_connect_error(); }result = mysqli_query(con,"SELECT * FROM tblFirst;"); if (!result)
    echo "query failed...".mysqli_error();

Here is the output:

object(mysqli)#1 (19) { ["affected_rows"]=> int(0) ["client_info"]=> string(79) "mysqlnd 5.0.12-dev - 20150407 - Id: 3591daad22de08524295e1bd073aceeff11e6579" ["client_version"]=> int(50012) ["connect_errno"]=> int(0) ["connect_error"]=> NULL ["errno"]=> int(0) ["error"]=> string(0) "" ["error_list"]=> array(0) { } ["field_count"]=> int(0) ["host_info"]=> string(23) " via TCP/IP" ["info"]=> NULL ["insert_id"]=> int(0) ["server_info"]=> string(23) "8.0.25-0ubuntu0.21.04.1" ["server_version"]=> int(80025) ["stat"]=> string(135) "Uptime: 13713 Threads: 2 Questions: 29 Slow queries: 0 Opens: 241 Flush tables: 3 Open tables: 160 Queries per second avg: 0.002" ["sqlstate"]=> string(5) "00000" ["protocol_version"]=> int(10) ["thread_id"]=> int(16) ["warning_count"]=> int(0) } query failed...

I also had a look at the other question with the same subject (MySQL Database accepting remote connections, but not from PHP), but does not give many pointers.

I also experimented with the following commands (and playing with some other options!):

connection = mysqli_init();connection->options(MYSQLI_OPT_SSL_VERIFY_SERVER_CERT,false);
connection->ssl_set( "/etc/ssl/private/ssl-cert-snakeoil.key", "/etc/ssl/certs/ssl-cert-snakeoil.pem", NULL, NULL, NULL );connection->real_connect(" ,"user","XXXXX","myDB", NULL, NULL,NULL, MYSQLI_CLIENT_SSL );

In the file

, the following lines are commented:

# bind-address          =
# mysqlx-bind-address   =

Any pointers?

Anonymous Asked question May 14, 2021