如何从Yii2连接到RDS数据库? [英] How to connect to an RDS database from Yii2?

查看:130
本文介绍了如何从Yii2连接到RDS数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将基于Yii2的应用程序部署到AWS Elastic Beanstalk上,并且我已经在Elastic Beanstalk上使用数据库(已经具有表)创建了RDS实例.但是我收到此错误:"SQLSTATE [HY000] [2002] php_network_getaddresses:getaddrinfo失败:名称或服务未知"

I have deployed a Yii2 based app onto AWS Elastic Beanstalk, also I have created the RDS instance with a database (it already has tables) on Elastic Beanstalk. However I received this error: "SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known"

所有文件均已正确上传到AWS实例.

All the files are uploaded correctly to the AWS instance.

/common/config/main-local.php文件具有:

The file /common/config/main-local.php has:

'components' => [
        'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'mysql:host=',
            'dsn' => 'mysql:host=RDS_HOSTNAME:RDS_PORT;dbname=RDS_DB_NAME',
            'username' => 'RDS_USERNAME',
            'password' => 'RDS_PASSWORD',
            'charset' => 'utf8',
        ],

可能是什么问题?谢谢.

What could be wrong? Thanks.

推荐答案

我猜您想通过环境变量传递数据库信息.您可能要尝试如下修改代码.

I am guessing that you want pass db information through environment variables. You may want to try to revise the code as below.

'components' => [
    'db' => [
        'class' => 'yii\db\Connection',
        'dsn' => 'mysql:host=',
        'dsn' => 'mysql:host='.$_SERVER['RDS_HOSTNAME'].':'.$_SERVER['RDS_PORT'].';dbname='.$_SERVER['RDS_DB_NAME'],
        'username' => $_SERVER['RDS_USERNAME'],
        'password' => $_SERVER['RDS_PASSWORD'],
        'charset' => 'utf8',
    ],

您可以引用 http://strong.配置环境属性部分中的//docs.aws.amazon.com/elasticbeanstalk/latest/dg/AWSHowTo.RDS.html#rds-external-ec2classic .希望这行得通.

You can reference http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/AWSHowTo.RDS.html#rds-external-ec2classic in To configure environment properties section to configure your environment variables. Hope this works.

这篇关于如何从Yii2连接到RDS数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