PHPUnit tests in Lumen won't connect to local mysql database

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



PHPUnit tests in Lumen won't connect to local mysql database



I have a lumen app that can query a local mysql database with no problems. However, when I try to run PHPUnit tests it fails to connect to my local database, but it will connect to an external one without any problems. I get the following error:


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

Caused by
PDOException: SQLSTATE[HY000] [2002] No such file or directory



Once again, I have absolutely no problem querying my local database through the regular routes of my app. It will connect to an external database in PHPUnit tests, but fails to connect to the local databasel



In my .env file I have the following


######## LOCAL DATABASE ########
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=name_of_my_db
DB_USERNAME=my_username
DB_PASSWORD=my_password

######## External DATABASE ########
# DB_CONNECTION=mysql
# DB_HOST=host_of_external_server
# DB_PORT=3306
# DB_DATABASE=name_of_my_db
# DB_USERNAME=my_username
# DB_PASSWORD=my_password



I can uncomment the external connection and it works 100% of the time. I also tried using 127.0.0.1 as the DB_HOST field but it still fails, but with the following error.


PDOException: SQLSTATE[HY000] [2002] Connection refused



I have not touched my phpunit.xml file and I've cleared my cache more times than I can count.





The lumen app can connect either to remote or local. But UnitTest can only connect to remote. Please confirm.
– bcperth
Aug 12 at 4:53





The difference between local and remote access is that mySQL uses a socket file instead of TCP/IP for local connections, so there may be a small config issue.
– bcperth
Aug 12 at 4:55





@bcperth yes, that's correct. Lumen app can connect to either remote or local, but PHPUnit can only connect to remote. I'm going to attempt one of these solutions, but I'd still like to know why PHPUnit can't use the standard connections in Lumen stackoverflow.com/questions/25495364/…
– user10194756
Aug 12 at 23:26






@bcperth I was able to get the mysql connection to work using this laravel package packagist.org/packages/stechstudio/laravel-ssh-tunnel . If you post your suggestion as an answer I'll mark it as correct and follow up to it with some details that might help others in the future. Thanks!
– user10194756
Aug 13 at 1:03





Not sure how I helped exactly except to trigger a few neuron connections somehow - but thanks!
– bcperth
Aug 13 at 1:38




1 Answer
1



You have confirmed that the Lumen app can connect to remote or local, but UnitTest can only connect to remote.



The difference between local and remote access is that mySQL uses a socket file instead of TCP/IP for local connections, so there may be a small config issue.



I understand you have now solved it using the laravel ssh tunnel package below.



https://packagist.org/packages/stechstudio/laravel-ssh-tunnel



As per your suggestion it would be great if you add some implementation/config details here for others readers :-)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard