I finally got a MySQL database hooked up to a Rails 3.2 app after spending half the day figuring it out.
What I don't understand though is how Rails knows where to find the database?
With the default Sqlite3 it's easy enough in that it's embedded in the app so it's just a matter of specifying the path in the database.yml (db/devlopment.sqlite3).
But, when it's MySQL, I thought I'd have to specify the path of the database I'd created from the MySQL command line. Turns out I don't have to, all I have to do is give the name of the database and user login credentials.
So when I run a migration in Rails, how does it know where the database is?
That's called a reasonable default. Take a look in your database.yml file, you might find a line like this:
socket = socket: /tmp/mysql.sock
AFAIK, if that's not explicitly there, it is used as the default in the absence of other parameters (such as host and port).
Actually, Rails doesn't know anything about it. The modules which handle the connection to the database are the adapters used by Active Record. Rails only provides a kind of "configuration convention" in the case of the database.yml file to make it somehow "common" to all database possibilities, but internally they use this information to setup each database connection in its own unique protocol. I don't know about SQlite, but the mysql and mysql2 handlers are not mantained by the rails team
https://github.com/brianmario/mysql2
Here you can check out the interface Rails/ActiveRecord uses to setup the MySql Connection.
So, to sum it up, Rails doesn't know. It just needs the information you setup in the database.yml to infer which adapter they should use (go to your database.yml and you'll see something like "adapter: mysql2" or "adapter: "sqlite") and the necessary information to establish the connection. As #pjam described, the protocol used for accessing the mysql service externally is expressed by the pattern mysql://localhost:3306/DB_NAME?user=YOUR_USER_NAME&password=YOUR_PASSWORD , and this is actually how the adapter sets the connection up. Just it is not done by Rails.
I believe it finds it with the url of your local database, which is something like
mysql://localhost:3306/DB_NAME?user=YOUR_USER_NAME&password=YOUR_PASSWORD
So it only needs DB_NAME, YOUR_USER_NAME & YOUR_PASSWORD, which are provided in your database.yml
Related
Basically, if I have two rails applications, with different database.yml settings, both connecting to the same database, will they conflict with each other?
Or is database.yml specific to the local application?
Essentially, does rails create just a raw database connection like mysql workbench would? Or is there more to it?
The .yml file is simplyba connection config for your app. Its totally fine to connect to the same app with different .yml
I'm having a lot of run with Ruby, creating some basic web apps. When looking at the logs of the rails server in terminal, I see mysql queries.
Refinery::User Load (0.2ms) SELECT "refinery_users".* FROM "refinery_users" WHERE "refinery_users"."id" = 1 LIMIT 1
Theses are relating to databases that I've created, but where do these databases exist? In the rails server? Where is the rails server stored in OSX? Can I browse what's inside, specifically, the databases?
Thanks, I know this doesn't have much practical use, but I want to understand the concepts behind what's going on, rather than just having superficial knowledge.
By default, Rails uses SQLite3. The database files are stored in the /db directory in the root of your app. There should be a file called development.sqlite3.
To access that database, open a terminal session, go to the root directory of your app and enter sqlite3 db/development.sqlite3. More info on the sqlite shell here: http://www.sqlite.org/sqlite.html
Rather than messing around in the SQLite shell, I think you'd be better off 1) looking at /db/schema.rb to see the structure of your database and 2) using rails console to look at the data.
If you want to know, for any given Rails app, what database it's using, look at /config/database.yml. That gives you the adapter, name of the database, location, etc.
Also, SQLite is generally just for kicking off development. I wouldn't recommend using it when your schema starts getting more complex. Personally, I never use it. I immediately set up a mySQL database for any new Rails projects.
I have a domain name and my rails application.
Is there any way to connect with database without giving the IP address of that host name?
I need what changes are needed in my database.yml file.
The error you're seeing is almost certainly because you don't have a root#localhost user in your mysql database. It has nothing to do with the database configuration file.
You need to add the root#localhost user to your mysql database.
It goes without saying that you should not be using your root user in production and it is probably a good idea not to use it in dev either as it's level of privileges may be hiding some problems.
What errors are you seeing?
If you're asking how to connect to a database without using an IP address, that depends on whether or not your database running on the same machine that's hosting your rails app?
If you're database is on the same machine as your rails app, you can just use localhost as your database host name and that should work (assuming your database is running with the default configurations).
At first I searched in Stack Overflow about the difference between MySQL and SQLite. So there is some answers but still I want to know something. When accessing MySQL I need to give host, username, password and database parameters. But when accessing SQLite I just give database name. Can anyone explain me this why, and for security reasons also, should not be there password for SQLite database, can not be it just downloaded for example from server, so I am totally stuck. So need your help.
The difference is that MySQL is a database server, while SQLite is a database engine that works against single database files.
You are correct that the SQLite database file could just be downloaded if it's placed right in the web application where anyone can reach it.
Most server providers offer a folder where you can place files like this, so that the web application itself can reach it, but it's not directly downloadable.
You can also optionally add protection to the SQLite database file. See this question:
Password Protect a SQLite DB. Is it possible?
SQLite is mainly used for mobile and tablet apps.So they are just used for a single system.
But in MySql its a complete database and can be used from multiple system. So for increasing security ,we have to give all these. So that no one can misuse the data...
SQLite is just a file. If you have read access to the file that is the implicit security model (as well as the possibility to use database encryption).
MySQL is a network service. It can listen on an internet facing socket, meaning anyone in the world can access it if they have the right authentication credentials.
SQLite is file based, MySQL is a service which runs on the server. With SQLite you don't need to enter the database name, but actually the database file.
If you put the database file at an inaccessible location, you should be totally fine and the user will not be able to download the file.
I have deployed my Rails 3.1 app with the MySQL database to Heroku and there everything works fine. I mean, into database are saved the chars right (seems to be used UTF charset on a databases on Heroku).
But when I will run the command heroku db:pull (this command will download a whole database from Heroku into the database on localhost), so the downloaded data stored in databased have bad coding - a chars are displayed bad (it looks like my local MySQL database have a different set up of charset than the MySQL on Heorku).
Could anyone give me a tip, how I can find the set up of charset used on Heroku database and how to use it on my local MySQL database?
Many thanks!
All is not lost - you really don't have to use PostgreSQL if you don't want to.
If your database is small enough (which it will have to be since the Heroku PostGres DB is also 5Mb) and you would prefer to remain on mySQL then you could use the ClearDB mySQL addon - http://addons.heroku.com/cleardb - their entry level DB is free and is the same size as the Heroku Shared PostGres DB that you get by default but be careful that the number of connections is limited so don't be going crazy with your web dyno counts.
Once you add the addon if you look at the output of heroku config then you can use the DATABASE_URL to create a connection in your favourite mySQL administration tool locally to restore/backup etc data to ClearDB. You may even find heroku db:push would work but personally I've not tried that so would be guessing.
The problem is that Heroku does not use a MySQL database in production, but a PostgreSQL database.
Therefore you will run into all sorts of issues pulling and pushing data from a different database engine. Taps is an activerecord based process that will reduce this problem but not all the time.
Ideally you want to use PostgresSQL on your development machine (install via Homebrew for simplicity on OSX) and you'll not see any more of these problems.
Alternatively, use one of the MySQL addons as described in the comments in the question.