perlmariadbdbimaxscale

Perl DBI with Mariadb Maxscale


I have set up a Mariadb Maxscale (1 Primary and 2 Replicas) replication cluster for experimentation purposes. As I understand it the main purpose for using Maxscale is that it decides what server to query. Accordingly, I am unsure how to direct my DBI query to Maxscale instead of a specific server.

I usually use Perl DBI to connect to databases in the following manner;

my $dbh = DBI->connect("DBI:mysql:database=$db_name};$server",$user,$passwd);

Can someone tell me how to do this with Maxscale?

maxctrl list servers
┌─────────┬─────────────┬──────┬─────────────┬─────────────────┬──────────┬─────────────────┐
│ Server  │ Address     │ Port │ Connections │ State           │ GTID     │ Monitor         │
├─────────┼─────────────┼──────┼─────────────┼─────────────────┼──────────┼─────────────────┤
│ server1 │ 192.168.0.2 │ 3306 │ 0           │ Master, Running │ 0-2-1438 │ MariaDB-Monitor │
├─────────┼─────────────┼──────┼─────────────┼─────────────────┼──────────┼─────────────────┤
│ server2 │ 192.168.0.3 │ 3306 │ 0           │ Slave, Running  │ 0-2-1438 │ MariaDB-Monitor │
├─────────┼─────────────┼──────┼─────────────┼─────────────────┼──────────┼─────────────────┤
│ server3 │ 192.168.0.4 │ 3306 │ 0           │ Slave, Running  │ 0-2-1438 │ MariaDB-Monitor │
└─────────┴─────────────┴──────┴─────────────┴─────────────────┴──────────┴─────────────────┘

I seem to be able to connect as I can do:

$ mysql -h 192.168.0.1 -umaxscale -ppasswd -P4006 -e 'SHOW DATABASES;'
+--------------------+
| Database           |
+--------------------+
| admin              |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+

I just can't figure out how to successfully query via Perl DBI.


Solution

  • MaxScale is just a proxy, so you should be able to connect as you would usually do when connecting to MariaDB:

    my $dsn = "DBI:mysql:database=$database;host=$max_scale_host;port=$max_scale_port";
    my $dbh = DBI->connect($dsn, $user, $password);
    

    You just have to make sure your DBI is aiming at the MaxScale host and port.