"Can't connect to MySQL server" through php/apache but command line php works.

I am trying to connect to a remote myql database server which is running centos 4.4 with mysql 5.0.27.

The website server is using apache (httpd 2.2.3), PHP 5.1.6 and Mysql Client API version 5.0.22.

Using a basic php script that does notthing but use mysql_connect to connect to the database server I get the following behaviour.

When run by accessing the php file through apache I get the error
"2003 : Can't connect to MySQL server on 'xx.xx.xx.xx' (13)"
where 2003 is the mysql_errno and the rest is mysql_error (I replaced my actal db server ip with xx for obvious reasons).

But If I run the same file from the command line "php connect.php" it connects to the db server no problem. As well if I run "mysql -h xx.xx.xx.xx -u ASDF -p" I can connect no problem as well. And just to be sure I have turned off iptables while trying to debug this problem.
I have setup a mysql db on the webserver and had no problem connecting to localhost... It is only a problem when trying to access a remote db server.

I have run the same php file from other servers and they have no problems connecting when run through apache (they have different centos versions but all 4 and above).

I have looked everywhere I can think to try and find some firewall like setting that is only active when running php/mysql through httpd but have come up empty. I hope there is just some simple httpd.conf setting that I have overlooked but at this point I am desperate to find it...

Thanks for reading.
Jamie
terabit_caAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

richswyattCommented:
I don't think we're having a firewall issue.

Can you show the code for how you're trying to do the mysql_connect...

I ran across a post the other day where the failure to connect was simply a semantics problem. I'd like to see what you have (of course leaving out vital information such as user/passwords)... :-)
0
terabit_caAuthor Commented:
if(!($link = mysql_connect($serverName, $serverUsr, $serverPass, $serverDb, $serverPort))){
      echo mysql_errno()." : ";
      echo mysql_error()."<br>\n";
}

mysql_close($link);


But running it on other servers there is no problem so it can't be semantics...
0
richswyattCommented:
Alright - fair enough. I didn't read that part. :-)

So - if I read right - you are able to connect to the same host, same DB, from other servers using the same script?

It is just from this one particular server that you cannot connect to this particular host/db, right?
0
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

richswyattCommented:
Also - I misspoke about a firewall issue - but you did say that there are NO firewalls installed on this box or there are?
0
terabit_caAuthor Commented:
Yes that is correct. WIth this "webserver" I cannot connect to any remote db (through apache). But when the php file is run from shell it connects without a problem.
0
terabit_caAuthor Commented:
I have shut down iptable while trying to figure this out.
0
richswyattCommented:
Did you compile Apache? If so - did you use --with-mpm=worker ?

Check your Apache configuration. If --with-mpm=worker was used in the compile - you will never be able to connect to remote MySQL servers. In that case Apache needs to be re-compiled without that flag.
0
terabit_caAuthor Commented:
'./configure' '--build=i686-redhat-linux-gnu' '--host=i686-redhat-linux-gnu' '--target=i386-redhat-linux-gnu' '--program-prefix=' '--prefix=/usr' '--exec-prefix=/usr' '--bindir=/usr/bin' '--sbindir=/usr/sbin' '--sysconfdir=/etc' '--datadir=/usr/share' '--includedir=/usr/include' '--libdir=/usr/lib' '--libexecdir=/usr/libexec' '--localstatedir=/var' '--sharedstatedir=/usr/com' '--mandir=/usr/share/man' '--infodir=/usr/share/info' '--cache-file=../config.cache' '--with-libdir=lib' '--with-config-file-path=/etc' '--with-config-file-scan-dir=/etc/php.d' '--disable-debug' '--with-pic' '--disable-rpath' '--without-pear' '--with-bz2' '--with-curl' '--with-exec-dir=/usr/bin' '--with-freetype-dir=/usr' '--with-png-dir=/usr' '--enable-gd-native-ttf' '--without-gdbm' '--with-gettext' '--with-gmp' '--with-iconv' '--with-jpeg-dir=/usr' '--with-openssl' '--with-png' '--with-pspell' '--with-expat-dir=/usr' '--with-pcre-regex=/usr' '--with-zlib' '--with-layout=GNU' '--enable-exif' '--enable-ftp' '--enable-magic-quotes' '--enable-sockets' '--enable-sysvsem' '--enable-sysvshm' '--enable-sysvmsg' '--enable-track-vars' '--enable-trans-sid' '--enable-yp' '--enable-wddx' '--with-kerberos' '--enable-ucd-snmp-hack' '--with-unixODBC=shared,/usr' '--enable-memory-limit' '--enable-shmop' '--enable-calendar' '--enable-dbx' '--enable-dio' '--with-mime-magic=/etc/httpd/conf/magic' '--without-sqlite' '--with-libxml-dir=/usr' '--with-xml' '--with-apxs2=/usr/sbin/apxs' '--without-mysql' '--without-gd' '--without-odbc' '--disable-dom' '--disable-dba' '--without-unixODBC' '--disable-pdo' '--disable-xmlreader' '--disable-xmlwriter'


This is what phpinfo() gives. No mention of mpm
0
richswyattCommented:
Is this server Redhat?

Try opening SELinux->SELinux Service Protection and then disable the SELinux protection for myslq daemon
0
terabit_caAuthor Commented:
The web server is CentOS 5 and is in a data center. I only have remote access to it... Do you know how I would do that command from shell?
0
richswyattCommented:
That doesn't apply then, I do not believe. Hmm.
0
terabit_caAuthor Commented:
Wow did that take me forever... I have waisted so much time trying to track this down. It turns out that it is a firewall like setting that is causing the problems. The short solution is to type "setenforce 0" to turn off SELinux. Up until now I didn't even know about the existance of SELinux

Here is a link that describes it (I have it going to the Q/A that gave me the above). There is more info in the link that will describe how to turn SELinux off at boot.
http://docs.fedoraproject.org/selinux-faq-fc3/index.html#id2826056

This same "feature" was responsible for another problem I was having on another server. In that case it was not allowing symbolic links pointing to seperate mounts from inside my document root. I looked forever at the "Options AllowSymLinks" scratching my head but running "setenforce 0" fixed that too...

I don't endorse just turning SELinux off but if you are having mysterious access problems try turning it off just to see if its the source.

Two major headaches gone :-)
0
richswyattCommented:
Aha - I thought that was the problem - but I wasn't familiar enough with CentOS to know if SELinux applied. Sorry.
0
Computer101Commented:
PAQed with points refunded (500)

Computer101
EE Admin
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.