如何在 Ruby on Rails 中连接到 MySQL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15613646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:40:48  来源:igfitidea点击:

How can I connect to MySQL in Ruby on Rails?

ruby-on-railsrubyruby-on-rails-3ruby-on-rails-3.1

提问by jesper

I am really new in Ruby on Rails. I have read this tutorialand it sounds really easy.

我真的是 Ruby on Rails 的新手。我已经阅读了本教程,听起来很简单。

But how can I connect to my database (MySQL) or what does Rails use? In php I'd use...

但是如何连接到我的数据库 (MySQL) 或 Rails 使用什么?在 php 中我会使用...

mysql_connect("...","...","...");
mysql_select_db("...");

I have searched google and cannot find any useful tips.

我在谷歌上搜索过,找不到任何有用的提示。

回答by wintermeyer

Have a look into the configuration file config/database.yml

查看配置文件 config/database.yml

You need to setup your configuration there. Here is an example for the production environment:

您需要在那里设置您的配置。以下是生产环境的示例:

production: 
   adapter: mysql2
   encoding: utf8 
   database: example 
   pool: 10 
   username: example 
   password: secure 
   socket: /var/run/mysqld/mysqld.sock 
   reconnect: true

In addition to that you have to add gem 'mysql2'in your Gemfile and run bundle install.

除此之外,您还必须添加gem 'mysql2'Gemfile 并运行bundle install.

回答by Stobbej

You don't have to do those things manually, check this: http://guides.rubyonrails.org/configuring.html#configuring-a-database

您不必手动执行这些操作,请检查:http: //guides.rubyonrails.org/configuring.html#configuring-a-database

回答by harkirat1892

Contents of my config/database.ymlfile:

我的config/database.yml文件内容:

# Ensure the MySQL gem is defined in your Gemfile
#   gem 'mysql2'
#
# Install MySql gem if not already there.
# Below command installs some pre-requisites for the installation:
#   sudo apt-get install libmysqlclient-dev mysql-client
# After above, this to finish gem installation:
#   gem install 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: YOUR_DATABASE_HERE
  pool: 5
  username: root
  password: root

As the comments above the configurations say, you might need to install the mysql2 gem first via the terminal. After installation is finished, do a bundle installand rake db:migrateand can then access the database via phpmyadmin too.

正如配置上面的评论所说,您可能需要首先通过终端安装 mysql2 gem。安装完成后,执行一个bundle installrake db:migrate然后也可以通过phpmyadmin访问数据库。

I had just stumbled upon this question an hour ago, more than 2 years later since the question was asked. Although I understand this is very late and for sure OP must have solved this, for the sake of other beginner users like me who might be coming here for a solution, I thought of writing my solution here. Hope it helps.

一个小时前我刚刚偶然发现了这个问题,距提出这个问题已经过去了 2 年多。虽然我知道这已经很晚了,而且 OP 肯定已经解决了这个问题,但为了像我这样可能来这里寻求解决方案的其他初学者用户,我想在这里写下我的解决方案。希望能帮助到你。