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

时间:2019-06-07 11:00:28

标签: pdo drupal-8

我是Drupal 8的新手,并且从构建示例站点开始。我的所有设置都正确,但是下面出现了PDO连接错误。我很确定这与主机名有关,但我不知道是什么。

我的settings.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错误日志显示:

  

[Fri Jun 07 03:47:32.183065 2019] [php7:notice] [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)\ 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 [内部功能]:   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,   '数据库')\ 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},引荐来源: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隧道(localhost:1180)访问它。如果尝试将selinux暂时设置为宽松,但这没有帮助。

有人可以指出这是怎么回事吗?


更新:重新启动等后,错误有所改变,网页显示:

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)

3 个答案:

答案 0 :(得分:0)

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

无论如何,还是要解决这个问题。 MySQL需要注意的一件事是,出于性能原因,通常通过Unix域套接字(即文件系统中的“文件”)来转移本地主机连接。有时,由于套接字不存在或建立连接的用户无法访问套接字,或者用户在MySQL权限表中将“ localhost”包含在“ 127.0.0.1”中时,您会得到拒绝权限失败,反之亦然。最后,在混合使用IPv6的情况下,某些OS网络堆栈现在默认为“ :: 1”(IPv6等效于127.0.0.1)。

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

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

HTH

答案 1 :(得分:0)

经过大量实验后,问题似乎源于在CentOS 7上安装PHP 7.2(仅PHP 5.x附带)

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

我无法追溯到单个模块/设置/包装。但是解决方案是不要在CentOS 7上尝试(对PHP7x软件包使用remi repo)

答案 2 :(得分:0)

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

因此,不久之后:

setsebool -P httpd_can_network_connect_db 1