简体   繁体   中英

Google App Engine PHP and Cloud SQL

I'm attempting to connect to my Google Cloud SQL from my local development Google App PHP server. But PHP doesn't see function mysql_connect , it doesn't see class mysqli and it raises exception could not find driver when I'm attempting to connect with PDO class.


I run my development PHP server with a command:

"C:\\Users\\pasha\\appengine-php-sdk-1.8.0\\google_appengine\\dev_appserver.py" --php_executable_path="C:\\Users\\pasha\\php\\php-cgi.exe" C:\\Work\\gaetest\\


The app.yaml file has these contents:

application: gaetest
version: 1
runtime: php
api_version: 1

handlers:

- url: /(.*)
  script: app/\1

The testing sript is:

define('MYSQL_HOST', "/cloudsql/sample-project:sample-sql-instance");
define('MYSQL_LOGIN', "sample-login");
define('MYSQL_PASSWORD', "sample-pass");
define('MYSQL_DB', "sample-db");

echo "\nfunction_exists('mysql_connect'):\n"; 
var_dump(function_exists('mysql_connect'));

echo "\nclass_exists('mysqli'):\n"; 
var_dump(class_exists('mysqli'));   

try{

    echo "\nUse PDO\n";
    $db = new PDO(
        'mysql:unix_socket='.MYSQL_HOST.';dbname='.MYSQL_DB.';charset=utf8',
        MYSQL_LOGIN,
        MYSQL_PASSWORD
    );
}catch(Exception $e){
    var_dump($e->__toString());
}

Output of the testing script:

function_exists('mysql_connect'):
bool(false)

class_exists('mysqli'):
bool(false)

Use PDO
string(371) "exception 'PDOException' with message 'could not find driver' in C:\Work\gaetest\app\test.php:19
Stack trace:
#0 C:\Work\gaetest\app\test.php(19): PDO->__construct('mysql:unix_sock...', 'sample-login', 'sample-pass')
#1 C:\Users\pasha\appengine-php-sdk-1.8.0\google_appengine\google\appengine\tools\devappserver2\php\setup.php(45): require('C:\Work\gaetest...')
#2 {main}"

How can I use Google Cloud SQL from my PHP development server? Thank you.

You cannot connect to the real CloudSQL instance from your development server. You need to test against a local install of MySQL when developing your app.

Follow the instructions here:

https://developers.google.com/appengine/downloads#Google_App_Engine_SDK_for_PHP

HOWEVER, you should use this configure command:

./configure --prefix=$PWD/installdir --enable-bcmath --with-mysql --with-pdo-mysql

You might be able to run that configure command after-the-fact, but I'm unsure TBH

听起来您需要在本地环境中安装mysql和mysqli扩展。

I have sold my problem.

  1. I have renamed php.ini-development file in php folder to php.ini
  2. Then I have enabled the needed extensions in my php.ini
  3. I have decommented the line extension_dir = "ext" in my php.ini , so php will now see the extensions.

That's all. Thank you.

You have to copy php.ini into the project local path (the same directory as the app.yaml file) with the pdo extension uncommented.

https://cloud.google.com/appengine/docs/standard/php/config/php_ini

It works for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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