Google App Engine:如何使用Zend DB连接到Cloud SQL [英] Google App Engine: How to connect to Cloud SQL with Zend DB

查看:123
本文介绍了Google App Engine:如何使用Zend DB连接到Cloud SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Zend DB Adapter类与MySql连接的Zend Framework 1.12应用程序。我需要尽快进行部署,并正在调查Google App Engine。



目前的问题是我无法将应用程序连接到Google Cloud SQL。此Google页面 https://developers.google.com/appengine/docs/ php / cloud-sql / ,给出了一个使用PHP和PDO的例子。它使用DSN作为参数。



Zend DB Adapter类没有将DSN作为参数,而是从其接收的其他参数构造它。我试图设计Zend DB Adapter参数来给出正确的DSN,但仍然不是运气。这里是我的尝试:

$ $ p $ $ code $ $ config $ new Zend_Config
array $ b $'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'
]



);

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

消息中的异常是


mysql驱动程序目前尚未安装


有什么想法?



在这个阶段,我只是从桌面上的GAE SDK尝试它。我没有将应用程序上传到云端。我怀疑这是问题。 (我的工作站IP被授权使用Cloud Sql实例)。

解决方案

我自己没有使用Zend Framework和AppEngine,但根据我的经验(不是我已经发现它在任何地方记录),如果您使用root用户从AppEngine应用程序连接到Cloud SQL,则不应提供密码或无法连接。



同样如文档所示(这里是: https://developers.google.com/appengine/docs/php/cloud-sql/#PHP_Connecting_to_your_Cloud_SQL_instance ),unix_socket选项的格式应该是/ cloudsql / project-name:instance-name



因此,我会尝试重写您的$ config,如下所示:

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



);


I have a Zend Framework 1.12 application that uses the Zend DB Adapter class to connect with MySql. I need to deploy it soon and am investigating Google App Engine.

My current problem is that I can't connect the application to Google Cloud SQL. This Google page, https://developers.google.com/appengine/docs/php/cloud-sql/, gives an example using PHP and PDO. It uses a DSN as a parameter.

The Zend DB Adapter class does not take a DSN as a parameter, but instead constructs it from the other parameters it receives. I have tried to engineer the Zend DB Adapter params to give the correct DSN, but still not luck. 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

Any ideas?

At this stage I am just trying it from the GAE SDK on my desktop. 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).

解决方案

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.

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

Therefore I would try rewriting your $config as follows:

$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'
                ]
            )
        )
    )
);

这篇关于Google App Engine:如何使用Zend DB连接到Cloud SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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