如何在 Rails 中通过 Gemfile 安装 mysql2

how to install mysql2 via Gemfile in Rails

我开始了我的 rails 项目,当我进入 rails 控制台时,我遇到了以下错误,

$ rails c
Could not find gem 'mysql2 (>= 0.3.18, < 0.5)' in any of the gem sources listed in your Gemfile.
Run `bundle install` to install missing gems.

我试了bundle install 结果如下。

An error occurred while installing mysql2 (0.4.10), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.4.10' --source 'https://rubygems.org/'` succeeds before
bundling.

In Gemfile:
  mysql2

如何修复此类错误?我怀疑版本 conflict.if 有人已经经历过这样的 issue.please 让我知道谢谢。

您的计算机上必须安装 mysql-server,

通过这台 link MySQL Installation Guide

在您的计算机上安装 MySQL

然后 捆绑安装 添加 gem 'mysql2' 到你的 Gemfile 之后。

在你的 gemfile 中,你需要

gem 'mysql2'

然后在添加

之后尝试 bundle install

您可以从 Ubuntu 存储库中的软件包安装 MySQL 服务器和客户端。作为安装过程的一部分,您将为 root 用户设置密码。此信息将来会进入您的 Rails 应用程序的 database.yml 文件。

sudo apt-get install mysql-server mysql-client libmysqlclient-dev

安装 libmysqlclient-dev 为您提供编译 mysql2 gem 所需的文件,当您设置Rails应用