简体   繁体   English

Google App Engine:如何使用Zend DB连接到Cloud SQL

[英]Google App Engine: How to connect to Cloud SQL with Zend DB

I have a Zend Framework 1.12 application that uses the Zend DB Adapter class to connect with MySql. 我有一个Zend Framework 1.12应用程序,该应用程序使用Zend DB Adapter类与MySql连接。 I need to deploy it soon and am investigating Google App Engine. 我需要尽快部署它,并且正在调查Google App Engine。

My current problem is that I can't connect the application to Google Cloud SQL. 我当前的问题是我无法将应用程序连接到Google Cloud SQL。 This Google page, https://developers.google.com/appengine/docs/php/cloud-sql/ , gives an example using PHP and PDO. 此Google页面https://developers.google.com/appengine/docs/php/cloud-sql/给出了使用PHP和PDO的示例。 It uses a DSN as a parameter. 它使用DSN作为参数。

The Zend DB Adapter class does not take a DSN as a parameter, but instead constructs it from the other parameters it receives. Zend DB Adapter类不将DSN作为参数,而是从其接收的其他参数构造它。 I have tried to engineer the Zend DB Adapter params to give the correct DSN, but still not luck. 我试图设计Zend DB Adapter参数来提供正确的DSN,但仍然不走运。 Here's my attempt: 这是我的尝试:

$config = new Zend_Config(
    array(
        'database' => array(
            'adapter' => 'Pdo_Mysql',
            'params'  => array(
                'host'     => 'test-instance',
                'dbname'   => 'my_db_name',
                'username' => 'root',
                'password' => 'rootpassword',
                'charset'  => 'utf8',
                'driver_options'  => [
                    'unix_socket' => '/cloudsql/test-project'
                ]
            )
        )
    )
);

try {
    $this->_db = Zend_Db::factory($config->database);
    $this->_db->getConnection();
} catch (Exception $e){    
    Zend_Registry::get('logger')->debug('Cannot create the connection...');
    Zend_Registry::get('logger')->debug(print_r($e, true));
}

The exception in the message is 消息中的例外是

The mysql driver is not currently installed 当前未安装mysql驱动程序

Any ideas? 有任何想法吗?

At this stage I am just trying it from the GAE SDK on my desktop. 在此阶段,我只是通过台式机上的GAE SDK进行尝试。 I have not uploaded the application to the cloud. 我尚未将应用程序上传到云。 I doubt this is the issue though. 我怀疑这是问题所在。 (My workstation IP is authorised to use the Cloud Sql instance). (我的工作站IP被授权使用Cloud Sql实例)。

I have not used the Zend Framework with AppEngine myself, but in my experience (not that I have found it documented anywhere), if you are connecting to Cloud SQL from an AppEngine app using the "root" user, you should not provide the password, or it fails to connect. 我自己没有将Zend Framework与AppEngine一起使用,但是根据我的经验(并不是我发现它在任何地方都有记录),如果您使用“ root”用户从AppEngine应用连接到Cloud SQL,则不应提供密码,否则无法连接。

Also as shown in the documentation (here: https://developers.google.com/appengine/docs/php/cloud-sql/#PHP_Connecting_to_your_Cloud_SQL_instance ), the unix_socket option should be in the format /cloudsql/project-name:instance-name 同样如文档所示(在这里: https : //developers.google.com/appengine/docs/php/cloud-sql/#PHP_Connecting_to_your_Cloud_SQL_instance ),unix_socket选项的格式应为/ cloudsql / project-name:instance-名称

Therefore I would try rewriting your $config as follows: 因此,我将尝试如下重写$ config:

$config = new Zend_Config(
    array(
        'database' => array(
            'adapter' => 'Pdo_Mysql',
            'params'  => array(
                'host'     => '',
                'dbname'   => 'my_db_name',
                'username' => 'root',
                'password' => '',
                'charset'  => 'utf8',
                'driver_options'  => [
                    'unix_socket' => '/cloudsql/test-project:test-instance'
                ]
            )
        )
    )
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM