0

So I want 2 databases in a single project. I used the code from the answer here So this is my code:

.env

APP_NAME=.....
APP_ENV=local
APP_KEY=......
APP_DEBUG=true
APP_URL=http://Test_Ticket.test

DB_CONNECTION=mysql
DB_HOST=192.168.56.56
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

DB_CONNECTION_SECOND=mysql
DB_HOST_SECOND=192.168.56.56
DB_PORT_SECOND=3306
DB_DATABASE_SECOND=homestead2
DB_USERNAME_SECOND=homestead
DB_PASSWORD_SECOND=secret

database.php:

'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
        ],

    

'mysql2' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST_SECOND', '192.168.56.56'),
            'port' => env('DB_PORT_SECOND', '3306'),
            'database' => env('DB_DATABASE_SECOND', 'homestead2'),
            'username' => env('DB_USERNAME_SECOND', 'homestead'),
            'password' => env('DB_PASSWORD_SECOND', 'secret'),
            'unix_socket' => env('DB_SOCKET', ''),
        ],

Homestead.yaml:

ip: 192.168.56.56
memory: 2048
cpus: 2
provider: virtualbox
authorize: ~/.ssh/id_rsa.pub
keys:
    - ~/.ssh/id_rsa
folders:
    -
        map: 'C:\Users\lilia\Documents\GitHub\Test tickets'
        to: /home/vagrant/code
sites:
    -
        map: homestead.test
        to: /home/vagrant/code/public
databases:
    - homestead
    - homestead2
features:
    -
        mysql: true
    -
        mariadb: false
    -
        postgresql: false
    -
        ohmyzsh: false
    -
        webdriver: false
    -
        mongodb: false
services:
    -
        enabled: [mysql]
name: test-tickets
hostname: test-tickets

After I use the command vagrant up, if I understand well, I should have my 2 databases created, but how can I actually connect to both of them if they have the same host and port? I'm using phpstorm and I can only connect to my first "homestead" database. I don't know what to specify to connect to the second one... Those are the parameters I'm using and the response I'm getting:

enter image description here

Also, when I'm trying to run a migration on my second database (homestead2) I'm getting pretty much the same error:

Migration:

Schema::connection('mysql2')->create('files', function (Blueprint $table) { 
... 
});

And the response I'm getting :

SQLSTATE[HY000] [1049] Unknown database 'homestead2'

So obviously I understand that the second database hasn't been created... So what did I actually did wrong here? Sorry if the question may seem stupid but I'm pretty new to all this...

Edit: changing the database.php or use the command

vagrant up --provision

doesn't change anything

Lilian Carion
  • 111
  • 1
  • 9

2 Answers2

1

I have tried to connect 2 databases in the same project by following the steps below:

  1. Added DB_EXT_CONNECTION for the 2nd db.

.env file example

  1. In config->database.php file I added:

database.php

Finally, because you have changed the .env file, you have to run the command vagrant reload --provision.

Tinxuanna
  • 206
  • 1
  • 3
  • 16
-1

Hosts in Env. and Hosts in database.php are not the same

yossi
  • 3,090
  • 7
  • 45
  • 65
  • Yeah but why can I connect to one database but not the second one if both my databases in database.php have the same parameter for the host? Even if it doesn't match my .env, I can still connect to my first database – Lilian Carion Jul 21 '22 at 12:43
  • could you develop please? – Lilian Carion Jul 21 '22 at 13:54