This question already has answers here:
SQLSTATE[HY000] [2002] Connection refused within Laravel homestead
(31 answers)
PDOException SQLSTATE[HY000] [2002] No such file or directory
(37 answers)
Closed 1 year ago.
I'm having issues connecting to a docker instance of MySQL through Laravel. I've found that by using Tinker, I can connect successfully to the database. e.g, with Tinker, if I run
DB::select("select * from users;");
I'm able to get results. However, if I make a route
Route::post('/sql', function(Request $request){
return DB::select('select * from appointments;')->get();
});
and hit it through PostMan, I get back
"message": "SQLSTATE[HY000] [2002] Connection refused (SQL: select * from `users` where `users`.`id` = 1 limit 1)",
I currently have the .env file set with
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=test_project
DB_USERNAME=root
DB_PASSWORD=password
I've read that many people have trouble with using 127.0.0.1, but I've found that when I switch the DB_HOST to localhost, tinker doesn't even successfully connect. Instead, I get
Illuminate/Database/QueryException with message 'SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from users;)'
I've already tried
php artisan route:clear
php artisan config:clear
php artisan cache:clear
But I cannot get this to work. Any help or insight is greatly appreciated!
Related
I am using Laravel 9.x and XAMPP. The php artisan migrate.
command works great. When I use postman to implement the register function, it returns this error:
"message": "SQLSTATE[HY000] [2002] Connection refused (SQL:
SELECT count(*) AS aggregate FROM `users`
WHERE `email` = quangthin2000#gmail.com)",
"exception": "Illuminate\\Database\\QueryException",
My .env file:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=testproject
DB_USERNAME=root
DB_PASSWORD=
The error is Connection Refused, your application is failing to even connect to the SQL server in the first place. There may not even be a problem with your business logic because you're failing before you even get to that point. Make sure your database is actually running and accessible on the host and port you specified.
Add this line to your .env file
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
This question already has answers here:
Laravel SQLSTATE[HY000] [2002] Connection refused
(11 answers)
Closed 1 year ago.
Illuminate\Database\QueryException : SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = pracatice and table_name = migrations)
First check the composer require doctrine/dbal is installed
if this composer is not installed then install it
Check the database port number is correct or not with other information like
DB_HOST=127.0.0.1,DB_PORT=3307,DB_DATABASE=pracatice,DB_USERNAME=root,DB_PASSWORD=
Check the files permission for project this is also most important for running php artisan migrate
Then retry php artisan migrate
In the .env file, try to change your DB_HOST from localhost to 127.0.0.1.
While trying to load my laravel project, i got this error SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known (SQL: select * from `users` where `email` =eniola#gmail.com limit 1) . From my search for answers online i figured the issue is from the database details in the .env file, however none of the solutions proposed works.
here are my db details, i used valet.
DB_HOST=127.0.0.1
DB_PORT=80
DB_DATABASE=projectname
DB_USERNAME=root
DB_PASSWORD=
I already ran php artisan migrate:refresh as well as php artisan config:clear, but i get the same error
This solved the password hashing issue with sql version 8. I added the last line of code to my .env file.
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=db_name
DB_USERNAME=root
DB_PASSWORD=password
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock```
I'm using Laravel 5 on Windows 10, VB, homestead and I am still new, I've just started learning Laravel.
When I run my project in Chrome using DB_HOST=127.0.0.1 in .env file I get this error message:
PDOException in Connector.php line 55:
SQLSTATE[HY000] [2002] Connection refused
and when I change DB_HOST to localhost everything is fine except when I am using tinker (php artisan tinker). I get this message:
PDOException with message 'SQLSTATE[HY000] [2002] No such file or directory'
Database works with DB_HOST=localhost and tinker DB_HOST=127.0.0.1
So, I can't use both at the same time, everytime I must change DB_HOST in .env file from localhost to 127.0.0.1
What can I do to fix this issue?
If you're using Homestead, your DB_HOST should be set to localhost.
The reason why in such case artisan (migrate) cannot connect to database is because you are not executing the commands inside your virtual machine.
To do it the right way connect to your vm using:
vagrant ssh
and once logged in, continue as you'd normally.
Hope that helps!
I want to use a MySQL database with my Laravel 5.2 framework. I'm not able to access phpMyAdmin after I run php artisan serve and open a localhost page.
My .env file :
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_DATABASE=people
DB_USERNAME=pftest
DB_PASSWORD="pftest_2016#9"
After doing this, I ran php artisan migrate and got the following error:
[PDOException] could not find driver
Following which, I have installed the php-mysql extension and checked for its presence in php.ini file.
But I'm still getting the following error :
[PDOException] SQLSTATE[HY000] [2002] Connection refused
So what is the issue and how to solve it?
In the .env file, set
APP_ENV=local
DB_PORT=3306
and then run php artisian migrate --env="local"
Same is valid for dev,production env.