Not sure which Database rails is using - ruby-on-rails-3

I am working on a rails app, where I have created users with scaffolding and saved some entries through the browser. It seems to be doing everything correctly.
The problem is I am not sure which database the app is using. I have mysql but no changes are happening there. Is rails using sqlite instead? If so, how can I switch to mysql or another database?

You need to configure mysql database in config/database.yml. Refer http://edgeguides.rubyonrails.org/configuring.html#configuring-a-database
Here is a sample from my local settings for reference
development: &default
adapter: mysql2
database: invoice_development
encoding: utf8
min_messages: warning
pool: 5
timeout: 5000
user_name: root
password: root
socket: /var/run/mysqld/mysqld.sock
test:
<<: *default
database: invoice_test

If you are running your application locally, it is most likely using the development database configured in config/database.yml.
To double check, you can check the value of Rails.env when running your application. E.g. (from the console):
2.2.0 :001 > Rails.env
=> "development"

Related

Discord.js Database

I have made a bot called "Hello Bot" but I want to have a database connection so I can do more things but I can not find a way to do it on will you help me?
You can use something like SQLite or Enmap depending on what you're trying to do. You could also link into MySQL by running npm install mysql in your bot's directory and adding the following code at the top of your bot's js file:
const mysql = require('mysql');
const connection = mysql.createConnection({
host : 'hostname',
port : 'portnum',
user : 'db_user',
password : 'db_user_password',
database : 'db_name',
charset : 'utf8mb4'
});
Note that you will have to install and setup MySQL on your server and create your database.
Here are a couple more links regarding setting my Node.js with Database access:
https://hackernoon.com/setting-up-node-js-with-a-database-part-1-3f2461bdd77f
https://tr-center.com/line/discord-js-bot-tutorial-part-6-xp-system-with-mysql-E5TVwyiCuYA.html
I'm a little late to the game on this one. But what I did with mine is used a atlas mongodb. I have my bot hosted on Amazon's AWS so connecting to a cloud DB was pretty easy from a nodejs project.

How to query a remote SQLAnywhere16 db using Rails?

I am trying to set up a Rails app to query a remotely hosted SQLAnywhere 16 db and to display some query information as HTML. I'm using current versions of Ruby and Rails on Mac El Capitan. The remote server is running Windows. The server is located in my building and I am on the same LAN, so if there's a simpler solution that involves accessing the box itself physically I'm open to that.
On my Windows machine I am able to connect to the server and do queries on the db using a SQL client.
I have sqlanywhere and activerecord-sqlanywhere-adapter gems installed. I also have the SQL Anywhere 16 client installed on my (Mac) machine. The remote db set up in my database.yml file as another db:
my_external_database:
adapter: sqlanywhere
encoding: utf-8
username: (username)
password: (password)
server: (name of server)
database: (db name)
host: (IP of server)
I'm calling it like so:
class MyExternalModel < ActiveRecord::Base
establish_connection(:my_external_database)
self.table_name = '<a particular table in the db>'
self.primary_key = 'primarykey'
end
When I go into the rails console and do MyExternalModel.first to test the connection, I get:
LoadError: Could not load SQLAnywhere DBCAPI library
After doing some googling, this seems to be a problem with configuring SQLAnywhere. However, I believe I configured it properly (in bash using bin64 config), like so:
. /Applications/SQLAnywhere16/samples/sample_config64.sh
Enter destination directory for copy of the samples [/Users/(user)/sa16_samples]:
Copying samples...
Done
Setting up data sources...
SQL Anywhere Data Source Utility Version 16.0.0.2041
Configuration "SQL Anywhere 16 Demo" written to file /Users/(user)/.odbc.ini
SQL Anywhere Data Source Utility Version 16.0.0.2041
Configuration "SQL Anywhere 16 CustDB" written to file /Users/(user)/.odbc.ini
Done
Setting up sample_env script...
Done
This is as far as I'm able to get. Any recommendations are greatly appreciated!
I use SQLAnywhere for a production server and it works fine using the very same gem you're using. I use a Mac for development and my database is running in Windows (or Linux).
In order to get rid of this error, try the command (on your Mac):
# adjust SQLAnywhere to your version -> 16
source /Applications/SQLAnywhere12/System/bin64/sa_config.sh
I'm using Rails 4 and SQLAnywhere 12 (but tested with 16 and it works too).
After that, try to load console (rails c) and do some tests there.

Error with syncdb Django app to Azure SQL using pyodbc

