how to manage database in laravel - database

i want to use multiple databases, i have 1db/user and 1 main db,already set main db in config file but how to manage user database(db name = {user_id}_db). thanks in advance
this is my config code
'main' => [
'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,
],

You can use following configuration for multiple dbs.
<?php
return array(
'default' => 'mysql',
'connections' => array(
# primary
'mysql' => array(
'driver' => 'mysql',
'host' => 'ip1',
'database' => 'db1',
'username' => 'user',
'password' => 'pwd'
'charset' => 'utf8',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
),
# secondary db connection
'mysql2' => array(
'driver' => 'mysql',
'host' => 'dbip2',
'database' => 'db2',
'username' => 'user',
'password' => 'pwd'
'charset' => 'utf8',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null
),
'sqlite' => array(
'driver' => 'sqlite',
'database' => __DIR__.'/../database/db.sqlite',
'prefix' => '',
),
),
);
Then you can use the the connection() method:
$connection = ‘mysql2’;
Schema::connection($connection)->create(function(Blueprint $table) {
//
});

Creating DB for each user is useless, even if you are doing it for security or any other purpose whatsoever. Useful SO link MySQL performance: multiple tables vs. index on single table and partitions
Your question (connecting to multiple databases in Laravel) is already answered here. How to use multiple database in Laravel
UPDATE
I guess, you want to create new databases on the fly and connect to them (without using .env configuration). Create dynamic connections instead of specifying 'connection-name' when trying to connect to a new database. Then, using Config::set you can create new temporary connections and then use DB::connection with the temporary name you just created. read more https://lukevers.com/2015/03/25/on-the-fly-database-connections-with-laravel-5

Related

How to access remote database from laravel 4.2

I am currently do some development in a early developed system developed from laravel 4.2.
I run it in my local machine. At the same time now I have to access to a another System's database.
For testing purposes i have created that db in my local server. I don't have direct access to that db in real situation. Only i have a view. So please tell me how should i configure my system to achieve that task.
First you have to add a separate database connection to your other system's database, so in your app/config/database.php you can have this:
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'forge',
'username' => 'forge',
'password' => '123456',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'mysql2' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'forge2',
'username' => 'forge2',
'password' => '123456',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
In order to perform query to the other database you have to do this:
$users = DB::connection('mysql2')->select('select * from users where id = ?', array(1));
You may see this code in the Laravel's documentation: https://laravel.com/docs/4.2/database#accessing-connections

Laravel change database connection [duplicate]

This question already has answers here:
How to use multiple databases in Laravel
(7 answers)
Laravel Change Connection Dynamically
(2 answers)
Closed 5 years ago.
How can a database connection be changed when user is admin role? Admin database contains information that must not be accessible to other user's and requirement also is that database must be located on own server. I do have currently separate configuration for admin connection.
'mysql' => [
'host' => 'admin.psa44.localhost'
'driver' => 'mysql',
'database' => 'database',
'username' => 'root',
'password' => 'secretpassword',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
],
I want that no hardcoding is in application so how can Laravel do the switch when necessary? Thanks for any help.
You need to create new database config rule in appconfig.
'mysql' => [
'host' => 'admin.psa44.localhost'
'driver' => 'mysql',
'database' => 'database',
'username' => 'root',
'password' => 'secretpassword',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
],
'mysql_admin' => [
'host' => 'admin.psa44.localhost'
'driver' => 'mysql',
'database' => 'admindatabase',
'username' => 'root',
'password' => 'secretpassword1',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
],
I named mysql_admin. In migration when use
Schema::create('some_table', function($table)
to create table ,use
Schema::connection('mysql_admin')->create('some_table', function($table)
to create that table in other database.
For query to that database do something like this
$admins = DB::connection('mysql_admin')->select(...);
And you can to define connection for model
class AdminModel extends Eloquent {
protected $connection = 'mysql_admin';
}

When setting Laravel's DB, try to dynamically generate it

The names of DB are board 1, board 2, board 3, board 4 to board 50. It takes time to create multiple connections each time, and I will have to do manual work every time I add it.
There is no solution for getting an error if you like the existing method in database.php?
for($board_no=1;$board_no<=50;$board_no++){
'board'.$board_no => [
'driver' => 'mysql',
'host' => 'boardip',
'port' => '3306',
'database' => 'board'.$board_no,
'username' => 'boardadmin',
'password' => 'boardadminpassword',
'unix_socket' => '',
'charset' => 'utf8',
'collation' => 'utf8_general_ci',
'prefix' => null,
'engine' => null
],
}
To create a config on the fly:
Do this in your controller, it would just be appended to "database.connections" config.
Config::set('database.connections.key', array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'dbname',
'username' => 'dbuser',
'password' => 'dbpass',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
));
Then you can connect to the db using:
DB::connection('key');

how to connect to multiple databases in drupal in order to display the content in a table

i am using drupal in order to create a website on my localhost machine.
i have a mysql database that i want to connect it to the drupal website and display its data in a table.
this is what i did in settings.php file
//connect to external database
$databases['sitesdb']['default'] = array (
'database' => 'sitesdb',
'username' => 'xxxxxxx',
'password' => 'xxxxxxxx',
'prefix' => '',
'host' => 'localhost',
'port' => '3306',
'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
'driver' => 'mysql',
);
and in the drupal i download the Views Database Connector (VDC) module in order to connect to the external database.
but it did not work .
second method i used views and created content types that are the fields table of the external databases.
but i do not know how to continue to display the data.
I was using migrate module and multiple DB configuration looked like:
$databases = array (
'default' =>
array (
'default' =>
array (
'database' => 'main_db',
'username' => 'XXX',
'password' => 'XXX',
'host' => 'localhost',
'port' => '',
'driver' => 'mysql',
'prefix' => '',
),
),
'legacy' =>
array (
'default' =>
array (
'database' => 'additional_db',
'username' => 'XXX',
'password' => 'XXX',
'host' => 'XXX',
'port' => '',
'driver' => 'mysql',
'prefix' => '',
),
),
);

DB::reconnect() in Schema Facade?

I am working on a multiple database system. System will integrate clients local mssql server.
I will store credential info in my master database. And i want to migrate some tables to my customers local database.
As you know we can choose connection as below.
Schema::connection('sqlsrv')->create('products', function (Blueprint $table) {
});
But I have to use it dynamically.
This connection reads from config/database.php
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
],
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('SQLSRV_HOST', 'localhost'),
'database' => env('SQLSRV_DATABASE', 'forge'),
'username' => env('SQLSRV_USERNAME', 'forge'),
'password' => env('SQLSRV_PASSWORD', ''),
'collation' => 'Turkish_CI_AS',
'charset' => 'utf8',
'prefix' => env('SQLSRV_PREFIX', ''),
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
],
],
I did it in using DB Facade.
public function getConnection($connection_id)
{
/** Bağlantı Bilgilerini Çeker */
$connection = Connection::find($connection_id);
try
{
/** Bağlantı Ayarları */
config()->set('database.connections.sqlsrv.host', $connection->host);
config()->set('database.connections.sqlsrv.database', $connection->db);
config()->set('database.connections.sqlsrv.username', $connection->username);
config()->set('database.connections.sqlsrv.password', $connection->password);
config()->set('database.connections.sqlsrv.charset', $connection->charset);
config()->set('database.connections.sqlsrv.collation', $connection->collation);
config()->set('database.connections.sqlsrv.prefix', $connection->prefix);
/** Ayarlar ile yeniden bağlanma */
return DB::reconnect($connection->driver);
}catch (\Exception $e)
{
throw new \Exception($e);
}
}
Is there any method or solution like DB::reconnect() in Schema Facade ?
Here's what I ended up doing. Hope it helps your case.
// Erase the tenant connection, thus making Laravel
// get the default values all over again.
DB::purge('tenant');
// Make sure to use the database name we want to establish a connection.
Config::set('database.connections.tenant.host', $company->database->main_server);
Config::set('database.connections.tenant.database', $company->mysql_database);
Config::set('database.connections.tenant.username', $company->mysql_username);
Config::set('database.connections.tenant.password', $company->mysql_password);
// Rearrange the connection data
DB::reconnect('tenant');
// Ping the database. This will thrown an exception in case
// the database does not exists.
Schema::connection('tenant')->getConnection()->reconnect();
I noticed that if you do not DB::purge, Schema will not be able to pick up any connection change.
On the other hand, if you do purge, but the database doesn't exist, you won't get an exception until you actually try to do something with the database connection. The reconnect() in the last line will trigger that.

Resources