[英]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. 我正在尝试从本地开发的Google App PHP服务器连接到我的Google Cloud SQL。 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. 但是PHP没有看到
mysql_connect
函数,没有看到mysqli
类,并且在我尝试与PDO类连接时引发了could not find driver
异常。
I run my development PHP server with a command: 我使用以下命令运行开发PHP服务器:
"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: app.yaml
文件包含以下内容:
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? 如何在我的PHP开发服务器上使用Google Cloud SQL? Thank you.
谢谢。
You cannot connect to the real CloudSQL instance from your development server. 您无法从开发服务器连接到真实的CloudSQL实例。 You need to test against a local install of MySQL when developing your app.
开发应用程序时,您需要针对MySQL的本地安装进行测试。
Follow the instructions here: 请按照以下说明进行操作:
https://developers.google.com/appengine/downloads#Google_App_Engine_SDK_for_PHP 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 您也许可以事后运行该configure命令,但是我不确定TBH
听起来您需要在本地环境中安装mysql和mysqli扩展。
I have sold my problem. 我卖了我的问题。
php.ini-development
file in php
folder to php.ini
php
文件夹中的php.ini-development
文件重命名为php.ini
php.ini
php.ini
启用了所需的扩展 extension_dir = "ext"
in my php.ini
, so php will now see the extensions. php.ini
中将行extension_dir = "ext"
分解为注释,因此php现在将看到扩展名。 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. 您必须将php.ini复制到项目本地路径(与app.yaml文件相同的目录)中,且未注释pdo扩展名。
https://cloud.google.com/appengine/docs/standard/php/config/php_ini https://cloud.google.com/appengine/docs/standard/php/config/php_ini
It works for me. 这个对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.