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

More than 3 years have passed since last update.

django.db.utils.OperationalError: (2002, "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)")

Posted at

目的

djangorestframeworkを利用してAPIを作成しようとした.
既存のsqliteではなく、MySQLを使ってみようと思い、python manage.py runserverを実行してみた。

すると下記のエラーに苦しめられた。

Can't connect to local MySQL server through socket '/tmp/mysql.sock

結論

Can't connect to local MySQL server through socket '/tmp/mysql.sock

このエラーは

$ mysql.server start

で解決できた。

実施環境

ハードウェア環境

項目 情報
OS macOS Catalina(10.15.7)
ハードウェア MacBook Air (11-inch, Early 2015)
プロセッサ 1.6 GHz デュアルコアIntel Core i5
メモリ 4 GB 1600 MHz DDR3
グラフィックス intel HD Graphics 6000 1536 MB

ソフトウェア環境

項目 情報
homebrew 3.3.8
mysql Ver 8.0.27 for macos10.15 on x86_64
python 3.8.12
django 3.0.0
anaconda 4.10.1
pip 21.2.4

原因究明

Can't connect to local MySQL server through socket '/tmp/mysql.sock
こちらのエラーを日本語に直して考えてみると、socketを通してMySQLサーバーにアクセスできないと言っている。

必要な設定などは済んでいるのになんでだろう。
この記事などを参考に、djangoのsettings.pyを変更などしてpython manage.py runserverをもう一度を実行するもエラーに苦しめられる。

しかしよく考えるとMySQLを接続するのにMySQLを起動しているのか?と気づいた。
ここで指摘されているように、MySQLの起動ができていなかった。

$ mysql.server start

を行い、MySQLを実行した後にもう一度

python manage.py runserver

を実行する。
MySQLにログインする。

mysql -h localhost -u root

成功。

参考

Django の runserver 立ち上げたら "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)" とかなる場合のメモ
Can't connect to local MySQL server through socket '/tmp/mysql.sock

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