2

I have the following params in settings.php to connect to an external database .

$databases['d8243']['default'] = array (
  'database' => 'drupal8',
  'username' => 'root',
  'password' => '123123',
  'prefix' => '',
  'host' => '192.168.1.243',
  'port' => '3306',
  'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
  'driver' => 'mysql',
);

And am using the following code to access some data from the db .

\Drupal\Core\Database\Database::setActiveConnection('d8243');
$query = "Select * from node__field_target_config_controller";
$result = db_query($query);
$obj = $result->fetchObject();
echo json_encode($obj);
exit;

My current server and the external db server are in a same network .

But am geting the following error :

exception 'PDOException' with message 'SQLSTATE[28000] [1045] Access denied for user 'root'@'192.168.1.245' [error] (using password: YES)' in /var/www-drupal/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php:149

Is there a different method to connect to remote db in D8 ? or how to do it ?

When i checked d6/d7 versions have an option to specify remote db like

$db_url = 'mysql://username:[email protected]/databasename';

how to do this in D8 ?

2 Answers 2

3

You can use like:

$other_database = array(
  'database' => 'drupal8',
  'username' => 'root', // assuming this is necessary
  'password' => '123123', // assuming this is necessary
  'host' => '192.168.1.243', // assumes localhost
  'port' => '3306',
  'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
  'driver' => 'mysql', // replace with your database driver
);
// replace 'YourDatabaseKey' with something that's unique to your module
Database::addConnectionInfo('d8243', 'default', $other_database);
db_set_active('d8243');
// execute queries here
db_set_active()

You can access your other database like above. For reference check this link.

8
  • am not clear with the YourDatabaseKey , what should given in there ?
    – KTM
    Oct 17, 2017 at 14:09
  • Edited answer. Please check
    – Ajay Reddy
    Oct 17, 2017 at 14:11
  • am getting the same error : exception 'PDOException' with message 'SQLSTATE[28000] [1045] Access denied for user 'root'@'192.168.1.245' [error] (using password: YES)' in /var/www-drupal/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php:149 Note that the access deny problem is for the server 245 which is my current server
    – KTM
    Oct 17, 2017 at 14:17
  • 1
    Can you try using "getConnection" instead of "addConnectionInfo".
    – Ajay Reddy
    Oct 17, 2017 at 14:36
  • 1
    Let us continue this discussion in chat.
    – Ajay Reddy
    Oct 17, 2017 at 14:36
0

Set up your main database and external database in your settings file:

$databases['default']['default'] = array (
  'database' => 'mysite_local',
  'username' => 'root',
  'password' => 'root',
  'prefix' => '',
  'host' => 'localhost',
  'port' => '',
  'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
  'driver' => 'mysql',
);

$databases['migration_database']['default'] = array (
  'database' => 'drupal8',
  'username' => 'root',
  'password' => '123123',
  'prefix' => '',
  'host' => '192.168.1.243',
  'port' => '3306',
  'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
  'driver' => 'mysql',
);

Then in your code that needs to access the other database, you can switch between databases, using:

db_set_active('migration_database');

and to switch back:

db_set_active('default');
4
  • am getting the same error : exception 'PDOException' with message 'SQLSTATE[28000] [1045] Access denied for user 'root'@'192.168.1.245' [error] (using password: YES)' in /var/www-drupal/core/lib/Drupal/Core/Database/Driver/mysql/C‌​onnection.php:149 Note that the access deny problem is for the server 192.168.1.245 which is my current server
    – KTM
    Oct 17, 2017 at 15:05
  • 2
    Yes .245 is your current IP, that’s what the error message says. you can read it as “someone from this ip tried to log in but they’re not allowed”. It doesn’t refer to the mysql server ip
    – Clive
    Oct 17, 2017 at 16:58
  • @Clive so its a mysql permission issue for the [email protected] to the server 192.168.1.243 right , not the issue of host specified ? And how to resolve it ?
    – KTM
    Oct 18, 2017 at 7:44
  • @Clive Yes , it was a permission issue for the 245 user to access 243 . If some one answer it properly , i can accept it
    – KTM
    Oct 18, 2017 at 8:12

Your Answer

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

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