如何将Bluemix中的PHP应用程序转换为使用SQL数据库而不是mySQL数据库? [英] How can I convert a PHP application in Bluemix to use SQL Database instead of mySQL database?
问题描述
我一直在关注本教程:构建和部署使用PHP和MySQL的IBM Bluemix上的REST API 。
I have been following this tutorial: "Build and deploy a REST API on IBM Bluemix with PHP and MySQL."
本教程使用mySQL。我想使用SQL数据库服务,它使用DB2,我想。该教程实现一个REST API。当我尝试使用这些链接进行REST调用时,我没有得到任何回报。
The tutorial uses mySQL. I want to use the SQL Database service, which uses DB2, I think. The tutorial implements a REST API. When I try making a REST call using these links I get nothing in return.
例如,以下是我如何使用我的Bluemix应用程序的REST API:
For example, here's how I use my Bluemix application's REST API:
- http://products-api-db2-test2-5.mybluemix.net/v1/products.json
- http://products-api-db2-test2-5.mybluemix.net/v1/products
- http://products-api-db2-test2-5.mybluemix.net/v1/products.json
- http://products-api-db2-test2-5.mybluemix.net/v1/products
我从下面的链接下载了源代码,并将 manifest.yml
给它一个独特的名字和主持人。我也试图编辑 index.php
连接到SQL数据库服务,而不是mySQL数据库服务。
I have downloaded the source code from the link below and edited the manifest.yml
to give it a unique name and host. I have also attempted to edit the index.php
to connect to the SQL Database service as opposed to the mySQL database service.
我的代码存储库位于: vvaswani | product-api 。
My code repository is here: vvaswani | products-api.
将源上传到Bluemix后,我使用以下查询在数据库中创建一个表:
After uploading the source to Bluemix, I create a table in the database using the following query:
CREATE TABLE products
(
id INT,
name VARCHAR(5),
price decimal(5,2)
);
我还使用以下查询填充数据库:
I also populate the database with with the following query:
INSERT INTO products (id, name, price) VALUES
(1, 'Garden spade', 15.99),
(2, 'Cotton hammock', 54.50),
(3, 'Single airbed', 35.49);
我将数据库绑定到我刚刚上传的应用程序(这是本教程的源代码)。但是如上所述,当我尝试访问REST端点时,我得到一个空白页。我想我应该在我的网页浏览器中看到JSON。所以我认为我的应用程序和数据库之间的连接有问题。
I bind the database to the app I just uploaded (which is the source code from the tutorial). But as I said above, when I try accessing the REST endpoint,s I get a blank page. I think I should be seeing JSON in my web browser. So I think there is something wrong with my connection between my app and database.
在 index.php
文件中,有一个指定与mySQL数据库的连接的部分。
In the index.php
file, there is a section specifying the connection to the mySQL database.
// get MySQL service configuration from BlueMix
$services = getenv("VCAP_SERVICES");
$services_json = json_decode($services, true);
$mysql_config = $services_json["mysql-5.5"][0]["credentials"];
$db = $mysql_config["name"];
$host = $mysql_config["host"];
$port = $mysql_config["port"];
$username = $mysql_config["user"];
$password = $mysql_config["password"];
// initialize Eloquent ORM
$capsule = new Capsule;
// use for BlueMix development
$capsule->addConnection(array(
'driver' => 'mysql',
'host' => $host,
'port' => $port,
'database' => $db,
'username' => $username,
'password' => $password,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => ''
));
我想我需要编辑这个,但我不完全确定如何。我已经尝试了几件事情。例如将 $ services_json [mysql-5.5] [0]
更改为 $ services_json [sqldb] [0]
。我也尝试从 driver'=> 'mysql'
to 'driver'=> 'sqldb'
。
I think I need to edit this but I am not exactly sure how to. I have tried a few things. For example changing the $services_json["mysql-5.5"][0]
to $services_json["sqldb"][0]
. I also tried changing the driver from driver' => 'mysql'
to 'driver' => 'sqldb'
.
我认为凭据可能很重要,所以在这里。 mySQL凭据:
I think the credentials are probably important so here they are. mySQL credentials:
{
"mysql-5.5": [
{
"name": "mysql-338",
"label": "mysql-5.5",
"plan": "100",
"credentials": {
"name": "d80979f54940b447ab178d6b2ea42a7f6",
"hostname": "198.11.234.66",
"host": "198.11.234.66",
"port": 3307,
"user": "uyhDjPNyxI357",
"username": "uyhDjPNyxI357",
"password": "XXXXXXXX",
"uri": "mysql://uyhDjPNyxI357:pZoFBeIIg0i16@198.11.234.66:3307/d80979f54940b447ab178d6b2ea42a7f6"
}
}
]
}
SQL数据库凭据:
{
"sqldb": [
{
"name": "SQL Database-78",
"label": "sqldb",
"plan": "sqldb_free",
"credentials": {
"port": 50000,
"db": "SQLDB",
"username": "user04128",
"host": "75.126.155.153",
"hostname": "75.126.155.153",
"jdbcurl": "jdbc:db2://75.126.155.153:50000/SQLDB",
"uri": "db2://user04128:dAxOPnUHTJ6G@75.126.155.153:50000/SQLDB",
"password": "XXXXXXXX"
}
}
]
}
似乎mySQL数据库凭据有一个name属性在索引中用于引用数据库。其中SQL数据库凭据具有db属性。所以我可以在此期间尝试玩。
It seems that the mySQL database credentials have a name attribute that is used in the index to reference the database. Where as the SQL Database credentials have a db property. So I might try playing around with that in the meantime.
对于长篇文章,对不起。所有这一切都是我的新生。
Sorry for the long post. All of this is new too me.
干杯。
推荐答案
如果要通过Bluemix(通过Cloud Foundry)运行它,您将需要一个带有DB2驱动程序的PHP buildpack 支持。这应该工作: https://github.com/ibmdb/db2heroku-buildpack-php。
If you want to run it on Bluemix (via Cloud Foundry) you will need a PHP buildpack with DB2 driver support. This one should work: https://github.com/ibmdb/db2heroku-buildpack-php.
工作示例这里:
提示:排除这样的问题,尝试 cf logsAPP_NAME--recent
命令。阅读更多 here
Tip: to troubleshoot problems like this try the cf logs "APP_NAME" --recent
command. Read more here
这篇关于如何将Bluemix中的PHP应用程序转换为使用SQL数据库而不是mySQL数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!