Laravel MySQL database setup error - php

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.

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

"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

Error while trying to load my laravel project on laravel valet

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

Running Laravel 5 project with DB_HOST=localhost and 127.0.0.1 PDOException

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!

Categories