简体   繁体   中英

How to connect to a SQL Database-s2 from a .php application in BlueMix

So I have created a www site on my private server. It's basically a PHP + javascript page. Now I need to move it to BlueMix. The only problem is that while I used a MySQL db on my private server now I need to use SQL Database-s2 and all (both my php page and the db) on BlueMix.

Mike

You have to parse db information from VCAP_SERVICES\\service credentials.

If your database service is "SQL Database" you can connect to SQLDB using this example code:

//parse VCAP_SERVICES Environment variable
$vcap_services = $_ENV["VCAP_SERVICES"];
$services_json = json_decode($vcap_services,true);
$sqldb = $services_json["sqldb"];
if (empty($sqldb)) {
    echo "No sqldb service instance is bound. Please bind a sqldb service instance";
    return;
}

//Get Credentials object (db,host,port,username,password)
$sqldb_config = $services_json["sqldb"][0]["credentials"];

$conn_string = "DRIVER={IBM DB2 ODBC DRIVER};DATABASE=".
   $sqldb_config["db"].
   ";HOSTNAME=".
   $sqldb_config["host"].
   ";PORT=".
   $sqldb_config["port"].
   ";PROTOCOL=TCPIP;UID=".
   $sqldb_config["username"].
   ";PWD=".
   $sqldb_config["password"].
   ";";


$conn = db2_connect($conn_string, '', ''); //db connection

the information is automatically retrieved from the VCAP_SERVICES. The application read the env. variable and retrieves automatically the username,host,db password field.

but you can manually set db, host, port, username, password looking at service credentials of the service:

How to retrieve credentials information:

  • from DASHBOARD UI

    1. click on your service icon in the dashboard UI
    2. in the new page, in the left menu, click on "Service Credentials"
    3. If you don't see any credentials but you see the button "ADD CREDENTIALS", click on this button, edit the name credentials as you want e click on "ADD" button.
    4. In this way you will see the service information (credentials)
  • from CF command line

    cf env your_app_name

In the code we are using DB2 module: use the following buildpack https://github.com/ibmdb/db2heroku-buildpack-php when you are using SQLDB with PHP. It will install the 'ibm_db2' php module for your use.

You can set the buildpack when you push your application on Bluemix:

cf push <your_app_name> -b https://github.com/ibmdb/db2heroku-buildpack-php

You can deploy your application on Bluemix using the PHP buildpack. After creating the SQL Database instance and binding it to the PHP application on Bluemix, to connect to the SQLDB service in Bluemix you can use db2_connect with the credentials retrieved from the VCAP_SERVICES environment variable. I don't know your business requirements, however if you want to keep consistency across the environments and don't want to migrate to another RDBMS you could try ClearDB , that is a reliable, fault tolerant, geo-distributed database-as-a-service for your MySQL powered applications.

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