1
2

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.

miriwo毎日投稿のついで(内容はランダム)Advent Calendar 2019

Day 11

Rails6のwebアプリをherokuでデプロイしてdb:migrateしたときのエラーを解決した話

Posted at

目的

  • コマンドheroku run rake db:migrateを実行したときに出たエラーの解決したときの話をまとめる

エラー概要

  • herokuにpush後にコマンドheroku run rake db:migrateを実行した際に下記のエラーが出た。

    $ heroku run rake db:migrate
    Running rake db:migrate on ⬢ study-record... up, run.3764 (Free)
    rake aborted!
    Mysql2::Error::ConnectionError: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
    /app/vendor/bundle/ruby/2.5.0/gems/mysql2-0.5.2/lib/mysql2/client.rb:90:in `connect'
    /app/vendor/bundle/ruby/2.5.0/gems/mysql2-0.5.2/lib/mysql2/client.rb:90:in `initialize'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/mysql2_adapter.rb:24:in `new'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/mysql2_adapter.rb:24:in `mysql2_connection'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:879:in `new_connection'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:923:in `checkout_new_connection'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:902:in `try_to_checkout_new_connection'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:863:in `acquire_connection'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:587:in `checkout'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:431:in `connection'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:1111:in `retrieve_connection'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-6.0.0/lib/active_record/connection_handling.rb:231:in `retrieve_connection'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-6.0.0/lib/active_record/connection_handling.rb:199:in `connection'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-6.0.0/lib/active_record/tasks/database_tasks.rb:238:in `migrate'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-6.0.0/lib/active_record/railties/databases.rake:85:in `block (3 levels) in <top (required)>'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-6.0.0/lib/active_record/railties/databases.rake:83:in `each'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-6.0.0/lib/active_record/railties/databases.rake:83:in `block (2 levels) in <top (required)>'
    /app/vendor/bundle/ruby/2.5.0/gems/rake-13.0.0/exe/rake:27:in `<top (required)>'
    /app/vendor/bundle/ruby/2.5.0/gems/bundler-2.0.2/lib/bundler/cli/exec.rb:74:in `load'
    /app/vendor/bundle/ruby/2.5.0/gems/bundler-2.0.2/lib/bundler/cli/exec.rb:74:in `kernel_load'
    /app/vendor/bundle/ruby/2.5.0/gems/bundler-2.0.2/lib/bundler/cli/exec.rb:28:in `run'
    /app/vendor/bundle/ruby/2.5.0/gems/bundler-2.0.2/lib/bundler/cli/exec.rb:28:in `run'
    /app/vendor/bundle/ruby/2.5.0/gems/bundler-2.0.2/lib/bundler/cli/exec.rb:28:in `run'
    /app/vendor/bundle/ruby/2.5.0/gems/bundler-2.0.2/lib/bundler/cli.rb:465:in `exec'
    /app/vendor/bundle/ruby/2.5.0/gems/bundler-2.0.2/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
    /app/vendor/bundle/ruby/2.5.0/gems/bundler-2.0.2/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
    /app/vendor/bundle/ruby/2.5.0/gems/bundler-2.0.2/lib/bundler/vendor/thor/lib/thor.rb:387:in `dispatch'
    /app/vendor/bundle/ruby/2.5.0/gems/bundler-2.0.2/lib/bundler/cli.rb:27:in `dispatch'
    /app/vendor/bundle/ruby/2.5.0/gems/bundler-2.0.2/lib/bundler/vendor/thor/lib/thor/base.rb:466:in `start'
    /app/vendor/bundle/ruby/2.5.0/gems/bundler-2.0.2/lib/bundler/cli.rb:18:in `start'
    /app/vendor/bundle/ruby/2.5.0/gems/bundler-2.0.2/exe/bundle:30:in `block in <top (required)>'
    /app/vendor/bundle/ruby/2.5.0/gems/bundler-2.0.2/lib/bundler/friendly_errors.rb:124:in `with_friendly_errors'
    /app/vendor/bundle/ruby/2.5.0/gems/bundler-2.0.2/exe/bundle:22:in `<top (required)>'
    /app/bin/bundle:104:in `load'
    /app/bin/bundle:104:in `<main>'
    Tasks: TOP => db:migrate
    (See full trace by running task with --trace)
    

エラーの予想

  • pushまでは問題なかったため、直前に実施したDB系の設定がおかしいのでは??と予想した。
  • ソケット系のエラーが出ているので、その辺の情報も怪しいと思った。

