diff --git a/core/lib/Drupal/Core/Command/DbImportCommand.php b/core/lib/Drupal/Core/Command/DbImportCommand.php
index de41089d2b1f6ae3720990c7f244e130dbebd931..a39f6cbde68e6af6be359f9cc09f7998adaef5ea 100644
--- a/core/lib/Drupal/Core/Command/DbImportCommand.php
+++ b/core/lib/Drupal/Core/Command/DbImportCommand.php
@@ -58,6 +58,8 @@ protected function execute(InputInterface $input, OutputInterface $output) {
    *   Path to dump script.
    */
   protected function runScript(Connection $connection, $script) {
+    $old_key = Database::setActiveConnection($connection->getKey());
+
     if (substr($script, -3) == '.gz') {
       $script = "compress.zlib://$script";
     }
@@ -67,6 +69,7 @@ protected function runScript(Connection $connection, $script) {
     catch (SchemaObjectExistsException $e) {
       throw new \RuntimeException('An existing Drupal installation exists at this location. Try removing all tables or changing the database prefix in your settings.php file.');
     }
+    Database::setActiveConnection($old_key);
   }
 
 }
diff --git a/core/modules/system/tests/src/Kernel/Scripts/DbImportCommandTest.php b/core/modules/system/tests/src/Kernel/Scripts/DbImportCommandTest.php
index 9dc3407eb2d102d4af19488dc8d18dc5eb6c479c..8cf23d5b7198a6a088dd8b642d798c33070b6ffa 100644
--- a/core/modules/system/tests/src/Kernel/Scripts/DbImportCommandTest.php
+++ b/core/modules/system/tests/src/Kernel/Scripts/DbImportCommandTest.php
@@ -8,7 +8,6 @@
 namespace Drupal\Tests\system\Kernel\Scripts;
 
 use Drupal\Core\Command\DbImportCommand;
-use Drupal\Core\Config\DatabaseStorage;
 use Drupal\Core\Database\Database;
 use Drupal\KernelTests\KernelTestBase;
 use Symfony\Component\Console\Tester\CommandTester;
@@ -54,20 +53,25 @@ class DbImportCommandTest extends KernelTestBase {
 
   /**
    * Test the command directly.
+   *
+   * @requires extension pdo_sqlite
    */
   public function testDbImportCommand() {
-    /** @var \Drupal\Core\Database\Connection $connection */
-    $connection = $this->container->get('database');
-    // Drop tables to avoid conflicts.
-    foreach ($this->tables as $table) {
-      $connection->schema()->dropTable($table);
-    }
+    $connection_info = array(
+      'driver' => 'sqlite',
+      'database' => ':memory:',
+    );
+    Database::addConnectionInfo($this->databasePrefix, 'default', $connection_info);
 
     $command = new DbImportCommand();
     $command_tester = new CommandTester($command);
-    $command_tester->execute(['script' => __DIR__ . '/../../../fixtures/update/drupal-8.bare.standard.php.gz']);
+    $command_tester->execute([
+      'script' => __DIR__ . '/../../../fixtures/update/drupal-8.bare.standard.php.gz',
+      '--database' => $this->databasePrefix,
+    ]);
 
     // The tables should now exist.
+    $connection = Database::getConnection('default', $this->databasePrefix);
     foreach ($this->tables as $table) {
       $this->assertTrue($connection
         ->schema()