25

Today, I removed and reinstalled the latest version of lampp in order to move to php 5.30, and suddenly a very simple app is failing to connect to the mysql database. I'm using PDO to connect, and receiving the following error:

Warning: PDO::__construct() [pdo.--construct]: [2002] Invalid argument (trying to connect 
via unix://) in /home/raistlin/www/todoapp/home.php on line 9

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002]
Invalid argument' in /home/raistlin/www/todoapp/home.php:9 Stack trace: #0
/home/raistlin/www/todoapp/home.php(9): PDO->__construct('mysql:host=loca...', 'USER', 
'PASSWORD') #1 {main} thrown in /home/raistlin/www/todoapp/home.php on line 9

I am not catching the error at the moment, for the sake of debugging it.

The following code is enough to replicate the issue on my system:

<?php
$DBACCESS = array(
    "connstring"=>"mysql:host=localhost;dbname=todoapp",
    "host"=>"localhost",
    "user"=>"user",
    "password"=>"password",
    "todoapp"=>"todoapp"
    );

    echo implode('<br \>',$DBACCESS);

    $dbh = new PDO($DBACCESS['connstring'],$DBACCESS['user'],$DBACCESS['password']);

    $dbh = null;
?>

Looking online, I've found one or two other people with the same issue, but none of them have received a response, much less a working one. Does anyone know what is happening? Is there something I missed in the configuration? What do I need to do to fix it?

krdluzni
  • 799
  • 1
  • 9
  • 10

5 Answers5

52

Usually means that you need to specify TCP/IP (1), or tell MySQL where your Unix socket is (2):

  1. "mysql:host=127.0.0.1" or "mysql:host=localhost;port=3306"
  2. "mysql:unix_socket=/var/run/mysqld/mysqld.sock"
rkosegi
  • 12,129
  • 5
  • 46
  • 75
TML
  • 11,974
  • 3
  • 34
  • 43
  • 1
    I'm not sure why MySQL sometimes takes "localhost" to mean "I'm going to use a Unix socket" – TML Sep 16 '09 at 21:18
  • 1
    Perfect. The problem was solved with option 2, although with the xampp lampp installation, I needed to use /opt/lampp/var/mysql/mysql.sock. – krdluzni Sep 16 '09 at 21:46
  • Sure. Glad you were able to find the socket - I just threw an example in there and prayed you'd find it. :) – TML Sep 16 '09 at 21:47
  • 3
    Same problem here too. @krdluzni, seeing your comment I just did this: `pdo_mysql.default_socket=/opt/lampp/var/mysql/mysql.sock` in the /opt/lampp/etc/php.ini file, and it worked. I would otherwise be wondering what configuration error I must have made in Zend Framework. So thanks for posting this! – namespaceform Oct 30 '09 at 07:15
  • Simply add port in you settings like `'host' => env('DB_HOST', '192.168.1.165'), 'port' => env('DB_PORT', '3317'),` `instead` defining 192.168.1.165:3317 – Ashwani Panwar Jul 20 '15 at 10:09
  • I don't have php.ini file in my magento root directory. What should I do? Can anyone please suggest me? – Dipesh Raichana Dec 26 '16 at 15:28
  • @Exception use ` – TML Dec 28 '16 at 15:55
  • @TML: Thanks for reply. I have used that function and I got this path: /usr/local/lib/php.ini But I am not able to find usr directory in magento installation. Would it be somewhere else? – Dipesh Raichana Dec 28 '16 at 16:57
  • @Exception it's in the root of the filesystem; you should speak with your Systems Admin/vendor for help on configuring this. – TML Dec 28 '16 at 23:02
  • @TML: Thanx for the help!! – Dipesh Raichana Dec 29 '16 at 05:54
24

You can also use 127.0.0.1, rather than specifying "localhost", in your db connection string to avoid this issue altogether.

jefftulsa
  • 241
  • 2
  • 2
9

You might want to modify php.ini so PDO can find mysql.sock by specifying the pdo_mysql.default_socket = /opt/lampp/var/mysql/mysql.sock (in the case of xampp). Don't forget to restart Apache after changing php.ini.

(Sorry, this seems to be a repeated solution).

reeserc
  • 91
  • 1
  • 1
4

I'm using MAMP 2.0.1 and Symfony 1.4 with Doctrine for this project.

Third option worked for me with a small modification: in /config/databases.yml

dsn: 'mysql:unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock;dbname=MY_DB_NAME;'
tacheshun
  • 41
  • 1
2

The most common cause of an error like this would be that MySQL isn’t running.

dakab
  • 4,576
  • 8
  • 38
  • 56
pilsetnieks
  • 10,005
  • 12
  • 45
  • 60