Connecting to SQL Server 2005 from a ROR application in Ubuntu - ruby-on-rails

I tried to connect to SQL Server from a ROR application in Ubuntu. For that I did the following things.
I installed the following libraries in my Ubuntu 10.04
unixodbc
unixodbc-dev
freetds-dev
libdbd-odbc-ruby
tdsodbc
After installing these, I created the dsn and made the required changes in odbc.ini and freetds.conf files.
Then I tested the connection using "tsql" like below
tsql -S <dsn name> -U <username> -P <pwd>
For this command, I am getting the following error
Adaptive Server is unavailable
I also checked the freetds log; it is saying "connection timedout".
I am sure that we are fine with the configurations (remote) in our SQL Server side.
When I run tsql -C it is saying TDS Version is 5. I am not sure how it is saying TDS 5, because I installed the latest FreeTDS version. And I don't know how to update my TDS version.
FYI, I heard that TDS version 5 can't be used for making connections with SQL Server. Is that right?
Please guide me
Thanks,
Neo

freetds.conf allows you to specify which version of the TDS protocol you want to use. Just add a tds version = 8.0 under the [global] section or the section you added for your server. If not specified, it defaults to version 5.0, which seems to be your case. For more info, look at http://www.freetds.org/userguide/freetdsconf.htm

I realize this is not an answer to your specific question, but it is some advice from the last time I tried to go from Linux to SQL Server. It was back in 2009, so it's possible something has changed since then.
Anyway, we had so much trouble with it we gave up and decided to use JRuby.
It resulted in very little effort to get the connection up and running using activerecord-jdbc-adapter and we've run into no problems with compatibility. (I take that back, there was one problem I had to fix where we were selecting limited rows in mssql from table with no primary key, identity column, or column named "id")
I have been able to get it working in the past doing it like you're trying to, but it was always a lot more effort than I had to go through with JRuby.

Related

Do I need FreeTDS to use TinyTDS under Windows?

I'm trying to connect to SQL Express from a Rails app, and I've installed the TinyTDS gem. I keep getting an error:
--------------------
Unable to connect: Adaptive Server is unavailable or does not exist
--------------------
Everything I find on Google relates to FreeTDS. Do I need it in order to use TinyTDS on Windows? The TinyTDS github page says it requires it but it makes no mention of Windows.
Do I need drivers or something else? I already isntalled the 'activerecord-sqlserver-adapter' gem.
If I do need FreeTDS, where do I get it?
There is a binary version of Tiny_TDS that is installed on Windows when you install this gem. This includes FreeTDS, so there's no need to install it separately. Only time you have to worry about FreeTDS on Windows is if you want to work with SQL Database on Windows Azure, which requires you to manually build FreeTDS.
There's a couple things that could go wrong that might cause a connection failure:
SQL Express doesn't install a default instance, but instead installs a 'sqlexpress' instance. Maybe try connecting to 'servername\sqlexpress'.
SQL Express may not allow network connections. If you're trying this over the network you may have to enable networking.
TCP/IP and named pipes may not be enabled. I think Tiny_TDS uses TCP/IP.
SQL Express might be using a dynamic port rather than the expected 1433, so maybe check ports.
Here's an article that might help:
http://blogs.msdn.com/b/sqlexpress/archive/2005/05/05/415084.aspx.

RoR app deployed on Heroku and working with SQL Server database

