I have tried db connection by add pem cert files and pdo_mysql setup. But always getting below error.
SQLSTATE[HY000] [2002] Connection refused
Laravel config/database.php configured like below.
'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' => '',
'sslmode' => env('DB_SSLMODE', 'prefer'),
'options' => array(
PDO::MYSQL_ATTR_SSL_CA => 'certs/ca-cert.pem',
PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false
),
'strict' => true,
'engine' => null,
],
Related
I have the following problem, when I configure my database in the .env file and carry out the migrations, I see that they are carried out but in a different DB than the one I have configured, and the truth is that I do not know why, I attach images.
mi archivo .env, apunta mi base de datos prueba2:
enter image description here
but the migration is done to my other database bdperformlead:
enter image description here
check some configuration
config > database.php
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'read' => [
'host' => [
env('SLAVE_DB_HOST', '127.0.0.1'),
],
'port' => env('SLAVE_DB_PORT', '3306'),
'database' => env('SLAVE_DB_DATABASE', 'forge'),
'username' => env('SLAVE_DB_USERNAME', 'forge'),
'password' => env('SLAVE_DB_PASSWORD', ''),
],
'write' => [
'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' => 'utf8',
'collation' => 'utf8_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'),
]) : [],
],
if database name get using .env, check .env properly and run
php artisan optimize:clear
I'm working with laravel on Manjaro and I installed php and Mariadb.
in my laravel project when it wants to connect to database it show me "Illuminate\Database\QueryException
could not find driver (SQL: select * from users where email = sample.co#example.com limit 1) ";
database config:
'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'),
]) : [],
],
My env file:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=farsifor_m
DB_USERNAME=root
DB_PASSWORD=mypassword
I replaced php8 by php 7.4 . It solved the issue.
I change the configuration to connect database MySQL server workbench, I have entered host, username, and password in .env but I have error SQLSTATE[HY000] [1045] Access denied for user.
But I have entered in config/database.php connection success.
My .env file contains:
DB_CONNECTION=mysql
DB_HOST=0.0.0.0
DB_PORT=3306
DB_DATABASE=xxxx
DB_USERNAME=xxxx
DB_PASSWORD=xxxxxx
My config/database.php is :
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', 'localhost'),
'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'),
]) : [],
],
in dabase.php I delete env and entered connection to this code connection success
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', 'localhost'),
'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'),
]) : [],
],
in dabase.php I delete env and entered connection to this code connection success
I try to make 2 connections to a database in laravel. I always get:
error database [] not configured.
Pls help me master.
This is my controller:
class DashboardController extends Controller
{
public function index(request $request)
{
$tes = DB::connection('mysql2')->select('SELECT * from hari');
// $tes = tes::find(1);
dd($tes);
}
---env--
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=crud55
DB_USERNAME=root
DB_PASSWORD=
DB_DATABASE_2=db_susantokun
DB_USERNAME_2=root
DB_PASSWORD_2=
------------database.php
'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', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE_2', 'db_susantokun'),
'username' => env('DB_USERNAME_2', 'root'),
'password' => env('DB_PASSWORD_2', ''),
'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'),
]) : [],
],
This error occurs when calling http://localhost:8000/dashboard
this is InvalidArgumentException
Database [mysql2] not configured.
how to fix it?
You may have to rebuild the configuration cache. Run the following command in your laravel root directory:
php artisan config:cache
after chnage .env file you should restart php artisan serve service
I was unable to use other database table in my laravel project.
I did R & D to find the answer and face difficulty!
After resolving I thought to put it on stackoverflow!
Open Config/database.php
Add
'options' => [ \PDO::ATTR_EMULATE_PREPARES => true ],
in mysql section.
Resultant will be like this
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'DB'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
'options' => [ \PDO::ATTR_EMULATE_PREPARES => true ],
],
Enjoy using other database table in join!