User Tools

Site Tools


perlsql

Perl, SSH and Mysql

Linux example

First connect a port from another system. System A is running Mysql, system B is a client wanting to access the database.

export A=192.168.1.95
export B=192.168.1.41

pi@B:~/pl$ ssh -f sql@A -N -L 3336:127.0.0.1:3306
# It's confusing, but this connects port 3306 on A to port 3336 on B
pi@B:~/pl$ cat test.pl
#!/usr/bin/perl

use strict;
use warnings;
use DBI qw(:sql_types);

my $host = "localhost";
my $database = "example";
my $port = 3336;
my $user = "user1";
my $pw = "123password";

$dbh = DBI->connect("DBI:mysql:database=$database;host=$host;port=$port", $user, $pw)
  || die "Connect failed: $DBI::errstr\n";
  print "OK\n";
  
perlsql.txt · Last modified: 2023/02/20 13:02 by admin