craftcms / cms

Build bespoke content experiences with Craft.
https://craftcms.com
Other
3.22k stars 627 forks source link

Database Connection Error after Craft setup #6047

Closed michielkempen closed 4 years ago

michielkempen commented 4 years ago

Description

For the past 5 hours, I have been trying to set up a new Craft CMS application, but I keep running into the same Database Connection Error.

My setup is as follows:

Once the PHP container is started, I run the following command to configure Craft:

root@117529a33f7f:/var/www/html# ./craft setup

Generating a security key ... done (Fny5j3e6SeeqBXxz_uZlBkgSDPPN8oP-)

Which database driver are you using?: mysql
Database server name or IP address: db
Database port: 3306
Database username: root
Database password: secret
Database name: craft3
Database table prefix:
Testing database credentials ... success!
Saving database credentials to your .env file ... done

Install Craft now?: yes

Username: admin
Email: kempenmichiel@gmail.com
Password: secret
Confirm: secret
Site name: Blog
Site URL: https://michielkempen.com
Site language: [en-US]
*** installing Craft
    > create table {{%assetindexdata}} ... done (time: 0.025s)
    > create table {{%assets}} ... done (time: 0.026s)
    > create table {{%assettransformindex}} ... done (time: 0.028s)
    ...
    > add foreign key volumes_fieldLayoutId_fk: {{%volumes}} (fieldLayoutId) references {{%fieldlayouts}} (id) ... done (time: 0.101s)
    > add foreign key widgets_userId_fk: {{%widgets}} (userId) references {{%users}} (id) ... done (time: 0.094s)
    > populating the info table ... done
    > saving default site data ... done
    > saving the first user ... done
*** installed Craft successfully (time: 16.518s)

As you can see in the console logs, everything goes well. Craft is able to connect to my database and create all the necessary tables.

However, as soon as the configuration process has finished, whatever I do; visit the dashboard, run a console command, etc. I always get the Database Connection Error shown below.

root@117529a33f7f:/var/www/html# ./craft
Exception 'craft\errors\DbConnectException' with message 'Craft CMS can’t connect to the database with the credentials in config/db.php.'

in /var/www/html/vendor/craftcms/cms/src/db/Connection.php:171

