Question

I'm having this strange problem. We have a ubuntu 12.04 server with 2 NICs. One public with 172.30.1.1, the other is private with 192.168.1.1. We have a MySQL server running. In /etc/mysql/my.conf, we have bind-address = 0.0.0.0.

A table is created and privilege granted.

CREATE DATABASE db;
GRANT ALL ON db.* TO 'user0'@'%' IDENTIFIED BY 'password';
GRANT ALL ON db.* TO 'user0'@'localhost' IDENTIFIED BY 'password';

The hostname is 'myhost', and it's in /etc/hostname. /etc/hosts has

127.0.0.1       localhost
127.0.1.1       myhost
192.168.1.1     myhost

When I connect with 172.30.1.1, it's fine. But when I use 192.168.1.1, the access is denied. mysql -h 192.168.1.1 -uuser0 -ppassword ERROR 1045 (28000): Access denied for user 'user0'@'myhost' (using password: YES) I have the user table like this.

    mysql> SELECT user, host FROM mysql.user;
    +------------------+-----------+
    | user             | host      |
    +------------------+-----------+
    | user0            | %         |
    | root             | 127.0.0.1 |
    | root             | ::1       |
    |                  | localhost |
    | debian-sys-maint | localhost |
    | user0            | localhost |
    | root             | localhost |
    |                  | myhost    |
    | root             | myhost    |
    +------------------+-----------+

I observed the difference is that I have 192.168.1.1 myhost line in /etc/hosts file; not 172.30.1.1. If I remove that line or change the hostname after the IP, it will work fine. If I add 172.30.1.1 myhost to /etc/hosts, then I can't connect with 172 IP. How to explain this?

Was it helpful?

Solution

Usually anonymous users (empty username) have the effect like that. When looking for a match in mysql.user table, the ''@myhost found first (since '' matches every user), and 'user0'@'%' is ignored.

The best practice is to remove empty usernames.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top