craftcms / nitro

Speedy local dev environment for @craftcms.
https://getnitro.sh
MIT License
178 stars 24 forks source link

CMS 2.x datatabase connection #353

Closed smockensturm closed 2 years ago

smockensturm commented 3 years ago

Description

Craft can’t connect to the database with the credentials in craft/config/db.php. Can, however, connect to database with this standalone php script so creds check out.

What might cause this? drivers? extensions?

Steps to reproduce

  1. Try to fire up Craft CMS
  2. Fire up the standalone php script

Additional info

CMS version: 2.6.3011

Nitro CLI: 2.0.7 Nitro gRPC: 2.0.7 Docker API: 1.41 (1.12 min) Docker CLI: 1.41

Stack trace:

0 /app/craft/app/etc/web/WebApp.php(591): Craft\AppBehavior->createDbConnection()

1 /app/craft/app/framework/base/CModule.php(103): Craft\WebApp->getComponent('db')

2 /app/craft/app/etc/requirements/Requirements.php(93): CModule->__get('db')

3 /app/craft/app/etc/requirements/RequirementsChecker.php(42): Craft\Requirements::getRequirements()

4 /app/craft/app/controllers/TemplatesController.php(123): Craft\RequirementsChecker->run()

5 /app/craft/app/framework/web/actions/CInlineAction.php(49): Craft\TemplatesController->actionRequirementsCheck()

6 /app/craft/app/framework/web/CController.php(308): CInlineAction->runWithParams(Array)

7 /app/craft/app/framework/web/CController.php(286): CController->runAction(Object(CInlineAction))

8 /app/craft/app/framework/web/CController.php(265): CController->runActionWithFilters(Object(CInlineAction), Array)

9 /app/craft/app/framework/web/CWebApplication.php(282): CController->run('requirementsche...')

10 /app/craft/app/controllers/InstallController.php(63): CWebApplication->runController('templates/requi...')

11 /app/craft/app/framework/web/actions/CInlineAction.php(49): Craft\InstallController->actionIndex()

12 /app/craft/app/framework/web/CController.php(308): CInlineAction->runWithParams(Array)

13 /app/craft/app/framework/web/CController.php(286): CController->runAction(Object(CInlineAction))

14 /app/craft/app/framework/web/CController.php(265): CController->runActionWithFilters(Object(CInlineAction), Array)

15 /app/craft/app/framework/web/CWebApplication.php(282): CController->run('')

16 /app/craft/app/etc/web/WebApp.php(795): CWebApplication->runController('install/')

17 /app/craft/app/etc/web/WebApp.php(203): Craft\WebApp->_processInstallRequest()

18 /app/craft/app/framework/base/CApplication.php(185): Craft\WebApp->processRequest()

19 /app/craft/app/index.php(62): CApplication->run()

20 /app/public/index.php(19): require_once('/app/craft/app/...')

21 {main}

