LoginSignup
2
1

More than 3 years have passed since last update.

【Mysql】接続できないエラー原因と対処まとめ

Posted at

はじめに

mysqlに接続できないエラーに何度も直面し、その度調べていたので、今回は備忘録としてまとめてみました。
参考になれば幸いです

(1)sockファイルでのエラー

エラー内容

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

原因

mysql.sockファイルがないこと

対処

# sockファイルを作成する
$ sudo touch /tmp/mysql.sock
# 動作確認→別のエラーが発生している
$ mysql -u root
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (38)

原因

mysql.sockファイルが入っているディレクトリの権限が原因

対処

# 権限変更する
$ sudo chown mysql:mysql /tmp
# サーバーを立ち上げる(ここ重要)
$ sudo mysql.server start
# 動作確認
$ mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.22 Homebrew

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>

完了!

(2)PIDファイルに関するエラー

発生事例とエラー内容

mysqlサーバーを再起動しようとしたら下記エラーが発生。

$ mysql.server start
Starting MySQL
.. ERROR! The server quit without updating PID file (/usr/local/var/mysql/****.pid).

原因

  1. pidファイルがない
  2. pidファイルがあるフォルダの権限がおかしい
# 「/usr/local/var/mysql」にpidファイルがあるか確認
$ ls /usr/local/var/mysql
#=> ファイル一覧が表示されるのでpidファイルがあるか確認する

対処

1. 「/usr/local/var/mysql」にpidファイルを作成

# pidファイルを作成
$ touch /usr/local/var/mysql/*****.local.pid

※「*****」に入る文字はマシンのホスト名
※ホスト名はuname -nで確認できる。

2. pidファイルがあるフォルダの権限を変更する

# /user/local/var/mysql以下のファイルの所有者をすべて_mysqlにする
$ sudo chown -R _mysql:_mysql /usr/local/var/mysql/
# 再度コマンドを打つと動作する
$ sudo mysql.server restart
Password:
Shutting down MySQL
. SUCCESS!
Starting MySQL
. SUCCESS!

完了!!

参考

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