【问题标题】:Multiple laravel connection with mysql与mysql的多个laravel连接
【发布时间】:2017-03-21 08:31:56
【问题描述】:

我想用 MySQL 中的两个数据库做两个 Laravel 连接。在我的 database.php 文件中我添加了 mysql2 连接。

 <?php

 return [

/*
|--------------------------------------------------------------------------
| PDO Fetch Style
|--------------------------------------------------------------------------
|
| By default, database results will be returned as instances of the PHP
| stdClass object; however, you may desire to retrieve records in an
| array format for simplicity. Here you can tweak the fetch style.
|
*/

'fetch' => PDO::FETCH_CLASS,

/*
|--------------------------------------------------------------------------
| Default Database Connection Name
|--------------------------------------------------------------------------
|
| Here you may specify which of the database connections below you wish
| to use as your default connection for all database work. Of course
| you may use many connections at once using the Database library.
|
*/

'default' => env('DB_CONNECTION', 'mysql'),

/*
|--------------------------------------------------------------------------
| Database Connections
|--------------------------------------------------------------------------
|
| Here are each of the database connections setup for your application.
| Of course, examples of configuring each database platform that is
| supported by Laravel is shown below to make development simple.
|
|
| All database work in Laravel is done through the PHP PDO facilities
| so make sure you have the driver for your particular database of
| choice installed on your machine before you begin development.
|
*/

'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', 'db1'),
        'username' => env('DB_USERNAME', 'root'),
        'password' => env('DB_PASSWORD', 'password1'),
        'charset' => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix' => '',
        'strict' => false,
        'engine' => null,
    ],


    'mysql2' => [
        'driver' => 'mysql',
        'host' => 'localhost',
        'port' => '3306',
        'database' => 'db2',
        'username' => 'root',
        'password' => 'password2',
        'charset' => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix' => '',
        'strict' => false,
        'engine' => null,
    ],





    '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',
    ],

],

/*
|--------------------------------------------------------------------------
| Migration Repository Table
|--------------------------------------------------------------------------
|
| This table keeps track of all the migrations that have already run for
| your application. Using this information, we can determine which of
| the migrations on disk haven't actually been run in the database.
|
*/

'migrations' => 'migrations',

/*
|--------------------------------------------------------------------------
| Redis Databases
|--------------------------------------------------------------------------
|
| Redis is an open source, fast, and advanced key-value store that also
| provides a richer set of commands than a typical key-value systems
| such as APC or Memcached. Laravel makes it easy to dig right in.
|
*/

'redis' => [

    'cluster' => false,

    'default' => [
        'host' => env('REDIS_HOST', 'localhost'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', 6379),
        'database' => 0,
    ],

],

];

在我的路线文件中我添加了这条路线

Route::get('/', function()
{

$users =DB::connection('mysql2')->select('select * from users')->get();

return $users;

});

但是当我浏览得到查询结果时

QueryException in Connection.php line 729:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.users' doesn't exist

我需要有人帮助我。

【问题讨论】:

  • Laravel 版本?
  • laravel 版本 5
  • 你能显示整个database.php文件吗?
  • 你应该说完整版:) Laravel 连接从 5.2->5.3->5.4 发生了很大变化
  • 你确定mysql2上的用户名和密码正确吗?

标签: laravel laravel-5


【解决方案1】:
//use model
$user = new \App\User();
$user->setConnection('mysql2');
$users = $user->all();
return $users;

Laravel 5.3

【讨论】:

    【解决方案2】:

    database.php 文件的默认 env() 直接指向 .env 文件,所以找不到表。去掉 env(),就像上层一样。

    【讨论】:

    • 有时我可以在 .env 文件中建立两个 MySQL 连接吗?
    【解决方案3】:

    我将 routes.php 上的函数更改为以下内容,问题解决了 (删除函数)。

    Route::get('/', function()
    {
    
        $users =DB::connection('mysql2')->select('select * from users');
    
        return $users;
    
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-03
      • 1970-01-01
      • 2019-05-24
      • 2019-03-05
      • 1970-01-01
      • 2014-04-01
      • 1970-01-01
      相关资源
      最近更新 更多