1

I am new to Drupal 8 and am starting by building a sample site. I have everything setup right, but I'm getting the PDO connection error below. I'm pretty sure it has to do with hostname but I can't figure out what.

My settings.php looks like this:

$databases['default']['default'] = array (
   'database' => 'website_pet',
   'username' => 'drupal',
   'password' => 'mypass',
   'host' => '127.0.0.1',
   'port' => '3306',
   'driver' => 'mysql',
   'prefix' => '',
   'collation' => 'utf8mb4_general_ci',
 );

And in my.cnf I have added:

max_allowed_packet=100M 

And my httpd error log shows:

[Fri Jun 07 03:47:32.183065 2019] [php7:notice] [pid 19631] [client ::1:58796] PDOException: SQLSTATE[HY000] [2002] Permission denied in /var/www/html/core/lib/Drupal/Component/DependencyInjection/PhpArrayContainer.php on line 79 #0 /var/www/html/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php(420): PDO->__construct('mysql:host=127....', 'drupal', 'mypass', Array)\n#1 /var/www/html/core/lib/Drupal/Core/Database/Database.php(371): Drupal\Core\Database\Driver\mysql\Connection::open(Array)\n#2 /var/www/html/core/lib/Drupal/Core/Database/Database.php(166): Drupal\Core\Database\Database::openConnection('default', 'default')\n#3 [internal function]: Drupal\Core\Database\Database::getConnection('default')\n#4 /var/www/html/core/lib/Drupal/Component/DependencyInjection/PhpArrayContainer.php(79): call_user_func_array('Drupal\\Core\\Dat...', Array)\n#5 /var/www/html/core/lib/Drupal/Component/DependencyInjection/Container.php(171): Drupal\Component\DependencyInjection\PhpArrayContainer->createService(Array, 'database')\n#6 /var/www/html/core/lib/Drupal/Component/DependencyInjection/PhpArrayContainer.php(260): Drupal\Component\DependencyInjection\Container->get('database', 1)\n#7 /var/www/html/core/lib/Drupal/Component/DependencyInjection/PhpArrayContainer.php(62): Drupal\Component\DependencyInjection\PhpArrayContainer->resolveServicesAndParameters(Array)\n#8 /var/www/html/core/lib/Drupal/Component/DependencyInjection/Container.php(171): Drupal\Component\DependencyInjection\PhpArrayContainer->createService(Array, 'cache.container')\n#9 /var/www/html/core/lib/Drupal/Core/DrupalKernel.php(543): Drupal\Component\DependencyInjection\Container->get('cache.container')\n#10 /var/www/html/core/lib/Drupal/Core/DrupalKernel.php(904): Drupal\Core\DrupalKernel->getCachedContainerDefinition()\n#11 /var/www/html/core/lib/Drupal/Core/DrupalKernel.php(476): Drupal\Core\DrupalKernel->initializeContainer()\n#12 /var/www/html/core/lib/Drupal/Core/DrupalKernel.php(692): Drupal\Core\DrupalKernel->boot()\n#13 /var/www/html/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))\n#14 {main}, referer: http://localhost:1180/core/install.php

I can connect to the database through both:

mysql -u drupal -pmypass -h 127.0.0.1 website_pet
mysql -u drupal -pmypass -h localhost website_pet

so I know it's not a permission error to the database. The website is hosted in the cloud, and I'm accessing it through a SSH tunnel (localhost:1180). If have tried setting selinux to permissive temporarily but that didn't help.

Can someone point to what's wrong with this?


UPDATE: After some restarts etc the error changes a bit, the webpage shows:

Additional uncaught exception thrown while handling exception.
Original
Drupal\Core\Database\DatabaseExceptionWrapper: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away: CREATE TABLE {cache_default} ( `cid` VARCHAR(255) CHARACTER SET ascii BINARY NOT NULL DEFAULT '' COMMENT 'Primary Key: Unique cache ID.', `data` LONGBLOB NULL DEFAULT NULL COMMENT 'A collection of data to cache.', `expire` INT NOT NULL DEFAULT 0 COMMENT 'A Unix timestamp indicating when the cache entry should expire, or -1 for never.', `created` DECIMAL(14, 3) NOT NULL DEFAULT 0 COMMENT 'A timestamp with millisecond precision indicating when the cache entry was created.', `serialized` SMALLINT NOT NULL DEFAULT 0 COMMENT 'A flag to indicate whether content is serialized (1) or not (0).', `tags` LONGTEXT NULL DEFAULT NULL COMMENT 'Space-separated list of cache tags for this entry.', `checksum` VARCHAR(255) CHARACTER SET ascii COLLATE ascii_general_ci NOT NULL COMMENT 'The tag invalidation checksum when this entry was saved.', PRIMARY KEY (`cid`), INDEX `expire` (`expire`), INDEX `created` (`created`) ) ENGINE = InnoDB DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci COMMENT 'Storage for the cache API.'; Array ( ) in Drupal\views\ViewsData->cacheSet() (line 209 of /var/www/html/core/modules/views/src/ViewsData.php).

