This page looks best with JavaScript enabled
⚠️

【Rails】database.ymlって何?

 ·  ☕ 3 分で読めます
✏️

はじめに

RUNTEQ内でチーム開発をしているのですが、みんな大好き環境構築は最初の難関でございます。
特にデータベースでハマったことがない人はいないんではないでしょうか。

そんなきっかけで、database.ymlの設定について、自分の理解の振り返りも兼ねて書いておきます。

database.yml.defaultって何?

チーム開発ではよくあるのですが、database.yml.defaultは実際に使うファイルではありません。
あくまで初期設定ファイルとして置いてあるだけで、実際は各開発者がこれをコピーしてローカル開発用に編集します。

$ cp config/database.yml.default config/database.yml

そういうわけでdatabase.ymlは共有しないので、gitignoreされています。

database.ymlって何?

Railsでデータベースに接続するときは、このdatabase.ymlを見に行きます。
じゃあdatabase.ymlは何なのかというと、データベースとの接続情報をコード化しているだけです。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
default: &default
  adapter: mysql2
  encoding: utf8mb4
  charset: utf8mb4
  collation: utf8mb4_bin
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password:
  socket: /tmp/mysql.sock

development:
  <<: *default
  database: hoge_development

test:
  <<: *default
  database: hoge_test

production:
  <<: *default
  database: hoge_production
  username: hoge
  password: <%= ENV['HOGE_DATABASE_PASSWORD'] %>

例えば、MySQLではデータベースに接続するためのオプションとして、以下のようなものがあります。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
$ mysql --help
# 一部抜粋
Usage: mysql [OPTIONS] [database]
  -?, --help          Display this help and exit.
  -h, --host=name     Connect to host.
  -p, --password[=name]
                      Password to use when connecting to server. If password is
                      not given it's asked from the tty.
  -P, --port=#        Port number to use for connection or 0 for default to, in
                      order of preference, my.cnf, $MYSQL_TCP_PORT,
                      /etc/services, built-in default (3306).
  -S, --socket=name   The socket file to use for connection.
  -t, --table         Output in table format.
  -u, --user=name     User for login if not current user.
  -V, --version       Output version information and exit.

上記のdatabase.ymlのコードはmysql -u root -S /tmp/mysql.sock -t hoge_developmentと同じ意味です。

-p も –password も指定しない場合は、パスワードは送信されません。
MySQL :: MySQL 5.6 リファレンスマニュアル :: 4.2.2 MySQL サーバーへの接続

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$ mysql -u root -S /tmp/mysql.sock -t hoge_development

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 MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 114
Server version: 5.7.32 Homebrew

Copyright (c) 2000, 2020, 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> select * from users;
+----+-------------+---------------------+----------------------------+----------------------------+
| id | name        | email               | created_at                 | updated_at                 |
+----+-------------+---------------------+----------------------------+----------------------------+
| 14 | hoge        | test@example.com    | 2021-04-11 21:12:07.798476 | 2021-04-11 21:12:07.798476 |
| 16 | aiandrox    | aiandrox7@gmail.com | 2021-04-29 18:43:07.895733 | 2021-04-29 18:43:07.895733 |
+----+-------------+---------------------+----------------------------+----------------------------+
2 rows in set (0.01 sec)

他には、デフォルト値で-h localhost -P 3306があります。
(特に何もいじっていない場合。実際のデフォルト値はmy.confを参照)
基礎MySQL ~その2~ my.cnf (設定ファイル) - Qiita

なので、rails db:createなどのコマンドで接続ができないという場合は、ローカルのデータベース接続設定とdatabase.ymlの値を合わせる必要があります。
まずはmysqlコマンドでデータベースに接続することを目指しましょう。

その他

この前提は、本番環境にデプロイするときのdatabase.ymlの設定をするときにも参考になります。

また、Sequel Proなどのツールを用いてデータベースに接続するときも同じような感じです。

image

Share on

END
END
@aiandrox

 
目次