$gateway.open("target.server", 3306) do |port|
is more equivalent to, in this case,
ssh -L #{port}:target.server:3306 -N user@target.server
which may very well fail if your mysql server only listens on 127.0.0.1 (or on an internal IP address, or firewalled to only allow connections via internal networks, all of which are reasonable and normal configurations).
Probably you want:
$gateway.open("127.0.0.1", 3306) do |port|
instead in this case.