MySQL php can't connect to localhost, 127.0.0.1 working
Solution 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
Solution 2
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]#
Related videos on Youtube
![Yujin Boby](https://i.stack.imgur.com/74HSI.png?s=256&g=1)
Yujin Boby
Updated on September 18, 2022Comments
-
Yujin Boby almost 2 years
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
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 #
-
Admin over 7 yearsobviously 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. -
Admin over 7 yearsplease run
ls -la /var/lib/mysql/
andls -Za /var/lib/mysql/
(as root, not as user apache) and edit the output into your question.
-
-
dortegaoh over 7 yearsHow is the mysql client supposed to do the login when it can't even access the file socket?
-
Gaurav Pundir over 7 yearsI 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.
-
dortegaoh over 7 yearsIf this were a permission problem inside mysql the error message would look like this:
Access denied for user 'user'@'localhost' (using password: YES)
. -
Gaurav Pundir over 7 yearsYeah 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.
-
Yujin Boby over 7 yearsThis is CentOS 7 server. SELinux is disabled. # sestatus SELinux status: disabled #
-
ivanivan over 7 yearsThis. Additionally, whenever you specify "localhost" or "localhost:port" as server, the MySQL client library will override this and try to connect to a local socket (named pipe on Windows), so if your not set up to use sockets/pipes then you have to specify 127.0.0.1