Jump to content

could not connect to the db: reason : SQLSTATE[HY000] [2002] No such file or directory


Recommended Posts

good day dear php-experts,

 

todays issue: could  not connect to the db: reason : SQLSTATE[HY000] [2002] No such file or directory

 

while i try to install a script on a server i get back the following error 

 

cannot connect to the db :: just try again  

reason: SQLSTATE[HY000] [2002] No such file or directory

i tried it several times - but without any success;:  i googled the error 

https://stackoverflow.com/questions/29695450/pdoexception-sqlstatehy000-2002-no-such-file-or-directory

Quick test (run in shell):


php -r "new PDO('mysql:hostname=localhost;dbname=test', 'username', 'password');"
SQLSTATE[HY000] [2002] No such file or directory means php cannot find the mysql.default_socket file. Fix it by modifying php.ini file. 
On Mac it is mysql.default_socket = /tmp/mysql.sock (See PHP - MySQL connection not working: 2002 No such file or directory)
SQLSTATE[HY000] [1044] Access denied for user 'username'@'localhost' CONGRATULATION! You have the correct mysql.default_socket 
setting now. Fix your dbname/username/password.
Also see Error on creating connection to PDO in PHP

 

and the following ideas: 

You need to change host from localhost to 127.0.0.1
Laravel 4: In your app/config/database.php try changing host from localhost to 127.0.0.1
Laravel 5: In the .env file, change DB_HOST from localhost to 127.0.0.1
Source: PDOException SQLSTATE[HY000] [2002] No such file or directory
shareeditflag
 

see more here

https://stackoverflow.com/questions/29695450/pdoexception-sqlstatehy000-2002-no-such-file-or-directory

 

well all the trials faied so far

again me-  here some more infos: 

running a server  and yes mysql is installed. 

running PHP Version 5.6.39
mysqlnd 5.0.11-dev - 20120503 

mysqli.default_host    localhost    localhost
mysqli.default_port    3306    3306
mysqli.default_pw    no value    no value
mysqli.default_socket    /var/run/mysql/mysql.sock    /var/run/mysql/mysql.sock

hmmm - i currently wonder why it does not work


any idea how to check things !? Look forward to hear from you +


regards 


here some more findings: : the folks / and user that face the same issue with the mentioned survey script as i do - they have posted some ideas and findings - food for thought: 


cf: https://www.limesurvey.org/forum/installation-a-update-issues/108028-cdbconnection-failed-to-open-the-db-connection-sqlstate-hy000-2002

klaus said: I run LimeSurvey on Linux for a few years now. After a reboot, probably an update, lime does not start anymore. I get the error 

CDbConnection failed to open the DB connection: SQLSTATE[HY000] [2002] No such file or directory


Following my research I looked for the connectionstring in the config.php and found:

'connectionString' => 'mysql:unix_socket=/usr/local/LimeSurvey/var/LimeSurvey_mysqld.sock;dbname=limesurvey;',


So I looked for the

/usr/local/LimeSurvey/var/LimeSurvey_mysqld.sock 


file but it was not there. Further research results: change the connectionString to:


...change the connectionString to:

'connectionString' => 'mysql:host=127.0.0.1;unix_socket=/usr/local/LimeSurvey/var/LimeSurvey_mysqld.sock;dbname=limesurvey;', 


and this following idea:

Try
'connectionString' =>'mysql:host=localhost;port=3306;dbname=limesurvey;',

Are you sure DB still active mysql are on the same server?

I was able to fix it. The error showed that /tmp/mysql.sock was missing so I created a symbolic link with this command.

ln -s /usr/local/lib/mysql.sock /tmp/mysql.sock

see the tread for more infos: https://www.limesurvey.org/forum/installation-a-update-issues/108028-cdbconnection-failed-to-open-the-db-connection-sqlstate-hy000-2002

conclusio: do you think that i have to do some corrections in the paths and the paths to socket!?


i try to figure out what goes on here ...

any idea how to check things !? Look forward to hear from you 

i can do some tests with a spimple test the connection script : 





<?php
if(function_exists('mysqli_connect')){
if(!($link = mysqli_connect('localhost','username','password','my_db'))){
die('could not connect: ' . mysqli_error($link));
}
} else {
die("don't have mysqli");
}
echo 'connect successfully';
mysqli_close($link);[/CODE]


If we want to know the hostname of the Mysql-database - then we can use this following query in the terminal resp. the
MySQL Command line-terminal: 

we can run the following command in the terminal: 


SHOW VARIABLES WHERE Variable_name = 'hostname';
mysql> SHOW VARIABLES WHERE Variable_name = 'hostname';
+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| hostname          | Dell  |
+-------------------+-------+
11 row in set (0.00 sec)


 

It will give us all the hostname-data for mysql.

and furthermore : if we want to get more - if we want to know the username of our Mysql then we can run more commands to get these data;

We can run this query on MySQL Command line client --

select user();   


mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

 

 

It will give us the username for mysql.

but if we want to get more data the n eg - if we want to know the port number of the local host on which Mysql is running 
we can find out thhis with the following command,. 

SHOW VARIABLES WHERE Variable_name = 'port';
mysql> SHOW VARIABLES WHERE Variable_name = 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |
+---------------+-------+
1 row in set (0.00 sec)

 

this command is very vers intersting: It will give us the port number on which MySQL is running.

 

but i have some questions - If a installation attempt can't find my socket file, or if we have multiple MySQL servers running on our computer, 
we must enter the location of the socket file. 

this means wee have to see Where are the MySQL's Files? for common socket file locations.

sometimes we can try using localhost instead of 127.0.0.1. MySQL treats the hostname localhost specially.
 

well the question is - where do i need to add the socketpath - and where do i need to enter #"localhost" - or  127.0.0.1.

 

love to hear from you

Alternatively you can use the "@@" prefix for system variables E.G.

mysql> select user(), @@hostname, @@port;

+----------------+-----------------+--------+
| user()         | @@hostname      | @@port |
+----------------+-----------------+--------+
| root@localhost | DESKTOP-DCGAC4S |   3306 |
+----------------+-----------------+--------+

 

  • Like 1
On 10/3/2019 at 2:19 PM, Barand said:

 

 

Alternatively you can use the "@@" prefix for system variables E.G.


mysql> select user(), @@hostname, @@port;

+----------------+-----------------+--------+
| user()         | @@hostname      | @@port |
+----------------+-----------------+--------+
| root@localhost | DESKTOP-DCGAC4S |   3306 |
+----------------+-----------------+--------+

 

hello dear Baraqnd 

 

many many thanks for the quick answer - and the idea - that sounds very interesting. 

 

regards

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.