How to divide read/write database setting in Laravel? - php

This is recommended regulation for database divide setting in Laravel.
'mysql' => [
'read' => [
'host' => '192.168.1.1',
],
'write' => [
'host' => '196.168.1.2'
],
'sticky' => true,
'driver' => 'mysql',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
],
But if I want to use different access information as DB_NAME, USER_ID, PASS etc to each read/write databases, then how could I make it? Thank you.

You can create two connections then specify the connection with Eloquent using the on method:
'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,
],
'mysql2' => [
'driver' => 'mysql',
'host' => env('DB_HOST2', '127.0.0.1'),
'port' => env('DB_PORT2', '3306'),
'database' => env('DB_DATABASE2', 'forge'),
'username' => env('DB_USERNAME2', 'forge'),
'password' => env('DB_PASSWORD2', ''),
'unix_socket' => env('DB_SOCKET2', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
User::on('mysql2')->where('id', $id)->update($data);
Or using Query Builder:
DB::connection('mysql2')->table('users')->where('id', $id)->update($data);

You can declare another db conecction like this:
'mysql' => [
'read' => [
'host' => '192.168.1.1',
],
'write' => [
'host' => '192.168.1.1'
],
'sticky' => true,
'driver' => 'mysql',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
],
'writecon' => [
'read' => [
'host' => '196.168.1.2',
],
'write' => [
'host' => '196.168.1.2'
],
'driver' => 'mysql',
'port' => env('DB2_PORT', '3306'),
'database' => env('DB2_DATABASE', 'db2'),
'username' => env('DB2_USERNAME', 'somename'),
'password' => env('DB2_PASSWORD', 'somepass'),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
],
Then in your .env add:
DB2_PORT=3306
DB2_DATABASE=db2
DB2_USERNAME=somename
DB2_PASSWORD=somepass
And you can use it like this:
$someModel->setConnection('writecon');
$someModel->save();
not tested but you can try it.

Related

Problems specifying Laravel Eloquent database at runtime

Laravel 5.8. I am trying to save an Eloquent model, choosing the database connection at runtime:
$catModel = new Cat;
if (env('USE_STAGING')) {
$catModel->setConnection('staging');
}
$cat = $catModel::create(['name' => $request->name]);
My config/database.php file:
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'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,
],
'staging' => [
'driver' => 'mysql',
'host' => env('STAGING_DB_HOST', '127.0.0.1'),
'port' => env('STAGING_DB_PORT', '3306'),
'database' => env('STAGING_DB_DATABASE', 'forge'),
'username' => env('STAGING_DB_USERNAME', 'forge'),
'password' => env('STAGING_DB_PASSWORD', ''),
'unix_socket' => env('STAGING_DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
],
My problem is that each time I run this code, the cat is created in the mysql (default) database, rather than the staging database, even when USE_STAGING is true.
The problem is the static call of the create() method.
By changing $catModel::create() to $catModel->create() it should use the correct connection:
$catModel = new Cat;
if (env('USE_STAGING')) {
$catModel->setConnection('staging');
}
$cat = $catModel->create(['name' => $request->name]);

Sub-Domain Routing over rides and runs the last sub domain in web.php

I need to change the database configuration according to the subdomain that hits the URL. But now, all the subdomains will be overridden by the last subdomain.
I created a group of routes with subdomain and configured DB using Config::set function. This works, but always takes the last subdomain group.
Route::domain('s1.xyz.com')->group(function () {
\Config::set("database.connections.mysql", [
'driver' => 'mysql',
'host' => 'localhost',
'port' => env('DB_PORT', 'xxxx'),
'database' => 'xx',
'username' => 'xxx',
'password' => 'xxxx',
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null
]);
Route::resource('/', 'LoginController');
});
Route::domain('s2.xyz.com')->group(function () {
\Config::set("database.connections.mysql", [
'driver' => 'mysql',
'host' => 'localhost',
'port' => env('DB_PORT', 'yyyy'),
'database' => 'yy',
'username' => 'yyy',
'password' => 'yyyy',
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null
]);
Route::resource('/', 'LoginController');
});
When s1.xyz.com is given, xx database should be used and the URL should be s1.xyz.com only.
But now though I give s1.xyz.com, once I hit submit button in the login form, it redirects to s2.xyz.com
$domain = substr (Request::root(), 7);
switch($domain){
case 's1.xy.com':
Route::domain('s1.xyz.com')->group(function () {
\Config::set("database.connections.mysql", [
'driver' => 'mysql',
'host' => 'localhost',
'port' => env('DB_PORT', 'xxxx'),
'database' => 'xx',
'username' => 'xxx',
'password' => 'xxxx',
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null
]);
Route::resource('/', 'LoginController');
});
break;
case 's2.xyz.com'
Route::domain('s2.xyz.com')->group(function () {
\Config::set("database.connections.mysql", [
'driver' => 'mysql',
'host' => 'localhost',
'port' => env('DB_PORT', 'yyyy'),
'database' => 'yy',
'username' => 'yyy',
'password' => 'yyyy',
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null
]);
Route::resource('/', 'LoginController');
});
break;}

SQLSTATE[HY000]: [Microsoft][ODBC Driver 11 for SQL Server]TDS Protocol error

I tried to connect to a new bbdd from laravel giving me the error that appears in the title
I leave below the configuration file of bbdd (database.php) and as we call it from the controller
'reporting' => [
'driver' => 'mysql',
'host' => env('DB_REPORTING_HOST', 'localhost'),
'port' => env('DB_REPORTING_PORT', '3306'),
'database' => env('DB_REPORTING_DATABASE', 'reporting'),
'username' => env('DB_REPORTING_USERNAME', 'paco'),
'password' => env('DB_REPORTING_PASSWORD', 'qwerty'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
],
and the call
$DB = DB::connection('reporting');
$DB->table('clients')->get();
fix it
'reporting' => [
'driver' => 'mysql',
'host' => '127.0.0.1',
'port' => '3306',
'database' =>'reporting',
'username' => 'paco',
'password' => 'qwerty',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
And in the controller
$dataL = DB::connection('reporting');//->select('select * from leyenda');
$dataL2 = $dataL->table('familias')->get()

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 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