Laravel - Issues trying to connect project to database - php

I'm trying to connect laravel to the database, but I'm having issues.
I'm getting these errors:
1 PDOException::("SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: NO)")
C:\Users\andre\Dropbox\College\Project\Rentable\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php : 68
2 PDO::__construct("mysql:host=localhost;port=3308;dbname=rentable", "root", "", [])
C:\Users\andre\Dropbox\College\Project\Rentable\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php : 68
This is my .env file
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3308
DB_DATABASE=rentable
DB_USERNAME=root
DB_PASSWORD=
This is a screenshot of the database on the localhost:

Most likely the default password for local installations is either root , secret or password, try those in your .env

Check your database parameters in config/database.php. You should have something like this
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'database_name'),
'username' => env('DB_USERNAME', 'database_username'),
'password' => env('DB_PASSWORD', 'database_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'),
]) : [],
],

Related

Laravel Error SQLSTATE[HY000] [2002] No such file or directory in Laravel 6

i have some issue about connection on laravel into database MySQL like this.
My laravel files and database has been published on hosting.
Illuminate\Database\QueryException.
SQLSTATE[HY000] [2002] No such file or directory (SQL: SELECT * FROM tbl_master_users WHERE identity_number = '12345' AND password ='password')
I tried to looking for some reference to solve this issue, but it's not working on me.
this is .env file : (I tried to change DB_HOST into 127.0.0.1 but it still same)
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=db_users
DB_USERNAME=uname
DB_PASSWORD=password
this is my configuration on database.php file :
'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' => false,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
Socket :

I cannot access my second database in Laravel (.env & database.php edited !)

I have a database which runs on local that I created with TablePlus. I can access it without any problem. I tested with Insomnia and I can get the info I need. I search how to connect to a second database that is live. I changed my .env file and my database.php to create another access to the second database. But I have an error message:
SQLSTATE[HY000][1045] Access denied for user 'root'#'host' (using passowrd: YES)
where host is the name I found in PHP MyAdmin in the variables tab.
How can I acccess the second DB ? What am I doing wrong ? Do I need to change the env('DATABASE_URL') for the second connection in database.php ?
.env:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=first_db
DB_USERNAME=root
DB_PASSWORD=root
DB_SECOND_CONNECTION=mysql2
DB_SECOND_HOST=host //(again, PHP MyAdmin -> variables tab)
DB_SECOND_PORT=3306
DB_SECOND_DATABASE=second_db
DB_SECOND_USERNAME=*username*
DB_SECOND_PASSWORD=*password*
database.php:
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'first_db'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'root'),
'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'),
]) : [],
],
'mysql2' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_SECOND_HOST', 'host'), //(again, PHP MyAdmin -> variables tab)
'port' => env('DB_SECOND_PORT', '3306'),
'database' => env('DB_SECOND_DATABASE', 'secondd_db'),
'username' => env('DB_SECOND_USERNAME', 'username'),
'password' => env('DB_SECOND_PASSWORD', 'password'),
'unix_socket' => env('DB_SECOND_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'),
]) : [],
],
],
As suggested by #Collin above, the root user I was using didn't have the permissions. Creating a new user with all privileges in PHP MyAdmin solved the issue.
If you want to check if the database connection is actually accesable you could try the following.
First clear your config using the following command:
php artisan config:clear
Then check if the connection is accesable using the following code:
try {
DB::connection('mysql2')->getPdo();
} catch (\Exception $e) {
die("Could not connect to the database. Please check your configuration. error:" . $e );
}
If that doesn't work check if the user has the right permissions.

I can't connect to mysql DB connection refused

I have tried to connect to the DB mysql using php artisan migrate command and gives me this error
Exception trace:
1 PDOException::("SQLSTATE[HY000] [2002] Connection refused")
/Users/hamad/Desktop/web/php/test/vendor/laravel/framework/srcIlluminate/Database/Connectors/Connector.php:70
2 PDO::__construct("mysql:host=127.0.0.1;port=3306;dbname=laravel", "root", "", [])
/Users/hamad/Desktop/web/php/test/vendor/laravel/framework/src/Illuminate/Database/Connectors/.Connector.php:70
my set up in the env file is
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 is
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'laravel'),
'username' => env('DB_USERNAME', 'root'),
'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'),
]) : [],
],
I have tried to use localhost instead of 127.0.0.1 but did not work
thanks for your help
I changed from xampp to mamp program and worked fine
I think it is from the localserver and the port number
xampp using 192.168.64.2 as localserver
mamp uses 127.0.0.1 as localserver

Database Access denied on Laravel in Production

I'm trying to publish my Laravel 5.8 app on production hosting. I've uploaded the files and have made the changes in .env file. But still while accessing my application., it throws error:
Access denied for user 'root'#'127.0.0.1' (using password: NO)
Below is my .env file:
DB_CONNECTION=mysql
DB_HOST= ip
DB_PORT=3306
DB_DATABASE=secret
DB_USERNAME='secret'
DB_PASSWORD='secret'
I have also made changes in database.php:
'users' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', 'secret'),
'port' => env('DB_PORT', '3306'),
'database' => env(‘DB_DATABASE', 'secret'),
'username' => env('DB_USERNAME', 'secret'),
'password' => env('DB_PASSNORD', 'secret'),
'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: :I'IYSQL_ATTR_SSL_CA => env( 'MYSQL_ATTR_SS L_CA' ),
]) : [],
},
Try.
php artisan config:cache
then
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=secret //enter your right databse name
DB_USERNAME=secret //please check your username
DB_PASSWORD=secret //please check your password
or
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=secret //enter your right databse name
DB_USERNAME=root //please check your username
DB_PASSWORD=enter your correct password //please check your password
change your databse.php
'users' => [
'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_PASSNORD', ''),
'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: :I'IYSQL_ATTR_SSL_CA => env( 'MYSQL_ATTR_SS L_CA' ),
]) : [],
},
As a comment suggest, use localhost instead of 127.0.0.1, but also, same error occurs to me and the problem was the Laravel project cache: just use php artisan config:cache to re-cache the files, or just delete all the files inside the bootstrap/cache folder

I can't create migration Laravel5 Access denied for user 'homestead'#'localhost' (using password: YES)

Terminal show me this message:
my env:
my database.php:
'default' => env('DB_CONNECTION', 'mysql'),
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'laravelup'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', '1'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
How can I solve this issue ?
Just change your .env file like this
DB_DATABASE=laravelup
DB_USERNAME=root
DB_PASSWORD= //blank here
And it will be working.
I maybe laravel configration cache problem.
If you type this command "php artisan config:clear" and try again, then it will be working.

Categories