gpt4 book ai didi

使用 Laravel 通过 SSL 连接 MySQL

转载 作者:行者123 更新时间:2023-12-03 09:31:58 25 4
gpt4 key购买 nike

我在 Laravel 应用程序中通过 ssl 连接到我的数据库时出现问题。我的配置如下:

       'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
'sslmode' => 'require',
'options' => array(
PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false,
PDO::MYSQL_ATTR_SSL_KEY => '/certs/client-key.pem',
PDO::MYSQL_ATTR_SSL_CERT => '/certs/client-cert.pem',
PDO::MYSQL_ATTR_SSL_CA => '/certs/ca.pem',
),
],

DB_CONNECTION=mysql
DB_HOST=xxx.xxx.xxx.xxx
DB_PORT=3306
DB_DATABASE=db_name
DB_USERNAME=db_user
DB_PASSWORD=xxxx

这些设置在 MySQL 服务器上给了我以下错误。
2018-10-30T09:18:25.403712Z 71 [Note] Access denied for user 'user'@'xxx.xxx.xxx.xxx' (using password: YES)

通过 mysql-client 它可以完美地从客户端服务器运行。
mysql -u user -p -h xxx.xxx.xxx.xxx --ssl-ca=/certs/ca.pem --ssl-cert=/certs/client-cert.pem --ssl-key=/certs/client-key.pem

与上述命令连接时在客户端上执行的“\s”命令的输出。
mysql  Ver 15.1 Distrib 10.1.26-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2

Connection id: 16
Current database: database
Current user: user@xxx.xxx.xxx.xxx
SSL: Cipher in use is DHE-RSA-AES256-SHA
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server: MySQL
Server version: 5.7.24-0ubuntu0.18.10.1 (Ubuntu)
Protocol version: 10
Connection: 167.99.215.179 via TCP/IP
Server characterset: latin1
Db characterset: latin1
Client characterset: utf8mb4
Conn. characterset: utf8mb4
TCP port: 3306
Uptime: 23 min 29 sec

Threads: 2 Questions: 38 Slow queries: 0 Opens: 135 Flush tables: 1 Open tables: 128 Queries per second avg: 0.026
--------------

该应用程序在基于 php:7.2.11-fpm 的 docker 容器内运行图片。并配置了以下 php 扩展。
RUN docker-php-ext-install bcmath zip pdo_mysql json tokenizer
MySQL版本:
Server version: 5.7.24-0ubuntu0.18.10.1 (Ubuntu)

PHP版本:
PHP 7.2.11 (cli) (built: Oct 16 2018 00:46:29) ( NTS )

PDO 版本:
PDO

PDO support => enabled
PDO drivers => sqlite, mysql

pdo_mysql

PDO Driver for MySQL => enabled
Client API version => mysqlnd 5.0.12-dev - 20150407 - $Id: 38fea24f2847fa7519001be390c98ae0acafe387

OpenSSL 版本:
openssl

OpenSSL support => enabled
OpenSSL Library Version => OpenSSL 1.1.0f 25 May 2017
OpenSSL Header Version => OpenSSL 1.1.0f 25 May 2017
Openssl default config => /usr/lib/ssl/openssl.cnf

Directive => Local Value => Master Value
openssl.cafile => no value => no value
openssl.capath => no value => no value

对此有何想法?这让我忙了几个小时......

最佳答案

我终于解决了这个问题。上面的配置其实很好。我直接在 docker 容器中工作。由于某种原因,配置保存在缓存中。

以下命令未清除配置缓存:

php artisan config:clear
php artisan config:cache
php artisan cache:clear

当我创建一个新用户来连接数据库以测试某些内容时,我注意到了这一点。我使用新配置重建容器,现在一切正常。

关于使用 Laravel 通过 SSL 连接 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53061182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com