Drupal\views\ViewsData->cacheSet('views_data', Array) (Line: 257)
Drupal\views\ViewsData->getData() (Line: 160)
Drupal\views\ViewsData->get('block_content') (Line: 91)
Drupal\views\Plugin\Derivative\ViewsEntityRow->getDerivativeDefinitions(Array) (Line: 101)
Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator->getDerivatives(Array) (Line: 87)
Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator->getDefinitions() (Line: 284)
Drupal\Core\Plugin\DefaultPluginManager->findDefinitions() (Line: 175)
Drupal\Core\Plugin\DefaultPluginManager->getDefinitions() (Line: 147)
views_theme(Array, 'module', 'views', 'core/modules/views') (Line: 447)
Drupal\Core\Theme\Registry->processExtension(Array, 'views', 'module', 'views', 'core/modules/views') (Line: 334)
Drupal\Core\Theme\Registry->build() (Line: 233)
Drupal\Core\Theme\Registry->get() (Line: 86)
Drupal\Core\Utility\ThemeRegistry->initializeRegistry() (Line: 67)
Drupal\Core\Utility\ThemeRegistry->__construct('theme_registry:runtime:seven', Object, Object, Array, 1) (Line: 253)
Drupal\Core\Theme\Registry->getRuntime() (Line: 142)
Drupal\Core\Theme\ThemeManager->render('html', Array) (Line: 437)
Drupal\Core\Render\Renderer->doRender(Array, 1) (Line: 195)
Drupal\Core\Render\Renderer->render(Array, 1) (Line: 139)
Drupal\Core\Render\Renderer->Drupal\Core\Render\{closure}() (Line: 582)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 140)
Drupal\Core\Render\Renderer->renderRoot(Array) (Line: 66)
Drupal\Core\Render\BareHtmlPageRenderer->renderBarePage(Array, Object, 'install_page', Array) (Line: 76)
Drupal\Core\ProxyClass\Render\BareHtmlPageRenderer->renderBarePage(Array, Object, 'install_page', Array) (Line: 1067)
install_display_output(Array, Array) (Line: 167)
install_drupal(Object) (Line: 44)
Additional
Drupal\Core\Database\DatabaseExceptionWrapper: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away: CREATE TABLE {cache_default} ( `cid` VARCHAR(255) CHARACTER SET ascii BINARY NOT NULL DEFAULT '' COMMENT 'Primary Key: Unique cache ID.', `data` LONGBLOB NULL DEFAULT NULL COMMENT 'A collection of data to cache.', `expire` INT NOT NULL DEFAULT 0 COMMENT 'A Unix timestamp indicating when the cache entry should expire, or -1 for never.', `created` DECIMAL(14, 3) NOT NULL DEFAULT 0 COMMENT 'A timestamp with millisecond precision indicating when the cache entry was created.', `serialized` SMALLINT NOT NULL DEFAULT 0 COMMENT 'A flag to indicate whether content is serialized (1) or not (0).', `tags` LONGTEXT NULL DEFAULT NULL COMMENT 'Space-separated list of cache tags for this entry.', `checksum` VARCHAR(255) CHARACTER SET ascii COLLATE ascii_general_ci NOT NULL COMMENT 'The tag invalidation checksum when this entry was saved.', PRIMARY KEY (`cid`), INDEX `expire` (`expire`), INDEX `created` (`created`) ) ENGINE = InnoDB DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci COMMENT 'Storage for the cache API.'; Array ( ) in Drupal\views\ViewsData->cacheSet() (line 209 of /var/www/html/core/modules/views/src/ViewsData.php).

