27
20

More than 5 years have passed since last update.

mysql -u root -p でERROR 1045 (28000)に遭遇する

Last updated at Posted at 2019-05-07

「mysql -u root -p」コマンドでmysqlにログインすると以下のエラーが発生

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

対処方法

こちらの記事を参考に以下のコマンドを打っていく


まずmysqlを一旦停止

# service mysql stop


次に権限システムを起動せずにMySQLを起動

# mysqld_safe --skip-grant-tables &


上のコマンドを打った後に再度mysqlにログインを試みると...

# mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)


エラーが発生。一旦DBサーバからexitしてmysqlを打つとこちらでも似たようなエラーが発生。

$ mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)


そこで次はこちらの記事を参考に対処していく。mysqlサーバに接続する際にはmysql.sockファイルを使っているがそのファイルが消えている可能性が。そこでmysql.sockファイルを作り、mysqlを再起動する

$ sudo touch /tmp/mysql.sock
$ sudo mysql.server restart
 ERROR! MySQL server PID file could not be found!
Starting MySQL
.Logging to '/usr/local/var/mysql/[ホームディレクトリ名].local.err'.
 ERROR! The server quit without updating PID file (/usr/local/var/mysql/[ホームディレクトリ名].local.pid).

権限を確認してみる

$ ls -la /usr/local/var/mysql


それでも権限は正常にユーザー名だったので、こちらを参考にmysqlのプロセスを確認してみる

$ ps -ef | grep mysql

確認するとmysqldのプロセスが稼働していたので、こちらは大丈夫なよう。

結果

以下のコマンドでmysqlサーバをrestartし、socketのエラーは解消しました!

$ mysql.server restart
 ERROR! MySQL server PID file could not be found!
Starting MySQL
. SUCCESS!


ただ他の問題は解決せず

# mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
# service mysql stop
$ mysqld_safe --skip-grant-tables &
[1] 2643
[ホームディレクトリ名]:project [ユーザー名]$ 2019-05-06T23:35:44.6NZ mysqld_safe Logging to '/usr/local/var/mysql/[ホームディレクトリ名].local.err'.
2019-05-06T23:35:44.6NZ mysqld_safe A mysqld process already exists


プロセスがすでに走っているとのことなので再度プロセスを調べてみる

$ ps aux| grep mysqld

3つのプロセスが走っていたのでこれらを全てkillする(参考


その後

$ mysql.server start
Starting MySQL
 SUCCESS!

これで行けた!!

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

Copyright (c) 2000, 2019, 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>
27
20
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
27
20