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

ferenax's avatar

Setting up MariaDB with Laravel 5

I have been trying to get my application to connect to MariaDB for some time now without success and it's been driving me crazy !

I have been following the Laravel 5 from scratch tutorial and have been to the end of it, however, upon deciding to go from a Sqlite database connection to MariaDB I have not been able to connect to the database. I am using a Homestead and vagrant setup to launch my app locally.

Executing my app returns this error :

PDOException in Connector.php line 47:
SQLSTATE[HY000] [1049] Unknown database 'data'

here's my Database.php file:

'default' => 'mysql',

'connections' => [

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

        'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', '0.0.0.0'),
            'database'  => env('DB_DATABASE', 'laravel'),
            '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', ''),
            'prefix'   => '',
        ],

    ],

Here's my .env file :

APP_ENV=local
APP_DEBUG=true
APP_KEY=ZP9Ao5RJhZjS7EqJuaYRltSIC7HTL4vp

DB_HOST=localhost
DB_DATABASE=data
DB_USERNAME=root
DB_PASSWORD=secret

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null

this error dump :

in Connector.php line 47
at PDO->__construct('mysql:host=localhost;dbname=data', 'root', 'secret', array('0', '2', '0', false, '0')) in Connector.php line 47
at Connector->createConnection('mysql:host=localhost;dbname=data', array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'data', 'username' => 'root', 'password' => 'secret', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'name' => 'mysql'), array('0', '2', '0', false, '0')) in MySqlConnector.php line 20
at MySqlConnector->connect(array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'data', 'username' => 'root', 'password' => 'secret', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'name' => 'mysql')) in ConnectionFactory.php line 58
at ConnectionFactory->createSingleConnection(array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'data', 'username' => 'root', 'password' => 'secret', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'name' => 'mysql')) in ConnectionFactory.php line 47
at ConnectionFactory->make(array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'data', 'username' => 'root', 'password' => 'secret', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false), 'mysql') in DatabaseManager.php line 177
at DatabaseManager->makeConnection('mysql') in DatabaseManager.php line 65
at DatabaseManager->connection(null) in Model.php line 3134
at Model::resolveConnection(null) in Model.php line 3100
at Model->getConnection() in Model.php line 1895
at Model->newBaseQueryBuilder() in Model.php line 1836
at Model->newQueryWithoutScopes() in Model.php line 1810
at Model->newQuery() in Model.php line 3321
at Model->__call('get', array()) in SongsController.php line 20
at Song->get() in SongsController.php line 20
at SongsController->index()
at call_user_func_array(array(object(SongsController), 'index'), array()) in Controller.php line 246
at Controller->callAction('index', array()) in ControllerDispatcher.php line 162
at ControllerDispatcher->call(object(SongsController), object(Route), 'index') in ControllerDispatcher.php line 107
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 108
at ControllerDispatcher->callWithinStack(object(SongsController), object(Route), object(Request), 'index') in ControllerDispatcher.php line 67
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\SongsController', 'index') in Route.php line 198
at Route->runWithCustomDispatcher(object(Request)) in Route.php line 131
at Route->run(object(Request)) in Router.php line 691
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
at Pipeline->then(object(Closure)) in Router.php line 693
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 660
at Router->dispatchToRoute(object(Request)) in Router.php line 618
at Router->dispatch(object(Request)) in Kernel.php line 210
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in VerifyCsrfToken.php line 43
at VerifyCsrfToken->handle(object(Request), object(Closure)) in VerifyCsrfToken.php line 17
at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 55
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 61
at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 36
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 40
at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 42
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
at Pipeline->then(object(Closure)) in Kernel.php line 111
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 84
at Kernel->handle(object(Request)) in index.php line 53

Upon trying a lot of workarounds unsuccessfully, I have decided to try and remove any trace of MariaDB and Mysql altogether from my ubuntu installation, but I'm still getting this error now when I execute my app:

PDOException in Connector.php line 47:
SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES)

How is it even possible when I have made sure to uninstall MariaDB and mysql, what am I doing wrong ?

0 likes
7 replies
noeldiaz's avatar

That is strange, since I use MariaDB and so far it has been 100% compatible with all MySQL settings and processes. Your first error, the one where it couldn't find the database "data", so at that point it was connecting it seems? Did you create the database before you tried connecting to it? If not that could explain your problem, since if not it would have given you a connection error.

And as far as removing them both, are you sure they are both gone? Try checking for the process:

ps -Af |grep "mysql"

Or check to see if the binaries are still installed:

which mysql

Because it seems you still have at least one of them running and listening for connections. Now, does this mean you still had both installed? Maybe MySQL was still running even though you where looking at MariaDB?

1 like
ferenax's avatar

I have finally found the origin of the issue : I am using Homestead which comes up with Mysql, so the whole time the app was trying to connect to the Homestead Mysql and not to my regular MariaDB installed on my ubuntu machine. Which prompts me to wonder of how I can install MariaDB into my Homestead machine, and if I really need to do it or I can work with my app just fine using Mysql and then making the switch to MariaDB in production environment without any difficulties.

1 like
constb's avatar

@ferenax in my experience everything that works with mysql also works with maria without modifications.

1 like
FatLeo's avatar

In your's Database.php, you have:

'database'  => env('DB_DATABASE', 'laravel'), 

And in your's .env you have:

DB_DATABASE=data

In other words your database is incorrect. You need put the same on this two local (.env and Database.php).

atilkan's avatar

@FatLeo You have falcon eyes. Nice catch. BTW, i looked at the source of env() function. Those files doesn't have to be the same. You can leave blank those areas on database.php file. I tested it.

radodi's avatar

@FatLeo 'database' => env('DB_DATABASE', 'laravel'),

If DB_DATABASE is not specified in the .env file, use 'laravel'.

@ferenax

DB_HOST=localhost DB_DATABASE=data DB_USERNAME=root DB_PASSWORD=secret

I see that your password for user root is set to 'secret'. If you don't have password leave it DB_PASSWORD= or change value.

eusapxai's avatar

Check for the port as mariadb and mysql can not be run on the same port simultaneously. just double check for the port number and you are good to go.

Please or to participate in this conversation.