6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ワイルドカードで作ったmysqlユーザにパスなしでログインできてしまう

6
Last updated at Posted at 2014-12-02

正確には、ログインしたように見えているだけだった。
以下に、その原因と解決方法を書く。

環境

  • Mysql 5.1
  • localhostからmysql環境を利用

現象

create user abc@"%" identified by 'abc' abcというパスワードのabcユーザを作成するも
mysql -u abc -p でabcを入力してもログインできない。
mysql -u abcではログインができる。
しかし、ログインしても権限がなく何もできないので
実際にはログインしたように見えるだけである。

DBを見てみるとabcというユーザは存在する

select user ,host from mysql.user;
+------+-----------+
| user | host      |
+------+-----------+
| abc  | %         |
| root | 127.0.0.1 |
| root | ::1       |
|      | localhost |
| pma  | localhost |
| root | localhost |
+------+-----------+

# なぜログインできたように見えるのか
mysql -u abcでログインをして、
current_user()を見てみると下のように@localhost、つまり匿名ユーザでログインしている状態になっている

```sql
mysql -u abc
select current_user();
mysql> select current_user();
+----------------+
| current_user() |
+----------------+
| @localhost     |
+----------------+
1 row in set (0.00 sec)

これは下のuserが空白になっているところの
localhostに接続していることを意味している。

+------+-----------+
| user | host      |
+------+-----------+
| abc  | %         |
| root | 127.0.0.1 |
| root | ::1       |
|      | localhost |
| pma  | localhost |
| root | localhost |
+------+-----------+

このことから以下のことが考えられる。

  • localhostの環境からつないだ場合は、hostがlocalhostのところだけ見に行く
  • だからlocalhostの匿名ユーザで勝手に入った
  • さらに、匿名ユーザで入ったことから、 %はlocalhostを含まない

今回引っかかったのは原因、以下の2つ

  • ワイルドカード%がlocalhostを含むと勘違いしていること
  • 匿名ユーザの存在に気が付かないこと

現象への対応

思いつく対応は2つ

  1. %のホストに対して、localhostの環境から無理やりつなぐ
# mysql -u abc -h 192.168.1.22 -p
Enter password: ***

mysql> select current_user();
+----------------+
| current_user() |
+----------------+
| abc@%          |
+----------------+
1 row in set (0.00 sec)

上のように、abc@%でアクセスが確認できる

  1. abc@localhostを作成する

匿名ユーザは削除

また匿名ユーザは必要ないと思われるので削除

DELETE FROM mysql.user WHERE User = ;

--結果反映
FLUSH PRIVILEGES;
6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?