Drupal 8 连接获得 PDO 权限被拒绝
Posted
技术标签:
【中文标题】Drupal 8 连接获得 PDO 权限被拒绝【英文标题】:Drupal 8 connection getting PDO permission denied 【发布时间】:2019-10-22 21:16:59 【问题描述】:我是 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] [client ::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)
【问题讨论】:
【参考方案1】:SELinux 很有可能参与其中。假设因为您使用的是127.0.0.1
而不是localhost
?我遇到了完全相同的错误(但我真的试图连接到远程主机),这个 answer 帮助了我。
因此,很快:
setsebool -P httpd_can_network_connect_db 1
【讨论】:
我在 Redhat 发行版中也遇到了同样的问题。我必须让 SELinux 允许它工作。这个 SO 答案对我也有帮助 - ***.com/a/51421397/719081【参考方案2】:经过大量实验后,问题似乎源于在 CentOS 7 上安装 PHP 7.2。(仅随 PHP 5.x 提供)
永远无法让它工作,但在构建了一个包含 PHP 7 的新 ubuntu 之后,一切似乎都可以工作了。
我无法将其追溯到单个模块/设置/包。但解决方案是不要在 CentOS 7 上尝试(对 PHP7x 软件包使用 remi repo)
【讨论】:
【参考方案3】:首先,对 Drupal 使用 root 用户是个坏主意——如果有人发现你的弱点,你只是让他们访问你数据库上的所有内容,可能还有系统的其余部分。虽然这可能是一个开发系统,但养成坏习惯是不明智的。
不管怎样,这个问题。 MySQL 需要注意的一件事是,出于性能原因,localhost 连接通常通过 Unix 域套接字(即文件系统中的“文件”)转移。有时您会收到权限被拒绝失败,因为套接字不存在或对建立连接的用户不可访问,或者用户在 MySQL 权限表中包含了“localhost”,而它应该是“127.0.0.1”,反之亦然。最后,在这种与 IPv6 的混合中,一些操作系统网络堆栈现在默认为“::1”(IPv6 相当于 127.0.0.1)。
仔细检查数据库权限表包含的内容:所有三个(主机、用户、密码)必须匹配才能授予访问权限。我的经验是 wildcard-host: "%" 不可靠,所以我避免使用它。
您可以尝试在 drupal 和 MySQL 的权限表中将 127... 设置为“localhost”。
HTH
【讨论】:
我将用户名切换为“drupal”,将 drupal@localhost 和 drupal@127.0.0.1 添加到 MySQL 用户表中。同样的错误。我确认 mysql 只在 ip4 0.0.0.0 上监听。我通过 mysql 命令行确认用户 drupal 可以从 localhost 和 127.0.0.1 主机访问此表以上是关于Drupal 8 连接获得 PDO 权限被拒绝的主要内容,如果未能解决你的问题,请参考以下文章
SQLSTATE[HY000] [2002] 权限被拒绝 Laravel PDO 驱动程序(凭证通过工匠迁移工作)
“连接被远程接口拒绝”使用 PDO 连接到 Firebird 3
在docker上使用laravel时无法连接到mysql数据库(未找到,pdo,连接被拒绝)
令人困惑的仅 PDO 问题:无法通过套接字连接/访问被拒绝/无法连接到服务器(共享主机)
Drupal:PDOException:SQLSTATE [HY000] [2002]连接在lock_may_be_available()中被拒绝