2020/02/28
docker×railsでmysqlを使って開発しています。
table同士を外部キーで紐付けしたく、modelとmigrationファイルを作ったところ、作る際に紐付け先のtable名を間違えていました。
ただマイグレーション実行前にmigrationファイルとmodelの中身を編集し直したので結果、大丈夫だったのですがしっかり作れているか不安だった為、テーブルの中身を確認したいと思いました。
railsではrails dbconsoleというコマンドライン上からデータベースの中身の確認や編集をすることができるコマンド(らしい)があります。
今回それをつかって確認しようと思いましたが上記コマンドを実行しようとするとエラーになりました。
調べていくとdockerfileが原因らしく、以下の記事を参考にdockerfileを編集したらうまくいくようになりました。
https://qiita.com/ryosk7/items/077ea98a88ec3df289c8
一応、動くようにはなったのですが、dockerのイメージやらコンテナやら用語がイマイチ難しくてわかっていません。
rails $ docker-compose run web rails dbconsole
Starting rails_db_1 ... done
Enter password:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.29 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [app_development]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| app_development |
| app_test |
| mysql |
| performance_schema |
| sys |
+--------------------+
6 rows in set (0.00 sec)
MySQL [app_development]>
ちなみに rails dbconsole からでなく普通に接続したらデータベースの中身が違ったのですがなぜでしょう、、、
$ mysql -u root -p[パスワード]
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.16 Homebrew
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
mysql>
参考:
https://jp.techcrunch.com/2016/10/17/20161016wtf-is-a-container/
https://knowledge.sakura.ad.jp/15253/