部署 [已解决] 在 CentOS 上部署 Redmine 时,总是因为 mysql2 失败

joke1943 · December 19, 2014 · Last by zowbman replied at June 24, 2016 · 11935 hits

这几天在正式环境上部署 Redmine,之前在自己的 Mac 上已经试着部署过一次。 这次由于 CentOS 上部署,以前完全没有遇到过这个问题,实在头大,请教各位! 先说下过程: 先用 rvm 安装 ruby,成功。

$ ruby -v
ruby 2.1.4p265 (2014-10-27 revision 48166) [x86_64-linux]

然后 gem 安装 rails,成功

$ rails -v
Rails 4.1.8

然后下载 redmine-2.4.7,解压 运行命令

$ ruby script/rails server webrick -e production

然后提示

Could not find gem 'mysql2 (~> 0.3.11) ruby' in the gems available on this machine. 
Run `bundle install` to install missing gems.

根据提示,打命令

gem install mysql2 -v '0.3.17'

报错,以下是错误信息:

Building native extensions.  This could take a while...
ERROR:  Error installing mysql2:
    ERROR: Failed to build gem native extension.

    /usr/local/rvm/rubies/ruby-2.1.4/bin/ruby -r ./siteconf20141219-14269-9ceayj.rb extconf.rb
checking for ruby/thread.h... yes
checking for rb_thread_call_without_gvl() in ruby/thread.h... yes
checking for rb_thread_blocking_region()... yes
checking for rb_wait_for_single_fd()... yes
checking for rb_hash_dup()... yes
checking for rb_intern3()... yes
checking for mysql_query() in -lmysqlclient... yes
checking for mysql.h... no
checking for mysql/mysql.h... yes
checking for errmsg.h... no
-----
errmsg.h is missing.  please check your installation of mysql and try again.
-----
*** 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.

Provided configuration options:
    --with-opt-dir
    --without-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/usr/local/rvm/rubies/ruby-2.1.4/bin/ruby
    --with-mysql-dir
    --without-mysql-dir
    --with-mysql-include
    --without-mysql-include=${mysql-dir}/include
    --with-mysql-lib
    --without-mysql-lib=${mysql-dir}/lib
    --with-mysql-config
    --without-mysql-config
    --with-mysql-dir
    --without-mysql-dir
    --with-mysql-include
    --without-mysql-include=${mysql-dir}/include
    --with-mysql-lib
    --without-mysql-lib=${mysql-dir}/lib
    --with-mysqlclientlib
    --without-mysqlclientlib

extconf failed, exit code 1

Gem files will remain installed in /usr/local/rvm/gems/ruby-2.1.4/gems/mysql2-0.3.17 for inspection.
Results logged to /usr/local/rvm/gems/ruby-2.1.4/extensions/x86_64-linux/2.1.0/mysql2-0.3.17/gem_make.out

但是

$ find / -name errmsg.h
/usr/lib/errmsg.h 
/usr/include/mysql/errmsg.h

系统里已经安装好了 mysql 环境了 求助!

2015-1-8 前段时间忙了忙其他项目,今天又重新捡起这个问题研究了一下。 既然 adapter 用不了 mysql2,就只能用 mysql 了,但是又出了问题,大致意思是说从数据库中读出来编码有问题。 按照这位大牛说的解决了: http://www.cnblogs.com/kting/archive/2011/09/15/2177530.html

把他的帖子 copy 过来,就算记录问题了。

感谢各位对我这个问题的回复!十分感谢!!!

以下转自网络,感谢作者

问题现象:

  在.html.erb中显示mysql中的中文字符时会出现 

incompatible character encodings: UTF-8 and ASCII-8BIT 错误
问题可能原因:



mysql_adapater出来的数据是ASCII-8BIT
解决方法:



修改根目录下的\Ruby192\lib\ruby\gems\1.9.1\gems\activerecord-3.0.9\lib\active_record\connection_adapters中的
mysql_adapter.rb文件
 def select(sql, name = nil)
          @connection.query_with_result = true
          result = execute(sql, name)
          rows = []
          result.each_hash { |row| rows << row }
          result.free
          @connection.more_results && @connection.next_result    # invoking stored procedures with CLIENT_MULTI_RESULTS requires this to tidy up else connection will be dropped

      # add begin
      if @config[:encoding] && @config[:encoding]=="utf8"
            rows.each do |row|
              row.each do |key, value|
                if (value.class == String)
                  value.force_encoding("UTF-8")
                end
              end
            end
          end
      # add end
          rows
        end

需要装 mysql-devel 这个包

sudo yum install mysql-devel

#1 楼 @suupic 已经安装了啊

$ yum install mysql-devel

Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Setting up Install Process
Package mysql-devel-5.1.73-3.el6_5.x86_64 already installed and latest version
Nothing to do

安装 mysql

yum install mysql yum install mysql-devel yum install mysql-embedded yum install mysql-embedded-devel yum install mysql-server

vim /etc/my.cnf 在这行 symbolic-links=0 下增加: default-character-set = utf8

添加到启动 chkconfig mysqld on 验证: chkconfig --list mysqld

如果 mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off ← 如果 2--5 为 on 的状态就 OK

启动 mysql: /etc/rc.d/init.d/mysqld start

#5 楼 @badboy 感谢,情况是这样的,公司原来的 redmine 是 2.2.3 的,现在从新部署一套新的,我选择的版本是 2.4.7,原来的数据迁移过来,发生了这个问题。但是如果我新建一个数据库,就不会遇到这个问题。

@joke1943 版本不同,数据库字段啥的,也可能不同,所以。。。不晓得啦

是由于其 C++的编译器出了错误,使用此命令安装: sudo apt-get install libmysql++-dev

你好,我用的是 1.9.3 的版本,mysql_adapter.rb 文件的内容有些不一样。 怎么办呢?

def select(sql, name = nil, binds = [])
        @connection.query_with_result = true
        rows = exec_query(sql, name, binds).to_a
        @connection.more_results && @connection.next_result    # invoking stored procedures with CLIENT_MULTI_RESULTS requires this to tidy up else connection will be dropped
        rows
end

#10 楼 @wilask 没关系的,我使用的是 2.1.4p265,把这段代码加上就好

我今天遇到的是 CentOS 上的 pg native extention 的错误,和你的错误有相似之处。请参考:

CentOS pg 安装过程

$ bundle install 时报 pg native extention 的错误,解决见: 版本 pgsql9.2 -centos -5

$ curl -O  http://yum.postgresql.org/9.2/redhat/rhel-5-x86_64/pgdg-centos92-9.2-6.noarch.rpm
$ rpm -ivh pgdg-centos92*.rpm
$ yum install postgresql92-devel        
$ gem install pg -- --with-pg-config=/usr/pgsql-9.2/bin/pg_config

http://stackoverflow.com/questions/13702417/postgresql-gem-pg-was-unable-to-install

#11 楼 @joke1943 加入这段代码确实可以解决问题,但是整个系统所有操作都受到影响,响应时间太长了几乎是影响使用。所以还是没办法使用的

#13 楼 @wilask 通过这段代码找到问题原因,adapter 使用 mysql,从数据库读出来的数据如果有中文,编码会有问题。所以 adapter 最好还是要 mysql2,我这里是实在安装不上。

#14 楼 @joke1943 对对,我用 mysql2 就正常了

zowbman in [Topic was deleted] mention this topic. 24 Jun 11:27
You need to Sign in before reply, if you don't have an account, please Sign up first.