Drupal\views\ViewsData->cacheSet('views_data:block_content', Array) (Line: 176)
Drupal\views\ViewsData->get('block_content') (Line: 91)
Drupal\views\Plugin\Derivative\ViewsEntityRow->getDerivativeDefinitions(Array) (Line: 101)
Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator->getDerivatives(Array) (Line: 87)
Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator->getDefinitions() (Line: 284)
Drupal\Core\Plugin\DefaultPluginManager->findDefinitions() (Line: 175)
Drupal\Core\Plugin\DefaultPluginManager->getDefinitions() (Line: 147)
views_theme(Array, 'module', 'views', 'core/modules/views') (Line: 447)
Drupal\Core\Theme\Registry->processExtension(Array, 'views', 'module', 'views', 'core/modules/views') (Line: 334)
Drupal\Core\Theme\Registry->build() (Line: 233)
Drupal\Core\Theme\Registry->get() (Line: 86)
Drupal\Core\Utility\ThemeRegistry->initializeRegistry() (Line: 67)
Drupal\Core\Utility\ThemeRegistry->__construct('theme_registry:runtime:seven', Object, Object, Array, 1) (Line: 253)
Drupal\Core\Theme\Registry->getRuntime() (Line: 142)
Drupal\Core\Theme\ThemeManager->render('html', Array) (Line: 437)
Drupal\Core\Render\Renderer->doRender(Array, 1) (Line: 195)
Drupal\Core\Render\Renderer->render(Array, 1) (Line: 139)
Drupal\Core\Render\Renderer->Drupal\Core\Render\{closure}() (Line: 582)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 140)
Drupal\Core\Render\Renderer->renderRoot(Array) (Line: 66)
Drupal\Core\Render\BareHtmlPageRenderer->renderBarePage(Array, 'Error', 'install_page', Array) (Line: 76)
Drupal\Core\ProxyClass\Render\BareHtmlPageRenderer->renderBarePage(Array, 'Error', 'install_page', Array) (Line: 1067)
install_display_output(Array, Array, Array) (Line: 260)
_drupal_log_error(Array, 1) (Line: 602)
_drupal_exception_handler(Object)
TSG
  • 4,242
  • 9
  • 61
  • 121

3 Answers3

3

There is a high chance this is SELinux gets involved into. Suppose because you are using 127.0.0.1 instead of localhost? I've had exactly the same error (but I'm really tried to connect to a remote host) and this answer helped me.

Thus, to be shortly:

setsebool -P httpd_can_network_connect_db 1
Vertigo
  • 1,847
  • 22
  • 19
  • I also had the same issue in Redhat distro. I had to make SELinux permissive to make it work. This SO answer was also helpful for me - https://stackoverflow.com/a/51421397/719081 – Zuhair Jul 08 '21 at 06:16
1

After much experimentation the problem sees to stem from installing PHP 7.2 on CentOS 7. (Which only ships with PHP 5.x)

Could never get it to work, but after building a new ubuntu which includes PHP 7 all seemed to work.

I could not trace this down to a single module/setting/package. But the solution is don't attempt on CentOS 7 (using remi repo for PHP7x packages)

TSG
  • 4,242
  • 9
  • 61
  • 121
0

First of all, it's a bad idea to use the root user for Drupal - should somebody find a weakness you've just let them access to everything on your database, and probably the rest of the system too. While this might be a dev system it is unwise to get into bad habits.

Anyway on to the issue. One thing to be aware of with MySQL is that localhost connections are normally diverted via a Unix domain Socket (i.e. a "file" in the filesystem) for performance reasons. Sometimes you get a permission denied failure because the socket is not present or is not accessible to the user making the connection, or the user has included "localhost" in the MySQL permissions table when it should be "127.0.0.1" or vice versa. Finally, in this mix, with IPv6, some OS network stacks now default to "::1" (the IPv6 equivalent of 127.0.0.1) instead.

Double check what the DB permissions table contains: all three (host, user, password) must match for access to be granted. My experience is that the wildcard-host: "%" is not reliable, so I avoid using it.

You could try setting the 127... to "localhost" in both drupal and in the permissions table in MySQL.

HTH

rivimey
  • 921
  • 1
  • 7
  • 24
  • I switched the username to 'drupal', added both drupal@localhost and drupal@127.0.0.1 to the MySQL users tables. Same error. I confirmed mysql listening on ip4 0.0.0.0 only. I confirmed through mysql commandline that user drupal can access this table from localhost and 127.0.0.1 hosts – TSG Jun 07 '19 at 11:43