php - Override core Magento DB connection using environment variables -
i trying use environment variables loaded magento application override default database connection credentials.
i've managed 'almost' working using getenv('my_custom_var'), trying use same method override database credentials, sensitive data can stored within 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 - copy of 1 core pool)
// function overrides core 1 protected function _initbaseconfig() { varien_profiler::start('mage::app::init::system_config'); $this->_config->loadbase(); /* read db connection config 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 $connection created default global database connection used site-wide, code should appear if enter pure random entries db_host/db_password etc.. still connects database suggests isn't overriding default database settings configured magento setup.
any ideas on how $connection override , become 'global' database connection?
p.s apologies in advance if question similar previous one, original question bit of general question whereas 1 more focused particular section.
update... have open local.xml within app/etc directory , set 'default setup' database connection inactive (by changing active node value 0) , expected returns error. appear overriding function doesn't seem override initial db connection.. ideas guys?
for mysql db can next:
- copy file lib/zend/db/adapter/mysqli.php app/code/local/zend/db/adapter/mysqli.php
- open app/code/local/zend/db/adapter/mysqli.php , find _connect() function.
in 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
);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
);clear cache , restart mysql. other databases check files in lib/zend/db/adapter folder (db2.php, oracle.php, sqlsrv.php).
Comments
Post a Comment