Receiving "SQLSTATE[HY000] [2002] Connection refused error" in Laravel Project - php

I am working on my Mac and i am trying to connect to database as shown in the env file i have mention the db name but i still getting this error on my laravel project. i have tried it on core php project it work but i get this error while connecting laravel project to database.
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = cart and table_name = migrations and table_type = 'BASE TABLE') at vendor/laravel/framework/src/Illuminate/Database/Connection.php:671
667| // If an exception occurs when attempting to run a query, we'll format the error
668| // message to include the bindings with SQL, which will make this exception a
669| // lot more helpful to the developer instead of just the database's errors.
670| catch (Exception $e) {
671| throw new QueryException(
672| $query, $this->prepareBindings($bindings), $e
673| );
674| }
675|

try this
php artisan config:clear
and then
php artisan migrate
that if you are sure the database already exists

Related

'Connection refused' in Laravel when I try 'php artisan migrate'

When I try php artisan migrate in MacOS terminal, it shows the following -
Illuminate\Database\QueryException
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = project and table_name = migrations and table_type = 'BASE TABLE')
at vendor/laravel/framework/src/Illuminate/Database/Connection.php:759
755▕ // If an exception occurs when attempting to run a query, we'll format the error
756▕ // message to include the bindings with SQL, which will make this exception a
757▕ // lot more helpful to the developer instead of just the database's errors.
758▕ catch (Exception $e) {
➜ 759▕ throw new QueryException(
760▕ $query, $this->prepareBindings($bindings), $e
761▕ );
762▕ }
763▕ }
+36 vendor frames
37 artisan:37
Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
I have tried
php artisan config:clear
php artisan migrate:install
but it's still showing the same message.
Check .env file. In my case, I had to change DB_PORT from 3307 to 3306 (check what's your port)

Laravel migration throws "SQLSTATE[HY000] [2002] php_network_getaddresses" exception

When I run the php artisan migrate command, I get the following error:
Illuminate\Database\QueryException
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution (SQL: select * from information_schema.tables where table_schema = uploadify and table_name = migrations and table_type = 'BASE TABLE')
at vendor/laravel/framework/src/Illuminate/Database/Connection.php:703
699▕ // If an exception occurs when attempting to run a query, we'll format the error
700▕ // message to include the bindings with SQL, which will make this exception a
701▕ // lot more helpful to the developer instead of just the database's errors.
702▕ catch (Exception $e) {
➜ 703▕ throw new QueryException(
704▕ $query, $this->prepareBindings($bindings), $e
705▕ );
706▕ }
707▕ }
+36 vendor frames
37 artisan:37
Illuminate\Foundation\Console\Kernel::handle()
Also, MySQL server is running and I can connect to it using the credentials provided in the .env file. Is this somehow related to Sail?
If so, how can I tell my Laravel application to use the local machine environment instead of the Sail?
Ok, there was a typo in .env for DB_HOST and I didn't notice it. Changing it back to localhost solved my problem.

Laravel migration "Connection refused" [2002] | Database correct | fresh install

I installed fresh laravel and edited .env.
Added my MAMP host:127.0.0.1, database:"abc", username:root, password:root and it then tried run command: php artisan migrate --pretend
Its gives this error:
Illuminate\Database\QueryException
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = abc and table_name = migrations and table_type = 'BASE TABLE')
at vendor/laravel/framework/src/Illuminate/Database/Connection.php:671
667| // If an exception occurs when attempting to run a query, we'll format the error
668| // message to include the bindings with SQL, which will make this exception a
669| // lot more helpful to the developer instead of just the database's errors.
670| catch (Exception $e) {
> 671| throw new QueryException(
672| $query, $this->prepareBindings($bindings), $e
673| );
674| }
675|
+37 vendor frames
38 artisan:37
Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
Tried change also mamp pro port to 8889 and same issue.

I typed 'php artisan migrate' in cmd then it shows me error

Illuminate\Database\QueryException
could not find driver (SQL: select * from information_schema.tables where table_schema = cvs and table_name = migrations and table_type = 'BASE TABLE')
at C:\xampp\htdocs\cvs\vendor\laravel\framework\src\Illuminate\Database\Connection.php:671
667| // If an exception occurs when attempting to run a query, we'll format the error
668| // message to include the bindings with SQL, which will make this exception a
669| // lot more helpful to the developer instead of just the database's errors.
670| ``` catch (Exception $e) {
671| throw new QueryException(
672| $query, $this->prepareBindings($bindings), $e
673| );
674| }```
675|
1 C:\xampp\htdocs\cvs\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70
PDOException::("could not find driver")
2 C:\xampp\htdocs\cvs\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70
PDO::__construct()
Did you enable php_mysqli in the php.ini file?
Look for this line in php.ini file and make sure it is uncommented.
extension=php_mysqli.so
It is usually problem that the DB_USERNAME, DB_PASSWORD credentials are wrong.
It is also necessary to verify that the bd engine driver is activated.
PDO MySQL Driver
phpinfo() output

Database migration not working in Laravel 7

I get this type of exception in all my Laravel projects.
Illuminate\Database\QueryException
SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client (SQL: select * from information_schema.tables where table_schema = orchid and table_name = migrations and table_type = 'BASE TABLE')
at vendor/laravel/framework/src/Illuminate/Database/Connection.php:671
667| // If an exception occurs when attempting to run a query, we'll format the error
668| // message to include the bindings with SQL, which will make this exception a
669| // lot more helpful to the developer instead of just the database's errors.
670| catch (Exception $e) {
> 671| throw new QueryException(
672| $query, $this->prepareBindings($bindings), $e
673| );
674| }
675|
+34 vendor frames
35 artisan:37
Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
I tried to solve it through .env setup.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=orchid
DB_USERNAME=root
DB_PASSWORD=password
But it's not working.
After then I installed composer require doctrine/dbal package in my Laravel project but it's not working. I cannot understand how to solve it.
I try this solution. It helps me.
Log in as root to mysql
Run this sql command:
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

Categories