LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (13) (https://www.linuxquestions.org/questions/linux-newbie-8/can%92t-connect-to-local-mysql-server-through-socket-%91-var-lib-mysql-mysql-sock%92-13-a-4175460946/)

kwatts59 05-06-2013 07:24 PM

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.

linuxlover.chaitanya 05-07-2013 03:56 AM

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.

kwatts59 05-08-2013 11:48 AM

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 ..."

kwatts59 05-08-2013 12:36 PM

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.

kwatts59 05-08-2013 03:30 PM

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.

jpollard 05-08-2013 04:19 PM

Quote:

Originally Posted by kwatts59 (Post 4947427)
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.


All times are GMT -5. The time now is 07:44 AM.