LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-06-2013, 08:24 PM   #1
kwatts59
Member
 
Registered: Aug 2012
Posts: 34

Rep: Reputation: Disabled
Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (13)


When I try accessing MySQL from my webpage, I get a blank screen. I can access mysql from my terminal no problem.

I checked my /var/log/httpd/error_log and the following errors appear when I try accessing the webpage
Code:
PHP Warning: mysql_pconnect(): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (13) 
in /var/www/html/shenlab/Connection/connShen.php on line 9, referrer: http://shenlab.sols.unlv.edu/shenlab/
PHP Fatal error: Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (13) in 
/var/www/html/shenlab/Connections/connShen.php on line 9, referrer: http://shenlab.sols.unlv.edu/shenlab/
The php file
/var/www/html/shenlab/Connection/connShen.php has the following code
Code:
<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_connShen = "localhost";
$database_connShen = "shenlab";
$username_connShen = "wrkydb";
$password_connShen = "notshownforsecurity";
$connShen = mysql_pconnect($hostname_connShen, $username_connShen, $password_connShen) or trigger_error(mysql_error(),E_USER_ERROR); 
?>
I tried changing the hostname as follows but neither worked
$hostname_connShen = "127.0.0.1";
$hostname_connShen = "localhost:/data1/mysql/mysql.sock";



The /etc/my.cnf contains the following
Code:
[mysqld]
datadir=/data1/mysql
socket=/data1/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mysqld according to the
# instructions in http://fedoraproject.org/wiki/Systemd

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

[client]
socket = /data1/mysql/mysql.sock

I also tried setting the following booleans but no luck
# setsebool httpd_can_network_connect_db 1
# setsebool httpd_can_network_connect 1


The only thing that seems to work is turning off selinux with "setenforce 0".
Once I turn off selinux, the web page works perfectly. When I turn it back on, the web page goes blank.
Turning off selinux is not an option for me.

If anyone has something else for me to try, it would be greatly appreciated.
Thanks in advance.
 
Old 05-07-2013, 04:56 AM   #2
linuxlover.chaitanya
Senior Member
 
Registered: Apr 2008
Location: Nagpur, India
Distribution: Cent OS 5/6, Ubuntu Server 10.04
Posts: 4,629

Rep: Reputation: Disabled
The socket configuration in my.cnf differs from the socket on which you are trying to connect mysql. You may want to check with --socket or -S command line option to verify mysql connection. In addition, if you do not want to turn off SELinux, you will need to configure it to allow connections to MySQL port and particular socket.
 
Old 05-08-2013, 12:48 PM   #3
kwatts59
Member
 
Registered: Aug 2012
Posts: 34

Original Poster
Rep: Reputation: Disabled
Thank you for your timely reply.
I did notice that the error message states "Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’" and the my.cnf has the line "socket=/data1/mysql/mysql.sock"

I changed the $hostname in the connShen.php file to $hostname_connShen = "localhost:/data1/mysql/mysql.sock";

That fixed the error message. Now the error message states "Can’t connect to local MySQL server through socket ‘/data1/mysql/mysql.sock’"

However, I still get an error message and I still cannot access the webpage unless selinux is turned off.

What do you mean by "--socket or -S command line option"?
I typed --socket at the linux prompt and I get the message "--socket : command not found ..."
 
Old 05-08-2013, 01:36 PM   #4
kwatts59
Member
 
Registered: Aug 2012
Posts: 34

Original Poster
Rep: Reputation: Disabled
I figured out what you meant by "--socket".
I logged in as root and turned on selinux using the "setenforce 1" command.
I logged in as a user and I typed in the following at the linux command prompt
$ mysql -u kwatanabe -p --socket=/data1/mysql/mysql.sock
I entered my password and I got in no problem. I connected to the database and I performed a query on one of the tables.

I still cant access the webpage though.
When I turn off selinux using the "setenforce 0" command, the webpage starts working. Wierd.
 
Old 05-08-2013, 04:30 PM   #5
kwatts59
Member
 
Registered: Aug 2012
Posts: 34

Original Poster
Rep: Reputation: Disabled
I figured out the problem!
I can't believe such a small problem can cause such a nightmare.

I entered the following command as root
# restorecon -v /data1

and then everything started working!
I hate LINUX.
 
Old 05-08-2013, 05:19 PM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Quote:
Originally Posted by kwatts59 View Post
I figured out the problem!
I can't believe such a small problem can cause such a nightmare.

I entered the following command as root
# restorecon -v /data1

and then everything started working!
I hate LINUX.
It isn't a "small problem". It is a security protection. It prevents unauthorized access to a domain socket. The "restorecon" sets the security label on the file (which obviously was created while SELinux was not active). Since the label was not set - SELinux identified it as a security violation, and blocked access (otherwise you might not have been talking to the real database system). Once the label was properly set, other, possibly improper, access to the file was blocked, and valid access restored.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
HTTPD: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' markseger Fedora - Installation 2 02-10-2011 12:53 AM
Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) sammy007 Linux - Networking 1 01-30-2010 07:32 AM
Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' mohadesz Linux - Server 3 03-15-2008 06:06 AM
Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' mathimca05 Linux - Newbie 2 10-17-2007 03:04 AM
Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) sunlinux Linux - Software 1 11-07-2006 01:08 AM


All times are GMT -5. The time now is 11:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration