简体   繁体   English

将数据库设置存储在 .ENV 或主数据库中。 对于具有多个数据库的 laravel,每个用户一个数据库

[英]Store database settings on .ENV or in a master database. For laravel with multiple dbs, one database per user

We're starting a Laravel API from scratch where each user has his own MYSQL server (Amazon RDS instance).我们从头开始创建 Laravel API,每个用户都有自己的 MYSQL 服务器(Amazon RDS 实例)。

The user's databases list will be less than 100 users, so less than 100 databases instances.用户的数据库列表将少于 100 个用户,因此少于 100 个数据库实例。

We're discussing with my partner because he's storing each server settings in the.env file (APPROACH_1), he has a script that appends the DB settings for each new user, he stores the hostname, user, password... in the.env.我们正在与我的合作伙伴讨论,因为他将每个服务器设置存储在 .env 文件(APPROACH_1)中,他有一个脚本为每个新用户附加数据库设置,他将主机名、用户、密码......存储在.env 文件中。环境。

Like this:像这样:

DB_CONNECTION=master_db
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=master_db
DB_USERNAME=master_db_usr
DB_PASSWORD=master_db_pwd

DB_CONNECTION=user_1
DB_HOST="user_1.us-east-2.rds.amazonaws.com"
DB_PORT=3306
DB_DATABASE=user_1_db
DB_USERNAME=user_1_usr
DB_PASSWORD=user_1_pwd

DB_CONNECTION=user_2
DB_HOST="user_2.us-east-2.rds.amazonaws.com"
DB_PORT=3306
DB_DATABASE=user_2_db
DB_USERNAME=user_2_usr
DB_PASSWORD=user_2_pwd
...

For me, having the settings in the.env is ugly and hard to maintain, I'm suggesting to him to keep the master DB settings in the env.对我来说,在 .env 中设置很难维护,我建议他将主数据库设置保留在 env 中。 But store the user's databases settings in the master database (APPROACH_2):但是将用户的数据库设置存储在主数据库(APPROACH_2)中:

在此处输入图像描述

But he says that I'm breaking the laravel framework because laravel database config file app/config/database.php cannot look for database settings directly on the database, the file expects the settings from the.env file但他说我打破了 laravel 框架,因为 laravel 数据库配置文件 app/config/database.php 无法直接从文件数据库中查找设置文件,

Is my partner right and we must keep the user's databases settings in the env file??我的合作伙伴是否正确,我们必须将用户的数据库设置保留在 env 文件中?

Thanks!谢谢!

I have a similar approach on a multi-tenant app, and I store data on the database.我在多租户应用程序上有类似的方法,并将数据存储在数据库中。 Then I have a middleware based on the subdomain that sets up the database configs.然后我有一个基于设置数据库配置的子域的中间件。

You can use this as an example:您可以以此为例:

<?php
namespace App\Http\Middleware;

use Closure;
use App\Models\Tenant;
use Illuminate\Http\Request;

class SubdomainSetup
{

    public function handle(Request $request, Closure $next)
    {
        $subdomain = $request->server()['HTTP_HOST'];

        $tenant = Tenant::where('subdominio', $subdomain)->where('habilitado', 1)->first();

        if (!$tenant) {
            dd('Invalid subdomain.');
        }

        config([
            'database.connections.mysql.tenantid' => $tenant->tenantid,
            'database.connections.mysql.host'     => $tenant->host,
            'database.connections.mysql.database' => $tenant->database,
            'database.connections.mysql.username' => $tenant->username,
            'database.connections.mysql.password' => $tenant->password,
        ]);

        return $next($request);
    }
}

This code changes the config file dynamically so everything is transparent on the Laravel side.此代码动态更改配置文件,因此 Laravel 端的所有内容都是透明的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM