1

I can't run /.craft commands and get the following error:

Craft can’t connect to the database. Check your connection settings.    

Unknown command: image-optimize/optimize/create
Caused by: Exception 'yii\base\InvalidRouteException' with message 'Unable to resolve the request "image-optimize/optimize/create".'

in /Users/danlee/Sites/galileo-servd/vendor/yiisoft/yii2/base/Module.php:543

Stack trace:
#0 /Users/danlee/Sites/galileo-servd/vendor/yiisoft/yii2/console/Application.php(181): yii\base\Module->runAction('image-optimize/...', Array)
#1 /Users/danlee/Sites/galileo-servd/vendor/craftcms/cms/src/console/Application.php(89): yii\console\Application->runAction('image-optimize/...', Array)
#2 /Users/danlee/Sites/galileo-servd/vendor/yiisoft/yii2/console/Application.php(148): craft\console\Application->runAction('image-optimize/...', Array)
#3 /Users/danlee/Sites/galileo-servd/vendor/yiisoft/yii2/base/Application.php(392): yii\console\Application->handleRequest(Object(craft\console\Request))
#4 /Users/danlee/Sites/galileo-servd/craft(43): yii\base\Application->run()
#5 {main}

I can connect to the database without issue and browse the site and admin console so I'm not sure why I get the error.

Already tried:

  • hard coding the database values in db.php file but get the same error
  • we're using .env.php files instead of .env so I checked that the executable is correctly defined in the craft file in the root
  • I have ticked 'allow network access to MySQL' in MAMP under services > MySQL
  • I have made sure the recommended settings for MAMP are in place from the knowledge base article to ensure MAMPs PHP executable is being used and MYSQL server

Additional info:

  • Craft CMS: 3.6.11.2
  • PHP version: 7.4.12
  • Database driver & version: MySQL 5.7.32
  • Plugins & versions: Image Optimize 1.6.30
  • Setup: MAMP

Console Logged Output

