LoginSignup
0
0

More than 1 year has passed since last update.

Rails と SQL Server の組み合わせで DB ユーザーのパスワードを '#' からはじまるものを使うと Rails アプリから接続できない

Last updated at Posted at 2021-08-10

要約

記事タイトルのままですが、 DB ユーザーのパスワードを '#' からはじまるものにしていると Rails アプリから DB に接続できません。

利用しているバージョン

パスワードを '#' からはじまるものにしているとき

sqlcmd を利用すると接続できます。

[user@machine app]$ sqlcmd -H db -U user -P "#hogehoge"
1> use my_database
2> go
Changed database context to 'my_database'.
1> select count(id) from samples
2> go

-----------
          0

(1 rows affected)
1> quit
[user@machine app]$

しかし、 Rails アプリからは接続できません。

[user@machine app]$ cat .env
DATABASE_HOST=db
DATABASE_USERNAME=user
DATABASE_PASSWORD="#hogehoge"
[user@machine app]$ bin/rails r 'Sample.find 1'
/app/vendor/bundle/ruby/3.0.0/gems/tiny_tds-2.1.5/lib/tiny_tds/client.rb:60:in `connect': Login failed for user 'user'. (TinyTds::Error)
    from /app/vendor/bundle/ruby/3.0.0/gems/tiny_tds-2.1.5/lib/tiny_tds/client.rb:60:in `initialize'
    ...
    from bin/rails:4:in `<main>'
[user@machine app]$

パスワードを '#' からはじまらないものにしているとき

[user@machine app]$ cat .env
DATABASE_HOST=db
DATABASE_USERNAME=another_user
DATABASE_PASSWORD="not-starts-with-hash"
[user@machine app]$ bin/rails r 'Sample.find 1'
/app/vendor/bundle/ruby/3.0.0/gems/activerecord-6.1.4/lib/active_record/core.rb:338:in `find': Couldn't find Sample with 'id'=1 (ActiveRecord::RecordNotFound)
    from /app/vendor/bundle/ruby/3.0.0/gems/railties-6.1.4/lib/rails/commands/runner/runner_command.rb:45:in `<main>'
    ...
    from bin/rails:4:in `<main>'
[user@machine app]$

.find には失敗していますが、接続はできています。

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