Help us understand the problem. What is going on with this article?

mysqlへログインできない問題

More than 1 year has passed since last update.

基本的なことだがmysqlのログイン時の挙動でつまづいたのでメモ

コマンドメモ

▼登録済みユーザーの確認
SELECT user, host, password FROM mysql.user;
+----------+-----------+-------------------------------------------+
| user     | host      | password                                  |
+----------+-----------+-------------------------------------------+
| root     | localhost | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| root     | localenv  |                                           |
| root     | 127.0.0.1 |                                           |
| root     | ::1       |                                           |
|          | localhost |                                           |
|          | localenv  |                                           |
| hogehoge | %         | *0B025000DCF68B1D374153A7005639E320D41D5B |
+----------+-----------+-------------------------------------------+
▼ユーザー作成
CREATE USER 'user名'@'ホスト名' IDENTIFIED BY 'パスワード';
例) CREATE USER hogehoge@'%' IDENTIFIED BY 'hogehoge';

※ホスト名の指定なしなら「%」がデフォルトで設定される
※明示的に「%」を指定する場合はシングルクォートで囲む

▼権限の表示
SHOW GRNTS FOR 'user名'@'ホスト名 or IPアドレス';
▼パスワードの設定
SET PASSWORD FOR 'ユーザー名'@'ホスト名' = PASSWORD('パスワード');
例) SET PASSWORD FOR 'root'@'localhost' = PASSWORD('root');
▼ユーザーの削除
DROP USER 'user名'@'ホスト名 or IPアドレス';

ログインできない問題の解決

ユーザーを作って、パスワードを設定したのにログインしようとするとエラー

mysql -uhogehoge -phogehoge
Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'hogehoge'@'localhost' (using password: YES)

どうやら、アクセスしたい6行目の「hogehoge@'%'」よりも先に
5行目の「''@localhost」の匿名ユーザーに該当すると判断されるようです
今回、私には匿名ユーザーは必要無かったので消しました

mysql> DROP USER ''@'localhost';
Query OK, 0 rows affected (0.01 sec)

mysql> SELECT user, host, password FROM mysql.user;
+----------+-----------+-------------------------------------------+
| user     | host      | password                                  |
+----------+-----------+-------------------------------------------+
| root     | localhost | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| root     | localenv  |                                           |
| root     | 127.0.0.1 |                                           |
| root     | ::1       |                                           |
|          | localenv  |                                           |
| hogehoge | %         | *0B025000DCF68B1D374153A7005639E320D41D5B |
+----------+-----------+-------------------------------------------+

はい、これでログインできました!
ユーザーを作成したのに、ログインできない!となったら他のユーザーが悪さしていないか確かめた方がよさそうです
...というか不要なユーザーはすべて消した方がいいですね

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした