-1

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,
],
HilAy Patel
  • 1
  • 1
  • 3
  • every time new user register i'm creating new db – HilAy Patel Jan 30 '19 at 04:45
  • For all users there will be different databases? – Pragna Jan 30 '19 at 05:02
  • 1 main db for auth and setup and 1 db par user – HilAy Patel Jan 30 '19 at 05:10
  • creating DB for each user is useless, even if you are doing it for security or any other purpose whatsoever. useful SO link https://stackoverflow.com/questions/16721772/mysql-performance-multiple-tables-vs-index-on-single-table-and-partitions#answer-16768830 your question is already answered here https://stackoverflow.com/questions/31847054/how-to-use-multiple-database-in-laravel#answer-31847198 – Hamza Rashid Jan 30 '19 at 05:22
  • this not useless, i have 10000+order/min and all user have it own custom product – HilAy Patel Jan 30 '19 at 05:26
  • please read up on that SO link https://stackoverflow.com/questions/16721772/mysql-performance-multiple-tables-vs-index-on-single-table-and-partitions#answer-16768830. it's not advisable to create multiple databases. but if you insist, then close one connection to database and connect to another database connection. but you need to have config in .env file as well as config/database.php https://stackoverflow.com/questions/31847054/how-to-use-multiple-database-in-laravel#answer-31847198 – Hamza Rashid Jan 30 '19 at 05:41

2 Answers2

0

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

Hamza Rashid
  • 1,329
  • 15
  • 22
  • https://stackoverflow.com/questions/31847054/how-to-use-multiple-database-in-laravel#answer-31847198 this not I'm looking for, in this answered he use Multiple Database Connections which already define but i don't have any connection or can say it is not possible to define connection in config file – HilAy Patel Jan 30 '19 at 05:32
0

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

//

});
sjayendra
  • 91
  • 3
  • 9