Rails and Azure: TinyTds::Error: Adaptive Server connection failed

Rails and Azure: TinyTds::Error: Adaptive Server connection failed

我正在尝试在 Azure 上使用 SQL 配置我的 Rails 项目。我正在使用 Mac OS X 10.11.

这是我的一部分 config/database.yml:

staging:
  adapter: sqlserver 
  mode: dblib 
  host: db-staging.database.windows.net
  port: 1433 
  database: db-staging
  username: myuser@db-staging
  password: mypass
  timeout: 5000
  azure: true 

当我 运行 tsql 似乎一切正常时:

$ tsql -H db-staging.database.windows.net -U myuser -P 'mypass' -v -p 1433 -D db-staging
locale is "en_US.UTF-8"
locale charset is "UTF-8"
using default charset "UTF-8"
Setting db-staging as default database in login packet
Changed database context to 'db-staging'.
Changed language setting to us_english.
1> 

但是当我 运行 rake db:migrate 我收到这个错误 TinyTds::Error: Adaptive Server connection failed.

这是完整的轨迹:

$ rake db:migrate RAILS_ENV=staging --trace                                                                      
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
config.eager_load is set to nil. Please update your config/environments/*.rb files accordingly:

  * development - set it to false
  * test - set it to false (unless you use a tool that preloads your test environment)
  * production - set it to true

** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:migrate
rake aborted!
TinyTds::Error: Adaptive Server connection failed
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/tiny_tds-0.7.0/lib/tiny_tds/client.rb:74:in `connect'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/tiny_tds-0.7.0/lib/tiny_tds/client.rb:74:in `initialize'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/activerecord-sqlserver-adapter-4.2.10/lib/active_record/connection_adapters/sqlserver_adapter.rb:311:in `new'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/activerecord-sqlserver-adapter-4.2.10/lib/active_record/connection_adapters/sqlserver_adapter.rb:311:in `dblib_connect'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/activerecord-sqlserver-adapter-4.2.10/lib/active_record/connection_adapters/sqlserver_adapter.rb:302:in `connect'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/activerecord-sqlserver-adapter-4.2.10/lib/active_record/connection_adapters/sqlserver_adapter.rb:58:in `initialize'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/activerecord-sqlserver-adapter-4.2.10/lib/active_record/sqlserver_base.rb:17:in `new'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/activerecord-sqlserver-adapter-4.2.10/lib/active_record/sqlserver_base.rb:17:in `sqlserver_connection'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:438:in `new_connection'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:448:in `checkout_new_connection'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:422:in `acquire_connection'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:349:in `block in checkout'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:348:in `checkout'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:263:in `block in connection'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:262:in `connection'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:571:in `retrieve_connection'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/connection_handling.rb:113:in `retrieve_connection'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/connection_handling.rb:87:in `connection'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/migration.rb:912:in `initialize'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/migration.rb:819:in `new'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/migration.rb:819:in `up'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/migration.rb:797:in `migrate'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/rake-11.1.2/lib/rake/task.rb:248:in `call'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/rake-11.1.2/lib/rake/task.rb:248:in `block in execute'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/rake-11.1.2/lib/rake/task.rb:243:in `each'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/rake-11.1.2/lib/rake/task.rb:243:in `execute'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/rake-11.1.2/lib/rake/task.rb:187:in `block in invoke_with_call_chain'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/rake-11.1.2/lib/rake/task.rb:180:in `invoke_with_call_chain'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/rake-11.1.2/lib/rake/task.rb:173:in `invoke'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/rake-11.1.2/lib/rake/application.rb:150:in `invoke_task'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/rake-11.1.2/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/rake-11.1.2/lib/rake/application.rb:106:in `each'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/rake-11.1.2/lib/rake/application.rb:106:in `block in top_level'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/rake-11.1.2/lib/rake/application.rb:115:in `run_with_threads'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/rake-11.1.2/lib/rake/application.rb:100:in `top_level'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/rake-11.1.2/lib/rake/application.rb:78:in `block in run'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/rake-11.1.2/lib/rake/application.rb:176:in `standard_exception_handling'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/rake-11.1.2/lib/rake/application.rb:75:in `run'
/Users/monteirobrena/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/gems/2.0.0/gems/rake-11.1.2/bin/rake:33:in `<top (required)>'
/Users/monteirobrena/.rvm/gems/ruby-2.0.0-p481/bin/rake:23:in `load'
/Users/monteirobrena/.rvm/gems/ruby-2.0.0-p481/bin/rake:23:in `<main>'
Tasks: TOP => db:migrate

我看到了其他类似的问题,但没有帮助我:

TinyTds::Error: Adaptive Server connection failed

TinyTds Error: Adaptive Server connection timed out

[更新]

我试过完全按照这里的方式做: https://github.com/Azure/azure-sql-database-samples/tree/master/Ruby%20on%20Rails/Sample%20Mac

按照此 post 我找到了要在我的 config/database.yml 中设置的正确配置。

https://azure.microsoft.com/en-us/documentation/articles/sql-database-develop-ruby-simple-mac-osx/

staging:
  adapter: sqlserver
  username: 'username@database'
  password: 'password'
  host: 'db-staging.database.windows.net'
  port: 1433
  database: 'db-staging'
  azure: true

并在我的 Gemfile:

中添加此宝石
gem 'tiny_tds'
gem 'activerecord-sqlserver-adapter'

现在,当我访问我的项目文件夹和 运行 rails s 时,我可以连接到我的数据库并且一切正常。但是,如果我 运行 我的项目使用 Puma 和 Nginx,我收到这个错误:

TinyTds::Error (Adaptive Server connection failed):

[/更新]

需要自己安装FreeTDS,如果apt-get无法传递编译参数:

wget http://ibiblio.org/pub/Linux/ALPHA/freetds/stable/freetds-stable.tgz

$ tar -zxvf freetds-stable.tgz

$ cd freetds-0.91/

$ ./configure --with-tdsver=8.0 --with-openssl=/usr/bin

$ make

$ sudo make install

现在我可以访问我的数据库了:

$ tsql -C # OK!

$ TDSVER=8.0 tsql -H mydb.database.windows.net -p 1433 -U myuser@mydb -P mypassword -D mydb

当我 运行 并使用 Puma、Nginx 和 Capistrano 访问时一切正常。

在此处查看更多信息:

https://github.com/rails-sqlserver/tiny_tds/issues/266