<p>To remedy the issue here is some more detail on top of Craft's <a href="https://craftcms.com/knowledge-base/mamp-with-composer-and-mysql-on-the-command-line" rel="nofollow noreferrer">Knowledge Base Article</a> at Craft on setting up MAMP with Craft CMS:</p>
<ol>
<li>You want to ensure you are using the MAMP PHP executable and MAMP MySQL Server</li>
</ol>
<p>Create bash file in your user folder</p>
<p><code>touch ~/.bash_profile</code></p>
<p>Add the following to the top (make sure you change the PHP version to match <em>Craft Control Panel &gt; Utilities &gt; PHP Info</em>):</p>
<p><code>export PATH=&quot;/Applications/MAMP/Library/bin:/Applications/MAMP/bin/php/php7.0.27/bin:${PATH}&quot;</code></p>
<ol start="2">
<li><p>Ensure the Database server is set to '127.0.0.1' and not 'localhost'</p>
</li>
<li><p>Tick '<em>Allow network access to MySQL</em>' in MAMP under <em>Services &gt; MySQL</em></p>
</li>
<li><p>run <code>source ~/.bash_profile</code></p>
</li>
<li><p>restart MAMP server and try running a <code>/.craft</code> command (in my case it was <code>./craft image-optimize/optimize/create</code>)</p>
</li>
</ol>

CC BY-SA 4.0
2021-05-10T15:19:01.557Z
2021-05-10T15:19:01.557Z
Dan Lee
<p>I can't run /.craft commands and get the following error:</p>
<pre><code>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 &quot;image-optimize/optimize/create&quot;.'

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-&gt;runAction('image-optimize/...', Array)
#1 /Users/danlee/Sites/galileo-servd/vendor/craftcms/cms/src/console/Application.php(89): yii\console\Application-&gt;runAction('image-optimize/...', Array)
#2 /Users/danlee/Sites/galileo-servd/vendor/yiisoft/yii2/console/Application.php(148): craft\console\Application-&gt;runAction('image-optimize/...', Array)
#3 /Users/danlee/Sites/galileo-servd/vendor/yiisoft/yii2/base/Application.php(392): yii\console\Application-&gt;handleRequest(Object(craft\console\Request))
#4 /Users/danlee/Sites/galileo-servd/craft(43): yii\base\Application-&gt;run()
#5 {main}

</code></pre>
<p>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.</p>
<p><strong>Already tried:</strong></p>
<ul>
<li>hard coding the database values in db.php file but get the same error</li>
<li>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</li>
<li>I have ticked 'allow network access to MySQL' in MAMP under services &gt; MySQL</li>
<li>I have made sure the recommended settings for MAMP are in place from the <a href="https://craftcms.com/knowledge-base/mamp-with-composer-and-mysql-on-the-command-line" rel="nofollow noreferrer">knowledge base article</a> to ensure MAMPs PHP executable is being used and MYSQL server</li>
</ul>
<p><strong>Additional info:</strong></p>
<ul>
<li>Craft CMS: 3.6.11.2</li>
<li>PHP version: 7.4.12</li>
<li>Database driver &amp; version: MySQL 5.7.32</li>
<li>Plugins &amp; versions: Image Optimize 1.6.30</li>
<li>Setup: MAMP</li>
</ul>
<p><em><strong>Console Logged Output</strong></em></p>
<pre><code>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-&gt;__construct('mysql:host=loca...', 'root', 'root', Array)
#1 /Users/danlee/Sites/galileo-servd/vendor/yiisoft/yii2/db/Connection.php(631): yii\db\Connection-&gt;createPdoInstance()
#2 /Users/danlee/Sites/galileo-servd/vendor/craftcms/cms/src/db/Connection.php(152): yii\db\Connection-&gt;open()
#3 /Users/danlee/Sites/galileo-servd/vendor/craftcms/cms/src/base/ApplicationTrait.php(822): craft\db\Connection-&gt;open()
#4 /Users/danlee/Sites/galileo-servd/vendor/craftcms/cms/src/base/ApplicationTrait.php(296): craft\console\Application-&gt;getIsDbConnectionValid()
#5 /Users/danlee/Sites/galileo-servd/vendor/craftcms/cms/src/helpers/App.php(592): craft\console\Application-&gt;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-&gt;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-&gt;get('projectConfig', true)
#12 /Users/danlee/Sites/galileo-servd/vendor/craftcms/cms/src/console/Application.php(158): yii\base\Module-&gt;get('projectConfig', true)
#13 /Users/danlee/Sites/galileo-servd/vendor/craftcms/cms/src/base/ApplicationTrait.php(1295): craft\console\Application-&gt;get('projectConfig')
#14 /Users/danlee/Sites/galileo-servd/vendor/craftcms/cms/src/base/ApplicationTrait.php(1466): craft\console\Application-&gt;getProjectConfig()
#15 /Users/danlee/Sites/galileo-servd/vendor/craftcms/cms/src/base/ApplicationTrait.php(1419): craft\console\Application-&gt;_setTimeZone()
#16 /Users/danlee/Sites/galileo-servd/vendor/craftcms/cms/src/console/Application.php(46): craft\console\Application-&gt;_preInit()
#17 /Users/danlee/Sites/galileo-servd/vendor/yiisoft/yii2/base/BaseObject.php(109): craft\console\Application-&gt;init()
#18 /Users/danlee/Sites/galileo-servd/vendor/yiisoft/yii2/base/Application.php(212): yii\base\BaseObject-&gt;__construct(Array)
#19 /Users/danlee/Sites/galileo-servd/vendor/yiisoft/yii2/console/Application.php(90): yii\base\Application-&gt;__construct(Array)
#20 [internal function]: yii\console\Application-&gt;__construct(Array)
#21 /Users/danlee/Sites/galileo-servd/vendor/yiisoft/yii2/di/Container.php(420): ReflectionClass-&gt;newInstanceArgs(Array)
#22 /Users/danlee/Sites/galileo-servd/vendor/yiisoft/yii2/di/Container.php(171): yii\di\Container-&gt;build('craft\\console\\A...', Array, Array)
#23 /Users/danlee/Sites/galileo-servd/vendor/yiisoft/yii2/BaseYii.php(365): yii\di\Container-&gt;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}
</code></pre>

