LoginSignup
4
4

More than 5 years have passed since last update.

rails consoleにて、dbをproductionに接続する。

Posted at

何かとrails consoleでデータを加工したり、出力したりする際に、適当なスクリプト作成して、ローカルで確認して、スクリプトをプロダクションにコピーして、console立ち上げてとか、めんどい。

sshでトンネル接続して、そこに接続

database.ymlの設定

プロダクション環境のDB、ならびに、ssh設定

production_tunnel:
  adapter: mysql2
  host: 127.0.0.1
  port: 1234
  encoding: utf8
  reconnect: false
  database: <your databaes name>
  pool: 5
  timeout: 5000
  username: <your user name>
  password: <your password>
  socket: <your socket path if you need>

ssh接続

ssh username@host -L 1234:127.0.0.1:production_port

rails console

#起動
rails c
#接続先変更
ActiveRecord::Base.establish_connection(:production_tunnel)

以上

4
4
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
4
4