Drupal 8 连接获得 PDO 权限被拒绝



我是Drupal 8的新手,我从构建一个示例站点开始。 我已经正确设置了一切,但是我在下面收到PDO连接错误。 我很确定这与主机名有关,但我无法弄清楚是什么。

我的设置.php如下所示:

$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',
);

在my.cnf中,我添加了:

max_allowed_packet=100M 

我的httpd错误日志显示:

[周五 6 月 07 日 03:47:32.183065 2019] [php7:通知] [pid 19631] [客户端 ::1:58796] PDOException: SQLSTATE[HY000] [2002] 权限被拒绝/var/www/html/core/lib/Drupal/Component/DependencyInjection/PhpArrayContainer.php 在第 79 行 #0/var/www/html/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php(420): PDO->__construct('mysql:host=127....', 'drupal', 'mypass', Array)#1/var/www/html/core/lib/Drupal/Core/Database/Database.php(371): Drupal\Core\Database\Driver\mysql\Connection::open(Array)#2/var/www/html/core/lib/Drupal/Core/Database/Database.php(166): Drupal\Core\Database\Database::openConnection('default', 'default')#3 [内部函数]: Drupal\Core\Database\Database::getConnection('default')#4/var/www/html/core/lib/Drupal/Component/DependencyInjection/PhpArrayContainer.php(79): call_user_func_array('Drupal\\Core\\Dat...', Array)#5/var/www/html/core/lib/Drupal/Component/DependencyInjection/Container.php(171): Drupal\Component\DependencyInjection\PhpArrayContainer->createService(Array, 'database')#6/var/www/html/core/lib/Drupal/Component/DependencyInjection/PhpArrayContainer.php(260): Drupal\Component\DependencyInjection\Container->get('database', 1)#7/var/www/html/core/lib/Drupal/Component/DependencyInjection/PhpArrayContainer.php(62): Drupal\Component\DependencyInjection\PhpArrayContainer->resolveServicesAndParameters(Array)#8/var/www/html/core/lib/Drupal/Component/DependencyInjection/Container.php(171): Drupal\Component\DependencyInjection\PhpArrayContainer->createService(Array, 'cache.container')#9/var/www/html/core/lib/Drupal/Core/DrupalKernel.php(543): Drupal\Component\DependencyInjection\Container->get('cache.container')#10/var/www/html/core/lib/Drupal/Core/DrupalKernel.php(904): Drupal\Core\DrupalKernel->getCachedContainerDefinition()#11/var/www/html/core/lib/Drupal/Core/DrupalKernel.php(476): Drupal\Core\DrupalKernel->initializeContainer()#12/var/www/html/core/lib/Drupal/Core/DrupalKernel.php(692): Drupal\Core\DrupalKernel->boot()#13/var/www/html/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))#14 {main}, referer: http://localhost:1180/core/install.php

我可以通过以下两种方式连接到数据库:

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

所以我知道这不是数据库的权限错误。 该网站托管在云中,我正在通过SSH隧道(本地主机:1180)访问它。 如果尝试暂时将 selinux 设置为宽松,但这没有帮助。

有人可以指出这有什么问题吗?


更新:经过一些重新启动等后,错误发生了一些变化,网页显示:

