LoginSignup
11
3

MySQL で ERROR 1045 (28000)が出たときの対処法

Last updated at Posted at 2023-03-05

MySQL8.0.32にログインしようとしたところ、「ERROR 1045 (28000)」が出ました。

状況は以下の2通りです

mysql -u rootでエラーが出る場合

$ mysql -u root
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

-pをつけてそのあとにそのままパスワードを入れるとログインできました

$ mysql -u root -pパスワード
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 27
Server version: 8.0.32 Homebrew

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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> 

-pのあとは空白不要です。

MySQLをインストールしたばかりで、mysql -u root -pでエラーが出る場合

$ mysql -u root -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

この場合はおそらくのrootユーザーのパスワードが設定されていないことが原因であるため、一旦mysql -u rootでログインし、以下を実行すると上手くいくかと思います
set password for root@localhost = 'パスワード';

update mysql.user set password=password('パスワード') where user = 'root';はMySQL5系まで有効なコマンドで、8系ではエラーが出るため使えません

$ mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.32 Homebrew

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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> set password for root@localhost = 'パスワード';
Query OK, 0 rows affected (0.09 sec)

一旦ログアウトし再度mysql -u root -pでログインしてみます

mysql> \q
Bye
$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.32 Homebrew

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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> 

これでログインできました!

参考

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