Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't connect to remote Mysql server from php

  • Client System: CentOS Linux release 7.0.1406 (Core)
  • Client Ip: 222.222.222.222
  • http://test.com looks onto ip 222.222.222.222

  • Server system: Gentoo Base System release 1.12.13

  • Server ip: 111.111.111.111

Script for connect:

$connection = new \mysqli('111.111.111.111', 'user', 'password', 'db');
if (mysqli_connect_error()) {
    echo "Error: {$connection->connect_error}"; 
}
else {
    echo "Server Info: {$connection->server_info}";
}

When I'm trying to open in browser "http://test.com/test.php", I get:

Error: Can't connect to MySQL server on '111.111.111.111' (13)

... but when I'm starting this script from command line on the server 222.222.222.222, I have another result:

/usr/bin/php /var/www/reporter/test.php
Server Info: 5.5.14-enterprise-commercial-advanced-log

Directly connection from command line also correct:

mysql -h 111.111.111.111 -u user -p -P 3306
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 25236357
Server version: 5.5.14-enterprise-commercial-advanced-log MySQL Enterprise Server - Advanced Edition (Commercial)

Copyright (c) 2000, 2014, Oracle, Monty Program Ab and others.

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




Answer


Problem with client's environment - Selinux in Centos. Command fo fix it:

setsebool -P httpd_can_network_connect=1
like image 234
Anton Qcl Avatar asked Mar 17 '23 19:03

Anton Qcl


1 Answers

Problem with client's environment - Selinux in Centos. Command fo fix it:

setsebool -P httpd_can_network_connect=1
like image 180
Anton Qcl Avatar answered Mar 20 '23 10:03

Anton Qcl