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 172.16.10.5 -p
However, the connection through PHP fails and interestingly, there is no error. More precisely, the command mysqli_connect
returns a non-NULL object whereas mysqli_query
fails. Here is the PHP code:
<?php
error_reporting(E_ALL);
$con=mysqli_connect( "172.16.10.4","user","XXXXXX","myDB" );
var_dump($con);
$result = mysqli_query($con,"SELECT * FROM tblFirst;");
?>
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) "172.16.10.4 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("172.16.10.4 ,"user","XXXXX","myDB", NULL, NULL,NULL, MYSQLI_CLIENT_SSL );
In the file /etc/mysql/mysql.conf.d/mysqld.cnf
, the following lines are commented:
# bind-address = 127.0.0.1
# mysqlx-bind-address = 127.0.0.1
Any pointers?