我正在尝试使用 CodeIgniter PHP 框架版本 3 从 Heroku 连接到 amazon RDS MySQL。我按照 Heroku documentation about Amazon RDS 中列出的所有步骤进行操作。但无法弄清楚我做错了什么?
这是我的 CodeIgniter 数据库设置:
$db['rds'] = array(
'dsn' => '',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE,
'port' => 3306,
'encrypt' => [
'ssl_ca' => APPPATH."../.config/rds-combined-ca-bundle.pem",
'ssl_verify' => TRUE
]
);
$db['rds']['hostname'] = getenv('RDS_DATABASE_HOSTNAME');
$db['rds']['username'] = getenv('RDS_DATABASE_USERNAME');
$db['rds']['password'] = getenv('RDS_DATABASE_PASSWORD');
$db['rds']['database'] = getenv('RDS_DATABASE_DBNAME');
但我不断收到此错误:
mysqli::real_connect(): (HY000/2002): Connection timed out /app/vendor/codeigniter/framework/system/database/drivers/mysqli/mysqli_driver.php 201
这是我检查的内容:
- 我检查了 RDS 安全组和 CIDR/IP - 出站 0.0.0.0/0 存在
- 我添加了一个单独的 MySQL 用户,该用户具有 SELECT、UPDATE、INSERT、DELETE 和 REQUIRE SSL GRANT 权限
- 将组合证书文件添加到项目根目录
.config/rds-combined-ca-bundle.pem
并部署在Heroku上 - 在本地计算机上,相同的代码可以正常工作
- 尝试重新启动 RDS 实例
如果某些设置错误,我还应该查看哪里?
最佳答案
您的 CodeIgniter 代码需要连接到您的数据库,而不是相反。 RDS 安全组中的出站规则无济于事;您需要一个入站规则(粗体添加):
You must grant Heroku dynos access to your RDS instance. The recommended way to do this is to configure the RDS instance to only accept SSL-encrypted connections from authorized users and configure the security group for your instance to permit ingress from all IPs, eg
0.0.0.0/0
.
由于这实际上在您的防火墙中打开了通往整个互联网的漏洞,因此遵循其余说明并仅接受来自授权用户的加密连接非常重要。
关于php - Heroku 连接到 RDS MySQL 时出现连接超时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52627369/