简体   繁体   中英

Setting Up MySQL in Rails Application

I've started a new rails 3.2 project and by default it uses SQLite.

I have no data in the db yet and now I'm planning to use MySQL, so I added the gem 'mysql2' in Gemfile and ran 'bundle install'.

Now,

I'm a bit unsure of what to do. I've read that I should just change the config lines under database.yml .

But I'm wondering why I should enter the user and password values for MySQL.

Should these values be set up manually in MySQL?

If so, how is this done? And why doesn't SQLite ask for user and password?

Here is snippet of database.yml file for mysql as DB. Please ensure to add mysql2 inside Gemfile and run bundle install

# Ensure the MySQL gem is defined in your Gemfile
#   gem 'mysql2'
#
# And be sure to use new-style password hashing:
#   http://dev.mysql.com/doc/refman/5.0/en/old-client.html

development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: example_development
  pool: 5
  username: root
  password: root

http://www.tutorialspoint.com/ruby-on-rails/rails-database-setup.htm shows a quick example of the syntax you need. You're really close, you just have to create a new mysql user by granting it permissions to your database.

Btw, this is one of the reasons I prefer to develop with postgresql. Once you configure pg_hba.conf to grant permissions to your local users, you can leave off the username and password fields in your database.yml. When you couple this with deploying to heroku (where they overwrite your database.yml with their production values), it makes managing (and sharing) this file very simple.

  1. Configure MySQL with rails => Rails + MySQL
  2. Why doesn't SQLite ask for username/password => username in sqlite3

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