简体   繁体   中英

What database to use in production with Rails?

I'm pretty close to deploying my Rails app to a Digital Ocean VPS using this guide

But there's some confusion about the database part. On my development I've used the SQLite3 database. But the guide only shows MySQL and PostgreSQL. Is that because SQLite3 isn't meant for production services? And do I need to convert my SQLite3 database to a MySQL database in order to transfer the tables etc?

If your website is meant for simple data-storage and retrieval then you can do sqlite3, but with regard to mysql or pgsql, it lacks support of multiple user, concurrency. In addition to which if you want to insert like 10k datas at a time,then possibly you need to gooogle and comeback to SO for an answer , on how to insert that many records in least time. SQLite is used mostly for mobile games (data storage), by browsers (like firefox) to store data for single users, (mostly)

Now if you are deciding between Postrgres and Mysql, i would suggest Pgsql because not that mysql is bad, but depending on the engine you use, you may or mayn't have certain functionalities and i really kindof dislike the way mysql handles NOT NULL and DEFAULT values.

If you have an application where you are using multitenancy, for example a bank application where you have a single user table and then you want to distinguish your user data's from one another on a database level, pgsql works best, because of its schema support. For example in a college admission application, each college can have its separate schema structure.

For switching between databases you just need to modify yor database.yml file and then migrate it, as answered.

SQLite usually will work great as the database engine for low to medium traffic websites (which is to say, 99.9% of all websites).

http://www.sqlite.org/whentouse.html

However that said SQLite is not commonly available on hosting or SAAS platforms. As @raulchopi already said Postgres is a very good alternative with a lot of traction in the Ruby/Rails community. It is often preferred over MySQL since it uses a stricter interpretation of SQL standards and has a functional open-source community running the project (vs Oracle which has been known to try to kill off MySQL).

I would recommend that you setup ENV vars with your database details, and add to your database.yml .

production:
  adapter: postgresql
  encoding: utf8
  database: <%= ENV["MYAPP_DATABASE_NAME"] %>
  password: <%= ENV["MYAPP_DATABASE_PASSWORD"] %>
  host: <%= ENV["MYAPP_DATABASE_HOST"] %>
  pool: 5

This allows you to commit your database.yml (which you should since it is a core piece of configuration) even if your code is public.

You may also want to consider running Postgres in your test and development environments as it can help you to catch edge cases before you deploy.

You are right, SQLite is not meant for production. Postgres or MySQL is better for production environment. You have to edit your database.yml:

production:
  adapter: postgresql (or mysql2)
  encoding: utf8
  database: databasename
  username: yourusername
  password: yourpassword
  host: yourhost
  pool: 5

Then, in the console, migrate the database structure with the command:

rake db:migrate RAILS_ENV="production" 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM