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

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.

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 :

"Access denied for user, using password: yes" Laravel 8 Heroku

Here is the config/database.php file that I modified using this tutorial. It says that I need to attach the $DATABASE_URL=parse_url("mysql://####"); code block above the said php page. (the tutorial is using postgreSQL, while my system needs to use MySQL).
// attached $DATABASE_URL=parse_url("mysql://####") on the topmost part of the page
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => $DATABASE_URL['host'], // edited this line
'port' => $DATABASE_URL['port'], // edited this line
'database' => ltrim($DATABASE_URL['path'], '/'), // edited this line
'username' => $DATABASE_URL['user'], // edited this line
'password' => $DATABASE_URL['pass'], // edited this line
'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'),
]) : [],
],
below is the original mysql code
// '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'),
// ]) : [],
// ],
error ouput when running the system with the said problem
SQLSTATE[HY000] [1045] Access denied for user '{{redacted username}}'#'{{redacted ip address}}' (using password: YES) (SQL: select * from video_links where home_video = true and video_links.deleted_at is null limit 1)
Another problem I encounter while running the migration command in the Heroku CLI is this:
PHP Warning: Undefined array key "port" in /app/config/database.php on line 51
It seems like the $DATABASE_URL=parse_url("mysql://####") is not fetching the port value in its array.
How should I approach this?
You can leave config/database.php unchanged.
And add an ENV var DATABASE_URL:
In the settings or via CLI:
heroku config:set DATABASE_URL=YOUR_URL

Laravel 6 - 2nd Database configured in database.php but still received Database connection [] not configured

I want to create a second database to act as a backup. I've been following another SO question solution and other websites which leads to the same thing. I've added a new DB connection in .env and configured it in database.php, cleared out the cache using php artisan config:cache. When I try to migrate using php artisan migrate --database=backup_literature_review_management, it says InvalidArgumentException, Database connection [backup_literature_review_management] not configured.
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=literature_review_management
DB_USERNAME=root
DB_PASSWORD=
DB_CONNECTION_BACKUP=mysql
DB_HOST_BACKUP=127.0.0.1
DB_PORT_BACKUP=3306
DB_DATABASE_BACKUP=backup_literature_review_management
DB_USERNAME=root
DB_PASSWORD=
database.php
'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', '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'),
]) : [],
],
'mysql2' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST_BACKUP', '127.0.0.1'),
'port' => env('DB_PORT_BACKUP', '3306'),
'database' => env('DB_DATABASE_BACKUP', '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'),
]) : [],
],
],
Cleared cache before migrating
php artisan config:cache
Trying to migrate to 2nd DB
php artisan migrate --database="backup_literature_review_management"
Error
InvalidArgumentException, Database connection [backup_literature_review_management] not configured.
You are calling the wrong connection. Use this:
php artisan migrate --database="mysql2"
You can also define the connection inside the migration file like:
Schema::connection('mysql2')->create('table', function (Blueprint $table) {...
With this last command, you can ommit --database="mysql2 in the migration command. I personally prefer to declare the connection inside the migration file.
Hope it helped!

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

Laravel - Issues trying to connect project to database

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'),
]) : [],
],

Categories