0

I have a Simple PHP script, that is not able to connect to MySQL.

<?php

$link = mysql_connect("localhost", "user", "password");

if (!$link) {
    die('Could not connect: ' . mysql_error() . ' ' . mysql_errno());
} else {
    echo "connected";
}

When i access the page with url

http://mydomain/1.php

I get error

Could not connect: Permission denied 2002

If i change "localhost" with "127.0.0.1", it works.

If i run the program from command line, for example

php 1.php

it connects. To verify the problem, i run

[root@server1 ~]# php -r 'var_dump(mysql_connect("localhost:/var/lib/mysql/mysql.sock", "user", "password"));'
resource(5) of type (mysql link)
[root@server1 ~]# chsh apache --shell /bin/bash
Changing shell for apache.
chsh: Shell not changed.
[root@server1 ~]# su - apache
Last login: Fri Mar 17 02:30:39 CDT 2017 on pts/0
-bash-4.2$ php -r 'var_dump(mysql_connect("localhost:/var/lib/mysql/mysql.sock", "user", "password"));'
PHP Warning:  mysql_connect(): Permission denied in Command line code on line 1
bool(false)
-bash-4.2$ ls -l /var/lib/mysql/mysql.sock
ls: cannot access /var/lib/mysql/mysql.sock: Permission denied
-bash-4.2$ 

How do i fix this issue, this is CentOS 7 server with default php version. MySQL version is

[root@server1 ~]# mysql --version
mysql  Ver 15.1 Distrib 5.5.52-MariaDB, for Linux (x86_64) using readline 5.1
[root@server1 ~]# 

EDIT:

This is CentOS 7 Server, SELinux is disabled.

# sestatus
SELinux status:                 disabled
# 
  • obviously you have to fix the permissions of /var/lib/mysql/mysql.sock (and possibly the folders in that path). Also note that this could be a selinux issue. But without seeing the actual permissions this is just blind guessing. – Gerald Schneider Mar 17 '17 at 8:23
  • please run ls -la /var/lib/mysql/ and ls -Za /var/lib/mysql/ (as root, not as user apache) and edit the output into your question. – Gerald Schneider Mar 17 '17 at 10:32
  • @Gerald Schneider Schneider Here is the result of the two commands pastebin.com/raw/HF0hzQdm I understand it is a permission issue as user Apache can't access socket, i want to know the correct way to fix this problem. – Yujin Boby Mar 18 '17 at 2:44
1

I had the same problem a few hours ago (cpanel & cloudlinux)

My simple solution was to modify /etc/my.cnf:

[root@ds1 home]# cat /etc/my.cnf
[mysqld]
innodb_file_per_table=1
default-storage-engine=MyISAM
performance-schema=0
max_allowed_packet=268435456
open_files_limit=28574
#bind-address=127.0.0.1
bind-address=localhost

local-infile=0
[client]
socket=/var/lib/mysql/mysql.sock
host=localhost

[root@ds1 home]#

1

localhost and 127.0.0.1 are not the same for mysql, you need a grant for each of them.

https://stackoverflow.com/questions/19712307/mysql-localhost-127-0-0-1

0

I got it resolved by running

chmod 755 /var/lib/mysql
-1

Looks like you might have created the user which is bound with 127.0.0.1. Try one thing create another user like this in mysql.

create user 'my_user'@'localhost' identified by 'mypassword';

and grant same privileges to this user as the earlier one.

Note : You can keep the same username, or you can delete the earlier one and create the new one, its up to you.

  • How is the mysql client supposed to do the login when it can't even access the file socket? – Gerald Schneider Mar 17 '17 at 10:18
  • I don't think that's the problem here, as he says his client is able to access mysql with 127.0.0.1 but not localhost. – Gaurav Pundir Mar 17 '17 at 10:21
  • If this were a permission problem inside mysql the error message would look like this: Access denied for user 'user'@'localhost' (using password: YES). – Gerald Schneider Mar 17 '17 at 10:21
  • Yeah you got a point. Hi @HostOnNet can you please share whether apparmor is enabled or not. If yes can you please share the configuration for that. – Gaurav Pundir Mar 17 '17 at 10:27
  • This is CentOS 7 server. SELinux is disabled. # sestatus SELinux status: disabled # – Yujin Boby Mar 18 '17 at 2:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.