Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jackFlick's avatar

500 Internal Server Error on Laravel Forge 2nd Database

We're actually deploying our Laravel application now in Forge. But we are getting a 500 Internal Server Error when fetching the data from the 2nd database. We have public and private API from that database and both returns 500 error.

We tested both locally and it works fine.

Also tried using postman to access the API from the main database and it is returning the data fine. But for the 2nd database same issue.

Here's our .ENV file

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=schoolname
DB_USERNAME=root
DB_PASSWORD=

DB_CONNECTION_SECOND=mysql
DB_HOST_SECOND=127.0.0.1
DB_PORT_SECOND=3306
DB_DATABASE_SECOND=SCHL0000001
DB_USERNAME_SECOND=root
DB_PASSWORD_SECOND=

And the 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', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

        'SCHL0000001' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST_SECOND'),
            'port' => env('DB_PORT_SECOND'),
            'database' => env('DB_DATABASE_SECOND'),
            'username' => env('DB_USERNAME_SECOND'),
            'password' => env('DB_PASSWORD_SECOND'),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

We are using DigitalOcean managed database. We also tried using the local database of Forge. Same issue.

Migrations work fine. We checked both database and all have records inside. Our problem is just it's not getting anything from the 2nd database.

Is there a bearing using the MYSQL 8 in DigitalOcean? We tried using MySQL 5.7

Hope someone can help us :(

0 likes
6 replies
bobbybouwmann's avatar
Level 88

Well, you get a 500 error. So there must be an issue in the code. Did you check the log files in the storage/logs directory?

Make sure to double-check your environment variables and that they contain the correct values!

nexxai's avatar

Also make sure that if you're adding values to your .env file that you run php artisan config:clear and php artisan config:cache for it to pick up the new settings

1 like
jackFlick's avatar

@bobbybouwmann, It was actually working in our local server. It there a difference between using setConnection and DB::connection? We are using the setConnection to fetch the 2nd database. Not what's wrong with our .env file. We'll check the storage/logs.

jackFlick's avatar

@nexxai , yes that's the first 2 lines of our deployment script. migrations works fine on both database.

bobbybouwmann's avatar

@jvbalcita So the migrations do work? The problem is not in the database connection then. Have you looked in your log files? There should be at least something because of the 500 error.

jackFlick's avatar

@bobbybouwmann, yes all migrations work. We found the issue by changing the APP_DEBUG to true and found out that one of the controller namespace is incorrect but actually works in local. Issue resolved. :)

Please or to participate in this conversation.