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

年明けてRailsからMySQLに接続しようとしたらエラー出た時に試したこと 備忘録

Posted at

開発環境

  • Rails 7.0.8.7
  • Ruby 3.1.5
  • Homebrew 4.4.14
  • Mysql8.0

読んで欲しい人

  • Railsでサーバーを立ち上げようとした時、Console開こうとした時に下記のエラー出た人
Error: Failure while executing; `/bin/launchctl bootstrap system /Library/LaunchDaemons/homebrew.mxcl.mysql@8.0.plist` exited with 5.
  • Railsから下記のエラーで怒られた人
ActiveRecord::ConnectionNotEstablished: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
  • 過去の俺

エラーまでの経緯

※ 下記は/bin/launchctl bootstrap system /Library/LaunchDaemons/homebrew.mxcl.mysql@8.0.plist exited with 5.エラー出るまでに踏んだ経緯です

最初にbin/devを利用してサーバーを立ち上げました。

そうすると下記のエラーが

ActiveRecord::ConnectionNotEstablished: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

言われた通りsocketが原因で、DBに接続できないのかなぁと思いながらdatabase.ymlの中身を見に行くと

database.yml
development:
  adapter: mysql
  database: hoge_development
  username: root
  password:
  encoding: utf8
test:

# 省略

socketの指定がないではありませんか…なぜか

調べたところによるとmy.cnfclient セクションで記述されているから問題ないとのこと

my.cnfというのはざっくりいうとMySQLの設定ファイルのことです
ログの出力場所などが記述されていたりします

ここまできて、ようやくsocketの問題ではなさそうだなということに気づきます。

では、そもそもMySQLが動いてるのかをbrew services listコマンドで確認しに行きます

MySQLのstatusstoppedになっていました

なのでbrew services start mysql@8.0でMySQLを再起動しようとすると下記のログが吐かれます

    Warning: Taking root:admin ownership of some mysql@8.0 paths:
    /opt/homebrew/Cellar/mysql@8.0/8.0.40_5/bin
    /opt/homebrew/Cellar/mysql@8.0/8.0.40_5/bin/mysqld_safe
    /opt/homebrew/opt/mysql@8.0
    /opt/homebrew/opt/mysql@8.0/bin
    This will require manual removal of these paths using `sudo rm` on
    brew upgrade/reinstall/uninstall.
    Warning: mysql@8.0 must be run as non-root to start at user login!
    Bootstrap failed: 5: Input/output error
    Error: Failure while executing; `/bin/launchctl bootstrap system /Library/LaunchDaemons/homebrew.mxcl.mysql@8.0.plist` exited with 5.

解決策

ログで指示された通りに下記の手順を踏みました

  • /opt/homebrew/Cellar/mysql@8.0/8.0.40_5/bin
  • /opt/homebrew/Cellar/mysql@8.0/8.0.40_5/bin/mysqld_safe
  • /opt/homebrew/opt/mysql@8.0
  • /opt/homebrew/opt/mysql@8.0/bin

何のファイルを削除しているのか知らないのも嫌なのでざっくりと調べてみました

  • /opt/homebrew/Cellar/mysql@8.0/8.0.40_5/bin
    • MySQLのバイナリファイルが格納されているディレクトリ、ここにはMySQLの実行可能ファイルが含まれている
  • /opt/homebrew/Cellar/mysql@8.0/8.0.40_5/bin/mysqld_safe

    • エラー発生時のサーバーの再起動やエラーログ、サーバークラッシュした時の再起動とかを提供してくれるファイル
  • /opt/homebrew/opt/mysql@8.0

    • MySQLのシンボリックリンクが格納されているディレクトリ。HomebrewがインストールしたMySQLのバージョンに対するショートカットとかが入っているファイル
  • /opt/homebrew/opt/mysql@8.0/bin
    • MySQLのバイナリファイルへのシンボリックリンクが格納されているディレクトリ

上記4つをrmコマンドで削除します

次に下記を対応します

Error: Failure while executing; `/bin/launchctl bootstrap system /Library/LaunchDaemons/homebrew.mxcl.mysql@8.0.plist` exited with 5.

色々と調査したのですが、なぜゆえbootstrapが出てきたのか、homebrew.mxcl.mysql@8.0.plistファイルのどこに問題があったのかは結局分からずじまいでした…

身も蓋もありませんが、ここでbrew uninstall mysql@8.0の後にbrew install mysql@8.0で再度インストールします

もろもろ初期設定を行った後にbrew services start mysql@8.0を行います

すると下記エラー

`Error: Failure while executing; `/bin/launchctl bootstrap system /Library/LaunchDaemons/homebrew.mxcl.mysql@8.0.plist` exited with 5.`

嘘だろと思いbrew services listを確認

mysql@8.0  error root ~/Library/LaunchAgents/homebrew.mxcl.mysql@8.0.plist

statusにerrorが発生していました。

startがダメならこいつでどうだbrew services restart mysql@8.0

ここでやっとこさMySQLが起動してくれました。

感想

  • 全くもって何で解決したか謎なので、いつか再度調査します、多分…

参考

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