REQUEST_URI=/admin/install ` EDIT

MariaDB 10.5 PHP 7.4

ben-callaway commented 3 years ago

Also experiencing something like this, but with:

Craft CMS: 3.6.14 PHP: 7.4 MySQL: 8.0 Nitro CLI: 2.0.7 Nitro gRPC: 2.0.7 Docker API: 1.41 (1.12 min) Docker CLI: 1.41

Error: "No primary site exists"

Can connect to database using tableplus and the same credentials.

jasonmccallister commented 3 years ago

@smockensturm can you provide the full stacktrace, specifically the error message?

@ben-callaway does the database have content in it and the table prefix is correct?

smockensturm commented 3 years ago

`2021/05/14 14:12:22 [error] [exception.CDbException] SQLSTATE[HY000] [2002] No such file or directory 2021/05/14 14:12:22 [error] [application] CDbConnection failed to open the DB connection. 2021/05/14 14:12:22 [error] [application] CDbConnection failed to open the DB connection. 2021/05/14 14:12:22 [error] [exception.CDbException] SQLSTATE[HY000] [2002] No such file or directory 2021/05/14 14:12:22 [error] [application] CDbConnection failed to open the DB connection. 2021/05/14 14:12:22 [error] [application] CDbConnection failed to open the DB connection. 2021/05/14 14:12:22 [error] [exception.CDbException] SQLSTATE[HY000] [2002] No such file or directory 2021/05/14 14:12:22 [error] [application] CDbConnection failed to open the DB connection. 2021/05/14 14:12:22 [error] [application] CDbConnection failed to open the DB connection. 2021/05/14 14:12:22 [error] [exception.CDbException] SQLSTATE[HY000] [2002] No such file or directory 2021/05/14 14:12:22 [error] [application] CDbConnection failed to open the DB connection. 2021/05/14 14:12:22 [error] [application] CDbConnection failed to open the DB connection. 2021/05/14 14:12:22 [error] [exception.CDbException] SQLSTATE[HY000] [2002] No such file or directory 2021/05/14 14:12:22 [error] [application] CDbConnection failed to open the DB connection. 2021/05/14 14:12:22 [error] [application] CDbConnection failed to open the DB connection. 2021/05/14 14:12:22 [error] [exception.CDbException] SQLSTATE[HY000] [2002] No such file or directory 2021/05/14 14:12:22 [error] [application] CDbConnection failed to open the DB connection. 2021/05/14 14:12:22 [error] [application] CDbConnection failed to open the DB connection. 2021/05/14 14:12:22 [error] [exception.CDbException] SQLSTATE[HY000] [2002] No such file or directory 2021/05/14 14:12:22 [error] [application] CDbConnection failed to open the DB connection. 2021/05/14 14:12:22 [error] [application] CDbConnection failed to open the DB connection. 2021/05/14 14:12:22 [error] [exception.Craft\DbConnectException] Craft\DbConnectException: Craft can’t connect to the database with the credentials in craft/config/db.php. in /app/craft/app/etc/behaviors/AppBehavior.php:890 Stack trace:

0 /app/craft/app/etc/web/WebApp.php(591): Craft\AppBehavior->createDbConnection()

1 /app/craft/app/framework/base/CModule.php(103): Craft\WebApp->getComponent('db')

2 /app/craft/app/etc/requirements/Requirements.php(93): CModule->__get('db')

3 /app/craft/app/etc/requirements/RequirementsChecker.php(42): Craft\Requirements::getRequirements()

4 /app/craft/app/controllers/TemplatesController.php(123): Craft\RequirementsChecker->run()

5 /app/craft/app/framework/web/actions/CInlineAction.php(49): Craft\TemplatesController->actionRequirementsCheck()

6 /app/craft/app/framework/web/CController.php(308): CInlineAction->runWithParams(Array)

7 /app/craft/app/framework/web/CController.php(286): CController->runAction(Object(CInlineAction))

8 /app/craft/app/framework/web/CController.php(265): CController->runActionWithFilters(Object(CInlineAction), Array)

9 /app/craft/app/framework/web/CWebApplication.php(282): CController->run('requirementsche...')

10 /app/craft/app/controllers/InstallController.php(63): CWebApplication->runController('templates/requi...')

11 /app/craft/app/framework/web/actions/CInlineAction.php(49): Craft\InstallController->actionIndex()

12 /app/craft/app/framework/web/CController.php(308): CInlineAction->runWithParams(Array)

13 /app/craft/app/framework/web/CController.php(286): CController->runAction(Object(CInlineAction))

14 /app/craft/app/framework/web/CController.php(265): CController->runActionWithFilters(Object(CInlineAction), Array)

15 /app/craft/app/framework/web/CWebApplication.php(282): CController->run('')

16 /app/craft/app/etc/web/WebApp.php(795): CWebApplication->runController('install/')

17 /app/craft/app/etc/web/WebApp.php(203): Craft\WebApp->_processInstallRequest()

18 /app/craft/app/framework/base/CApplication.php(185): Craft\WebApp->processRequest()

19 /app/craft/app/index.php(62): CApplication->run()

20 /app/public/index.php(19): require_once('/app/craft/app/...')

21 {main}

REQUEST_URI=/admin/install


