Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql_connect() doesn't work when run by apache; works from command line

Tags:

php

mysql

I have a strange issue. I'm trying to write a simple php webpage on my server, but mysql_connect() doesn't connect to any server, either local or otherwise. Here's where it gets strange. If I take the same php script and run it from the commandline, the script works. phpinfo() indicates that both the file (being run by apache) and the commandline (run as root) are calling the same version of php, mysql is loaded, and the php.ini is the same.

Furthermore, I'm running a MediaWiki installation on this same server, and it's using the mysqld installed locally and works just fine, so I'm completely at a loss as to why the code isn't working. The error I receive on runtime:

Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (13)

(The IP is x'd out for the privacy of the owner of the server I'm connecting to)

like image 712
Skeeter Avatar asked Dec 02 '25 09:12

Skeeter


1 Answers

What operating system are you trying to connect from? It sounds like it may be an SELinux issue.

With SELinux, you can usually allow apache to make network connections by using

/usr/sbin/setsebool httpd_can_network_connect 1

OR /usr/sbin/setsebool httpd_can_network_connect true

An operating system without SELinux may have a similar protection mechanism.

like image 134
Reece45 Avatar answered Dec 04 '25 23:12

Reece45



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!