douzhi8244 2014-04-22 14:01
浏览 58

使用环境变量覆盖核心Magento DB连接

I am trying to use environment variables that are loaded into my Magento application to override the default database connection credentials.

I've managed to 'almost' get this working using the getenv('MY_CUSTOM_VAR'), however I am trying to use this same method to override the database credentials, so this sensitive data can be stored within the htaccess.

e.g

# .htaccess file
 SetEnv DB_USERNAME root
 SetEnv DB_PASSWORD password123
 SetEnv DB_HOST localhost

My App.php (within app/code/local/Mage/Core/Model/App.php - a copy of the one from the core pool)

// my function that overrides the core one
protected function _initBaseConfig()
{
    Varien_Profiler::start('mage::app::init::system_config');
    $this->_config->loadBase();

    /* Read DB connection config from environment variables */
    $connection = $this->_config->getNode('global/resources/default_setup/connection');
    $connection->setNode('host', getenv('DB_HOST'));
    $connection->setNode('username', getenv('DB_USERNAME'));
    $connection->setNode('password', getenv('DB_PASSWORD'));

    Varien_Profiler::stop('mage::app::init::system_config');
    return $this;
}

I want the $connection just created to be the default global database connection used site-wide, this code should appear to do that but if i enter pure random entries for DB_HOST/DB_PASSWORD etc.. it still connects to the database which suggests that it isn't overriding the default database settings configured with the Magento setup.

Any ideas on how to get this $connection to override and become the 'global' database connection?

P.S Apologies in advance if this question is similar to my previous one, the original question was a bit of a general question whereas this one is much more focused to particular section.

Update... I have open the local.xml within the app/etc directory and set the 'default setup' database connection to inactive (by changing the active node value to 0) and this as expected returns an error. It would appear the overriding function doesn't seem to like to override the initial db connection.. any ideas guys?

  • 写回答

1条回答 默认 最新

  • douqiao4450 2014-08-07 11:27
    关注

    For MySQL db you can do next:

    1. Copy file lib/Zend/Db/Adapter/Mysqli.php to app/code/local/Zend/Db/Adapter/Mysqli.php
    2. Open app/code/local/Zend/Db/Adapter/Mysqli.php and find _connect() function.
    3. In this function near line 317 you'll see:

      $_isConnected = @mysqli_real_connect(
      $this->_connection,
      $this->_config['host'],
      $this->_config['username'],
      $this->_config['password'],
      $this->_config['dbname'],
      $port
      );

    4. Redefine params:

      $this->_config['host'] = getenv('DB_HOST');
      $this->_config['username'] = getenv('DB_USERNAME');
      $this->_config['password'] = getenv('DB_PASSWORD');

      $_isConnected = @mysqli_real_connect(
      $this->_connection,
      $this->_config['host'],
      $this->_config['username'],
      $this->_config['password'],
      $this->_config['dbname'],
      $port
      );

    5. Clear cache and restart MySQL. For other databases check files in lib/Zend/Db/Adapter folder (DB2.php, Oracle.php, Sqlsrv.php).

    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改