简体   繁体   中英

how to connect to the database in openshift application

I did as following

MySQL 5.1 database added. Please make note of these credentials:

    Root User: xxxxxxx
    Root Password: xxxxxxx
    Database Name: php


 Connection URL: mysql://$OPENSHIFT_MYSQL_DB_HOST:$OPENSHIFT_MYSQL_DB_PORT/

You can manage your new MySQL database by also embedding phpmyadmin-3.4. The phpmyadmin username and password will be the same as the MySQL credentials above.

phpMyAdmin 3.4 added. Please make note of these MySQL credentials again:

    Root User: xxxxxxx
    Root Password: xxxxxxx

    URL: https://php-doers.rhcloud.com/phpmyadmin/

and i try to connect db using bellow PDO code .but it does not work

$dbh = new PDO('mysql:host=mysql://$OPENSHIFT_MYSQL_DB_HOST:$OPENSHIFT_MYSQL_DB_PORT/;dbname=php', "xxxxxx, "xxxxxx");

I don't know what is the connection URL mean ?

There is an error in your connection string plus $OPENSHIFT_MYSQL_DB_* are env variables and need to be fetched via getenv php function.

So try the following:

define('DB_HOST', getenv('OPENSHIFT_MYSQL_DB_HOST'));
define('DB_PORT',getenv('OPENSHIFT_MYSQL_DB_PORT')); 
define('DB_USER',getenv('OPENSHIFT_MYSQL_DB_USERNAME'));
define('DB_PASS',getenv('OPENSHIFT_MYSQL_DB_PASSWORD'));
define('DB_NAME',getenv('OPENSHIFT_GEAR_NAME'));

$dsn = 'mysql:dbname='.DB_NAME.';host='.DB_HOST.';port='.DB_PORT;
$dbh = new PDO($dsn, DB_USER, DB_PASS);

Open php myadmin from webconsole , and on left corner you will get some Ip

That is like xx.xx.xx.xx:3306 , so your hostname is xx.xx.xx.xx

Ip address wil work , I have tried and worked for me

below code worked for me ,try this it might help you

var mysql = require('mysql');

var mysql = require('mysql');
exports.pool = mysql.createPool({
    host: process.env.OPENSHIFT_MYSQL_DB_HOST || 'localhost',
      port: process.env.OPENSHIFT_MYSQL_DB_PORT || '3306',
      user: process.env.OPENSHIFT_MYSQL_DB_USERNAME || 'root',
      password: process.env.OPENSHIFT_MYSQL_DB_PASSWORD ||  '',
      database: 'chichat' || 'chichat',
      multipleStatements : true,
      charset: 'utf8'
});

I would like to add some information to future reference. Today I am trying to deploy a Drupal installation to Open Shift (OS) and tried to configure MySql. I connected to OS via SSH (rhc ssh ), went to app-root/data/sites/default. Following cat settings.php to see how OS configure database. Here it is:

// When run from Drush, only $_ENV is available.  Might be a bug
if (array_key_exists('OPENSHIFT_APP_NAME', $_SERVER)) {
  $src = $_SERVER;
} else {
  $src = $_ENV;
}
$databases = array (
  'default' => 
  array (
    'default' => 
    array (
      'database' => $src['OPENSHIFT_APP_NAME'],
      'username' => $src['OPENSHIFT_MYSQL_DB_USERNAME'],
      'password' => $src['OPENSHIFT_MYSQL_DB_PASSWORD'],
      'host' => $src['OPENSHIFT_MYSQL_DB_HOST'],
      'port' => $src['OPENSHIFT_MYSQL_DB_PORT'],
      'driver' => 'mysql',
      'prefix' => '',
    ),
  ),
);
$conf['file_private_path'] = $src['OPENSHIFT_DATA_DIR'] . 'private/';
$conf['file_temporary_path'] = $src['OPENSHIFT_TMP_DIR'] . 'drupal/';

So I created a php folder into git repository (see OS documentation about Drupal), copied a clean Drupal install to it and paste code above to settings.php. After pushing to git, restarted OS app and worked!

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