dil_bert Posted September 28, 2019 Share Posted September 28, 2019 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 Quote Link to comment https://forums.phpfreaks.com/topic/309296-could-not-connect-to-the-db-reason-sqlstatehy000-2002-no-such-file-or-directory/ Share on other sites More sharing options...
dil_bert Posted September 29, 2019 Author Share Posted September 29, 2019 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 Quote Link to comment https://forums.phpfreaks.com/topic/309296-could-not-connect-to-the-db-reason-sqlstatehy000-2002-no-such-file-or-directory/#findComment-1570052 Share on other sites More sharing options...
dil_bert Posted September 29, 2019 Author Share Posted September 29, 2019 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 Quote Link to comment https://forums.phpfreaks.com/topic/309296-could-not-connect-to-the-db-reason-sqlstatehy000-2002-no-such-file-or-directory/#findComment-1570054 Share on other sites More sharing options...
dil_bert Posted September 30, 2019 Author Share Posted September 30, 2019 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] Quote Link to comment https://forums.phpfreaks.com/topic/309296-could-not-connect-to-the-db-reason-sqlstatehy000-2002-no-such-file-or-directory/#findComment-1570076 Share on other sites More sharing options...
dil_bert Posted October 3, 2019 Author Share Posted October 3, 2019 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 Quote Link to comment https://forums.phpfreaks.com/topic/309296-could-not-connect-to-the-db-reason-sqlstatehy000-2002-no-such-file-or-directory/#findComment-1570250 Share on other sites More sharing options...
Barand Posted October 3, 2019 Share Posted October 3, 2019 Alternatively you can use the "@@" prefix for system variables E.G. mysql> select user(), @@hostname, @@port; +----------------+-----------------+--------+ | user() | @@hostname | @@port | +----------------+-----------------+--------+ | root@localhost | DESKTOP-DCGAC4S | 3306 | +----------------+-----------------+--------+ 1 Quote Link to comment https://forums.phpfreaks.com/topic/309296-could-not-connect-to-the-db-reason-sqlstatehy000-2002-no-such-file-or-directory/#findComment-1570251 Share on other sites More sharing options...
dil_bert Posted October 4, 2019 Author Share Posted October 4, 2019 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 Quote Link to comment https://forums.phpfreaks.com/topic/309296-could-not-connect-to-the-db-reason-sqlstatehy000-2002-no-such-file-or-directory/#findComment-1570320 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.