Question

How to connect Laravel app with MariaDB on Dokku

Hey all, I’ve been trying to get my Laravel application to connect to a MariaDB database for hours now without any success. There’s quite obviously something I’m missing but I’m not sure what exactly.

Here’s the error I’m getting from Laravel:

PDOException in Connector.php line 55:
SQLSTATE[HY000] [2002] Connection refused

Here’s my database.php:

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

'connections' => [

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

        'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', '0.0.0.0'),
            'database'  => env('DB_DATABASE', 'blog'),
            'username'  => env('DB_USERNAME', 'root'),
            'password'  => env('DB_PASSWORD', 'root'),
            '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', ''),
            'charset'  => 'utf8',
            'prefix'   => '',
        ],

    ],

Here’s my .env from the deployed app on Dokku

export APP_DEBUG='true'
export APP_ENV='production'
export BUILDPACK_URL='https://github.com/heroku/heroku-buildpack-php'
export DATABASE_URL='mysql://mariadb:-snip-b@dokku-mariadb-blog:3306/blog'
export DB_CONNECTION='mysql'
export DB_DATABASE='blog'
export DB_HOST='0.0.0.0'
export DB_PASSWORD='root'
export DB_USERNAME='root'
export DOKKU_APP_RESTORE='1'
export DOKKU_NGINX_PORT='80'

Laravel error dump:

in Connector.php line 55
at PDO->__construct('mysql:host=0.0.0.0;dbname=blog', 'root', 'root', array('0', '2', '0', false, false)) in Connector.php line 55
at Connector->createConnection('mysql:host=0.0.0.0;dbname=blog', array('driver' => 'mysql', 'host' => '0.0.0.0', 'database' => 'blog', 'username' => 'root', 'password' => 'root', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'name' => 'mysql'), array('0', '2', '0', false, false)) in MySqlConnector.php line 24
at MySqlConnector->connect(array('driver' => 'mysql', 'host' => '0.0.0.0', 'database' => 'blog', 'username' => 'root', 'password' => 'root', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'name' => 'mysql')) in ConnectionFactory.php line 61
at ConnectionFactory->Illuminate\Database\Connectors\{closure}()
at call_user_func(object(Closure)) in Connection.php line 846
at Connection->getPdo() in Connection.php line 714
at Connection->reconnectIfMissingConnection() in Connection.php line 603
at Connection->run('select * from `posts` where `published_at` <= ? and `is_draft` = ? order by `published_at` desc limit 11 offset 0', array(object(Carbon), '0'), object(Closure)) in Connection.php line 324
at Connection->select('select * from `posts` where `published_at` <= ? and `is_draft` = ? order by `published_at` desc limit 11 offset 0', array(object(Carbon), '0'), true) in Builder.php line 1461
at Builder->runSelect() in Builder.php line 1447
at Builder->get(array('*')) in Builder.php line 503
at Builder->getModels(array('*')) in Builder.php line 237
at Builder->get(array('*')) in Builder.php line 396
at Builder->simplePaginate('10') in BlogIndexData.php line 50
at BlogIndexData->normalIndexData() in BlogIndexData.php line 36
at BlogIndexData->handle()
at call_user_func_array(array(object(BlogIndexData), 'handle'), array()) in Container.php line 507
at Container->call(array(object(BlogIndexData), 'handle')) in Dispatcher.php line 82
at Dispatcher->Illuminate\Bus\{closure}(object(BlogIndexData))
at call_user_func(object(Closure), object(BlogIndexData)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(BlogIndexData))
at call_user_func(object(Closure), object(BlogIndexData)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Dispatcher.php line 83
at Dispatcher->dispatchNow(object(BlogIndexData)) in Dispatcher.php line 69
at Dispatcher->dispatch(object(BlogIndexData)) in DispatchesJobs.php line 17
at Controller->dispatch(object(BlogIndexData)) in BlogController.php line 15
at BlogController->index(object(Request))
at call_user_func_array(array(object(BlogController), 'index'), array(object(Request))) in Controller.php line 76
at Controller->callAction('index', array(object(Request))) in ControllerDispatcher.php line 146
at ControllerDispatcher->call(object(BlogController), object(Route), 'index') in ControllerDispatcher.php line 94
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 96
at ControllerDispatcher->callWithinStack(object(BlogController), object(Route), object(Request), 'index') in ControllerDispatcher.php line 54
at ControllerDispatcher->dispatch(object(Route), object(Request), '\Voident\Http\Controllers\BlogController', 'index') in Route.php line 174
at Route->runController(object(Request)) in Route.php line 140
at Route->run(object(Request)) in Router.php line 703
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 64
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 62
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Router.php line 705
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 678
at Router->dispatchToRoute(object(Request)) in Router.php line 654
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 53

Finally, here are all the commands I used to set up my app.

$ dokku apps:create app_name
$ dokku plugin:install https://github.com/dokku/dokku-mariadb.git mariadb

$ dokku mariadb:create blog
$ dokku mariadb:link blog blog
$ dokku mariadb:info blog blog
-> DSN: mysql://mariadb:-snip-@dokku-mariadb-blog:3306/blog

$ dokku config:set blog DB_CONNECTION=mysql DB_HOST=0.0.0.0 DB_DATABASE=blog DB_USERNAME=root DB_PASSWORD=root

$ dokku config:add BUILDPACK_URL=https://github.com/heroku/heroku-buildpack-php
Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Have you tried changing DB_HOST from 0.0.0.0 to localhost or 127.0.0.1 ?