Rails MSSQL connetion - TinyTds::Error: closed connection - ruby-on-rails

I'm currently trying to setup a Redmine installation for our team. I can't seem to establish a connection to our MSSQL server from within rails.
$ RAILS_ENV=production bundle exec rake db:migrate
rake aborted!
TinyTds::Error: closed connection
/home/admin/.rbenv/versions/2.6.6/bin/bundle:23:in `load'
/home/admin/.rbenv/versions/2.6.6/bin/bundle:23:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
I installed sqlcmd to test the connection outside out rails, and I managed to login and create and drop a table as a test. I suspect it's my database.yml. I have to connect to an MS SQL Instance, and the server in running on a different port. Tried to tweak around with the settings a bit, but couldn't get it working.
database.yml
production:
adapter: sqlserver
database: Redmine01
dataserver: server\instance
port: ####
username: user
password: "password"
encoding: utf8mb4
While I've played around with rails a bit before, my rails knowledge is definitely limited and I've never set up a connection to an MS SQL server or used the TinyTds gem, therefore I suspect that I simply didn't configure my environment correctly. Hence my post here. I'd really appreciate if one of you rails experts could take a look at the database config. The information I could find regarding configuring rails to connect to named instance combined with a non-standard SQL port were limited.
As a sidenote, for sqlcmd I used "-S server\instance,####" to establish the connection. Which I've surely tried in the database.yml too, but without any success.

In case anyone stumbles upon this, leaving out the instance does the job.
production:
adapter: sqlserver
database: Redmine01
dataserver: server
port: ####
username: user
password: "password"

Related

Rails: fe_sendauth: no password supplied (PG::ConnectionBad) from Ruby, but ok in Rails

I'm having problems assessing a postgres database from straight ruby.
I've created a Postgres database using Rails
>rails new www --database=postgresql
using Rails 4.2.5 and Postgres is 9.4
It produces the following config/database.yml file.
default: &default
adapter: postgresql
encoding: unicode
pool: 5
development:
<<: *default
database: www_development
test:
<<: *default
database: www_test
production:
<<: *default
database: www_production
username: www
password: <%= ENV['WWW_DATABASE_PASSWORD'] %>
I can run rails server, db:drop, db:create and db:migrate fine.
I can also access the database fine with psql
>psql www_development
But when I run the following app.rb from a non Rails project directory, I get a fe_sendauth: no password supplied (PG::ConnectionBad) error message.
It's clearly not a Rails issue. I've either missed something in my ruby or Postgres need a tweek to handle some difference between Rails and pure Ruby [that I'm not aware off]. I've also included Postgres' pg_hba.conf file.
At wits end trying to figure this one out. Any help would be much appreciated.
app.rb
require 'yaml'
require 'active_record'
require 'pg'
ActiveRecord::Base.establish_connection(
adapter: 'postgresql',
host: 'localhost',
database: 'www_development',
)
/etc/postgresql/9.4/main/pg_hba.conf
# TYPE DATABASE USER ADDRESS METHOD
local all postgres peer
local all all peer
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
You don't specify neither username, no password in your ActiveRecord::Base.establish_connection(. I assume you want to use some SUPERUSER without password for www_development database - right?
as per documentation peer auth does
Obtain the client's operating system user name from the operating
system and check if it matches the requested database user name.
That is why if you can psql without password, you should be able run app.rb with same OS user and environment without password. If you can't, then app.rb tries to connect with different username or so...
Options:
put username: postgres to ActiveRecord::Base.establish_connection(
change local all all peer to local all all trust
With ENV variable as password it's very likely that the variable itself is not present. Confirm the presence with printenv. You need to relogin/reboot for the variable to be accessible after you've included it in /etc/environment file for example. If this works, it's probably better than changing pg_hba.conf.
development:
<<: *default
database: postgres
username: postgres
password: postgres
# must specify the right DB, superuser and superuser Password as per postgreSQL setup
This worked for me, the only changes I needed to make. (ok fine i re-installed pgsql with 12.1)
I had this same issue when setting up a Rails 6 application.
The issue was that when start the rails server using rails server, and try to view the application from a browser, I get the error below:
ActiveRecord::ConnectionNotEstablished
fe_sendauth: no password supplied
Here's how I solved it:
The issue was caused by me not specifying/supplying the database password to be used by the application, so when the application tries to connect to the database specified in the config/database.yml it runs into that error.
I solved it by specifying the database password for the application in the config/database.yml:
# The password associated with the postgres role (username).
password: my-password
Also, ensure that you've created the database for the application if you've not created it already using:
rails db:create
Afterwhich I restarted the rails server:
rails server
This time when I tried viewing the application from the browser, it worked fine.
That's all.
I hope this helps
I was trying to use peer authentication which shouldn't ask for the password.
For me the issue was that I was specifying localhost in the database.yml so that was forcing the database driver to try to make a different type of connection (e.g., tcp, named pipe, etc)
Removing the "host: 'localhost'" line from my config solved the problem for me per this other answer: ActiveRecord connection to a localhost postgresql database in trust mode
Note that in my case I'm using the 'peer' method and not the 'trust' method but the solution is the same

database.yml file configuration and postgres - rake db:drop db:create db:migrate

I recently switched to postgres for a rails app in development since I deployed on Heroku and want to match production. Postgres is installed and working correctly for my application when I set the database name equal to 'postgres', which is the default name from what I understand.
However, I would like to change the database name for development, test and production. If I simply change the names in database.yml, and try to run a rake task, I get the following error in my terminal:
NOTICE: database "something_development" does not exist, skipping
Couldn't drop something_test : #<PG::Error: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?>
rake aborted!
FATAL: database "something_development" does not exist
Here is what my database.yml file currently looks:
development:
adapter: postgresql
host: localhost
database: something_development
username: name
test:
adapter: postgresql
database: something_test
pool: 5
timeout: 5000
production:
adapter: postgresql
host: localhost
username: name
database: something_production
Are there some other steps I am missing? Apologies if I am missing something obvious here! Thanks!
Changing the name in the database.yml file only changes the place that it looks for the database, it does not actually rename the database itself. If you change the specified location, you will need to rename the database to match, using a GUI tool (like navicat or pgadmin) or the command line.
Also, it is probably an obfuscation omission, but your posted output refers to both shredset_development and something_development. If this continues to fail, perhaps verify that all of your names are the same everywhere...

Errors when trying to prepare testing with Ruby on Rails that uses a SQL Server 2008 R2 database

What do I need to do to be able to run tests with a SQL Server 2008 database server using the gem activerecord-sqlserver-adapter?
I am able to connect to the development database just fine using a 32bit ODBC connection through dev settings in database.yml:
#SQL Server
development:
adapter: sqlserver
mode: odbc
dsn: <odbc_name>
username: <db_user>
password: <db_password>
host: <sql host>
I have started to setup some testing but run into an error when running:
rake db:test:prepare
Here is the error:
rake aborted!
ODBC::Error: 37000 (3708) [Microsoft][SQL Native Client][SQL Server]Cannot drop
the database 'master' because it is a system database.: DROP DATABASE [master]
Tasks: TOP => db:test:load => db:test:purge
(See full trace by running task with --trace)
This error doesn't make sense to me. Why is it trying to drop the master db?
My test setup in database.yml is exactly the same as my dev above except it points to a different odbc which points to a different database.
EDIT
I have noticed that when I run rake db:test:prepare the first step must be to delete the test database. So it knows what database it is supposed to use at first.
Why does it then try to delete the master?
Is this because when sql doesn't find the default database it gives the user master by default and rake db:test:prepare cannot create the users default db so it loops back to the beginning and tries to drop again?
I got this to work by specifying the name of the database in the test configuration:
# config/database.yml
...
test:
adapter: sqlserver
mode: odbc
dsn: <odbc_name>
database: <database_name>
username: <db_user>
password: <db_password>
...

Cannot seed database; not working because of refused connection?

When I try to seed my application I get the error:
No connection could be made because the - target machine actively refused it. - connect(2)
I believe the reason why is because I was having issues with mysql2 so I uninstalled it along with the MySQL 5.5 Servers and then switched to sqlite3. I think the server for mysql2 is running in the background so this could be the issue. How would I fix this? How would I turn off the Mysql2 local host server or whichever server it is that's causing this issue?
I am running on Windows 7 64-bit.
Rails 3.0.9
SQLite3 1.3.4
Thanks.
Note: I can migrate and drop fine.
Edit:
config/database.yml
# SQLite version 3.x
# gem install sqlite3
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
UPDATE:
I tried restarting the PC and also remade my application from scratch, still get the error. Disabled Windows Firewall/Comodo Firewall and tried again, still failure. I did a System Restore but this did not work either.
Here's the full rake db:seed: https://gist.github.com/1375566
SOLVED:
I have another application I ran rake db:seed in and it worked correctly, so as the accepted answer by clyfe pointed out, Sunspot was the issue. I put together again my application and stopped at adding sunspot and suddenly I got the error:
rake db:seed
(in C:/testagain)
Deleting database now...
rake aborted!
undefined method `searchable' for #<Class:0x52cdca0>
Which means the rake was reading my code inside of my UserPrice model that dealt with Sunspot:
class UserPrice < ActiveRecord::Base
# Sunspot and Websolr configuration.
#searchable do
# text :product_name do
# product.name
# end
# end
end
I commented this out and was able to seed correctly. Then I went on to try sunspot using these commands in the following order:
rails g sunspot_rails:install
rake sunspot:solr:start (also un-comment model)
rake sunspot:reindex
rails server
rake db:seed
Everything works as it should.
You are using Sunspot for indexing and search as I see from your gist https://gist.github.com/1375566
Make sure that the Solr server is started before you seed.
What happens is that:
when the model saves
it tries to send data to the Solr server for indexing
but it cannot connect
Possible issues:
the Solr server is not started
the Sunspot Solr connection is not configured corectly in /config/sunspot.yml
the port it's blocked by a firewall
If you haven't started a Solr server instance already, you can start the Sunspot-bundled Solr server with the following rake command:
rake sunspot:solr:start
I dont think it is because of mysql, I think it is a firewall issue on the port that you are trying to access. You can test by stopping the service or using the mysql workbench to stop the server on that machine. I doubt that will resolve the issue, since the firewall may be interfering.
Comment out the skip-networking in my.cnf in your MySQL configuration.

