It was my understanding that if MariaDB/MySQL sees localhost in config, it means 127.0.0.1 (loopback iip address) and really uses a socket to connect - default port 3306. Default for MariaDB/MySQL is to do a DNS check upon every client request. So moodle code is really a client ... coming from localhost.
My suggestion was to install the second instance of DB server where it listens on the true IP address or server on a higher port not within well-known or other ports typically used by AMP stack ... 3306,80,443,22,23,25 etc. - the 33306.
In looking at a 3.8 config-dist.php file:
$CFG->dbhost = 'localhost'; // eg 'localhost' or 'db.isp.com' or IP
which you would change localhost to the IP of your server.
In
$CFG->dboptions = array(
'dbsocket' => false, // should connection via UNIX socket be used?
// if you set it to 'true' or custom path
// here set dbhost to 'localhost',
// (please note mysql is always using socket
// if dbhost is 'localhost' - if you need
// local port connection use '127.0.0.1')
'dbport' => '', // the TCP port number to use when connecting
// to the server. keep empty string for the
// default port
change those to match server config - false and if you used my suggestion dbport would be 33306.
Am sure there is a good reason, but have to ask ... why not just upgrade the DB server rather than try to run 2????
'SoS', Ken