1
1

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 1 year has passed since last update.

MySQL接続のエラー(SQLSTATE[HY000] [1045]) の対処法

Last updated at Posted at 2023-04-06

<発生した問題>
xamppを使用してPHPとMySQLの勉強をしている時、
SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES)
が発生した。

<環境>
OS:Windows11 Home(Ver.22H2)
MySQL:Ver. 10.4.24
XAMPP、phpMyAdminを使用

<対処法>
①XAMPPでApacheとMySQLを起動する。
②XAMPPでShellを起動する。
③Shellに以下を入力してEnterを押す。

mysql -u root

※今回はこの時点でMySQLにログインできた。
以下のようなログが表示された。(一部紹介)

Welcome to the MariaDB monitor.  Commands end with ; or \g.

④Shellに以下を入力して、Enterを押す。

use mysql

⑤Shellに以下を入力して、Enterを押す。
(MySQLというデータベースを使用する。という指定を行う。)

flush privileges;

⑥Shellに以下を入力して、Enterを押す。

ALTER USER 'root'@'localhost' IDENTIFIED BY '任意のパスワード';

※'任意のパスワード'は設定したいパスワードを入力してください。

⑦XAMPPでMySQLを再起動する。Shellに以下を入力してEnterを押す。

mysql -u root -p

※パスワードを聞かれたら、⑥で設定したパスワードを入力し、Enterを押す。

⑧以下のようなログが出たらログイン成功。

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.4.24-MariaDB mariadb.org binary distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

最後に以下の変更もお忘れなく...。
・PDOに設定したパスワード
・phpMyAdminのconfig.inc.php内のパスワード
 ファイル場所…C:\xampp\phpMyAdmin\config.inc.php

$cfg['Servers'][$i]['password'] = 'xxxxxxxx';
$cfg['Servers'][$i]['controlpass'] = 'xxxxxxxx';

<参考サイト>
https://blog.senseshare.jp/forget-rootpassword.html

<エラーが発生した原因>
ibdata1ファイルを削除したことが原因だと思う。
エラーが発生する前にXAMPPのMySQLが起動しなかった。
MySQLのdataフォルダ内をbackupフォルダを使用して復元したが、
誤ってdataフォルダ内のibdata1ファイルを削除してしまった。

※この記事に関して、個人的にメモを残しているだけなので、
違っている点があれば申し訳ないです。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?