mysqlのエラー ERROR! The server quit without updating PID file

mysql.server startを入力した時、ERROR! The server quit without updating PID fileというエラーが発生した。


試したこと

以下の記事に書かれていることを試しました。

mysql 起動時のThe server quit without updating PID file エラーの回避法 - Qiita

touch /usr/local/var/mysql/*****.local.pid

pidファイルの作成をしましたが、エラーが解決できず。

sudo chown -R _mysql:_mysql /usr/local/var/mysql/

ファイルの所有者をすべて_mysqlにしたが、これでも解決できず。

次にパソコンを再起動したが、それでも解決できず途方にくれていました。


解決法

どうしよう!困った時のMac上のMySQLのアンインストール&再インストール、動作確認手順 - Qiita 上記のように関連ファイルを全て削除してアンインストールしました。

brew uninstall mysql   
brew install mysql@5.7
mysql.server start
Starting MySQL
 SUCCESS! 

無事成功しました。

gem 'fast_jsonapi'

fast_jsonapiとは

Netflixが提供しているgemで、JSONのserializerです。jsonのserializerとは、jsonを生成する仕組みのことです。

シリアライズ(serialize)とは、プログラミングでオプジェクト化されたデータを、ファイルやストレージに保存したり、ネットワークで送受信したりできるような形に変換することを言います。


実装

gem 'fast_jsonapi'
rails g serializer Article(モデル名) title contents status(カラム名を列挙)

article_serializer.rb

class ArticleSerializer
  include FastJsonapi::ObjectSerializer
  attributes :title, :contents, :status

# モデルの設定に合わせる
  belongs_to :user
end

article.rb

class Article < ApplicationRecord
  belongs_to :user
  has_many :comments, dependent: :destroy

  enum status: { draft: 0, in_review: 10, published: 20, archived: 30 }
end

コントローラー側で呼び出す 今回は、記事一覧を呼び出してみます。

controllers/api/v1/articles_controller.rb

module Api
  module V1
    class ArticlesController < BaseController
      def index
        articles = Article.all
        json_string = ArticleSerializer.new(articles).serialized_json
        render json: json_string
      end
    end
  end
end


github.com

An error occurred while installing puma (4.3.3), and Bundler cannot continue.

bundle update mimemagicをした後に、以下のエラーが発生しました。

An error occurred while installing puma (4.3.3), and Bundler cannot continue.
Make sure that `gem install puma -v '4.3.3' --source 'https://rubygems.org/'` succeeds before bundling.



解決法

% bundle config build.puma --with-cflags="-Wno-error=implicit-function-declaration"

% bundle install

その後bundle installすると、上手く行きました。

bundle install --path vendor/bundle

bundle install --path vendor/bundleができなエラー

git cloneをした後、bundle install --path vendor/bundleをすると、以下のエラーが発生しました。 Image from Gyazo



解決法

You'll need to update your bundle to a version other than mimemagic (0.3.4) that hasn't been removed in order to install.と記載されているので、 bundle update mimemagicを実行。

undefined method 'default' for ActionMailer:Module

Image from Gyazo

原因

エラー文をよく見ると、

class ApplicationMailer < ActionMailer::

継承文がなんかすごく変!途中で途切れていない?

class ApplicationMailer < ActionMailer::Baseと書き直してあげると、解決できました。

Active Storage ActiveStorage::Attached

site.rb

class Site < ApplicationRecord
  has_one_attached :og_image
  has_one_attached :favicon
  has_many_attached :main_images
end
  • favicon:アイコン画

  • og_image:画像を1枚添付できる

  • main_images:画像を複数枚添付できる

これらをrails consoleで見てみましょう

[25] pry(main)> site.favicon
=> #<ActiveStorage::Attached::One:0x00007fd76d0579a8
 @dependent=:purge_later,
 @name="favicon",
 @record=
  #<Site:0x00007fd76aeef9a0
   id: 1,
   name: "Blog",
   subtitle: "Very awesome!",
   description: "",
   created_at: Fri, 02 Jul 2021 07:40:50 JST +09:00,
   updated_at: Fri, 02 Jul 2021 08:44:57 JST +09:00>>
[27] pry(main)> site.og_image
=> #<ActiveStorage::Attached::One:0x00007fd76c532938
 @dependent=:purge_later,
 @name="og_image",
 @record=
  #<Site:0x00007fd76aeef9a0
   id: 1,
   name: "Blog",
   subtitle: "Very awesome!",
   description: "",
   created_at: Fri, 02 Jul 2021 07:40:50 JST +09:00,
   updated_at: Fri, 02 Jul 2021 08:44:57 JST +09:00>>
[28] pry(main)> site.main_images
=> #<ActiveStorage::Attached::Many:0x00007fd764515480
 @dependent=:purge_later,
 @name="main_images",
 @record=
  #<Site:0x00007fd76aeef9a0
   id: 1,
   name: "Blog",
   subtitle: "Very awesome!",
   description: "",
   created_at: Fri, 02 Jul 2021 07:40:50 JST +09:00,
   updated_at: Fri, 02 Jul 2021 08:44:57 JST +09:00>>

ここで気になったのがActiveStorage::Attached::OneとActiveStorage::Attached::Manyさらに、@dependent、@name、@recordです。



ActiveStorage::Attached::OneとActiveStorage::Attached::Many

site.rbでhas_one_attachedで定義された添付ファイルはActiveStorage::Attached::One

ActiveStorage::Attached::One

has_many_attached で定義された添付ファイルはActiveStorage::Attached::Manyです。

ActiveStorage::Attached::Many

ActiveStorage::Attachedについては、以下の記事が参考になります。ざっくり説明すると、ActiveStorage::Attached::OneおよびActiveStorage::Attached::Manyクラスの抽象ベースクラスで、レコードのblob関連へのプロキシアクセスを提供します。

api.rubyonrails.org



@dependent、@name、@record

そもそもこの3つはどこで定義されているの?

探してみるとありました!

$ rails active_storage:install

↑を実行したときに、このファイルが作られるみたいです。ちなみにprivate以下は、コードを書き換えています。 attached.rb

# frozen_string_literal: true

require "action_dispatch"
require "action_dispatch/http/upload"
require "active_support/core_ext/module/delegation"

module ActiveStorage
  # Abstract base class for the concrete ActiveStorage::Attached::One and ActiveStorage::Attached::Many
  # classes that both provide proxy access to the blob association for a record.
  class Attached
    attr_reader :name, :record, :dependent

    def initialize(name, record, dependent:)
      @name, @record, @dependent = name, record, dependent
    end

    private
      def create_blob_from(attachable)
        case attachable
        when ActiveStorage::Blob
          attachable
        when ActionDispatch::Http::UploadedFile, Rack::Test::UploadedFile
          ActiveStorage::Blob.create_after_upload! \
            io: attachable.open,
            filename: attachable.original_filename,
            content_type: attachable.content_type
        when Hash
          ActiveStorage::Blob.create_after_upload!(attachable)
        when String
          ActiveStorage::Blob.find_signed(attachable)
        else
          nil
        end
      end
  end
end

require "active_storage/attached/one"
require "active_storage/attached/many"
require "active_storage/attached/macros"

undefined method 'favicon' for nil:NilClass

突然以下のエラーに遭遇した。 Image from Gyazo

application_controller

  def current_site
    # @current_siteがnillであれば、Site.firstを代入する。
    @current_site ||= Site.first
  end
  helper_method :current_site

current_siteはちゃんと定義できているしな〜...

原因がわからず、かなり悩んでいました。


原因と解決

qiita.com この記事を参考に

rails db:migrate:reset
bundle exec rails db:seed_fu

を実行すると、エラーが解消できました。

どうやらDB周りがうまくいっていなかったようです。 db:seed_fuを再度bundleしたところうまくいきました。