How to use orchestral/tenanti in Laravel 5 to build a multi tenant application with multiple databases?

前端 未结 2 1478
不知归路
不知归路 2021-02-04 08:19

I am trying to build and application using Laravel 5. It is supposed to be a multi tenant database architecture using multiple databases. My employer requires this for security

2条回答
  •  深忆病人
    2021-02-04 08:49

    I've never used this package, but using the code you submitted above here's what I think is probably close to the right solution. You will probably still need to play with some of these values to get them correct:

    Migration Paths

    Since you're using the multi-database configuration, I believe you should be able to keep your migrations in the normal location, i.e. database/migrations. Tenanti will then create an exact replica of the database for each tenant in a different database. However, when you run php artisan tenanti:install user it might actually create a folder under database/ that indicates where you should put your migrations.

    What is a "driver"?

    The driver describes whether Tenanti will use a single or multiple databases, what models to use for determining different tenants, and where to store migrations. It is what you identified in the Tenanti config file you used above.

    Database Connection Selection

    You need to update config/database.php as follows. In a normal Laravel app, you would have the DB connection setup as follows:

     PDO::FETCH_CLASS,
            'default' => env('DB_CONNECTION', 'mysql'),
            'connections' => [
                'sqlite' => [ ...DB connection info... ],
                'mysql'  => [ ...DB connection info... ],
                'pgsql'  => [ ...DB connection info... ],
                'sqlsrv' => [ ...DB connection info... ],
            ],
            'migrations' => 'migrations',
            'redis' => [ ... ],
        ];
    

    However, in the case of Tenanti multi-database setup, you need to add in different connection info for each tenant's database. To do this you would add a new level to your database.php config file (this example assumes you're using mysql, but you could use any DB, or even different database engines for different tenants):

     PDO::FETCH_CLASS,
            'default' => env('DB_CONNECTION', 'mysql'),
            'connections' => [
                'tenants' => [
                    'user_1' => [
                        'driver'    => 'mysql',
                        'host'      => 'dbhost',     // for user with id=1
                        'database'  => 'dbname',     // for user with id=1
                        'username'  => 'dbusername', // for user with id=1
                        'password'  => 'dbpassword', // for user with id=1
                        'charset'   => 'utf8',
                        'collation' => 'utf8_unicode_ci',
                        'prefix'    => '',
                        'strict'    => false,
                    ],
                    'user_2' => [
                        'driver'    => 'mysql',
                        'host'      => 'dbhost',     // for user with id=2
                        'database'  => 'dbname',     // for user with id=2
                        'username'  => 'dbusername', // for user with id=2
                        'password'  => 'dbpassword', // for user with id=2
                        'charset'   => 'utf8',
                        'collation' => 'utf8_unicode_ci',
                        'prefix'    => '',
                        'strict'    => false,
                    ],
               ],
            ],
            'migrations' => 'migrations',
            'redis' => [ ... ],
        ];
    

    As you can see, each tenant has its own database instance that can be located on a different host and have a different username/password. Tenanti needs to be told how to figure out which database to use. This is what the documentation on Database Connection Resolver describes. In their example, they've named their tenant databases using acme_{$user->id} whereas in my example above I used user_{$user->id}.

    Like I said, I've never actually set this up myself, but these are my best guesses based on the docs, and having used other packages by this same developer. Hope this helps!

提交回复
热议问题