Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl How to change account host using DBI module?

My company has a Perl application in server with IP : 10.10.3.39

And because of the new rule implemented, I must migrate the database to MySQL DB in server with IP : 10.10.1.18

My company DB administrator has create an account and grant the access for the apps with username : 'user'@'10.10.3.39'. So the account just can be used from server with IP 10.10.3.39

I tried the connection in the server using command mysql -h 10.10.1.18 -u user -p

[hanief@dev39 project]$ mysql -h 10.10.1.18 -u user -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 19484169
Server version: 10.0.15-MariaDB mariadb.org binary distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

I have a database connection script test_db.pm in Perl like this:

use DBI;

$user = "user";
$pw = 'password';
$dbh = DBI->connect("DBI:mysql:database=mysql;host=10.10.1.18;mysql_enable_utf8=1",$user, $pw, %attr) or die "Cannot connect to MySQL server\n";

And then, I tried to run it using command perl test_db.pm, I got this error

[hanief@dev39 project]$ perl test_db.pm
DBI connect('database=mysql;host=10.10.1.18;port=3306;mysql_enable_utf8=1','user',...)
failed: Access denied for user 'user'@'%' to database 'mysql' at test_db.pm line 6.
Cannot connect to MySQL server

I don't know why the account name suddenly has @'%' behind it

I have tried to change the variable $user to :

  • $user = "user\@10.10.3.39";
failed: Access denied for user '[email protected]'@'10.10.3.39' (using password: YES) at test_db.pm line 6.
  • $user = "user\@'10.10.3.39'";
failed: Access denied for user 'user@'10.10.3.39''@'10.10.3.39' (using password: YES) at test_db.pm line 6.
  • $user = "'user'\@'10.10.3.39'";
failed: Access denied for user 'user'@'10.10.3.39'@'10.10.3.39' (using password: YES) at test_db.pm line 6.

But still, didn't work. The server still can't connected to DB server.

I Don't know why there's an extra @'10.10.3.39' behind the user account. And don't know why % before, suddenly changed to 10.10.3.39

Any solution for my case?

like image 849
Hanief Han Avatar asked Nov 04 '21 01:11

Hanief Han


People also ask

What is DBI module?

The DBI is a database access module for the Perl programming language. It provides a set of methods, variables, and conventions that provide a consistent database interface, independent of the actual database being used.

How do I know if Perl DBI module is installed on Linux?

If the module is not installed, then: $ perl -e 'use dbi' Can't locate dbi.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.14. 2 /usr/local/share/perl/5.14. 2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.14 /usr/share/perl/5.14 /usr/local/lib/site_perl .)


1 Answers

I'm not entirely sure what's going on here, but I think I can shed a little bit of light.

MySQL (and, hence, MariaDB) has a login system that isn't just dependent on usernames and passwords. It also takes into account the host that the connection is coming from. If you look at the mysql.user table, you'll see that the first three columns are Host, User and Password.

The Host column is interesting. It can either contain an IP address or a hostname, but it can also contain the symbol '%', which means "any host".

So when the DBA told you they had:

create an account and grant the access for the apps with username : 'user'@'10.10.3.39'

I'm betting they mean they've inserted the values user and 10.10.3.39 in the mysql.user table.

That means your $user variable needs to be set to user, not [email protected] - as MySQL will work out the IP address from your incoming connection.

So when you see errors like this:

failed: Access denied for user '[email protected]'@'10.10.3.39'

the bit inside the first pair of quotes ("[email protected]") is the username you're trying to use and the bit inside the second pair of quotes ("10.10.3.39") is the IP address that MySQL thinks you're connecting from. Obviously, that's not going to work because that username doesn't exist in the mysql.users table.

So, we're left looking at your first example:

$dbh = DBI->connect("DBI:mysql:database=mysql;host=10.10.1.18;mysql_enable_utf8=1",$user, $pw, %attr)
  or die "Cannot connect to MySQL server\n";

Which gives this error:

failed: Access denied for user 'user'@'%'

Now, the fact that this error uses "%" as your hostname, seems to imply that the MariaDB server didn't recognise the IP address that you're coming from as one of the specific IP addresses listed as allowed for your user.

So, two things I would double-check in this instance:

  • Are you definitely trying to connect from 10.10.3.39? This includes checking if there are any proxies between you and the DB server that might change your IP address.
  • Is the IP address in the mysql.user table really 10.10.3.39? Or does the value, perhaps, contain a typo?

I'm aware that this isn't a "here's the solution to your problem" post but, hopefully, it explains a bit more about what is going on and gives you a couple of avenues to investigate.

like image 148
Dave Cross Avatar answered Oct 17 '22 07:10

Dave Cross