`

jasonmccallister commented 3 years ago

@smockensturm what are the db credentials ports defined in your .env? I just realized you said you are using Craft 2.x. What are the values you entered in this step? https://craftcms.com/docs/2.x/installing.html#step-4-tell-craft-how-to-connect-to-your-database

smockensturm commented 3 years ago
array(
       'server'        => 'mariadb-10.5-3306.database.nitro',
       'user'          => 'nitro',
       'password'      => 'nitro',
       'database'      => 'dpl_prod'
    )
smockensturm commented 3 years ago

default table prefix 'craft'

jasonmccallister commented 3 years ago

Looks like there is a space at the end of the database name?

smockensturm commented 3 years ago

i think that is a typo here. sorry.

array( 'server' => 'mariadb-10.5-3306.database.nitro', 'user' => 'nitro', 'password' => 'nitro', 'database' => 'dpl_prod' ),

jasonmccallister commented 3 years ago

Can you nitro ssh and run php -m and provide the output? It appears the error is being thrown because an extension...

smockensturm commented 3 years ago

[PHP Modules] bcmath blackfire Core ctype curl date dom fileinfo filter ftp gd hash iconv imagick intl json libxml mbstring mysqli mysqlnd openssl pcre PDO pdo_mysql pdo_pgsql pdo_sqlite Phar posix readline redis Reflection session SimpleXML soap sodium SPL sqlite3 standard tokenizer xdebug xml xmlreader xmlwriter Zend OPcache zip zlib

[Zend Modules] Xdebug Zend OPcache blackfire

ben-callaway commented 3 years ago

@ben-callaway does the database have content in it and the table prefix is correct?

Yes, exact copy of what's live and running on MySQL 8. No table prefix used.

ben-callaway commented 3 years ago

Solved by using default port, thanks @jasonmccallister

# DATABASE
DB_DRIVER="mysql"
DB_SERVER="mysql-8.0-33061.database.nitro"
DB_USER="nitro"
DB_PASSWORD="nitro"
DB_DATABASE="mydatabase"
DB_SCHEMA="public"
DB_TABLE_PREFIX=""
DB_PORT="3306"
mizziness commented 3 years ago

Also having the same issue, also was able to work around with the default port, 3306, even though I have 3307 configured:

databases:
    - engine: mysql
      version: "8.0"
      port: "3307"

mysql-8.0-3307.database.nitro

ben-callaway commented 3 years ago

I believe the docs are going to change to make it clearer but…

“…the port is for external to allow you to use a GUI and run multiple databases/versions. Internally (meaning in the Docker network) it uses the default port (5432/3306) on the container since they are individual containers/ip addresses within the network”

So 3306 for .env but 33061 (or in your case 3307) for TablePlus, sequel pro etc

mizziness commented 3 years ago

I believe the docs are going to change to make it clearer but…

That would probably be good because right now it is confusing as all heck.

smockensturm commented 3 years ago

any movement on the suspected driver issue @jasonmccallister ?

Amusing anecdote: we have an Expression Engine 2.11.9 install running in the Nitro environment. We can shoehorn that into connecting, but not an old Craft. :)

edit: sorry. jason rather.

jasonmccallister commented 3 years ago

@mizziness and @ben-callaway prepping a release that will include the following on nitro ls output. Not perfect but it is a start.

Screenshot from 2021-05-18 11-12-11

@smockensturm that is really confusing, it should work as expected. I am going to ping someone on the team running 2.x and ask if there was something specific they needed to change/add.

ademers commented 3 years ago

I'm having same issue as https://github.com/craftcms/nitro/issues/353#issue-891691548. However, using mySQL instead of mariaDB. Also ran db-test.php, and no errors:

------------------------------------------------
Database Connection Test
PHP 7.3.11
DB driver: mysql
------------------------------------------------
👍 Connected and found 61 tables.
angrybrad commented 3 years ago

@ademers can you share your nitro.yaml file (edit out any sensitive bits) with just the Craft 2 site and i'll see if i can reproduce locally with that

ademers commented 3 years ago

@angrybrad Here ya go:

databases:
    - engine: mysql
      version: "5.7"
      port: "3306"
    - engine: mysql
      version: "8.0"
      port: "33061"
services:
    dynamodb: false
    mailhog: true
    minio: false
    redis: true
sites:
    - hostname: example.com.nitro
      path: ~/webdev/sites/example.com
      version: "7.0"
      webroot: public
      xdebug: false
      blackfire: false

The DB test mentioned in my previous post reports PHP 7.3.11. 🤔

angrybrad commented 3 years ago

@ademers guessing it's because in your config/db.php you're using 'port'=33061 and it should be 'port'=3306.

33061 would be the external port the MySQL container is exposed on and 3306 is the internal one - it's explained a bit in the docs here: https://craftcms.com/docs/nitro/2.x/how-nitro-works.html#databases

And Jason's recent ls change (which will be in the next release https://github.com/craftcms/nitro/issues/353#issuecomment-843261259) will make this easier to visualize.

ademers commented 3 years ago

@angrybrad I don't actually have a port specified in config/db.php, so I assume it's using the default which I assume is 3306 (?). I don't see any mention of port at https://craftcms.com/docs/2.x/installing.html#step-4-tell-craft-how-to-connect-to-your-database.

    'example.com.nitro' => array(
        'server' => 'mysql-5.7-3306.database.nitro',
        'user' => 'nitro',
        'password' => 'nitro',
        'database' => 'example_craft2',
    ),

I tried adding 'port' => '3306' to the above, but no change.

I can connect via TablePlus: https://cln.sh/0kU7cuVK5nE7STND5eHb

angrybrad commented 3 years ago

@ademers issue was unrelated to ports, but we tracked it back to multi-environment configs not working in Craft 2 under Nitro, because it checks $_SERVER['SERVER_NAME'] by default for the multi-env keys and that's coming back as _ in Nitro.

Likely because of https://github.com/craftcms/docker/blob/main/nginx/default.conf#L4 in the upstream Docker images.

As a workaround for now, you can define this in your public index.php:

define('CRAFT_ENVIRONMENT', 'my-env-config-key');

To force Craft 2 to use that particular env instead of checking $_SERVER['SERVER_NAME']

angrybrad commented 3 years ago

Another solution for this would be once https://github.com/craftcms/nitro/issues/18 is implemented, you can override the server name in the server block so $_SERVER['SERVER_NAME'] is populated properly.

smockensturm commented 3 years ago

Hi @angrybrad wondering if you are including the OP in these remedies, or am I just THAT special?

angrybrad commented 3 years ago

@smockensturm

1) Are you using multi-environment config in config/db.php? 2) Are you specifying a port in config/db.php?

smockensturm commented 3 years ago

@angrybrad

  1. yes
  2. no
angrybrad commented 3 years ago

@smockensturm It applies to you, then. :)

See https://github.com/craftcms/nitro/issues/353#issuecomment-843603882 for a workaround

pbmills commented 3 years ago

Hi, I am keep getting the error Craft can’t connect to the database with the credentials in craft/config/db.php.on Craft CMS 2.6.2954 on setting up on Nitro.

db.php

<?php

/**
 * Database Configuration
 *
 * All of your system's database configuration settings go in here.
 * You can see a list of the default settings in craft/app/etc/config/defaults/db.php
 */

return array(
    'initSQLs' => array("SET SESSION sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';"),
    // The database server name or IP address. Usually this is 'localhost' or '127.0.0.1'.
    'server' => 'mysql-8.0-3306.database.nitro',

    // The database username to connect with.
    'user' => 'nitro',

    // The database password to connect with.
    'password' => 'nitro',

    // The name of the database to select.
    'database' => 'flex',
    // The prefix to use when naming tables. This can be no more than 5 characters.
    'tablePrefix' => 'craft',

    'port' => '3306',

);

nitro.yaml

databases:
    - engine: mysql
      version: "8.0"
      port: "3306"
services:
    dynamodb: false
    mailhog: false
    minio: false
    redis: false
sites:
    - hostname: example.nitro
      path: ~/Sites/example_site
      version: "7.1"
      webroot: html
      xdebug: false
      blackfire: false

nitro ls

Hostname Type Internal Ports External Ports Status example.nitro site 3000,3001,8080 (uses proxy ports) running mysql-8.0-3306.database.nitro database 3306 3306 running nitro-proxy proxy 3000,3001,443,5000,80 3000,3001,443,5000,80 running

Can you please help me with this?

katrinkerber commented 3 years ago

@ademers issue was unrelated to ports, but we tracked it back to multi-environment configs not working in Craft 2 under Nitro, because it checks $_SERVER['SERVER_NAME'] by default for the multi-env keys and that's coming back as _ in Nitro.

Likely because of https://github.com/craftcms/docker/blob/main/nginx/default.conf#L4 in the upstream Docker images.

As a workaround for now, you can define this in your public index.php:

define('CRAFT_ENVIRONMENT', 'my-env-config-key');

To force Craft 2 to use that particular env instead of checking $_SERVER['SERVER_NAME']

I am running into the same issue trying to get an old Craft 2 site to run on Nitro.

What do I enter for "my-env-config-key" ?

smockensturm commented 3 years ago

What do I enter for "my-env-config-key" ?

typically this might be 'dev', 'production', 'staging' etc.

Also and otherwise, I've been using "_" in config files for local hostname (Instead of say foobar.dev or 'foobar.test') when firing up older Craft sites with Nitro.

katrinkerber commented 3 years ago

Thank you, I’ve just tried replacing all mysite.nitro keys in the db.php and general.php files to '_' and added define('CRAFT_ENVIRONMENT', '_'); to the index.php file but I am still getting the 'Craft can’t connect to the database' error :(

smockensturm commented 3 years ago

You won't need this line anymore.

define('CRAFT_ENVIRONMENT', '_');

katrinkerber commented 3 years ago

Still doesn’t work

smockensturm commented 3 years ago

Let's see your config files.

general.php
db.php
ademers commented 3 years ago

@katrinkerber What happens if you try:

define('CRAFT_ENVIRONMENT', 'mysite.nitro');

so that index.php looks like:

<?php
define('CRAFT_ENVIRONMENT', 'mysite.nitro');

// Path to your craft/ folder
$craftPath = '../craft';

// Do not edit below this line
$path = rtrim($craftPath, '/').'/app/index.php';

if (!is_file($path))
{
    if (function_exists('http_response_code'))
    {
        http_response_code(503);
    }

    exit('Could not find your craft/ folder. Please ensure that <strong><code>$craftPath</code></strong> is set correctly in '.__FILE__);
}

require_once $path;
katrinkerber commented 3 years ago

Let's see your config files.

general.php
db.php

db.php:

return array(
  '*' => array(
    'tablePrefix' => 'craft',
  ),
  // local
  '_' => array(
    'database' => 'craft_supafrank',
    'server' => 'mysql-8.0-3306.database.nitro',
    'user' => 'nitro',
    'password' => 'nitro',
    'port' => '3306',
    'initSQLs' => array("SET SESSION sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';")
  ),
   // production
  'supafrank.com' => array(
    xxxx
  )
);

general.php:

  // local
  '_' => array(
    'devMode' => true,
    'enableTemplateCaching' => false,
    'userSessionDuration' => false,
    'siteUrl' => 'https://supafrank.nitro',
  ),
katrinkerber commented 3 years ago

@katrinkerber What happens if you try:

define('CRAFT_ENVIRONMENT', 'mysite.nitro');

so that index.php looks like:

<?php
define('CRAFT_ENVIRONMENT', 'mysite.nitro');

// Path to your craft/ folder
$craftPath = '../craft';

// Do not edit below this line
$path = rtrim($craftPath, '/').'/app/index.php';

if (!is_file($path))
{
  if (function_exists('http_response_code'))
  {
      http_response_code(503);
  }

  exit('Could not find your craft/ folder. Please ensure that <strong><code>$craftPath</code></strong> is set correctly in '.__FILE__);
}

require_once $path;

I tried that but no change

pbmills commented 3 years ago

I have tried quite a few options as mention above but nothing has worked out for me. I had to gave up on this. :(

katrinkerber commented 3 years ago

I have tried quite a few options as mention above but nothing has worked out for me. I had to gave up on this. :(

Me too :(

pbmills commented 3 years ago

Will be really good if we get a solution around this as a lot of old website we will be wanting it to move to Craft 3 platform and so that we will be wanting to clone the Craft 2 site on Local Nitro and test and make necessary changes before we move it to Craft 3.

ctigelaar commented 2 years ago

I had to work on an old Craft 2 installation as well and had the same errors to get the installation working with Nitro. After SSH'ing in the containers I noticed that the config changes as mentioned in this thread were not reflecting in the files in the container.

I stumbled upon this StackExchange post, where they suggest to disable the checkbox "Use gRPC FUSE for file sharing" in the general Docker settings (OSX). I know it's not directly part of the solution for the problem here, but it might help someone else who's also having the same errors and can't figure it out why. See here: https://craftcms.stackexchange.com/questions/37588/template-and-other-files-do-not-update-with-nitro.

jasonmccallister commented 2 years ago

Hi, we are closing this issue as we have decided to retire Nitro, so no additional work will occur on this project. You can read the official blog post here https://craftcms.com/blog/retiring-craft-nitro. We appreciate everyones feedback and involvement and we look forward to refocusing our efforts on Cloud!

If you're looking for a new local development environment, we recommend DDEV and have a knowledge base article to help you with the transition: https://craftcms.com/knowledge-base/migrating-from-craft-nitro-to-ddev.