Is it feasible to have a Ruby on Rails app, which is:
a) deployed on Heroku, and
b) working with a remote SQL Server database?
I take it that I'll need unixODBC installed on Heroku, but I cannot find a way to do so. Is this possible?
Or, is there any other way (without ODBC?) to accomplish this?
Thank you very much for any guidance or tip.
Updated:
Some info on the subject:
1) Heroku pre-installs both unixODBC and FreeTDS by default, so you already have them.
2) Also, it is possible to run shell commands via Heroku Console in backticks, e.g.:
heroku console
`odbcinst`
(runs "odbcinst" command in Heroku shell and shows the result)
3) You do not have access to filesystem outside of your slice where the packages are installed. If you only need a driver path, Heroku support can provide it (/usr/lib/odbc/libtdsodbc.so in my case).
4) You cannot run sudo commands in Heroku shell.
At the moment, to connect to MS SQL Server you at least need to append ‘freetds.conf’ file. Even when using tinyTDS (there is an open ticket#2 in tinyTDS gitgub issue page). DSN-less connection instructions from "wiki.rubyonrails.org SLASH database-support SLASH ms-sql" didn’t work for me, I guess this connection requires some extra-configuration either.
‘freetds.conf’ cannot be modified without sudo. Therefore, I conclude that currently there is no way to make MS SQL and Heroku work together.
I’ve managed to set up this connection with EngineYard and activerecord-sqlserver-adapter.
I followed these instructions:
https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/wiki/Platform-Installation---Ubuntu
(there are only some filepath differences, e.g. ‘odbc.ini’ is located in ‘/etc/unicodbc’, not in ‘/etc’ - this is easy to work out).
I installed 'unixODBC' and 'freetds' packages using EY Unix Packages feature, and made all configurations manually through SSH. Sudo is available in EY (no password required). There is also Chef Recepes feature to automate those configurations (seems to be pretty easy, I'm going to try it tomorrow).
Hope this is helpful.
It is possible.
Because Heroku copies/symlinks its own config/database.yml over whatever you supply in your repository, you may need to take additional steps (e.g. in config/environments/production.rb or in config/initializers/remote_mssql_from_heroku.rb) to set up your application appropriately.
You will face the challenge, however, that traffic from Heroku to your MSSQL database will traverse the public internet. By default, this traffic will not be encrypted. Potentially everyone in the world will be able to monitor your traffic between your Heroku application and your database, and even alter the traffic in-flight, whether for benign or malicious purpose, without you being able to detect it. MS SQL offers the capability to connect over SSL. This capability requires explicit configuration in the MSSQL server, so you must be able to access and modify that configuration. Additionally, this configuration requires that your client library be up-to-date and capable of talking with MSSQL over SSL. Note that MSSQL server will enforce that your server certificate list a Common Name or Subject Alternative Name exactly matching or wildcard-matching the server's FQDN (at least, the FQDN that the server knows about), and that the client use an FQDN for the server exactly matching or wildcard-matching one of the names on the certificate.
I've successfully used the following article which uses Heroku's newer buildpack feature to use TinyTDS and connect remotely to SQL Server 2008 R2. I'm still investigating how I could encrypt traffic. Hope this helps others!
http://blog.firmhouse.com/connecting-to-sql-server-from-heroku-with-freetds-here-is-how-on-cedar#
We're having a similar problem where we're needing to import old data from a SQL Server database into our new app. The data isn't a straight table import, but needs to undergo some processing and conversions. We've built an import layer for this which lives in a private gem, so as to not pollute the new app with the old data conversion issues. This approach is also designed to permit incremental updates, as we get closer to launch we'll keep syncing records up to the moment of switch-over.
Heroku told us that it's not trivial to connect to SQLServer, in particular as they don't support FreeTDS. Their support staff recommended to run an instance with the import gem from a laptop in our office and configure it to connect to their database (which requires a dedicated DB, not the free shared one). This sounded like the most palatable approach to us.
Secondly, regarding security that was mentioned by #Justice, we discussed configuring SSL for SQLServer with the hosting company and they pointed out the complexities of this. They recommended VPN as an easier solution. As we don't have office-side VPN hardware, the simplest and free solution proved to be an SSH tunnel.
We've set up an SSH tunnel from the laptop to the SQLServer Windows box. That was straightforward. We had CopSSH installed on Windows (which comes with a Linux shell, by the way) and we were able to simply set up a tunnel, having the laptop talk to localhost for its SQLServer connection, i.e.:
ssh -L 1433:localhost:1433 user#windows_server_name
I did not know Heroku has FreeTDS on it? I was told they did not. TinyTDS if used with FreeTDS 0.91 can have a zero freetds.conf dependency and be driving by runtime connection args. We are looking into building an Ubuntu 10.4 native gem that statically links 0.91 with OpenSSL so you can just drop it into Heroku and us it to connect to Azure and/or you own outside DB.

rails ODBC data source problem?

Here's an odd situation, if I open a command prompt and start the mongrel server then everything works swimmingly. ActiveRecord talks nicely to the MSSQL server using ODBC if I have a User-DSN defined. But if remove the User-DSN and set just an identical System-DSN then odbc bombs:
[Microsoft][ODBC Driver Manager] Data
source name not found and no default
driver specified
I am running Rails on Windows 2003 Server 64bit, using DBI 0.4.1, DBD-ODBC 0.2.4, Rails 2.3.4. I've tried using ODBC manager for 32bit and double-checked that the connection is working from the ODBC Administration panel. Does anyone have any ideas?
Frustrating! As it turns out, I had to use regedit to access HKLM/Software/ODBC/ODBC.INI children key (DSN) and changed the SQLServer driver there to be c:\windows\system32\SQLSRV32.dll. Even though the ODBC Administration Panel had this specified and its tests ran fine, it wasn't working until I tweaked the registry. So odd..
Hope it helps someone else in the future.
For me the problem was that I was entering the ODBC connections into the 64 bit version of the ODBC Data Source Administrator. When I went to
C:\Windows\SysWOW64\odbcad32.exe
, and re-entered the drivers here (the others were not there!), and all worked fine after that.
blog entry where I found this

Ruby on Rails + Sql Server 2005

This has been addressed some time ago, but I've not had any luck following the steps outlined or advice suggested. Here's my situation:
I've installed SQL Server '05 and have another (non-Ruby) application utilizing it successfully.
I've got Rails up and running successfully (but only for MySQL.)
I've installed the Rails sqlserver adapter gem (as well as its dependencies.)
I've downloaded ADO.rb (now only available on older builds of Ruby-DBI) and placed it into c:\ruby\lib\ruby\site_ruby\1.8\DBD\ADO\ADO.rb.
I've configured database.yml to use the sqlserver adapter and pointed it towards my database with valid login information.
When I attempt to run a migration, I get the ugly 'Unable to load driver ADO (uninitialized constant DBI::DBD::ADO)' error.
Thanks immensely for any assistance.
So just make sure if you're working on Windows or Mac to set up an ODBC connection. You must indicate this explicitly in your database configuration for Rails.

OCIError (ruby on rails)

I am using rails freeze 1.2.3 to run a rails app. Because the app is on a remote machine, I used ssh tunnel (ssh -l -L) to show the app on my screen. When I ran it, it correctly prompted the login page, after I put in the info, I got this error:
OCIError in ServiceController
Error while trying to retrieve text for error ORA-12154
I have tried the same app on a different machine w/o using freeze (because that machine has rails version 1.2.3 while current one has 2.0.2). Is that where the error comes from?
Thanks.
That's an Oracle error. It sounds like your database setup is incorrect. Put the error number ORA-12154 in Google and you'll find some useful stuff.
ORA-12154 is a classic. As Sarah points out its nothing to do with your Rails or Ruby per se. ORA-12154 is generated when the Oracle Client can't connect to the oracle server. So most likely your setup is wrong in:
Your database.yml, if you can connect using tnsping or sqlplus (Oracle client utils) you probably just need to look at this.
Your Oracle networking setup, particularly your tnsnames.ora file (found in $ORACLE_HOME/network/admin if I remember correctly). This may need some extra help from a friendly DBA as the tnsnames.ora syntax has quirks. Check this link link text for more info.
Hope that helps.

Resources