reddekopp Posted October 21, 2008 Share Posted October 21, 2008 Hello all, this is my first post here and I just first want to say thanks for having such a wonderful site with lots of information. Now to the point: On our server at work we are running the following: -Apache 2.2.3-11 (httpd-2.2.3-11.el5_1.centos.3) -PHP 4.4.9 (built on the machine --with-mysql --with-apxs) -MySQL 4.1.22 (MySQL-client-standard-4.1.22-0.rhel3.x86_64.rpm & MySQL-server-standard-4.1.22-0.rhel3.x86_64.rpm) The older version of mysql is needed to support some older software we have. The Problem: Now the delima is as follows. I can connect to mysql when I run my php scripts from the command line, but when I access the php script using a web browser the script reports "Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (13)". Now this mysql.sock file does exist, and is connected to when the script is run from the command line. Does this mean some user/group permissions need to be changed? And if so where? I have been tackling this problem for days and am getting very frustrated lol. The php script -------------------------------------------------- <?php echo "This is a test php script:<br>\n"; if(!($conn = mysql_connect("localhost","*****","*****"))) { echo "Did not connect!<br>\n"; } echo "Done Script!\n"; ?> ---------------------------------------------- Output when run from command line: ---------------------------------------------- [root@m0bugz2 php-4.4.9]# php -f /var/www/html/dm/testsimple.php This is a test php script:<br> Done Script! ---------------------------------------------- Output when run from webbrowser: http://dhcp-199.********.ca/dm/testsimple.php ---------------------------------------------- This is a test php script: Did not connect! Done Script! ---------------------------------------------- Thanks in advance to all who reply! Quote Link to comment https://forums.phpfreaks.com/topic/129401-php-can-connect-to-mysql-from-command-line-but-not-via-apache/ Share on other sites More sharing options...
neogranas Posted October 21, 2008 Share Posted October 21, 2008 Do you have php-mysql installed? Quote Link to comment https://forums.phpfreaks.com/topic/129401-php-can-connect-to-mysql-from-command-line-but-not-via-apache/#findComment-670928 Share on other sites More sharing options...
trq Posted October 21, 2008 Share Posted October 21, 2008 Try using 127.0.0.1 instead of localhost. Quote Link to comment https://forums.phpfreaks.com/topic/129401-php-can-connect-to-mysql-from-command-line-but-not-via-apache/#findComment-670931 Share on other sites More sharing options...
reddekopp Posted October 21, 2008 Author Share Posted October 21, 2008 The output from the command line is indicating that php is connecting to mysql. So the php-mysql shouldn't be related to the Apache problem should it? The reason I am not doing a "yum install php-mysql" is because it then wants to upgrade my mysql to a 5.x version which I can't do due to legacy software which can't have a mysql > 4.1. Modules Installed by php: ctype ionCube Loader mysql overload pcre posix session standard tokenizer xml Since mysql appears to be a php module it shouldn't need a php-mysql install anyways. Correct me if I am wrong. Quote Link to comment https://forums.phpfreaks.com/topic/129401-php-can-connect-to-mysql-from-command-line-but-not-via-apache/#findComment-670935 Share on other sites More sharing options...
reddekopp Posted October 21, 2008 Author Share Posted October 21, 2008 Try using 127.0.0.1 instead of localhost. This worked!! Thank you so much. So can someone explain to me why this is so I have a better understanding? Thank you once again you are my hero!! hehe Quote Link to comment https://forums.phpfreaks.com/topic/129401-php-can-connect-to-mysql-from-command-line-but-not-via-apache/#findComment-670938 Share on other sites More sharing options...
trq Posted October 21, 2008 Share Posted October 21, 2008 127.0.0.1 goes through tcp/ip and localhost goes through the local socket. Quote Link to comment https://forums.phpfreaks.com/topic/129401-php-can-connect-to-mysql-from-command-line-but-not-via-apache/#findComment-670940 Share on other sites More sharing options...
neogranas Posted October 21, 2008 Share Posted October 21, 2008 http://isoredirect.centos.org/centos/3/updates/i386/RPMS/ You can use "Parent Directory" to go back and get your correct version of CentOS 3.x and then go to updates/arch/RPMS/ and get php-mysql Then use yum localinstall php-mysql-ver.ent.arch.rpm Quote Link to comment https://forums.phpfreaks.com/topic/129401-php-can-connect-to-mysql-from-command-line-but-not-via-apache/#findComment-670945 Share on other sites More sharing options...
reddekopp Posted October 21, 2008 Author Share Posted October 21, 2008 Awesome. It would be still nice to understand why the localhost didn't work. I think it has to do with permissions, as when I set Apache to use user:mysql group:mysql then it worked using localhost (I just tested this). But I will forget about persueing that fix and use this new approach. Thank you to everyone who replied, and those tips will come in handy for searching for other rpm's. ~Randy (Shadarap) Quote Link to comment https://forums.phpfreaks.com/topic/129401-php-can-connect-to-mysql-from-command-line-but-not-via-apache/#findComment-670958 Share on other sites More sharing options...
trq Posted October 21, 2008 Share Posted October 21, 2008 I think it has to do with permissions, as when I set Apache to use user:mysql group:mysql then it worked using localhost (I just tested this). But I will forget about persueing that fix and use this new approach. Yeah, I should have mentioned that my suggestion is just a quick hack, not a solution to the issue. Connecting to mysql via the local socket is apparently quicker than using tcp/ip, though I'm sure in most cases the difference would be negligible. Quote Link to comment https://forums.phpfreaks.com/topic/129401-php-can-connect-to-mysql-from-command-line-but-not-via-apache/#findComment-671214 Share on other sites More sharing options...
cinemaparadiso Posted December 24, 2011 Share Posted December 24, 2011 Hello, I know this is an old topic, but I just had the same issue. Fortunaltely, it was solved by using 127.0.0.1 instead of localhost. But have you found a better solution since? Using the socket is certainly abetter way to do this... By the way, my socket is there and functions, and I can connect to mysql without any problem from the command line (with mysqladmin for example). Could the problem bue aproblem of permissions on the socket itself? When I am at command line, I am root, but PHP/Apache uses www-data I assume... Thanks Quote Link to comment https://forums.phpfreaks.com/topic/129401-php-can-connect-to-mysql-from-command-line-but-not-via-apache/#findComment-1301148 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.