LoginSignup
11
11

More than 3 years have passed since last update.

【AWS EC2】ec2-userにログイン中、 $ mysql -u root -p でmySQLにログインしようとした時に、 Access denied for user 'root'@'localhost' (using password: ) エラーが出た場合の解決方法

Posted at

AWS EC2にRailsアプリをデプロイするための準備として、DB(MySQL 5.7)に接続時に少しだけハマったので備忘録的に残しておきます。
同じようなエラーが起きた方の助けになれば・・・。

下記コマンドでDBへ接続する。

$ mysql -u root -p
Enter password: 

あれ、パスワードって何だったっけ?
それらしい文字列を入力してみるて接続を試みるが、案の定下記のようなエラーが発生。

Access denied for user 'root'@'localhost' (using password: yes)

これは困った・・。そもそも設定した覚えもない?

パスワードを確認するには以下のコマンドで可能。一度確認してみよう。

cat /var/log/mysqld.log | grep password

するとまたこのエラー(catはファイルの中を確認する時のLinuxコマンド)。

Access denied for user 'root'@'localhost' (using password: yes)

とりあえずec2-userから以下のコマンドでrootユーザーに切り替える。

sudo su -

rootユーザーの状態で先ほどのパスワード確認のコマンドを入力する。

cat /var/log/mysqld.log | grep password

すると

2020-06/ [Note] A temporary password is generated for root@localhost: "あなたのパスワード"

パスワードを確認できた! (A temporary password is generated なので一時期的に発行された物?)

ちなみに" "の間の文字列があなたのパスワードです。

パスワードを確認したので、rootからexitでec2-userに切り替える

$ mysql -u root -p
Enter password: "あなたのパスワード"

以下が確認できたらMySQLにログイン成功!

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is ***
Server version: 5.7.30

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> 

以上です。

参考
AWS Amazon Linux2にMySQL5.7を構築する
AWSの初期設定でrootパスワードを設定する

11
11
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
11
11