調査

  1. 直前に設定していたDB系の設定を確認
    • 下記コマンドを実行して設定を確認した。

      $ heroku config
      
    • 上記コマンドの出力↓

      === study-record Config Vars
      CLEARDB_DATABASE_URL:     mysql://b60b5336b9085d:754f140c@us-cdbr-iron-east-05.cleardb.net/heroku_f69d4fc63e3b43f?reconnect=true
      DB_HOSTNAME:              us-cdbr-iron-east-05.cleardb.net
      DB_NAME:                  heroku_f69d4fc63e3b43f
      DB_PASSWORD:              754f140c
      DB_PORT:                  3306
      DB_USERNAME:              b60b5336b9085d
      LANG:                     en_US.UTF-8
      RACK_ENV:                 production
      RAILS_ENV:                production
      RAILS_LOG_TO_STDOUT:      enabled
      RAILS_SERVE_STATIC_FILES: enabled
      SECRET_KEY_BASE:          0d58d7c950379d4bfe741b3ae465b46aaa159ae398c2aaaea5010ae2d817b308a90a7d4702e6281ad609c94de1acf03cb50c1e39d21d9e66af636d812f2e823f
      

おかしいところ発見

  • 設定のCLEARDB_DATABASE_URLのURLがmysqlから始まっていることに気が付いた。

  • 自分が使用しているのmysql2なのにmysqlで良いのかと違和感があった。

  • 下記コマンドを実行してmysql2に修正した。

    $ heroku config:set DATABASE_URL='mysql2://b60b5336b9085d:754f140c@us-cdbr-iron-east-05.cleardb.net/heroku_f69d4fc63e3b43f?reconnect=true'
    
  • 再度下記コマンドを実行してheroku側でdb:migrateを行なったところ正常に実行できた。

    $ heroku run rake db:migrate
        Running rake db:migrate on ⬢ study-record... up, run.5104 (Free)
    D, [2019-12-10T15:16:34.677916 #4] DEBUG -- :    (2.4ms)  SET NAMES utf8mb4,  @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'),  @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
    D, [2019-12-10T15:16:34.715120 #4] DEBUG -- :    (2.2ms)  SELECT @@innodb_file_per_table = 1 AND @@innodb_file_format = 'Barracuda'
    D, [2019-12-10T15:16:34.732371 #4] DEBUG -- :    (16.9ms)  CREATE TABLE `schema_migrations` (`version` varchar(255) NOT NULL PRIMARY KEY) ROW_FORMAT=DYNAMIC
    D, [2019-12-10T15:16:34.752768 #4] DEBUG -- :    (13.9ms)  CREATE TABLE `ar_internal_metadata` (`key` varchar(255) NOT NULL PRIMARY KEY, `value` varchar(255), `created_at` datetime(6) NOT NULL, `updated_at` datetime(6) NOT NULL) ROW_FORMAT=DYNAMIC
    D, [2019-12-10T15:16:34.757508 #4] DEBUG -- :    (2.4ms)  SELECT GET_LOCK('3434563884671206245', 0)
    D, [2019-12-10T15:16:34.774649 #4] DEBUG -- :    (3.1ms)  SELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC
    I, [2019-12-10T15:16:34.775957 #4]  INFO -- : Migrating to CreateUsers (20191106122609)
    == 20191106122609 CreateUsers: migrating ======================================
    -- create_table(:users)
    D, [2019-12-10T15:16:34.795305 #4] DEBUG -- :    (15.9ms)  CREATE TABLE `users` (`id` bigint NOT NULL AUTO_INCREMENT PRIMARY KEY, `name` varchar(255), `email` varchar(255), `created_at` datetime(6) NOT NULL, `updated_at` datetime(6) NOT NULL) ROW_FORMAT=DYNAMIC
       -> 0.0168s
    == 20191106122609 CreateUsers: migrated (0.0169s) =============================
    
    D, [2019-12-10T15:16:34.811775 #4] DEBUG -- :    (4.0ms)  BEGIN
    D, [2019-12-10T15:16:34.814482 #4] DEBUG -- :   primary::SchemaMigration Create (2.6ms)  INSERT INTO `schema_migrations` (`version`) VALUES ('20191106122609')
    D, [2019-12-10T15:16:34.816842 #4] DEBUG -- :    (2.2ms)  COMMIT
    I, [2019-12-10T15:16:34.816964 #4]  INFO -- : Migrating to CreatePosts (20191110100157)
    == 20191110100157 CreatePosts: migrating ======================================
    -- create_table(:posts)
    D, [2019-12-10T15:16:34.832775 #4] DEBUG -- :    (15.0ms)  CREATE TABLE `posts` (`id` bigint NOT NULL AUTO_INCREMENT PRIMARY KEY, `content` text, `created_at` datetime(6) NOT NULL, `updated_at` datetime(6) NOT NULL) ROW_FORMAT=DYNAMIC
       -> 0.0154s
    == 20191110100157 CreatePosts: migrated (0.0155s) =============================
    
    D, [2019-12-10T15:16:34.835642 #4] DEBUG -- :    (2.3ms)  BEGIN
    D, [2019-12-10T15:16:34.838096 #4] DEBUG -- :   primary::SchemaMigration Create (2.3ms)  INSERT INTO `schema_migrations` (`version`) VALUES ('20191110100157')
    D, [2019-12-10T15:16:34.840385 #4] DEBUG -- :    (2.1ms)  COMMIT
    I, [2019-12-10T15:16:34.840476 #4]  INFO -- : Migrating to AddStudyTimeHashTagToPosts (20191112145912)
    == 20191112145912 AddStudyTimeHashTagToPosts: migrating =======================
    -- add_column(:posts, :study_time, "decimal")
    D, [2019-12-10T15:16:34.861032 #4] DEBUG -- :    (19.9ms)  ALTER TABLE `posts` ADD `study_time` decimal
       -> 0.0202s
    -- add_column(:posts, :hash_tag, "text")
    D, [2019-12-10T15:16:34.877828 #4] DEBUG -- :    (16.4ms)  ALTER TABLE `posts` ADD `hash_tag` text
       -> 0.0168s
    == 20191112145912 AddStudyTimeHashTagToPosts: migrated (0.0371s) ==============
    
    D, [2019-12-10T15:16:34.880640 #4] DEBUG -- :    (2.2ms)  BEGIN
    D, [2019-12-10T15:16:34.882939 #4] DEBUG -- :   primary::SchemaMigration Create (2.1ms)  INSERT INTO `schema_migrations` (`version`) VALUES ('20191112145912')
    D, [2019-12-10T15:16:34.885466 #4] DEBUG -- :    (2.3ms)  COMMIT
    D, [2019-12-10T15:16:34.898276 #4] DEBUG -- :   ActiveRecord::InternalMetadata Load (2.7ms)  SELECT `ar_internal_metadata`.* FROM `ar_internal_metadata` WHERE `ar_internal_metadata`.`key` = 'environment' LIMIT 1
    D, [2019-12-10T15:16:34.909203 #4] DEBUG -- :    (2.3ms)  BEGIN
    D, [2019-12-10T15:16:34.911642 #4] DEBUG -- :   ActiveRecord::InternalMetadata Create (2.3ms)  INSERT INTO `ar_internal_metadata` (`key`, `value`, `created_at`, `updated_at`) VALUES ('environment', 'production', '2019-12-10 15:16:34.906194', '2019-12-10 15:16:34.906194')
    D, [2019-12-10T15:16:34.914188 #4] DEBUG -- :    (2.3ms)  COMMIT
    D, [2019-12-10T15:16:34.916800 #4] DEBUG -- :    (2.4ms)  SELECT RELEASE_LOCK('3434563884671206245')
    

自分用メモ

  • db:migrate系のエラーはだいたい設定のミスなのでよく見直すこと。

付録

  • 誤っていたBDの設定ファイルと正常なDBの設定ファイルを下記に記載する。

  • 誤っていたDBの設定ファイル↓

    CLEARDB_DATABASE_URL:     mysql://b60b5336b9085d:754f140c@us-cdbr-iron-east-05.cleardb.net/heroku_f69d4fc63e3b43f?reconnect=true
    DB_HOSTNAME:              us-cdbr-iron-east-05.cleardb.net
    DB_NAME:                  heroku_f69d4fc63e3b43f
    DB_PASSWORD:              754f140c
    DB_PORT:                  3306
    DB_USERNAME:              b60b5336b9085d
    LANG:                     en_US.UTF-8
    RACK_ENV:                 production
    RAILS_ENV:                production
    RAILS_LOG_TO_STDOUT:      enabled
    RAILS_SERVE_STATIC_FILES: enabled
    SECRET_KEY_BASE:          0d58d7c950379d4bfe741b3ae465b46aaa159ae398c2aaaea5010ae2d817b308a90a7d4702e6281ad609c94de1acf03cb50c1e39d21d9e66af636d812f2e823f
    
  • 正常なDBの設定ファイル↓

    CLEARDB_DATABASE_URL:     mysql2://b60b5336b9085d:754f140c@us-cdbr-iron-east-05.cleardb.net/heroku_f69d4fc63e3b43f?reconnect=true
    DB_HOSTNAME:              us-cdbr-iron-east-05.cleardb.net
    DB_NAME:                  heroku_f69d4fc63e3b43f
    DB_PASSWORD:              754f140c
    DB_PORT:                  3306
    DB_USERNAME:              b60b5336b9085d
    LANG:                     en_US.UTF-8
    RACK_ENV:                 production
    RAILS_ENV:                production
    RAILS_LOG_TO_STDOUT:      enabled
    RAILS_SERVE_STATIC_FILES: enabled
    SECRET_KEY_BASE:          0d58d7c950379d4bfe741b3ae465b46aaa159ae398c2aaaea5010ae2d817b308a90a7d4702e6281ad609c94de1acf03cb50c1e39d21d9e66af636d812f2e823f
    
1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?