简体   繁体   中英

How to correctly install MySQL Ruby Gem

I am trying to install the Ruby GEM for MySQL. I am pretty new to Ruby.

I have RVM and Bundler installed. Should I install the gem via Bundler or RVM?

What would be the actual command to do so correctly?

Since you have RVM installed, create a gemset for your application. This'll isolate the gems for that app only and not affect the rest of the system.

rvm gemset create appname && rvm gemset use appname

Once you've done that, install gems without sudo, either manually or through bundler, ensuring the isolation.

Once you are inside the directory of your application, and after creating your gemset as Srdjan mentioned, you have two choices. Either run:

gem install mysql

or add this line to your Gemfile:

gem 'mysql'

Once you have added that line to your Gemfile, run:

bundle install

EDIT

You have to configure your database through the database.yml file for each one of your environments (development, staging and production). That should look like this:

development:
  adapter: mysql
  database: name_of_your_database
  encoding: utf8
  host: localhost
  username: root (replace with the actual username)
  password: root (replace with the actual password)

Once you have that set up, you can add migrations to add tables to your database. If this is a legacy database, you might need to generate some models from it. In order to do this you can use the legacy_database gem.

You don't have to handle the connection to the database yourself, Rails will do it for you. To access your tables and query them you can use the ActiveRecord methods.

For instance, suppose you have a table called users and you need to find a user by id, you could do this:

user = User.find(id)

Read the documentation to get running on querying using Active Record.

To get MySQL installed and running you'll need a few things:

  • The development headers installed for MySQL and Ruby
  • A working mysql_config or mysql_config5 binary
  • gem 'mysql' listed in your Gemfile

Once you've got all these in place, it should come together quite cleanly using the routine:

bundle install

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