I am trying to get my Django app running on Azure. I had the app up and running with some static pages, but I can't get the database working properly. I downloaded pyodbc so Django can talk to the Azure SQL database, and put it both on my local machine and the web server. I also upgraded the Azure Website to use Django-1.6 instead of the default 1.4, but I am still using Python 2.7
I have already set up Azure filewall rules to allow access from my ip address
In settings.py, I set up the DATABASES section like so:
'ENGINE': 'sql_server.pyodbc',
'NAME': 'django-sql',
'USER': '<username>#<server>',
'PASSWORD': '<password>',
'HOST': '<server>.database.windows.net',
'PORT': '1433',
'OPTIONS': {
'driver': 'SQL Server Native Client 11.0',
'MARS_Connection': True,
},
When I try to run syncdb from my local machine I get this error:
django.db.utils.Error: ('00000', '[00000] [iODBC][Driver Manager]dlopen({SQL Server Native Client 11.0}, 6): image not found (0) (SQLDriverConnect)')
I have tried reformulating settings.py a bit, and still get the same error. I tried putting the PORT number after the HOST name as it is shown in the Azure Portal, and I have tried using the full HOST name in USER.
Looks like iODBC is not supported. unixODBC, however, is supported. I was able to spin up an Azure Ubuntu VM and followed this guide in order to run syncdb. Not ideal but it'll work for now.

Unable to access external database from Heroku App

I have rails 3.2.13 application running on heroku. I want my application to access a remote MySQL database. For that I have changed the DATABASE_URL in heroku config. Here is how I did it
heroku config:add DATABASE_URL=mysql2://user_name:password#171.121.123.120:3306/database_name
Above command changes the database_url successfully.
But when I try to run heroku run rake db:migrate it errors out. Below is the error I get
Connecting to database specified by DATABASE_URL
rake aborted!
Access denied for user 'user_name'#'ec2-23-20-128-131.compute-1.amazonaws.com' (using password: YES)
/app/vendor/bundle/ruby/2.0.0/gems/mysql2-0.3.11/lib/mysql2/client.rb:44:in `connect'
I have added wild card like %.%.%.% at my MySQL server to allow any IP to access database but even that is not working. I have able to access database from my local dev machine after adding those wild cards.
Thanks
Do you have your credentials defined in /app/vendor/bundle/ruby/2.0.0/gems/mysql2-0.3.11/lib/mysql2/client.rb ? It looks like the migrate command is trying to pull the info from that file.
EDIT: sorry, the code block you had got cutoff on my screen. Does everything look right in the client.rb file?

How to create a non-system user when installing Postgresql 8.4 with Rails 3.2.1 on Ubuntu 10.04 VM?

Today I tried to install Rails 3.2.1 with Postgresql 8.4 on my local Ubuntu 10.04 VM. I basically followed the instructions from http://www.mcbsys.com/techblog/2011/10/set-up-postgresql-for-rails-3-1/ . The only thing I had to do differently was change a line in pg_hba.conf to "local postgres myapp trust" since the default user postgres didn't have a password and I had to create a postgresql user called "login_name" that matched my Linux system login user name (let's call it "login_name" for the sake of this example), otherwise I could not use rake db:create to create the db.
My database.yml file looked like this:
development:
adapter: postgresql
encoding: unicode
database: development_db
pool: 5
user: login_name
password: some_random_password
My question is why did I have to create a user name that matched my system login name to get this to work and is there a way around this? I Googled the heck out of this and really couldn't find a satisfactory answer.
Take a look at your PostgreSQL configuration, you'll find two related files there :
pg_hba.conf
pg_ident.conf
pg_hba controls the mechanism how to handle auth request, typically with an entry like this :
local all all ident
ident means, all local/unix-socket connections where done with the system user's identity.
If you have a "non-system" user, you need to create a map in pg_ident.conf for them :
# MAP OS-Name DB-Name
myusers user user
myusers user dbuser
Alter pg_hba.conf like this:
local all all ident map=myusers
Authentication is very well documented in the config files and online in the PostgreSQL Administration Guide
The name you specify in user: is the name used to log in; you do not have to have that be the system user if you don't want. The thing is, when the database is initialized its owner is (typically) set to the username who did the db_init, which can vary. So the username of the host and the username in the database tend to be loosely coupled.
From a security standpoint, a best practices is to run init_db as the postgres user, and then explicitly GRANT permissions, or change the owner of the template1 database (and password) to be the one you want your applications to use. Databases (more accurately schemas) will use the settings of template1 when they are created.
Practically speaking, on web-accessible servers, I like having a (system) username with limited access (little or no sudo) that has the same name as the database user. Set a name and use it everywhere. This makes it easier to copy databases, configure, and so on. Otherwise you end up getting "role foo not found" errors.
Rails makes it super easy to get going in development and that's good. It should not make it super-easy to create an insecure production environment :-)