简体   繁体   中英

PHP trying to connect to local mysql database instead of specified remote address

I am trying to connect to a remote MySQL database. The MySQL user has access from any IP address. I have root privileges on the Centos 6.8 VM that I am trying to connect from.

Yes I have googled this issue and so far nothing has worked. I am new to PHP so it is possible it is something simple. The path it is referring to '/var/lib/mysql/mysql.sock' does not exist but I feel like it shouldn't be looking for this socket because the DB is not on localhost. I have also double checked that my MySQL user has permission.

My code: index.php

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
  <?php 
   $user="myUsername";
   $password="myPW";
   $database="myDB";
   mysql_connect('xxx.x.xx.xx:xxxx',$user,$password);
   @mysql_select_db($database) or die( mysql_error());
   $query="SELECT * from myTable";
   mysql_query($query);
   mysql_close();
  ?>
 </body>
</html>

The error I get printed on the page:

Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

The error I get in the httpd/logs/error_log:

[Fri Jul 08 06:26:54 2016] [error] [client xxx.xxxxx] PHP Warning: mysql_connect(): Can't connect to MySQL server on 'xxx.x.xx.xx' (13) in /var/www/html/index.php on line 10

I was able to get it working by disabling selinux. This was appropriate for my testing situation but for production it would probably be better to actually configure selinux to allow PHP to connect to MySQL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM