0

For some specific automation reasons, I use the following snippet in settings.php rather than defining db credentials directly in it.

$databases = array(
  'default' => array(
    'default' => array(
      'database' => $_SERVER['DB_NAME'],
      'username' => ini_get('mysql.default_user'),
      'password' => ini_get('mysql.default_password'),
      'host' => 'localhost',
      'port' => '',
      'driver' => 'mysql',
      'prefix' => '',
    ),
  ),
);

And when I try to use drush, it won't pick up database credentials. How can I give the right database credentials to Drush? Can I define something in drushrc files per project?

The following is what I get from drush status.

Drupal version : 7.22
Site URI : http://default
Database driver : mysql
Database hostname : localhost
Database username :
Database name:
Default theme : garland
Administration theme : garland
PHP configuration : /etc/php5/cli/php.ini
Drush version : 5.4
Drush configuration :
Drupal root : /home/oep/www/dev/docroot Site path : sites/default
File directory path : sites/default/files

  • It might help if you would tell us why you needed credentials in php.ini in the first place. – Mołot Aug 22 '13 at 9:05
  • This is a shared hosting server with several users and several drupal projects. Each project is hosted under its home dir like /home/<project>/www/{dev,test,prod}. So each drupal site has dev, test and production instances. This is planned for a devops practice of having separate dev, staging and production envs. For connecting each env to its corresponding db instance, I have defined creds in Apache vhost and using the above snippet in settings.php for connecting to the right db instance of drupal. Hope I am clear. – Medhamsh Aug 22 '13 at 18:30
  • And here is the problem - CLI does not know about Apache in any way. And will not know. If you could extract credentials to good old inc file, and obtain it by using require ../database.inc (put above Drupal's directory), it would be much easier. But there is hardly a way to tell CLI about Apache's config. Certainly not the one I'd dare to use and believe it stable in automated environment. – Mołot Aug 23 '13 at 6:39
  • @Mołot, I have extracted credentials to an inc file in the server and I am doing a cp of default.settings.php to settings.php and having the include statement in it and pushed it to the git repository. But with this architecture, drupal takes to installation page all the time and my settings.php is getting overwritten in the server. I am not sure why this happens. Is there any thing in settings.php to stop it from redirecting to install.php but just execute the include statement adn get creds and use the existing database? – Medhamsh Sep 26 '13 at 7:43
1

As you can see, drush is run from command line interface, so it uses cli/php.ini and not regular one. If you feel like keeping password and username in ini file, you need to make sure there are synchronized between WWW and CLI php.ini files.

  • Thanks for a quick reply! I just want to know how to refer to that credentials file to drush. Where do we define in drush to look at that file for credetials? – Medhamsh Aug 22 '13 at 7:10
  • @Medhamsh you don't. And it's not about drush, really, but about PHP in general. ini_get() only looks at php.ini that's currently in place. Have you read it's page at php.net? There you may find some inspirations to alter your setup for drush compatibility. – Mołot Aug 22 '13 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.