Additional uncaught exception thrown while handling exception.
Original
DrupalCoreDatabaseDatabaseExceptionWrapper: 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 DrupalviewsViewsData->cacheSet() (line 209 of /var/www/html/core/modules/views/src/ViewsData.php).
DrupalviewsViewsData->cacheSet('views_data', Array) (Line: 257)
DrupalviewsViewsData->getData() (Line: 160)
DrupalviewsViewsData->get('block_content') (Line: 91)
DrupalviewsPluginDerivativeViewsEntityRow->getDerivativeDefinitions(Array) (Line: 101)
DrupalComponentPluginDiscoveryDerivativeDiscoveryDecorator->getDerivatives(Array) (Line: 87)
DrupalComponentPluginDiscoveryDerivativeDiscoveryDecorator->getDefinitions() (Line: 284)
DrupalCorePluginDefaultPluginManager->findDefinitions() (Line: 175)
DrupalCorePluginDefaultPluginManager->getDefinitions() (Line: 147)
views_theme(Array, 'module', 'views', 'core/modules/views') (Line: 447)
DrupalCoreThemeRegistry->processExtension(Array, 'views', 'module', 'views', 'core/modules/views') (Line: 334)
DrupalCoreThemeRegistry->build() (Line: 233)
DrupalCoreThemeRegistry->get() (Line: 86)
DrupalCoreUtilityThemeRegistry->initializeRegistry() (Line: 67)
DrupalCoreUtilityThemeRegistry->__construct('theme_registry:runtime:seven', Object, Object, Array, 1) (Line: 253)
DrupalCoreThemeRegistry->getRuntime() (Line: 142)
DrupalCoreThemeThemeManager->render('html', Array) (Line: 437)
DrupalCoreRenderRenderer->doRender(Array, 1) (Line: 195)
DrupalCoreRenderRenderer->render(Array, 1) (Line: 139)
DrupalCoreRenderRenderer->DrupalCoreRender{closure}() (Line: 582)
DrupalCoreRenderRenderer->executeInRenderContext(Object, Object) (Line: 140)
DrupalCoreRenderRenderer->renderRoot(Array) (Line: 66)
DrupalCoreRenderBareHtmlPageRenderer->renderBarePage(Array, Object, 'install_page', Array) (Line: 76)
DrupalCoreProxyClassRenderBareHtmlPageRenderer->renderBarePage(Array, Object, 'install_page', Array) (Line: 1067)
install_display_output(Array, Array) (Line: 167)
install_drupal(Object) (Line: 44)
Additional
DrupalCoreDatabaseDatabaseExceptionWrapper: 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 DrupalviewsViewsData->cacheSet() (line 209 of /var/www/html/core/modules/views/src/ViewsData.php).
DrupalviewsViewsData->cacheSet('views_data:block_content', Array) (Line: 176)
DrupalviewsViewsData->get('block_content') (Line: 91)
DrupalviewsPluginDerivativeViewsEntityRow->getDerivativeDefinitions(Array) (Line: 101)
DrupalComponentPluginDiscoveryDerivativeDiscoveryDecorator->getDerivatives(Array) (Line: 87)
DrupalComponentPluginDiscoveryDerivativeDiscoveryDecorator->getDefinitions() (Line: 284)
DrupalCorePluginDefaultPluginManager->findDefinitions() (Line: 175)
DrupalCorePluginDefaultPluginManager->getDefinitions() (Line: 147)
views_theme(Array, 'module', 'views', 'core/modules/views') (Line: 447)
DrupalCoreThemeRegistry->processExtension(Array, 'views', 'module', 'views', 'core/modules/views') (Line: 334)
DrupalCoreThemeRegistry->build() (Line: 233)
DrupalCoreThemeRegistry->get() (Line: 86)
DrupalCoreUtilityThemeRegistry->initializeRegistry() (Line: 67)
DrupalCoreUtilityThemeRegistry->__construct('theme_registry:runtime:seven', Object, Object, Array, 1) (Line: 253)
DrupalCoreThemeRegistry->getRuntime() (Line: 142)
DrupalCoreThemeThemeManager->render('html', Array) (Line: 437)
DrupalCoreRenderRenderer->doRender(Array, 1) (Line: 195)
DrupalCoreRenderRenderer->render(Array, 1) (Line: 139)
DrupalCoreRenderRenderer->DrupalCoreRender{closure}() (Line: 582)
DrupalCoreRenderRenderer->executeInRenderContext(Object, Object) (Line: 140)
DrupalCoreRenderRenderer->renderRoot(Array) (Line: 66)
DrupalCoreRenderBareHtmlPageRenderer->renderBarePage(Array, 'Error', 'install_page', Array) (Line: 76)
DrupalCoreProxyClassRenderBareHtmlPageRenderer->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)

这很有可能是 SELinux 参与其中的。假设是因为您使用的是127.0.0.1而不是localhost?我遇到了完全相同的错误(但我真的尝试连接到远程主机),这个答案对我有帮助。

因此,简而言之:

setsebool -P httpd_can_network_connect_db 1

经过大量实验,问题源于在 CentOS 7 上安装 PHP 7.2。 (仅随 PHP 5.x 一起提供)

永远无法让它工作,但是在构建了一个包含 PHP 7 的新 ubuntu 之后,一切似乎都可以工作。

我无法将其追溯到单个模块/设置/包。 但解决方案是不要尝试 CentOS 7(使用 remi 存储库为 PHP7x 软件包)

首先,使用 Drupal 的 root 用户是一个坏主意 - 如果有人发现弱点,你只是让他们访问你数据库上的所有内容,可能还有系统的其余部分。虽然这可能是一个开发系统,但养成坏习惯是不明智的。

无论如何,关于这个问题。MySQL需要注意的一件事是,出于性能原因,本地主机连接通常通过Unix域套接字(即文件系统中的"文件")转移。有时,您会收到权限被拒绝失败,因为套接字不存在或建立连接的用户无法访问,或者用户在MySQL权限表中包含"localhost",而它应该是"127.0.0.1",反之亦然。最后,在这种混合中,对于IPv6,一些操作系统网络堆栈现在默认为"::1"(相当于127.0.0.1的IPv6)。

仔细检查数据库权限表包含的内容:所有三个(主机、用户、密码)必须匹配才能授予访问权限。我的经验是通配符主机:"%"不可靠,所以我避免使用它。

您可以尝试在drupal和MySQL的权限表中将127...设置为"localhost"。

呵呵

最新更新