How can I query a database created using the previous core version from the new core version? I want to query users, nodes etc in my D7 database that I am currently migrating to D8 to compare with the migrated content.

I want to use custom Drush commands to run some checks.

Is this possible?
You can add external databases in your settings.php file:

$databases['external']['default']= array(
  'database'=&gt;'',
  'username'=&gt;'',
  'password'=&gt;'',
  'prefix'=&gt;'',
  'host'=&gt;'',
  'port'=&gt;'3306',
  'namespace'=&gt;'Drupal\\Core\\Database\\Driver\\mysql',
  'driver'=&gt;'mysql',
);


Then, in a custom module you can query for nodes, terms etc.

// Switch to external database
\Drupal\Core\Database\Database::setActiveConnection('external');

// Get a connection going
$db = \Drupal\Core\Database\Database::getConnection();

$vid = 5; // The vocabulary I want to query
$query = $db-&gt;select('taxonomy_term_data', 't');
$query
  -&gt;join('taxonomy_term_hierarchy', 'h', 'h.tid = t.tid');
$query
  -&gt;addTag('translatable')
  -&gt;addTag('taxonomy_term_access')
  -&gt;fields('t')
  -&gt;fields('h', array(
    'parent',
  ))
  -&gt;condition('t.vid', $vid)
  -&gt;orderBy('t.weight')
  -&gt;orderBy('t.name');
$results = $query-&gt;execute()-&gt;fetchAll();


Switch back to your current db

\Drupal\Core\Database\Database::setActiveConnection();


Save the results to a d8 vocabulary

foreach($results as $result) {
  Term::create([
    'tid' =&gt; $result-&gt;tid,
    'name' =&gt; $result-&gt;name,
    'vid' =&gt; 'my_d8_vocabulary_name'
   ])-&gt;enforceIsNew()-&gt;save();
}