Not able to set multiple connections in laravel - php

I want to set multiple connections in Laravel application.
I tried using database.php is:
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'reports_db'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'pass'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
'mysql1' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'a2z'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'root'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
],
and .env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=reports_db
DB_USERNAME=root
DB_PASSWORD=pass
CL_DB_CONNECTION=mysql1
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=a2z
DB_USERNAME=root
DB_PASSWORD=root
Inside controller
public function index()
{
$ad= new Ad;
$ad->setConnection('mysql1');
$ad = ad::get();
echo $job;
}
But I am not able to set up mysql1 connection.Can you please tell me how to achieve this?

you are actually using the same connection parameters both the time as you are using the same env values in your database.php:
Your .env file should contain something like this:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=reports_db
DB_USERNAME=root
DB_PASSWORD=pass
TWO_DB_CONNECTION=mysql1
TWO_DB_HOST=127.0.0.1
TWO_DB_PORT=3306
TWO_DB_DATABASE=a2z
TWO_DB_USERNAME=root
TWO_DB_PASSWORD=root
and database.php should be something like this:
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'reports_db'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'pass'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
'mysql1' => [
'driver' => 'mysql',
'host' => env('TWO_DB_HOST', 'localhost'),
'port' => env('TWO_DB_PORT', '3306'),
'database' => env('TWO_DB_DATABASE', 'a2z'),
'username' => env('TWO_DB_USERNAME', 'root'),
'password' => env('TWO_DB_PASSWORD', 'root'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
],

Related

can connect my laravel instance to rds in aws

I can not connect my rds to my instance it is a laravel project need to know what i am doing wrong i can not connect my rds
This is my .env file code
DB_CONNECTION=mysql
DB_HOST=scanvision.cp5ylt0elhnp.us-west-2.rds.amazonaws.com
DB_PORT=3306
DB_DATABASE=scanvision
DB_USERNAME=scanvision
DB_PASSWORD=password
This is database.php file code
'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' => true,
'engine' => null,
'options' => array(
PDO::MYSQL_ATTR_SSL_CA => $cert_base . '/rds-combined-ca-bundle.pem'
),
],
can any one guide me through this i have searched alot but still i am not able to connect

Laravel connect to sqlite: No connection could be made because the target machine actively refused it

I'am trying to connect a Laravel API to a sqlite file. But when i try to access a controller- method this error is given:
No connection could be made because the target machine actively refused it.
Here is my .env file:
APP_NAME=Laravel
APP_ENV=local
APP_KEY=xxxx
APP_DEBUG=true
APP_URL=xxxx
LOG_CHANNEL=stack
DB_CONNECTION=sqlite
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
And my config\database.php:
'default' => env('DB_CONNECTION', 'sqlite'),
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => database_path('my-database.db'),
'prefix' => '',
],
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'my-database'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
'sslmode' => 'prefer',
],
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
],
],
Does anyone have a clue what is wrong in my configuration?
Thanks in advance!

databse.php is not working in laravel

I am using laravel 5.4. I have configured my config\database.php for mysql but its not inserting the records into mysql. Please see below and please give the solutions for this. why it is not inserting. when I try to insert no response.
<?php
return [
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
],
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST', 'localhost'),
'port' => 27017,
'database' => env('DB_DATABASE', 'userr'),
//'username' => env('DB_USERNAME', ''),
//'password' => env('DB_PASSWORD', ''),
],
'couchdb' => array(
'driver' => 'couchdb',
'type' => 'socket',
'host' => 'localhost',
'ip' => null,
'port' => 5984,
'dbname' => 'userr',
//'user' => 'username',
//'password' => 'password',
'logging' => false,
),
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'insurance'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
'sslmode' => 'prefer',
],
],
?>
here is my .env file
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=insurance
DB_USERNAME=root
DB_PASSWORD=
DB_PORT_MONGO=27017
my controller code
public function insertlogin()
{
//$user = DB::connection('mongodb')->collection('users')->get();
$id = DB::table('login')->insert([
['username' => 'rajeshhhhh', 'password' => 'seven']
]);
return $id;
}
If I try manual connection then it will insert. below manual connection code
public function insertlogin()
{
//$user = DB::connection('mongodb')->collection('users')->get();
$id = DB::connection('mysql')->table('login')->insert([
['username' => 'rajeshhhhh', 'password' => 'seven']
]);
return $id;
}
If you have a Model to you table login, then you can use this method:
Login::create(['username' => 'set_your_username_here', 'password' => bcrypt('set_your_password_here')]);

Laravel 5 Connection refused

I am new to Laravel and I can't seem to connect to the database. Here's my configuration
Mac OSX El Capitan
MAMP Pro
I always get the error SQLSTATE[HY000] [2002] Connection refused.
I have my_users table in the database application. By the way I am following this tutorial in youtube. Link here
.env
APP_ENV=local
APP_DEBUG=true
APP_KEY=LVwc8N06OepmCfptIcATzNxN2pkYJJwf
DB_HOST=localhost
DB_DATABASE=application
DB_USERNAME=root
DB_PASSWORD=root
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
REDIS_HOST=localhost
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
my_users.php (Model)
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class my_users extends Model
{
//
}
routes.php
<?php
use App\my_users;
Route::get('/', function () {
$users = my_users::all();
});
database.php
return [
'fetch' => PDO::FETCH_CLASS,
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => database_path('database.sqlite'),
'prefix' => '',
],
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
],
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
],
],
'migrations' => 'migrations',
'redis' => [
'cluster' => false,
'default' => [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
],
];

Laravel Access denied

I get following error in my laravel application. ( I am using Laravel 5.1)
PDOException in Connector.php line 55:
SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: NO)
at the details it says that I do not parse any password:
array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'gen', 'username' => 'root', 'password' => '', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'name' => 'mysql'), array('0', '2', '0', false, false)) in MySqlConnector.php line 22
My .env file looks like this in Laravel:
APP_ENV=local
APP_DEBUG=true
APP_KEY=************************************
DB_HOST=localhost
DB_DATABASE=gen
DB_USERNAME=miad2
DB_PASSWORD=***************
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
I use phpmyadmin and created that user there.
database.php looks like this:
<?php
return [
'fetch' => PDO::FETCH_CLASS,
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => storage_path('database.sqlite'),
'prefix' => '',
],
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
],
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
],
],
'migrations' => 'migrations',
'redis' => [
'cluster' => false,
'default' => [
'host' => '127.0.0.1',
'port' => 6379,
'database' => 0,
],
],
];
I stuck here for some hours and do not know how to fix this, need some help.
I figured it out, so what I did now was changing the server name which was set to root and the password which was empty in the config.inc.php to that user i created. And Voila it worked.

Categories