Error while trying to load my laravel project on laravel valet - php

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```

Related

SQLSTATE[HY000] [2002] Connection refused with Laravel 9.x + docker

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

Laravel can't connect to mysql, but Tinker can [duplicate]

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!

"SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known. " In Laravel 8 [duplicate]

I deleted the migrations table from a Laravel 5.4 database named laravel. When I run php artisan migrate:install, I get this error:
[Illuminate\Database\QueryException]
SQLSTATE[HY000] [2002] No such file or directory
(SQL: select * from information_schema.tables where table_schema = laravel
and table_name = migrations)
I deleted and recreated the database. I also ran composer update. No luck. I can run the command in phpMyAdmin and create the table manually.
This problem sometimes also manifests itself with similar 2002 errors:
[Illuminate\Database\QueryException]
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known.
(SQL: select * from information_schema.tables where table_schema = laravel
and table_name = migrations and table_type = 'BASE TABLE')
[Illuminate\Database\QueryException]
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
(SQL: select * from information_schema.tables where table_schema = laravel
and table_name = migrations and table_type = 'BASE TABLE')
If you are using localhost as your DATABASE_HOST in the .env file, change it to 127.0.0.1, then run php artisan config:clear and now try php artisan migrate:install again.
Here's what I recommend for .env file:
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
Then in Database.php add folder location:
'unix_socket' => env('DB_SOCKET', '/Applications/MAMP/tmp/mysql/mysql.sock'),
Finally, try:
php artisan config:clear
php artisan migrate:install
I fixed this issue by setting environment variables in .env file:
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
Actually I just changed from DB_HOST=127.0.0.1 to DB_HOST=mysql.
If you are using MAMP Pro (not always necessary for the free version of MAMP) then the best thing to do is add this to your .env file:
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
This assumes your MAMP installation is at /Applications/MAMP of course (the default).
Credit: A comment in this question by #Uncoke
I am using MAMP on macOS and after editing localhost to 127.0.0.1 and port to 8888 this problem fixed by adding the following
'unix_socket' => env('DB_SOCKET', '/Applications/MAMP/tmp/mysql/mysql.sock'),
to config/database.php file.
For mac users, add this to your .env file
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
In some cases this may happen because mysql server is not running. It happened to me using Laravel 5.7 and restarting mysql server solved it. For ubuntu check the status of mysql server service mysql status. You can restart mysql server using command service mysql restart
Posting a resolution to my own question:
I deleted the folder and recreated the code base, making sure to point my environment to the correct database server. This time it worked. I don't know exactly what had gone missing to cause the original error.

SQLSTATE[HY000] [2002] No such file or directory migrate problem in mac

i got problem in here so i setup mac for development then i install with brew mysql 5.7
but i can't migrate at all
then i got this error message
Illuminate\Database\QueryException : SQLSTATE[HY000] [2002] No such file or directory (SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')
my .env looks like this
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=root
DB_PASSWORD=
but i can login into mysql CLI with brew i still don't understand where is the problem
UPDATE :
this is solved by installing the mysql_secure_installation
source FIX PROBLEM : https://www.youtube.com/watch?v=xX9W5dmEpO0&t=284s
fixing user grant : PHP with MySQL 8.0+ error: The server requested authentication method unknown to the client

Laravel MySQL database setup error

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.

Categories