An Exception PDO occurs when localhost is used as a host

Asked

Viewed 7,486 times

4

Consider the following adaptor class DbAdapterMySQL that extends the class PDO:

class DbAdapterMySQL extends \PDO implements DbInterface
{

    public function __construct(array $config)
    {
        $dsn = "mysql:dbname={$config['dbname']};host={$config['host']}";

        //A próxima linha é a 13
        parent::__construct($dsn, $config['username'], $config['passwd']);
    }

    //...
}

Note that for the constructor method an array is passed with the database access settings with the following structure:

db = MySQL
dbname = teste
username = 'root'
passwd = 'root'
host = 127.0.0.1

So far everything works perfectly, but if you replace the value of the index host for localhost the following error is returned:

Fatal error: Uncaught Exception 'Pdoexception' with message 'SQLSTATE[HY000] [2002] No such file or directory' in /home/Filipe/Projects/teste/lib/vendor/Testes/Db/Adapter/Dbadaptermysql.php:13 Stack trace: #0 in /home/Filipe/Projects/teste/lib/vendor/Testes/Db/Adapter/Dbadaptermysql.php on line 13

Why error occurs when using the localhost?

  • Using linux or mac?

  • @rray linux, I’ll add the tag.

  • 1

    That seems to be in the solution of the problem, pq well it is strange to try to connect in the bank and receive an Exception saying file not found.

  • @rray was the problem. The first part of the answer is enough to change the php.ini. Do you want to post the answer or do I?

  • I have no way to test, create a response with the details :), just cite the link.

2 answers

6


It seems that the problem is related to the connection of PHP with Mysql.

From what I understand, when we use localhost as host, PHP uses socket to connect to Mysql, unlike when we use 127.0.0.1 where the connection is made via TCP/IP.

If you use a Linux-based system, there must be a configuration file my.cnf which is used to configure Mysql.

In my case as I am using Ubuntu, the file is in the directory /etc/mysql/ and there was a line like this:

socket = /var/run/mysqld/mysqld.sock

PHP needs to use the same file and in my case it was not the same as Mysql used.

To fix the problem, I changed the php.ini adding the directory where the socket file was:

mysql.default_socket = /var/run/mysqld/mysqld.sock
mysqli.default_socket = /var/run/mysqld/mysqld.sock
pdo_mysql.default_socket = /var/run/mysqld/mysqld.sock

After the change, I just restarted apache:

sudo service apache2 restart

Source

  • @rray thanks for the help.

0

You need to configure the default_socket in the archive php.ini.

To locate the mysql socket:

mysql -u root -p -h 127.0.0.1 -e "select @@socket"

Located the socket, now just put the path in the file php.ini. Locate mysql.default_socket or mysqli.default_socket or pdo_mysql.default_socket and insert the walk as an example:

mysql.default_socket = /var/run/mysqld/mysqld.sock
mysqli.default_socket = /var/run/mysqld/mysqld.sock
pdo_mysql.default_socket = /var/run/mysqld/mysqld.sock

Browser other questions tagged

You are not signed in. Login or sign up in order to post.