Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I opened every links google results had thrown to me in regards to this MySQL error. I have been spending hours to get this to work..

I have a web server (apache in centos installed in host B, and want to connect to a MySQL server in host A) both hosts are in the same subnet.

I can access remote mysql using CLI mysql -u root -h <ip> -p

but cannot connect to mysql using PHP function mysql_connect()

I have already disabled SELINUX and system-config-firewall-tui and included below syntax in /etc/my.cnf

  [client]
  port =3306
  socket = /var/lib/mysql/mysql.sock

Some solutions found in the web tells me to type in the setbool associated with selinux, but the thing is, SELINUX is already disabled.

the odd thing is, I can connect thru CLI, but not in php function

also, I have php-mysql in both hosts


Here is my main.php

<?php
mysql_connect('192.168.8.136','root','admin') or die (mysql_error());
echo "Connected to MySQL server";

mysql_select_db("web") or die (mysql_error());
echo "Connected to Database";
?>

here is my /etc/my.cnf

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysqld/log
id-file=/var/run/mysqld/mysqld.pid

selinux status

SELinux status: disabled

netstat -atn tcp 0 0 0.0.0.0:3306 0.0.0.0:* Listen

error received at client where apache is hosted:

[error] [client 127.0.0.1] PHP warning: mysql_connect(); cant connect to mysql server (13) in /var/www/html/main.php on line 2

share|improve this question
1  
13 is an OS error code for permission denied. SELinux is disabled on host B, right ? just checking... –  melsayed Apr 29 '13 at 10:15
    
Are you sure that the IP that you connect from in php and the one from the command line are the same? –  NickW Apr 29 '13 at 10:16
    
yes. i disabled SELINUX in host A and B just to be sure. –  user1933824 Apr 29 '13 at 10:16
    
@NickW yes im very sure of it sir. –  user1933824 Apr 29 '13 at 10:16
    
What does the log on the mysql server say? BTW, just an simple question, does the password you use have any non-alphanumeric characters? –  NickW Apr 29 '13 at 10:19

4 Answers 4

Try this it should work

  <?php
  // Mysql settings
  $user   = "root";
  $password = "admin";
  $database = "IP";
  $host   = "yourhost";
  mysql_connect($host,$user,$password);
  mysql_select_db($database) or die( "Unable to select database");
  ?>
share|improve this answer
    
no, it didnt work. also made server restart after modifying. :( –  user1933824 Apr 29 '13 at 11:24
    
Note that putting everything in variables is not going to make any difference on the call itself. –  Alexis Wilke Aug 9 at 3:37

do you have something like this in my.cnf ?

 bind-address            = 192.168.8.136

and in php do you have safe_mode or apache mod_security enabled?

share|improve this answer

Check your MySQL root user "host" setting. Perhaps try with anyhost, 192.168.8.%, localhost, or "www.domain.tld". Probably a good idea to test with another user rather than using root...

share|improve this answer

Try these steps and see if it changes anything:

  1. Edit /etc/my.cnf and change bind-address = 127.0.0.1 to your Mysql server's ip.
  2. Allow root user to connect from any host

mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;

However, allowing remote root access is highly not recommend, you should create a new user and give it only the permissions it needs on a certain database and use that user.

Update: did not notice the post date at first, but lets hope this helps someone someday.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.