MySQL Rails: 安装mysql2插件失败 (mysql2-0.3.20)

MySQL Rails: 安装mysql2插件失败 (mysql2-0.3.20)

阅读更多:MySQL 教程

介绍

在Ruby on Rails开发中,数据库是一个非常重要的部分。而MySQL是Ruby on Rails最常用的关系型数据库之一。mysql2是一个Ruby gem包,用来连接和操作MySQL数据库。如果你在安装mysql2(mysql2-0.3.20)时遇到了问题,本篇文章会为你提供一些解决方案。

错误信息

首先,当你尝试运行下面的命令时:

gem install mysql2 -v '0.3.20'
Mysql

你将会看到下面的错误输出:

Fetching: mysql2-0.3.20.gem (100%)
Building native extensions.  This could take a while...
ERROR:  Error installing mysql2:
    ERROR: Failed to build gem native extension.

    /usr/local/bin/ruby extconf.rb
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for rb_wait_for_single_fd()... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lm... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lz... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lsocket... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lnsl... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lmygcc... no
checking for mysql_query() in -lmysqlclient... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.
Mysql

安装过程中遇到了错误,错误原因是没有安装mysql库。由于mysql2依赖于mysql库,因此必须先安装mysql库,然后再安装mysql2。

解决方案

在安装mysql2之前,需要先安装mysql库。

在Mac OS X上安装

在Mac OS X上,您可以使用Homebrew软件包管理器按如下方式安装mysql

brew install mysql
Mysql

在Ubuntu上安装

在Ubuntu上,您可以通过运行以下命令来安装mysql:

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

在CentOS上安装

在CentOS上,您可以通过运行以下命令来安装mysql:

sudo yum install mysql-server mysql-devel
Mysql

安装mysql库后,可以尝试重新安装mysql2:

gem install mysql2 -v '0.3.20'
Mysql

这时候你将会看到这样的输出:

Building native extensions.  This could take a while...
Successfully installed mysql2-0.3.20
Parsing documentation for mysql2-0.3.20
Installing ri documentation for mysql2-0.3.20
Done installing documentation for mysql2 after 0 seconds
1 gem installed
Mysql

恭喜您!现在您已经成功地安装了mysql2。现在可以使用Ruby on Rails连接到MySQL数据库并进行编程。

总结

本文介绍了如何解决安装mysql2(mysql2-0.3.20)时出现的问题。为此,我们需要安装MySQL库,再重新安装mysql2。我们希望这篇文章对您有所帮助。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册