ROR change application database from SQLite to PostgreSQL

I have a web application which uses SQLite. I deploy it on heroku which uses PostgreSLQ. This causes problems sometimes and I was advised to develop my app using PostgreSQL instead of SQLite.
I found out that I should modify database.yml like that (same for test and production):
development:
adapter: postgresql
database: my_database
username: my_username
password: my_passwod
host: /var/run/postgresql or localhost
Well the only database I've ever used is SQLite, so I just tried to take my chances, but failed. I filled this file with some random data.
rake db:migrate resulted in:
When I used host: localhost
> could not connect to server: Connection refused Is the server running
> on host "localhost" and accepting TCP/IP connections on port 5432?
When host: /var/run/postgresql
> could not connect to server: No such file or directory
> Is the server running locally and accepting connections on Unix domain socket
> "/var/run/postgresql/.s.PGSQL.5432"?
I suppose I should start PostgreSQL server first, but have no idea how to do this. Please give me a step by step answer how to move from a SQLite application to a working PostgreSQL application.
I would like to advise to you that you should download Postgresql including the PGADMIN itself which is easier to use than the psql terminal.
And I think when you download/install Postgresql from their official website... the package was complete already.
Upon installing, the postgresql will ask you a certain password that you will be using in accessing your postgresql server.
After the installation, open the PGADMIN and connect to the server. Enter your password (which you had declared during installation).
If you can't connect to the server, then edit the port. To do this, right click the server then go to properties... edit the port into something which is free. Example: 5433 and so on. It's up to you.
If everything's finally working... setup the correct config for your database.yml
This is important:
development:
adapter: postgresql
database: name_of_database_here
host: localhost
username: postgres
password: your_db_server_password_here
pool: 5
timeout: 5000
port: 5433
Okay from that config info above, specify the important parts. By default, your db server username is postgres and obviously your host is localhost because you are setting up under the development.
If your port is 5432 by default then just remove the port part.
Let's go to your gemfile.
In order for you to deploy your app in heroku. Use gem 'pg' instead of sqlite3.
If you have an existing sqlite3 database then put the gem inside the development group. In that case, Heroku will successfully bundle during git push heroku master process.
group :development do
gem 'sqlite3'
end
Your gem 'pg' can either go outside the groups or put it in your production group.
Important:
Before any deployment procedure, make sure that you can run the app locally (localhost). Then if everything's working... that's the time that you should organize the necessary stuffs appropriately.
If you wish to switch to Postgresql instead of sqlite3 after pushing the app to Heroku... you can do so by pgbackups add-on and pg_restore the dump file into your local postgresql db server.
That's it. Hope it helps.
Take a look on this guide https://www.digitalocean.com/community/tutorials/how-to-set-up-ruby-on-rails-with-postgres so you can recreate the process manually by changing your database.yml and Gemfile.
If you need to migrate your database information run
pgloader ./production.sqlite3 postgres://username:password#localhost/database
Check https://migara.li/2017/07/24/migrating-from-sqlite-to-postgres/
Other alternatives like taps aren't working right now
http://railscasts.com/episodes/342-migrating-to-postgresql

Resources