CC BY-SA 4.0
2021-05-04T14:54:55.180Z
How are you running your setup? Looks like you’re in a Mac? Docker? MAMP? Homebrew? etc
RitterKnight
CC BY-SA 4.0
2021-05-04T15:37:07.297Z
@RitterKnight This is on MAMP - I know I know
Dan Lee
CC BY-SA 4.0
2021-05-04T17:36:01.073Z
@DanLee If it's MAMP Pro, have you ticked 'allow network access to MySQL' under services > MySQL
dmatthams
CC BY-SA 4.0
2021-05-05T10:27:16.883Z
@dmatthams No I haven't - that doesnt seem to get rid of the MySQL error.
Dan Lee
CC BY-SA 4.0
2021-05-05T11:26:52.110Z
@DanLee try also changing host from localhost -> 127.0.0.1
dmatthams
CC BY-SA 4.0
2021-05-05T23:58:11.947Z
From what I recall, you have to jump through a few extra hoops for MAMP to connect to the database from the CLI: https://craftcms.com/knowledge-base/mamp-with-composer-and-mysql-on-the-command-line
Brad Bell
CC BY-SA 4.0
2021-05-10T15:02:10.880Z
@BradBell thanks for your comment - I have made sure everything is in place from that knowledge base article. I still get the error.
Dan Lee
CC BY-SA 4.0
2021-05-10T15:08:03.000Z
@dmatthams that seemed to do the trick! Setting DB_SERVER' => '127.0.0.1
Dan Lee
<craft3>
/.craft commands not running from root
# /.craft commands not running from root

(Asked by: username_0 on 2021-05-04)

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}

**Tags:** craft3
> **Comments:**
> **username_1**: How are you running your setup? Looks like you’re in a Mac? Docker? MAMP? Homebrew? etc  
> **username_0**: @RitterKnight This is on MAMP - I know I know  
> **username_2**: @DanLee If it's MAMP Pro, have you ticked 'allow network access to MySQL' under services > MySQL  
> **username_0**: @dmatthams No I haven't - that doesnt seem to get rid of the MySQL error.  
> **username_2**: @DanLee try also changing host from localhost -> 127.0.0.1  
> **username_3**: From what I recall, you have to jump through a few extra hoops for MAMP to connect to the database from the CLI: https://craftcms.com/knowledge-base/mamp-with-composer-and-mysql-on-the-command-line  
> **username_0**: @BradBell thanks for your comment - I have made sure everything is in place from that knowledge base article. I still get the error.  
> **username_0**: @dmatthams that seemed to do the trick! Setting DB_SERVER' => '127.0.0.1  

---

## 1 answers

### Answer by username_0 on 2021-05-10. Score: 2

To remedy the issue here is some more detail on top of Craft's [Knowledge Base Article](https://craftcms.com/knowledge-base/mamp-with-composer-and-mysql-on-the-command-line) 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}"`

2. Ensure the Database server is set to '127.0.0.1' and not 'localhost'
3. Tick '*Allow network access to MySQL*' in MAMP under *Services > MySQL*
4. run `source ~/.bash_profile`
5. restart MAMP server and try running a `/.craft` command (in my case it was `./craft image-optimize/optimize/create`)


---

