Where are databases stored in the rails server? - mysql

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.

Related

Is it possible to access/edit a remote SQLite3 database with Sequelize from a website

So I have an SQLite3 database with Sequelize on a VPS and a Website I hosted on Hostinger.
Is it possible to access and edit the data in the database from the website?
Any help is appreciated.
[my code is in nodejs]
Yes and No.
Easy and simple answer - now;
Longer - yes, but you would need to map the directories, so server running your sequelize will think it accesses normal directory.
You can download the database, edit it and then send it back. sqlite3 is file database, you dont really connect to it (usually)
SQLite is a database format based on a single file. It's simple but not massively performant. It has no networking features.
The SQLite client needs to have access to the filesystem that the database lives on.
If they are on different computers, then you could do it by using some kind of network filesystem (e.g. via fuse) but this would be really slow.
A better approach would be to run a webservice on the computer the file is stored on, and access the database through there.

What is a clean and simple method of backing up Ruby on Rails app and MySQL database?

I started learning Ruby and Rails over the past week or so. I have completely destroyed my app and had to redo everything a couple of times. haha
It is a giant pain rebuilding everything using only a backup folder of some copied files. I'm sure I'll break some more stuff in the future.
Are there any good gems or tools that will help me backup both the App and the MySQL database?
I have used this option for MySQL but it doesn't seem to work well when working with Rails:
mysqldump database_name > database_name.sql
First of all: you don't need to do backup of your application. You should use version control system (git, for example) and automated deployment (capistrano, chef or whatever). Using this two principles you can always redeploy your application on the broken VPS or transfer it to another server.
Then, about DB backup - just do it using your DB abilities (mysqldump in your case) :) You can set up, for example, CRON task, which will backup your database once a day. But don't forget that it will be much better if you will store your dump on the other server - in case of your server being completely broken.

Database during release and developing

I am new to Database programming and there is some general questions that I would like to ask. I created the schema in my localhost using mySQL and linked to eclipse. There are some problems that I do not know how to approach.
One of my friends would like to help to develop at his personal machine, but he could not link to my database server. So one way is to copy the schema to his mySQL and change the connection string, are there any better ways?
If I would to release the project and run it on different machines, will it affect the databases operation since the schema resides in my local server.
Is there ways to just like attach the database inside the project since it is a local database and I am not accessing it from any other programs.
Sorry if my questions sound very stupid. I am really new.
Your approach of running local test MySQL instance for every developer sounds fine.
However, if your application never needs any data shared (essentially database is always local as you stated), then you should ask yourself if MySQL really an appropriate solution for your application. I think SQLite is a better choice in that situation - it is likely to work faster than MySQL for local access, and it has essentially zero setup - no database daemon to run or anything like that which greatly simplifies application install.
Granted, SQLite has its limits, but I often use SQLite for my projects, and only if they grow large, I might migrate them to MySQL or PostgreSQL if task requires it.
Typical signs that SQLite may not cut it:
Many clients (10+) need to access database for writing
Total database size is very large - more than 10GB total

Can I run my own instance of MySQL?

I'm trying to migrate a web site to friend's server. The site uses MySQL, but he didn't previously have it set up. He's installed the package now and suggested that I could run my own instance of it. I'm at a loss for how to do so. Is it even possible? If so, how?
Some relevant information:
The OS in use is CentOS 5.9
Using MySQL 5.0.95
I only need the DB to be read locally via PHP when serving my site.
I have no root privileges on this system (although a full shell), but am close friends with the owner/administrator if that's necessary.
To clarify:
It's the daemon that I want to run my own instance of. So I guess what I want to know is if it's possible to have multiple users on the system running their own instances of mysqld containing different databases.
All I need this for is serving a web page. If I have to break down and switch to a pseudo-database using CSV files I will, but I'd much rather stick to MySQL if I can.
If MYSQL is setup on that machine yes you can run your own instance of it.
You can have it set up and your site given its on Database within that MYSQL instance and that specific MYSQL account can be used to access the tables etc. involved with your website.
Now if he is only putting MYSQL on the machine then having you set everything up you will just need an account for MYSQL and from ther eyou can get in and just create all the needed items that will allow your site to fully function.
EDIT
In response to your comment. You can you just can't clog the same port or have configurations that conflict each other. As long as it won't be to much of a performance hit and you configure a different user to start each instance you should be able to do Something like this.

What's the set up database on Heroku?

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.