I try to connect to mysql (mariadb) via socket connetcion. I have created the mysql user via unix_socket identification. Also I have given some privileges to the test db.

I wrote the conn.php file as:

try {
    $conn = new PDO("mysql:unix_socket=/var/run/mysqld/mysqld.sock;dbname=test", "myusername");
    echo "Connected!";
catch(PDOException $e) {
    echo "Not connected (!) : ".$e->getMessage();

The problem is that if I try on browser then I get access denied error like:

Not connected (!) : SQLSTATE[HY000] [1698] Access denied for user 'myusername'@'localhost'

However, If I try ssh shell by the same user, like:

php conn.php

then the connection is established. Also I can connect as:

mysql -u myusername -S '/var/run/mysqld/mysqld.sock'

The problem is the connection on http. What is wrong about socket connection? Is it possible to be about apache?

Your Answer

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

Browse other questions tagged or ask your own question.