rails5.2でMysqlに接続しようとするとLost connection to MySQL server at ‘reading initial communication packet’が出る

エラー

 こんな感じのエラーが出る。

Mysql2::Error::ConnectionError:
  Lost connection to MySQL server at 'reading initial communication packet', system error: 0

 以下の記事によると、mysqlの設定の問題らしいが、migrationの途中で出ることもあるので、これが問題とはいえないと思った。

https://kb.tableau.com/articles/issue/error-lost-connection-to-mysql-server-at-reading-initial-communication-packet-connecting-to-mysql?lang=ja-jp

解決方法

 Mysqlを再起動する。

~/d ❯❯❯ bundle exec rspec spec/controllers/users_controller_spec.rb                                                                                                                        

An error occurred while loading ./spec/controllers/users_controller_spec.rb.
Failure/Error: ActiveRecord::Migration.maintain_test_schema!

Mysql2::Error::ConnectionError:
  Lost connection to MySQL server at 'reading initial communication packet', system error: 0

Finished in 0.0003 seconds (files took 14.81 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples

~/d ❯❯❯ mysql.server restart                                                                                                                                                               
Shutting down MySQL
.. SUCCESS! 
Starting MySQL
.. SUCCESS! 
~/d ❯❯❯ bundle exec rspec spec/controllers/users_controller_spec.rb
....................................................................................

Finished in 7.26 seconds (files took 16.32 seconds to load)
84 examples, 0 failures

 解決は簡単だけど、いちいち再起動させるのは手間。。暇な時に根本原因を調べよう。

仙人みならい

在宅通信は、住まい・家計・道具・家の運用を見直し、安心して暮らすための記録です。

現在は、夫婦2人・フルリモート・宮崎市・車なし生活を前提に、選んだ理由、かかった費用、失敗、使ってみた感想を残しています。

仙人みならいをフォローする
PC・プログラミング余白