?
Solved

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

Posted on 2007-05-31
15
Medium Priority
?
2,903 Views
Last Modified: 2013-12-12
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
0
Comment
Question by:terabit_ca
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
15 Comments
 
LVL 5

Expert Comment

by:richswyatt
ID: 19194440
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
 

Author Comment

by:terabit_ca
ID: 19194539
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
 
LVL 5

Expert Comment

by:richswyatt
ID: 19194558
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
How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

 
LVL 5

Expert Comment

by:richswyatt
ID: 19194572
Also - I misspoke about a firewall issue - but you did say that there are NO firewalls installed on this box or there are?
0
 

Author Comment

by:terabit_ca
ID: 19194578
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
 

Author Comment

by:terabit_ca
ID: 19194597
I have shut down iptable while trying to figure this out.
0
 
LVL 5

Expert Comment

by:richswyatt
ID: 19194629
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
 

Author Comment

by:terabit_ca
ID: 19194643
'./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
 
LVL 5

Expert Comment

by:richswyatt
ID: 19194827
Is this server Redhat?

Try opening SELinux->SELinux Service Protection and then disable the SELinux protection for myslq daemon
0
 

Author Comment

by:terabit_ca
ID: 19195209
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
 
LVL 5

Expert Comment

by:richswyatt
ID: 19195219
That doesn't apply then, I do not believe. Hmm.
0
 

Author Comment

by:terabit_ca
ID: 19214159
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
 
LVL 5

Expert Comment

by:richswyatt
ID: 19216207
Aha - I thought that was the problem - but I wasn't familiar enough with CentOS to know if SELinux applied. Sorry.
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 19539223
PAQed with points refunded (500)

Computer101
EE Admin
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
In this article, we’ll look at how to deploy ProxySQL.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question