【転職102】本日やった事と感想+まだまだmysqlに苦戦中…

【終わったタスク】10/6(火)

◆勉強

・Ruby on Rails5 II(2.3h)

 →投稿一覧ページの続きを進める。

◆運動

無し。

◆その他

・ダイエットブログ更新。

・10/7の予定を決める。

・転職ブログ更新。

【感想】

10/6

今日は急に思い立ってYoutubeの動画でdockerxrorxmysqlで開発をしている動画を探して参考にしようと思い、↓の動画を参考にしてみました。

 

 

17:50:25

[~/sample_app03] % docker-compose run –rm web rails g post model content:text

Creating network “sample_app03_default” with the default driver

Creating sample_app03_db_1 … done

Creating sample_app03_web_run … done

Running via Spring preloader in process 22

Could not find generator ‘post’. 

Run `rails generate –help` for more options.

17:53:22

[~/sample_app03] % docker-compose run –rm web rails db:migrate

Starting sample_app03_db_1 … done

Creating sample_app03_web_run … done

rails aborted!

Mysql2::Error::ConnectionError: Unknown MySQL server host ‘db’ (-2)

/usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:90:in `connect’

/usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:90:in `initialize’

/usr/local/bundle/gems/activerecord-6.0.3.2/lib/active_record/connection_adapters/mysql2_adapter.rb:24:in `new’

/usr/local/bundle/gems/activerecord-6.0.3.2/lib/active_record/connection_adapters/mysql2_adapter.rb:24:in `mysql2_connection’

/usr/local/bundle/gems/activerecord-6.0.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:887:in `new_connection’

/usr/local/bundle/gems/activerecord-6.0.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:931:in `checkout_new_connection’

/usr/local/bundle/gems/activerecord-6.0.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:910:in `try_to_checkout_new_connection’

/usr/local/bundle/gems/activerecord-6.0.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:871:in `acquire_connection’

/usr/local/bundle/gems/activerecord-6.0.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:593:in `checkout’

/usr/local/bundle/gems/activerecord-6.0.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:437:in `connection’

/usr/local/bundle/gems/activerecord-6.0.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:1119:in `retrieve_connection’

/usr/local/bundle/gems/activerecord-6.0.3.2/lib/active_record/connection_handling.rb:221:in `retrieve_connection’

/usr/local/bundle/gems/activerecord-6.0.3.2/lib/active_record/connection_handling.rb:189:in `connection’

/usr/local/bundle/gems/activerecord-6.0.3.2/lib/active_record/tasks/database_tasks.rb:238:in `migrate’

/usr/local/bundle/gems/activerecord-6.0.3.2/lib/active_record/railties/databases.rake:86:in `block (3 levels) in <main>’

/usr/local/bundle/gems/activerecord-6.0.3.2/lib/active_record/railties/databases.rake:84:in `each’

/usr/local/bundle/gems/activerecord-6.0.3.2/lib/active_record/railties/databases.rake:84:in `block (2 levels) in <main>’

/usr/local/bundle/gems/railties-6.0.3.2/lib/rails/commands/rake/rake_command.rb:23:in `block in perform’

/usr/local/bundle/gems/railties-6.0.3.2/lib/rails/commands/rake/rake_command.rb:20:in `perform’

/usr/local/bundle/gems/railties-6.0.3.2/lib/rails/command.rb:48:in `invoke’

/usr/local/bundle/gems/railties-6.0.3.2/lib/rails/commands.rb:18:in `<main>’

/usr/local/bundle/gems/bootsnap-1.4.8/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require’

/usr/local/bundle/gems/bootsnap-1.4.8/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi’

/usr/local/bundle/gems/bootsnap-1.4.8/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register’

/usr/local/bundle/gems/bootsnap-1.4.8/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi’

/usr/local/bundle/gems/bootsnap-1.4.8/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require’

/usr/local/bundle/gems/activesupport-6.0.3.2/lib/active_support/dependencies.rb:324:in `block in require’

/usr/local/bundle/gems/activesupport-6.0.3.2/lib/active_support/dependencies.rb:291:in `load_dependency’

/usr/local/bundle/gems/activesupport-6.0.3.2/lib/active_support/dependencies.rb:324:in `require’

/app/bin/rails:9:in `<top (required)>’

/usr/local/bundle/gems/spring-2.1.1/lib/spring/client/rails.rb:28:in `load’

/usr/local/bundle/gems/spring-2.1.1/lib/spring/client/rails.rb:28:in `call’

/usr/local/bundle/gems/spring-2.1.1/lib/spring/client/command.rb:7:in `call’

/usr/local/bundle/gems/spring-2.1.1/lib/spring/client.rb:30:in `run’

/usr/local/bundle/gems/spring-2.1.1/bin/spring:49:in `<top (required)>’

/usr/local/bundle/gems/spring-2.1.1/lib/spring/binstub.rb:11:in `load’

/usr/local/bundle/gems/spring-2.1.1/lib/spring/binstub.rb:11:in `<top (required)>’

/app/bin/spring:15:in `<top (required)>’

bin/rails:3:in `load’

bin/rails:3:in `<main>’

Tasks: TOP => db:migrate

(See full trace by running task with –trace)

上記にて実施したが、”Unknown MySQL server host ‘db’ (-2) ”となってしまう。

ただ、docker上でrailsのコマンドを実行するときは、”docker-compose run web”が必須であることが学べました。

次に気がついたのが、”Unknown MySQL server host ‘db’ (-2) ”となるけど、”docker-compose up”をすると接続されると”db-1”となっているので、違うデータベースにアクセスしているのではないか?と思いました。

dockerのイメージを削除しても変わらないので、volumesで残っているのが影響しているのではないか?ということで、↓の記事を参考にvolumesを削除してみました。

 →:https://qiita.com/Ikumi/items/b319a12d7e2c9f7b904d

実行結果は変わらず。今日はここまで。