执行必要步骤

时间:2016-06-22 09:04:35

标签: php mysql laravel-5.2 lumen-5.2

我已阅读并尝试从这些链接执行这些步骤,但我的第二个数据库仍然无法配置。 Lumen - Create database connection at runtime Lumen Database [xxx] not configured

我做错了什么?有没有我错过的东西?

这是我的代码:

.env文件

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=database1
DB_USERNAME=root
DB_PASSWORD=''

DB_CONNECTION=mysql2
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=database2
DB_USERNAME=root
DB_PASSWORD=''

在我的app.php中我添加了:     $ APP->配置('数据库&#39);

database.php中

'connections' => [

    'mysql' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST', 'localhost'),
        'port'      => env('DB_PORT', 3306),
        'database'  => env('DB_DATABASE', 'database1'),
        'username'  => env('DB_USERNAME', 'root'),
        'password'  => env('DB_PASSWORD', ''),
        'charset'   => env('DB_CHARSET', 'utf8'),
        'collation' => env('DB_COLLATION', 'utf8_unicode_ci'),
        'prefix'    => env('DB_PREFIX', ''),
        'timezone'  => env('DB_TIMEZONE', '+00:00'),
        'strict'    => env('DB_STRICT_MODE', false),
    ],

    'mysql2' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST', 'localhost'),
        'port'      => env('DB_PORT', 3306),
        'database'  => env('DB_DATABASE', 'database2'),
        'username'  => env('DB_USERNAME', 'root'),
        'password'  => env('DB_PASSWORD', ''),
        'charset'   => env('DB_CHARSET', 'utf8'),
        'collation' => env('DB_COLLATION', 'utf8_unicode_ci'),
        'prefix'    => env('DB_PREFIX', ''),
        'timezone'  => env('DB_TIMEZONE', '+00:00'),
        'strict'    => env('DB_STRICT_MODE', false),
    ],

这是我使用的数据库的示例连接:

$checkduplicate = DB::connection('database2')->table('authusers')->select('username')->where('username', $username)->get();

这是在我的routes.php

$app->get('database2/register', 'Database2_DashboardController@registerAdmin');

$ app-> post(' database2 / register',' Database2_DashboardController @ registerAdmin');

1 个答案:

答案 0 :(得分:2)

使用连接变量' mysql2' 而不是数据库名称' database2'

 $checkduplicate = DB::connection('mysql2')->table('authusers')->select('username')->where('username', $username)->get();