如何在openshift应用程序中连接到数据库

时间:2021-12-11 05:00:20

I did as following

我做了如下

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

添加了MySQL 5.1数据库。请记下这些凭证:

    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来管理新的MySQL数据库。 phpmyadmin用户名和密码将与上面的MySQL凭据相同。

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

phpMyAdmin 3.4补充说。请再次记下这些MySQL凭据:

    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

我尝试使用波纹管PDO代码连接数据库。但它不起作用

$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 ?

我不知道连接URL是什么意思?

4 个解决方案

#1


19  

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

您的连接字符串中的错误加上$ OPENSHIFT_MYSQL_DB_ *是env变量,需要通过getenv php函数获取。

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);

#2


3  

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

从webconsole打开php myadmin,在左下角你会得到一些IP

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

这就像xx.xx.xx.xx:3306,因此您的主机名是xx.xx.xx.xx

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

Ip地址将工作,我已经尝试并为我工作

#3


2  

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'
});

#4


0  

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:

我想在将来参考中添加一些信息。今天我尝试将Drupal安装部署到Open Shift(OS)并尝试配置MySql。我通过SSH(rhc ssh)连接到操作系统,转到app-root / data / sites / default。下面的cat settings.php,看看OS如何配置数据库。这里是:

// 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!

所以我在git仓库中创建了一个php文件夹(参见关于Drupal的操作系统文档),将干净的Drupal安装复制到它并将上面的代码粘贴到settings.php。推送到git后,重启OS应用程序并开始工作!

#1


19  

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

您的连接字符串中的错误加上$ OPENSHIFT_MYSQL_DB_ *是env变量,需要通过getenv php函数获取。

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);

#2


3  

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

从webconsole打开php myadmin,在左下角你会得到一些IP

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

这就像xx.xx.xx.xx:3306,因此您的主机名是xx.xx.xx.xx

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

Ip地址将工作,我已经尝试并为我工作

#3


2  

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'
});

#4


0  

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:

我想在将来参考中添加一些信息。今天我尝试将Drupal安装部署到Open Shift(OS)并尝试配置MySql。我通过SSH(rhc ssh)连接到操作系统,转到app-root / data / sites / default。下面的cat settings.php,看看OS如何配置数据库。这里是:

// 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!

所以我在git仓库中创建了一个php文件夹(参见关于Drupal的操作系统文档),将干净的Drupal安装复制到它并将上面的代码粘贴到settings.php。推送到git后,重启OS应用程序并开始工作!