0

I want to do two Laravel connections with two databases in MySQL.In my database.php file I have added mysql2 connection.

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

],

];

And in my routes file I added this route

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

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

return $users;

});

But when I browse to get the result of the query

QueryException in Connection.php line 729:

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

I need someone helps me .

10
  • Laravel version? Commented Mar 21, 2017 at 8:32
  • laravel version 5 Commented Mar 21, 2017 at 8:37
  • Can you show the whole database.php file? Commented Mar 21, 2017 at 8:38
  • you should say full version :) Laravel connection has changed a lot from 5.2->5.3->5.4 Commented Mar 21, 2017 at 9:01
  • Are you sure the User and Password on mysql2 are right? Commented Mar 21, 2017 at 9:07

3 Answers 3

1
//use model
$user = new \App\User();
$user->setConnection('mysql2');
$users = $user->all();
return $users;

Laravel 5.3

Sign up to request clarification or add additional context in comments.

Comments

0

The default env() of database.php file direct to the .env file, so can't find table. Remove the env(), just like upper.

1 Comment

By occasion could I make two MySQL connections in .env file?
0

I changed my function on routes.php to the following and the problem solved (get function deleted).

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

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

    return $users;

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.