SQLSTATE[HY000] [2002] No such file or directory - php

I am new to Laravel. I am trying to create a laravel CRUD with resource controllers. However, when I try to run php artisan migrate I get the following error:
SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from
information_schema.tables where table_schema = homestead and
table_name = migrations)
Here is my database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'design'),
'username' => env('DB_USERNAME', 'homestead'),
'password' => env('DB_PASSWORD', 'secret'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
and this is my env file:
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:hLJn+XHKPjf5qEvROeyMMLIjw56Kg9RkXhZIlbjjne4=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=design
DB_USERNAME=homestead
DB_PASSWORD=secret
This is a common problem and I have tried many solutions but they wont work. Please help.

try to use PORT 3306 and change localhost to 127.0.0.1
for example
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=

Related

Access denied for user 'user'#'localhost' Laravel 5.8

I have this project running on Laravel 5.8 and Database on Mysql using XAMPP server.
This is my .env file:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
This is my config/database.php file:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => false,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
and also created a Database in Xampp named homestead.
this error is showing: SQLSTATE[HY000] [1045] Access denied for user 'user'#'localhost' (using password: YES) (SQL: select * from userswhereemail = test#gmail.com
you need to provide db_name, mysql user_name and password in your .env file.
for xampp default username is 'root' and no password.
so update your .env file
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=matkagu_satta
DB_PASSWORD=

How can I solve this problem in composer?

I am beginner of using laravel 6, so this the error message that I faced,
1) SQLSTATE[42S02]: Base table or view not found: 1146 Table
'forge.settings' doesn't exist (SQL: select * from settings where
auto_load_disabled is null)
2)SQLSTATE[42S02]: Base table or view not found: 1146 Table
'forge.settings' doesn't exist
even I have tried the command php artisan migrate:fresh but it exists the same error, the command include
php artisan serve, php artisan migrate, are also not work for me..
Below are the code,
In my .env file:
APP_NAME=Laravel
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost
LOG_CHANNEL=stack
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=forge
DB_USERNAME=root
DB_PASSWORD=
The command php artisan migrate:key is not work, the error message is same to the above 1) and 2)...
and I have also try to copy another app key that can run on the server but still not work.
The database I have already create on phpmyadmin named 'forge' using xampp server.
my database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'modes' => [
'STRICT_TRANS_TABLES',
'NO_ZERO_IN_DATE',
'NO_ZERO_DATE',
'ERROR_FOR_DIVISION_BY_ZERO',
'NO_AUTO_CREATE_USER',
'NO_ENGINE_SUBSTITUTION'
],
'engine' => 'InnoDB',
]
Can anyone help me? Please..

Connect my Laravel to a external Database

How can i connect my laravel to an extneral Database?
Example: I have a laravel on my local machine which is running on xammp. And i want it to connect to a cloud Server database.
Open the .env file and edit it. Just set up correct external DB credentials:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1 // set external DB_Host
DB_PORT=3306 // Your Database Port
DB_DATABASE= // Your Database Name
DB_USERNAME= // Your Database Username
DB_PASSWORD= // Your Database Password
DB_USERNAME should be set to root if you do not have a default username in the installation time
If no password is set on the database, just clear it DB_PASSWORD
After .env edit, must be clear cache:
php artisan config:cache
In .env file you can set DB_CONNECTION with your database name and applicable databases are given in /config/database.php which are (SQLite, MySQL, pgSQL, SQLSRV) after that just type your username, password, and database name and you can use that database with port number.
.env
DB_CONNECTION_SECOND=mysql
DB_HOST_SECOND=127.0.0.1
DB_PORT_SECOND=3306
DB_DATABASE_SECOND=laravel
DB_USERNAME_SECOND=root
DB_PASSWORD_SECOND=
config/database.php
'mysql_second' => [
'driver' => 'mysql',
'host' => env('DB_HOST_SECOND', '127.0.0.1'),
'port' => env('DB_PORT_SECOND', '3306'),
'database' => env('DB_DATABASE_SECOND', 'forge'),
'username' => env('DB_USERNAME_SECOND', 'forge'),
'password' => env('DB_PASSWORD_SECOND', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
],
If You are use Eloquent Model
add this line
protected $connection = "mysql_second";
If You are using Database Facades
DB::connection('mysql_second')->table('table_name')->get();
In .env(This is in root folder) file change below credentials accordingly with external DB connection:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
and in config/database.php change the below credentials if trying to connect with external MySQL otherwise there are more options for others or please mention from which DB you want to connect with
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
1. Enable MYSQL Remote
First, you need to read your hosting docs to get your server data and instructions to enable mysql remote for your local IP by whitelisting your IP or if you want to allow access to anyone you can use wildcard %
in my case HostGator: https://www.hostgator.com/help/article/how-to-connect-to-the-mysql-database-remotely,
2. Change .env configuration
then fill .env file with the provided data by your hosting provider.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1 //replace with your external server IP
DB_PORT=3306 // check open port for mysql usually is 3306
DB_DATABASE=laravel // replace with the name of your external database
DB_USERNAME=root //replace with the username associated with database
DB_PASSWORD= //put your username password

SQL Connection refused with Laravel

I'm aware this can be a recurring question, but none of the solutions i found online seem to be working with my problem.
I have a Laravel 5.8 project but i can't access to my database from a controller, all artisan command works fine (like php artisan migrate)
But when a try a simple thing like:
protected function test()
{
return User::all();
}
I get SQLSTATE[HY000] [2002] Connection refused (SQL: select * from `users` where `users`.`deleted_at` is null)
I simply trying to access my local database (from OSX)
My Config :
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database
DB_USERNAME=root
DB_PASSWORD=secret
database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'database'),
'username' => env('DB_USERNAME', 'database'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
],
My database is working (i can access it with an IDE or else)
Thanks for your help !
it isn't the best answer, but create new database user with all privileges and use that user.

Laravel PHP - SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known

I keep getting these errors when I run the command php artisan migrate
In Connection.php line 664:
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_s
chema.tables where table_schema = homestead and table_name = migrations)
In Connector.php line 67:
SQLSTATE[HY000] [2002] Connection refused
My.env file contains this:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
My database.php file contains this:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
Any ideas what's wrong? I've been looking for help online but none of them have worked so far.
try alternative,
create new database in phpmyadmin or sqlyog or whichever you have.
set the credentials and database name as per that.
then fire composer update followed by composer dump-autoload and php artisan config:cache
and then check again if its working
I hope this will solve your problem.

Categories