1

I've define my own database connection in my Drupal settings file as follow:

$databases['migrate']['default'] = array(
  'driver' => 'mysql',
  'database' => 'drupal',
  'username' => 'root',
  'password' => 'root',
  'host' => 'localhost',
  'prefix' => '',
);

To read that in Database object format, you can run:

drush eval 'print_r(Database::getConnectionInfo("default", "migrate"));'

And now I would like to have the string representation (similar to $db_url in Drupal 6) to use it in some drush commands on that database e.g. drush --db-url=${DB_URL} status.

So I would like to convert above Database array format into database URL string format (e.g. mysql://root:root@localhost/drupal).

Is there any easy way of doing that via function, PHP code or drush?


Additional info:

  • There is update_parse_db_url() function, but it's converting database connection URLs and return D7 compatible array.

3 Answers 3

5

Using Drush:

drush sa @site --with-db-url

Or, to isolate just the db-url:

drush sa @site --with-db-url --format=csv --fields=db-url --field-labels=0
2
  • 2 questions: 1) what version of Drush does the 2nd work it, its failing in 5.8 with unknown options (all after --with-db-url). 2) it looks like this solution will only work for sites you defined site-aliases for ?
    – tenken
    Commented Dec 16, 2013 at 16:36
  • 1) The second example is for Drush 6. Drush 6 stable was released on 15 August; not many fixes are being backported to Drush 5 any more, so folks should upgrade. 2) drush sa will look up the database connection from settings.php if it's not defined in the alias. If the alias is remote, try drush @remote sa @self --with-db-url Commented Dec 16, 2013 at 17:29
0

Here is one-liner:

drush eval '$db = Database::getConnection("default", "migrate")->getConnectionOptions();  echo $db["driver"] . "://" . $db["username"] . ":" . $db["password"] . "@" . $db["host"] . ":" . $db["port"] . "/" . $db["database"];'

For the long-term solution, this can be achieved by defining drush command in your module (foo.drush.inc):

/**
 * Implements hook_drush_command().
 */
function foo_drush_command() {
  $items = array();
  $items['get-db-uri'] = array(
    'callback'  => 'foo_drush_get_db_uri',
    'arguments' => array(
      'name' => array(
        'description' => dt('Database name.'),
        'required'    => TRUE,
      ),
      'key' => array(
        'description' => dt('Key.'),
        'required'    => TRUEs,
      ),
    ),
    'description' => dt('Returns database URI for a specified name and key.'),
    'examples' => array(
      'drush get-db-uri migrate default' => dt('Will return db uri in format: driver://user:pass@host:port/dbname'),
    ),
  );

  return $items;
}

/**
 * Implements hook_drush_help().
 */
function foo_drush_help($section) {
  switch ($section) {
    case 'get-db-uri':
      return dt("Returns database URI.");
  }
}

/**
 * Drush command (get-db-uri).
 *
 * Returns database URI for a specified name and key.
 *
 * @param string $name
 *   Database name.
 * @param string $key
 *   Key.
 */
function foo_drush_get_db_uri($name, $key) {
  $connection = Database::getConnection($key, $name);
  $info       = $connection->getConnectionOptions();

  echo $info['driver'] . '://' . $info['username'] . ':' . $info['password'] . '@' . $info['host'] . ':' . $info['port'] . '/' . $info['database'];
}

Example usage:

drush get-db-uri migrate default
0

In case you have bootstrapped drupal instance, drush and jq installed

drush status --fields="db-driver,db-hostname,db-port,db-username,db-password,db-name" --format="json" | jq '."db-driver"+"://"+."db-username"+":"+."db-password"+"@"+."db-hostname"+":"+."db-port"+"/"+."db-name"'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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