简体   繁体   English

如何正确安装 MySQL Ruby 宝石

[英]How to correctly install MySQL Ruby Gem

I am trying to install the Ruby GEM for MySQL.我正在尝试为 MySQL 安装 Ruby GEM。 I am pretty new to Ruby.我对 Ruby 很陌生。

I have RVM and Bundler installed.我安装了 RVM 和 Bundler。 Should I install the gem via Bundler or RVM?我应该通过 Bundler 还是 RVM 安装 gem?

What would be the actual command to do so correctly?正确执行此操作的实际命令是什么?

Since you have RVM installed, create a gemset for your application.由于您已经安装了 RVM,请为您的应用程序创建一个 gemset。 This'll isolate the gems for that app only and not affect the rest of the system.这将仅隔离该应用程序的 gem,而不影响系统的 rest。

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.完成此操作后,手动或通过捆绑程序安装不带 sudo 的 gem,以确保隔离。

Once you are inside the directory of your application, and after creating your gemset as Srdjan mentioned, you have two choices.进入应用程序目录后,按照 Srdjan 所述创建 gemset 后,您有两个选择。 Either run:要么运行:

gem install mysql

or add this line to your Gemfile:或将此行添加到您的 Gemfile:

gem 'mysql'

Once you have added that line to your Gemfile, run:将该行添加到 Gemfile 后,运行:

bundle install

EDIT编辑

You have to configure your database through the database.yml file for each one of your environments (development, staging and production).您必须通过database.yml文件为您的每个环境(开发、登台和生产)配置数据库。 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.为此,您可以使用legacy_database gem。

You don't have to handle the connection to the database yourself, Rails will do it for you.您不必自己处理与数据库的连接,Rails 会为您完成。 To access your tables and query them you can use the ActiveRecord methods.要访问您的表并查询它们,您可以使用ActiveRecord方法。

For instance, suppose you have a table called users and you need to find a user by id, you could do this:例如,假设您有一个名为users的表,并且您需要通过 id 查找用户,您可以这样做:

user = User.find(id)

Read the documentation to get running on querying using Active Record.阅读文档以使用 Active Record 进行查询。

To get MySQL installed and running you'll need a few things:要安装并运行 MySQL,您需要做一些事情:

  • The development headers installed for MySQL and Ruby为 MySQL 和 Ruby 安装的开发接头
  • A working mysql_config or mysql_config5 binary一个工作的mysql_configmysql_config5二进制文件
  • gem 'mysql' listed in your Gemfile Gemfile中列出的gem 'mysql'

Once you've got all these in place, it should come together quite cleanly using the routine:一旦你完成了所有这些,它应该使用例程非常干净地组合在一起:

bundle install

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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