lethal
By:
lethal

How to connect Laravel app with MariaDB on Dokku

February 11, 2016 5.4k views
Dokku MariaDB Deployment One-Click Install Apps Ubuntu

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
1 comment
1 Answer

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

  • Thank you for the quick answer! Yes, I have tried all of those and none of them work. What I find weird, and here again maybe I'm missing something, is that I have not seen anywhere the username or password for the DB. root & root are just my guesses from what I've seen on other sites. Is there any way to get that information? As I mentioned in my question, dokku mariadb:info blog blog only returns a DSN

    • @lethal - To test out if the root password is 'root', from the command line try

      echo "show databases; " | mysql --user=root --password=root
      

      If the password is incorrect, it will give you an "Access denied..." message. BTW, it would be odd and not recommended to leave your database password to something so simple. You could also change it if you don't know it. See 'man mysqladmin' and look up the 'password' command.

      • For the command, I'm getting this:

        root@Dokku:~# dokku run blog echo "show databases; " | mysql --user=root --password=root
        The program 'mysql' can be found in the following packages:
         * mysql-client-core-5.5
         * mariadb-client-core-5.5
         * mysql-client-core-5.6
         * percona-xtradb-cluster-client-5.5
        Try: apt-get install <selected package>
        
        [1]+  Stopped                 dokku run blog echo "show databases; " | mysql --user=root --password=root
        

        Am I supposed to install mariadb even though I installed the mariadb plugin in dokku? And yes, I know I shouldn't have a password so simple, it's only for testing purposes.

        • @lethal - I recommend you install both the server AND client for your database if you are running the database on the same droplet as your application. This helps in debugging as well as archiving your data.

          • Thank you. So, after installing both of these and setting DB_HOST=localhost I'm no longer getting the Connection refused error however I'm now getting this:
            SQLSTATE[HY000] [2002] No such file or directory

            I made sure to create the blog database beforehand.

    • If you used the DigitalOcean images you can see the location of the password in the message of the day I think. Try cat /etc/motd. Otherwise it can be in the user-specific mysql configuration file, which I think is ~/.my.cnf

      • I am using the DO One-click Dokku app. Should I use a standard Ubuntu droplet and install Dokku on it?

        Both /etc/motd/ and ~/.my.cnf return No such file or directory

        • Try /etc/motd instead. Otherwise I'm really not sure where the file is as I install MySQL by myself, not using the DO droplets.

Have another answer? Share your knowledge.