0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

MySQL「ERROR 1045 (28000): Access denied for user 'root'@'localhost'」

Posted at

概要

rootパスワードの再設定手順を備忘目的で書きます。

「ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)」といったエラーが発生した場合にお役立てください。

今回手順を実施している環境は下記の通りです。

  • OS:Amazon Linux 2
  • DB:MySQL 8.0

手順

手順の流れはざっくり下記の通り

  • セーフティモードでMySQLを起動
  • パスワードなしでrootユーザーにログイン
  • パスワードを再設定

早速手順に入りたいと思います。

まず、MySQLが起動している場合は、事前に停止しておきましょう。
sudo systemctl stop mysqld

次に、MySQLの設定ファイル「my.cnf」にセーフティモード起動のための設定を追記します。

# my.cnfの場所を特定
mysql --help | grep "my.cnf"
# 上記コマンドで先頭に出力されたパス配下のmy.cnfを編集
sudo nano /etc/my.cnf
# [mysqld]セクションに「skip-grant-tables」を追記
[mysqld]
skip-grant-tables

MySQLをセーフティモードで起動します。
sudo systemctl start mysqld

それでは、rootユーザーのパスワードを変更しましょう。

# MySQLにrootユーザーでログイン
mysql -u root
# rootユーザーのパスワードを変更
ALTER USER 'root'@'localhost' IDENTIFIED BY '[新しいパスワード]';
# 設定を反映
FLUSH PRIVILEGES;
# MySQLからログアウト
exit;

パスワード変更後は忘れずにmy.cnfの記載を元に戻しておきましょう。

# my.cnfの記載を戻す
sudo nano /etc/my.cnf
# [mysqld]セクションの下記記載を削除
[mysqld]
skip-grant-tables

まとめ

簡単ですが以上となります。

リセットは面倒なので、パスワードは確実に控えておきましょう!

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?