Stack trace:
#0 /var/www/html/vendor/yiisoft/yii2/db/Connection.php(1022): craft\db\Connection->open()
#1 /var/www/html/vendor/yiisoft/yii2/db/Connection.php(1009): yii\db\Connection->getMasterPdo()
#2 /var/www/html/vendor/yiisoft/yii2/db/mysql/QueryBuilder.php(406): yii\db\Connection->getSlavePdo()
#3 /var/www/html/vendor/yiisoft/yii2/db/mysql/QueryBuilder.php(430): yii\db\mysql\QueryBuilder->supportsFractionalSeconds()
#4 /var/www/html/vendor/yiisoft/yii2/db/mysql/QueryBuilder.php(59): yii\db\mysql\QueryBuilder->defaultTimeTypeMap()
#5 /var/www/html/vendor/craftcms/cms/src/db/mysql/QueryBuilder.php(27): yii\db\mysql\QueryBuilder->init()
#6 /var/www/html/vendor/yiisoft/yii2/base/BaseObject.php(109): craft\db\mysql\QueryBuilder->init()
#7 /var/www/html/vendor/yiisoft/yii2/db/QueryBuilder.php(116): yii\base\BaseObject->__construct(Array)
#8 /var/www/html/vendor/craftcms/cms/src/db/mysql/Schema.php(65): yii\db\QueryBuilder->__construct(Object(craft\db\Connection), Array)
#9 /var/www/html/vendor/yiisoft/yii2/db/Schema.php(248): craft\db\mysql\Schema->createQueryBuilder()
#10 /var/www/html/vendor/yiisoft/yii2/db/Connection.php(856): yii\db\Schema->getQueryBuilder()
#11 /var/www/html/vendor/yiisoft/yii2/db/Query.php(157): yii\db\Connection->getQueryBuilder()
#12 /var/www/html/vendor/yiisoft/yii2/db/Query.php(302): yii\db\Query->createCommand(Object(craft\db\Connection))
#13 /var/www/html/vendor/craftcms/cms/src/db/Query.php(188): yii\db\Query->scalar(Object(craft\db\Connection))
#14 [internal function]: craft\db\Query->scalar(Object(craft\db\Connection))
#15 /var/www/html/vendor/yiisoft/yii2/caching/DbQueryDependency.php(106): call_user_func(Array, Object(craft\db\Connection))
#16 /var/www/html/vendor/yiisoft/yii2/caching/DbQueryDependency.php(85): yii\caching\DbQueryDependency->executeQuery(Object(craft\db\Query), Object(craft\db\Connection))
#17 /var/www/html/vendor/yiisoft/yii2/caching/Dependency.php(86): yii\caching\DbQueryDependency->generateDependencyData(Object(yii\caching\FileCache))
#18 /var/www/html/vendor/yiisoft/yii2/caching/Cache.php(142): yii\caching\Dependency->isChanged(Object(yii\caching\FileCache))
#19 /var/www/html/vendor/yiisoft/yii2/caching/Cache.php(593): yii\caching\Cache->get('8e0694a5966c89b...')
#20 /var/www/html/vendor/craftcms/cms/src/services/ProjectConfig.php(1755): yii\caching\Cache->getOrSet('projectConfig:i...', Object(Closure), NULL, Object(yii\caching\DbQueryDependency))
#21 /var/www/html/vendor/craftcms/cms/src/services/ProjectConfig.php(1625): craft\services\ProjectConfig->_loadInternalConfigData()
#22 /var/www/html/vendor/craftcms/cms/src/services/ProjectConfig.php(1614): craft\services\ProjectConfig->_getStoredConfig()
#23 /var/www/html/vendor/craftcms/cms/src/services/ProjectConfig.php(399): craft\services\ProjectConfig->_getLoadedConfig()
#24 /var/www/html/vendor/craftcms/cms/src/base/ApplicationTrait.php(1426): craft\services\ProjectConfig->get('system.timeZone')
#25 /var/www/html/vendor/craftcms/cms/src/base/ApplicationTrait.php(1384): craft\console\Application->_setTimeZone()
#26 /var/www/html/vendor/craftcms/cms/src/console/Application.php(57): craft\console\Application->_preInit()
#27 /var/www/html/vendor/yiisoft/yii2/base/BaseObject.php(109): craft\console\Application->init()
#28 /var/www/html/vendor/yiisoft/yii2/base/Application.php(206): yii\base\BaseObject->__construct(Array)
#29 /var/www/html/vendor/yiisoft/yii2/console/Application.php(89): yii\base\Application->__construct(Array)
#30 /var/www/html/vendor/craftcms/cms/src/console/Application.php(48): yii\console\Application->__construct(Array)
#31 [internal function]: craft\console\Application->__construct(Array)
#32 /var/www/html/vendor/yiisoft/yii2/di/Container.php(400): ReflectionClass->newInstanceArgs(Array)
#33 /var/www/html/vendor/yiisoft/yii2/di/Container.php(159): yii\di\Container->build('craft\\console\\A...', Array, Array)
#34 /var/www/html/vendor/yiisoft/yii2/BaseYii.php(365): yii\di\Container->get('craft\\console\\A...', Array, Array)
#35 /var/www/html/vendor/craftcms/cms/bootstrap/bootstrap.php(245): yii\BaseYii::createObject(Array)
#36 /var/www/html/vendor/craftcms/cms/bootstrap/console.php(43): require('/var/www/html/v...')
#37 /var/www/html/craft(21): require('/var/www/html/v...')
#38 {main}

I tried every single solution that I could think of, but I cannot figure out why Craft is not able to reach the database. In my opinion, it doesn't even make sense, as Craft uses exactly the same credentials as during the installation only seconds ago when everything went well.

Does anyone have an idea what I could be missing here?

Additional info

ENVIRONMENT="dev"

SECURITY_KEY="Fny5j3e6SeeqBXxz_uZlBkgSDPPN8oP-"

DB_DSN="mysql:host=db;port=3306;dbname=craft3;"

DB_USER="root"

DB_PASSWORD="secret"

DB_SCHEMA=""

DB_TABLE_PREFIX=""

DEFAULT_SITE_URL="https://michielkempen.com"
brandonkelly commented 4 years ago

@michielkempen Can you try deleting your storage/runtime/cache folder?

michielkempen commented 4 years ago

Thank you very much for the response, @brandonkelly. This solves the problem! 🙏🏼

After so many hours of debugging, I was literally starting to go crazy.

But this is an issue with the framework, right? I don't think I came across any official resource stating that you should remove your cache folder after the installation process.

brandonkelly commented 4 years ago

Nope, it’s a bug that’s already been fixed for the next release (coming tomorrow) – ec201448b318fd32f00d3e31adb8cb9d89b4ea63. Sorry you got bit by it! There was no GitHub issue at the time, so I’ll point the release note to this one.

brandonkelly commented 4 years ago

Craft 3.4.19 is out now with that fix.