Ruby on Rails无法连接SQL Server 新手技术求助
Hey there, let's work through this SQL Server connection problem step by step—since you're new to Rails, we'll break down each potential issue clearly so you can get up and running.
1. First, Confirm Required Gems Are Installed
The activerecord-sqlserver-adapter gem is the official adapter for Rails, and tiny_tds handles the low-level database communication. Since you're on Ruby 2.4 (which is a bit older), you'll need to specify compatible versions in your Gemfile:
gem 'activerecord-sqlserver-adapter', '~> 5.2.0' # Matches your Rails 5.2.0 version gem 'tiny_tds', '~> 2.1.5' # This version supports Ruby 2.4
Run bundle install after updating the Gemfile. If you get compilation errors on Windows, make sure you have the Ruby DevKit set up for x64-mingw32—it's required to build native extensions for these gems.
2. Verify ODBC Driver & DSN Configuration
You're using mode: odbc in your config, so this is critical:
- Install the correct ODBC driver: For Windows, grab the 64-bit SQL Server ODBC driver (matching your Ruby architecture
x64-mingw32). Ensure it's compatible with your SQL Server version. - Test your DSN: Open the "ODBC Data Sources (64-bit)" tool on Windows, find your
sqlserverappDSN, and click "Test Connection". If this fails, fix the DSN first:- Make sure the SQL Server service is running (check Services.msc).
- Confirm your SQL Server allows SQL Server authentication (not just Windows auth).
- Double-check the database name
sqlserverappexists on your local server.
3. Fix Your database.yml Configuration
Your config looks mostly right, but let's clean up a few details:
- It looks like your
developmentsection was cut off—make sure it properly inherits the default settings. - If your password is empty, explicitly set it to
''(empty string) to avoid nil value issues.
Here's the corrected version:
default: &default adapter: sqlserver mode: odbc dsn: sqlserverapp username: prakash password: '' # Explicit empty string for no password host: localhost database: sqlserverapp development: <<: *default
Note: If your DSN already includes host/database details, you can remove those lines from database.yml to avoid conflicts.
4. Test the Connection Directly in Rails Console
The fastest way to get a specific error message is to test the connection in the Rails console:
- Run
rails cto start the console. - Execute
ActiveRecord::Base.connection—this will attempt to establish a connection.
If it fails, you'll get a detailed error (e.g., "login failed for user prakash", "could not find ODBC driver", etc.). That error message will point you exactly to what's broken.
5. Common Pitfalls to Watch For
- SQL Server Authentication Mode: Ensure your SQL Server instance is set to "Mixed Mode (Windows Authentication and SQL Server Authentication)". If you're trying to use Windows auth instead, replace
username/passwordwithtrusted_connection: true. - TCP/IP Protocol: In SQL Server Configuration Manager, make sure the TCP/IP protocol is enabled for your SQL Server instance (even for local connections, this is sometimes disabled by default).
- Ruby Version Compatibility: Ruby 2.4 is end-of-life, so newer gem versions won't support it—stick to the gem versions we listed earlier to avoid compatibility issues.
内容的提问来源于stack exchange,提问作者mallela prakash

