I was looking for the same solution and came across this:
https://blog.hauri.me/magento2-custom-database-connection.html
I didn't implement the resource model part since I don't need it now but the following part helped me a lot.
/** @var Magento\Framework\App\ResourceConnection $this->resourceConnection **/
$connection = $this->resourceConnection->getConnection('custom');
I use it my code like this (this is part of controller for test purposes)
in my code to get a custom connection
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\App\ResourceConnection $resourceConnection
)
{
parent::__construct($context);
$this->_resourceConnection = $resourceConnection;
}
public function execute()
{
$connection = $this->_resourceConnection->getConnection('custom');
$select = $connection->select()
->from(
['tt' => 'testtable']
);
$data = $connection->fetchAll($select);
print_r($data);
}
All the rest just like in here https://www.maxpronko.com/blog/new-database-connection-in-magento-2
I hope this helps!