如何为同一查询连接到laravel中的多个数据库?

时间:2017-02-11 07:52:36

标签: php mysql laravel

我想执行一个与laravel中所有数据库相关的原始查询。所以我不想指向一个特定的数据库。我怎么能在laravel中做到这一点?

这是我的代码:

$sql = 'SELECT table_schema \"DB_Name\",
                Round(Sum(data_length + index_length) / 1024 / 1024, 2) \"DB_Size_in_MB\"
        FROM  information_schema.tables
        GROUP BY table_schema';
$dbs_size = DB::statement($sql);

如您所见,我的查询未指向任何特定数据库。所以我想我不应该使用DB::。我怎么能这样做?

注意到我当前的代码抛出了这个糟糕的错误:(仅此而已)

  

糟糕,看起来出了问题。

2 个答案:

答案 0 :(得分:2)

首先,你过于宽松地使用双引号,这在MySQL查询中并不应该发生。

其次,根据https://laravel.com/docs/5.4/database#running-queriesstatement不返回任何内容。

您可以使用多种选项来使用多个数据库:

1)原始选择

$sql = 'SELECT table_schema DB_Name,
            Round(Sum(data_length + index_length) / 1024 / 1024, 2) DB_Size_in_MB
    FROM  information_schema.tables
    GROUP BY table_schema';
$dbs_size = DB::select($sql);

2)通过在config/database.php

中添加条目,将laravel配置为使用该数据库
//other database configs

"information_schema" => [
         'driver'    => 'mysql',
         'database'  => 'information_schema',
         'username'  => 'user',
         'password'  => 'pass',
         'charset'   => 'utf8',
         'collation' => 'utf8_unicode_ci',
         'prefix'    => ''
]

然后通过以下方式查询:

 $dbs_size = DB::connection("information_schema")->table("tables")->groupBy("table_schema")->select(DB::raw("...")); // rest of the query..

答案 1 :(得分:1)

.env文件。

DB_HOST=192.168.1.211
DB_DATABASE=sonirocks_dgfbh
DB_USERNAME=sonirocks_dgfbh
DB_PASSWORD=sonirocks_dgfbh

DB_HOST2=192.168.1.211
DB_DATABASE2=sonirocks_dgfbh_log
DB_USERNAME2=sonirocks_dgfbh
DB_PASSWORD2=sonirocks_dgfbh

<强>配置/ database.php中

'connections' => [

    'sqlite' => [
        'driver'   => 'sqlite',
        'database' => storage_path().'/database.sqlite',
        'prefix'   => '',
    ],

    'mysql' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST', 'localhost'),
        'database'  => env('DB_DATABASE', 'forge'),
        'username'  => env('DB_USERNAME', 'forge'),
        'password'  => env('DB_PASSWORD', ''),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
    ],

    'mysql2' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST2', 'localhost'),
        'database'  => env('DB_DATABASE2', 'forge'),
        'username'  => env('DB_USERNAME2', 'forge'),
        'password'  => env('DB_PASSWORD2', ''),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
    ],

    'pgsql' => [
        'driver'   => 'pgsql',
        'host'     => env('DB_HOST', 'localhost'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'charset'  => 'utf8',
        'prefix'   => '',
        'schema'   => 'public',
    ],

    'sqlsrv' => [
        'driver'   => 'sqlsrv',
        'host'     => env('DB_HOST', 'localhost'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'prefix'   => '',
    ],

],

应用/ DbLog.php

<?php namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class DbLog extends Model{
    protected $connection = '';

    protected $table = 'users';

    public function set_connection($val){
        $this->connection=$val;
    }
    public function select_all_db2_users(){
        $sql = "
            SELECT *
            FROM $this->table
            LIMIT 0,1;
        ";
        $results=DB::connection($this->connection)
            ->select(DB::raw($sql));
        return $results;
    }

    public function select_all_db_users(){
        $sql = "
            SELECT *
            FROM $this->table
            LIMIT 0,1;
        ";
        $results=DB::connection($this->connection)
            ->select(DB::raw($sql));
        return $results;
    }

    public function close_connection(){
        DB::disconnect('mysql2');
    }
}
?>

应用/ HTTP /控制器/ AdminController.php

<?php
namespace App\Http\Controllers;
use App\DbLog;
class AdminController extends Controller {
    function db1(){
        $DbLog=new DbLog();
        $DbLog->set_connection('mysql');
        $select_all_db2_users=$DbLog->select_all_db2_users();
        echo "<pre>";
        print_r ($select_all_db2_users);
        echo "</pre>";
    }

    function db2(){
        $DbLog=new DbLog();
        $DbLog->set_connection('mysql2');
        $select_all_db2_users=$DbLog->select_all_db2_users();
        echo "<pre>";
        print_r ($select_all_db2_users);
        echo "</pre>";
    }
}