Connecting to mysql running in container from host through perl DBI

Viewed 197

I am trying to connect to mysql running in container through perl script.

Container

1063b3f15b0f   mysql:latest            "docker-entrypoint.s…"   3 days ago   Up 3 days   0.0.0.0:3306->3306/tcp, 33060/tcp   test_db

Connections works from host with tcp protocol mysql -P 3306 --protocol=tcp -u root -p

But not sure how we can specify protocol with DBI in perl

use DBI;

my $driver = "mysql";
my $host = "127.0.0.1";
my $database = "users";
my $dsn = "DBI:$driver:database=$database,host=$host";
my $userid = "root";
my $password = "testpw";

my $dbh = DBI->connect($dsn, $userid, $password ) or die $DBI::errstr;


Fails with error

DBI connect('database=users,host=127.0.0.1','root',...) failed: Access denied for user 'root'@'localhost' (using password: YES) at update_db.pl line 10.
Access denied for user 'root'@'localhost' (using password: YES) at update_db.pl line 10.

Can someone help here to crack it.?

1 Answers

You don't need to specify the protocol. The fact that you're connecting to "127.0.0.1" (rather than "localhost") is enough to ensure that DBI will connect over TCP instead of using a Unix socket (see Connection Transport Protocols for details).

However, I suspect that your root user doesn't have permission to connect from just anywhere. What do you see if you run the following SQL:

select user, host from mysql.user where user = 'root';

The MySQL user module allows you to specify which hosts a user can connect from. A '%' in the host column will allow connections from any host. Otherwise you can only connect from certain hosts.

Related