2021-05-04 14:30:03 [-][-][-][error][craft\db\Connection::open] SQLSTATE[HY000] [2002] No such file or directory
2021-05-04 14:30:03 [-][-][-][error][craft\db\Connection::open] SQLSTATE[HY000] [2002] No such file or directory
2021-05-04 14:30:03 [-][-][-][error][craft\base\ApplicationTrait::getIsDbConnectionValid] There was a problem connecting to the database: Craft CMS can’t connect to the database with the credentials in config/db.php.
2021-05-04 14:30:03 [-][-][-][error][craft\errors\DbConnectException] PDOException: SQLSTATE[HY000] [2002] No such file or directory in /Users/danlee/Sites/galileo-servd/vendor/yiisoft/yii2/db/Connection.php:712
Stack trace:
#0 /Users/danlee/Sites/galileo-servd/vendor/yiisoft/yii2/db/Connection.php(712): PDO->__construct('mysql:host=loca...', 'root', 'root', Array)
#1 /Users/danlee/Sites/galileo-servd/vendor/yiisoft/yii2/db/Connection.php(631): yii\db\Connection->createPdoInstance()
#2 /Users/danlee/Sites/galileo-servd/vendor/craftcms/cms/src/db/Connection.php(152): yii\db\Connection->open()
#3 /Users/danlee/Sites/galileo-servd/vendor/craftcms/cms/src/base/ApplicationTrait.php(822): craft\db\Connection->open()
#4 /Users/danlee/Sites/galileo-servd/vendor/craftcms/cms/src/base/ApplicationTrait.php(296): craft\console\Application->getIsDbConnectionValid()
#5 /Users/danlee/Sites/galileo-servd/vendor/craftcms/cms/src/helpers/App.php(592): craft\console\Application->getIsInstalled()
#6 /Users/danlee/Sites/galileo-servd/vendor/craftcms/cms/src/config/app.php(241): craft\helpers\App::projectConfigConfig()
#7 [internal function]: {closure}()
#8 /Users/danlee/Sites/galileo-servd/vendor/yiisoft/yii2/di/Container.php(631): call_user_func_array(Object(Closure), Array)
#9 /Users/danlee/Sites/galileo-servd/vendor/yiisoft/yii2/BaseYii.php(349): yii\di\Container->invoke(Object(Closure), Array)
#10 /Users/danlee/Sites/galileo-servd/vendor/yiisoft/yii2/di/ServiceLocator.php(137): yii\BaseYii::createObject(Object(Closure))
#11 /Users/danlee/Sites/galileo-servd/vendor/yiisoft/yii2/base/Module.php(748): yii\di\ServiceLocator->get('projectConfig', true)
#12 /Users/danlee/Sites/galileo-servd/vendor/craftcms/cms/src/console/Application.php(158): yii\base\Module->get('projectConfig', true)
#13 /Users/danlee/Sites/galileo-servd/vendor/craftcms/cms/src/base/ApplicationTrait.php(1295): craft\console\Application->get('projectConfig')
#14 /Users/danlee/Sites/galileo-servd/vendor/craftcms/cms/src/base/ApplicationTrait.php(1466): craft\console\Application->getProjectConfig()
#15 /Users/danlee/Sites/galileo-servd/vendor/craftcms/cms/src/base/ApplicationTrait.php(1419): craft\console\Application->_setTimeZone()
#16 /Users/danlee/Sites/galileo-servd/vendor/craftcms/cms/src/console/Application.php(46): craft\console\Application->_preInit()
#17 /Users/danlee/Sites/galileo-servd/vendor/yiisoft/yii2/base/BaseObject.php(109): craft\console\Application->init()
#18 /Users/danlee/Sites/galileo-servd/vendor/yiisoft/yii2/base/Application.php(212): yii\base\BaseObject->__construct(Array)
#19 /Users/danlee/Sites/galileo-servd/vendor/yiisoft/yii2/console/Application.php(90): yii\base\Application->__construct(Array)
#20 [internal function]: yii\console\Application->__construct(Array)
#21 /Users/danlee/Sites/galileo-servd/vendor/yiisoft/yii2/di/Container.php(420): ReflectionClass->newInstanceArgs(Array)
#22 /Users/danlee/Sites/galileo-servd/vendor/yiisoft/yii2/di/Container.php(171): yii\di\Container->build('craft\\console\\A...', Array, Array)
#23 /Users/danlee/Sites/galileo-servd/vendor/yiisoft/yii2/BaseYii.php(365): yii\di\Container->get('craft\\console\\A...', Array, Array)
#24 /Users/danlee/Sites/galileo-servd/vendor/craftcms/cms/bootstrap/bootstrap.php(246): yii\BaseYii::createObject(Array)
#25 /Users/danlee/Sites/galileo-servd/vendor/craftcms/cms/bootstrap/console.php(51): require('/Users/danlee/S...')
#26 /Users/danlee/Sites/galileo-servd/craft(42): require('/Users/danlee/S...')
#27 {main}
8
  • How are you running your setup? Looks like you’re in a Mac? Docker? MAMP? Homebrew? etc – RitterKnight May 4 at 14:54
  • @RitterKnight This is on MAMP - I know I know – Dan Lee May 4 at 15:37
  • @DanLee If it's MAMP Pro, have you ticked 'allow network access to MySQL' under services > MySQL – dmatthams May 4 at 17:36
  • @dmatthams No I haven't - that doesnt seem to get rid of the MySQL error. – Dan Lee May 5 at 10:27
  • @DanLee try also changing host from localhost -> 127.0.0.1 – dmatthams May 5 at 11:26
2

To remedy the issue here is some more detail on top of Craft's Knowledge Base Article at Craft on setting up MAMP with Craft CMS:

  1. You want to ensure you are using the MAMP PHP executable and MAMP MySQL Server

Create bash file in your user folder

touch ~/.bash_profile

Add the following to the top (make sure you change the PHP version to match Craft Control Panel > Utilities > PHP Info):

export PATH="/Applications/MAMP/Library/bin:/Applications/MAMP/bin/php/php7.0.27/bin:${PATH}"

  1. Ensure the Database server is set to '127.0.0.1' and not 'localhost'

  2. Tick 'Allow network access to MySQL' in MAMP under Services > MySQL

  3. run source ~/.bash_profile

  4. restart MAMP server and try running a /.craft command (in my case it was ./craft image-optimize/optimize/create)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.