title: Override core Magento DB connection using environment variables
body: <p>I am trying to use environment variables that are loaded into my Magento application to override the default database connection credentials.</p>
  
  <p>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.</p>
  
  <p>e.g</p>
  
  <pre><code># .htaccess file
   SetEnv DB_USERNAME root
   SetEnv DB_PASSWORD password123
   SetEnv DB_HOST localhost
  </code></pre>
  
  <p>My App.php (within app/code/local/Mage/Core/Model/App.php - a copy of the one from the core pool) </p>
  
  <pre><code>// my function that overrides the core one
  protected function _initBaseConfig()
  {
      Varien_Profiler::start('mage::app::init::system_config');
      $this-&gt;_config-&gt;loadBase();
  
      /* Read DB connection config from environment variables */
      $connection = $this-&gt;_config-&gt;getNode('global/resources/default_setup/connection');
      $connection-&gt;setNode('host', getenv('DB_HOST'));
      $connection-&gt;setNode('username', getenv('DB_USERNAME'));
      $connection-&gt;setNode('password', getenv('DB_PASSWORD'));
  
      Varien_Profiler::stop('mage::app::init::system_config');
      return $this;
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>Any ideas on how to get this $connection to override and become the 'global' database connection?</p>
  
  <p>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.</p>
  
  <p>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?</p>