You are using a Unix socket. When reading "localhost" MySQL client libraries don't interpret it as TCP host "localhost" and resolve that name but use the default Socket location. For using TCP on the local machine you have to use 127.0.0.1
as hostname.
To specify the past use unix_socket
instead of host
in the DSN. The location of the socket used for localhost
can be defined at compile time or in some versions of PHP using pdo_mysql.default_socket
in the php.ini
.
From the PHP documentation about connection to MySQL using PDO: PDO_MYSQL DNS
The note at the very end says:
Unix only:
When the host name is set to "localhost", then the connection to the server is made thru a domain socket. If PDO_MYSQL is compiled against libmysql then the location of the socket file is at libmysql's compiled in location. If PDO_MYSQL is compiled against mysqlnd a default socket can be set thru the pdo_mysql.default_socket setting.
So in order to fix this you would have to properly configure in php.ini the location of your mysql.sock
Find your mysql.sock file. Common locations:
Edit your php.ini file and properly set the value for pdo_mysql.default_socket
Restart your Apache server to pickup the changes in the php.ini file
On Ubuntu, you can use this setting in php.ini
pdo_mysql.default_socket=/var/run/mysqld/mysqld.sock
I just added this line:
'unix_socket' => '/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock',
and all was well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With