MySQL php can't connect to localhost, 127.0.0.1 working

13,745

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]#

Share:
13,745

Related videos on Youtube

Yujin Boby
Author by

Yujin Boby

Updated on September 18, 2022

Comments

  • Yujin Boby
    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

    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
    # 
    
    • Admin
      Admin over 7 years
      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.
    • Admin
      Admin over 7 years
      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.
  • dortegaoh
    dortegaoh over 7 years
    How is the mysql client supposed to do the login when it can't even access the file socket?
  • Gaurav Pundir
    Gaurav Pundir over 7 years
    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.
  • dortegaoh
    dortegaoh over 7 years
    If 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
    Gaurav Pundir over 7 years
    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.
  • Yujin Boby
    Yujin Boby over 7 years
    This is CentOS 7 server. SELinux is disabled. # sestatus SELinux status: disabled #
  • ivanivan
    ivanivan over 7 years
    This. 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