标签: mysql2

如何使用 Ruby 保持 mysql 连接(mysql2 gem)(无 RoR)

我有一个使用 Rack 的非常简单的 Web 服务,没有 Rails,我将在每个有效连接上联系 MySQL。我想保持连接(例如在构造函数中),以便在每个请求中我都重用它。让我们这样说:

  class Service

    def initialize(dbhost, dbport, dbname, dbuser, dbpass)
      @client = Mysql2::Client.new(
          :host => dbhost, 
          :port => dbport, 
          :database => dbname,
          :username => dbuser, 
          :password => dbpass)
    end

    def call(env)
       # some logic that will call:
       results = _query(sql)
    end

    def _query(sql)
      results = @client.query(sql)
    end

  end

  Rack::Handler::Mongrel.run Service.new(DB_HOST, DB_PORT, DB_NAME, DB_USER, DB_PASS), :Port => 8123  
Run Code Online (Sandbox Code Playgroud)

使用上面的代码,该服务将获得一个 MySQL 连接,并且在每次请求时它都会执行查询。但是如何确保如果连接断开,该_query方法将重新连接?

ruby rack mysql2

3
推荐指数
1
解决办法
3547
查看次数

rails mysql2:如何验证 mysql 服务器的 SSL 证书?

我正在尝试通过 SSL 远程连接到 mysql 数据库,并验证服务器的证书以匹配用于连接到服务器的 DNS 域。

使用命令行mysql工具,我可以使用mysql --ssl-ca=/path/to/cacert.pem --ssl-verify-server-cert.

使用 rails mysql2,我设置了sslca:¹,这会导致未完全验证的 SSL 连接像mysql --ssl-ca=这样。--ssl-verify-server-cert如果服务器证书的域错误,我该怎么做才能使连接失败?

我尝试添加这对这个问题没有影响如下:flags: SSL_VERIFY_SERVER_CERTflags: CLIENT_SSL_VERIFY_SERVER_CERTflags: 1073741824,和secure_auth: true

¹要么sslca: /path/to/cacert.pem在配置/ database.yml中,或?sslca=/path/to/cacert.pem在一个mysql2://URL

ssl ruby-on-rails ssl-certificate mysql2

3
推荐指数
1
解决办法
3179
查看次数

Pluck 返回重复的结果

我面临以下问题。正如我们所看到的,我的game对象有两个关联的:players

=> game.players.count
2
Run Code Online (Sandbox Code Playgroud)

但是,该pluck方法返回重复的值:

=> game.players.pluck :name
["Alice", "Alice", "Bob", "Bob"]
Run Code Online (Sandbox Code Playgroud)

两者GamePlayer延伸ActiveRecord::Base. 我完全不知道这里到底发生了什么。

相关宝石:

rails 4.2.8
mysql2 0.3.21
Run Code Online (Sandbox Code Playgroud)

关于可能发生的事情有任何线索吗?提前致谢。

ruby activerecord ruby-on-rails mysql2 ruby-on-rails-4

3
推荐指数
1
解决办法
938
查看次数

尽管使用 `gem install...` 命令进行安装,mysql2 gem 在捆绑安装时失败

这是我的日志 bundle install

Fetching mysql2 0.5.2
Installing mysql2 0.5.2 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
...
...
...
Cannot find mysql_config at /usr/local/opt/mysql@5.6/bin/mysql_config
--with-ldflags=-L/usr/local/opt/openssl/lib
--with-cppflags=-I/usr/local/opt/openssl/include
-----
*** 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=/Users/admin/.asdf/installs/ruby/2.6.3/bin/$(RUBY_BASE_NAME)
    --with-mysql-dir
    --without-mysql-dir
    --with-mysql-include
    --without-mysql-include=${mysql-dir}/include …
Run Code Online (Sandbox Code Playgroud)

ruby rubygems ruby-on-rails mysql2

3
推荐指数
2
解决办法
1615
查看次数

sequelize 查找和更新没有模型的记录

我想使用 sequelize.js 和 mysql2 更新数据库记录我无权访问模型文件夹,或者它们没有被制作,所以有没有办法更新无法找到解决方案所有的解决方案都是我检查过的型号名称点更新

var Book = db.define(‘books’, {
 title: {
   type: Sequelize.STRING
 },
 pages: {
   type: Sequelize.INTEGER
 }
})


Book.update(
   {title: req.body.title},
   {returning: true, where: {id: req.params.bookId} }
 )
 .then(function([ rowsUpdate, [updatedBook] ]) {
   res.json(updatedBook)
 })
 .catch(e => console.log(e));
Run Code Online (Sandbox Code Playgroud)

我想要你的专家解决方案

mysql sql node.js mysql2 sequelize.js

3
推荐指数
1
解决办法
1626
查看次数

通过CentOS 64位在Ruby 1.9.2中安装rubygem'mysql2'时出现编译器错误

带有Ruby 1.8.7和1.9.2CentOS 64位服务器

{{没有RVM,需要它没有RVM ......目前所有的Ruby1.9.2二进制文件都是ruby19,同样可以访问,因此没有gem-path或任何类型的任何映射}}}

安装gem'mysql2'时出错...

我安装了所有依赖项,并且都是64位版本 ...

所有错误都属于{./client.h:13:错误:重新定义typedef'rb_unblock_function_t'}

{#warning ruby​​sig.h是否过时}可能是一个原因吗?

控制台捕获:

#gem19 install mysql2 -v 0.2.7 -- --with-mysql-dir=/usr/bin --with-mysql-config=/usr/bin/mysql_config --with-mysql-include=/usr/include/mysql

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

/usr/bin/ruby19 extconf.rb --with-mysql-dir=/usr/bin --with-mysql-config=/usr/bin/mysql_config --with-mysql-include=/usr/include/mysql
checking for rb_thread_blocking_region()... no
checking for mysql.h... yes
checking for errmsg.h... yes
checking for mysqld_error.h... yes
creating Makefile

make
gcc -I. -I/usr/include/ruby-1.9.1/x86_64-linux -I/usr/include/ruby-1.9.1/ruby/backward -I/usr/include/ruby-1.9.1 -I. -DHAVE_MYSQL_H …
Run Code Online (Sandbox Code Playgroud)

ruby centos5 rubygems compiler-errors mysql2

2
推荐指数
1
解决办法
2542
查看次数

heroku rake db:migrate ERROR"gem install activerecord-mysql-adapter"

即时通讯使用Rails 3.2.6试图运行

heroku run rake db:migrate
Run Code Online (Sandbox Code Playgroud)

我得到了adaptar错误

rake aborted!
Please install the mysql adapter: `gem install activerecord-mysql-adapter` (mysql is not part of the bundle. Add it to Gemfile.)
Run Code Online (Sandbox Code Playgroud)

在我的database.yml中,我将开发,生产和测试设置为:

adapter: mysql2
Run Code Online (Sandbox Code Playgroud)

这是我的宝石环境

?  my_app git:(master) gem env
RubyGems Environment:
  - RUBYGEMS VERSION: 1.8.24
  - RUBY VERSION: 1.9.3 (2012-11-10 patchlevel 327) [x86_64-darwin11.4.2]
  - INSTALLATION DIRECTORY: /Users/jcollyer/.rvm/gems/ruby-1.9.3-p327
  - RUBY EXECUTABLE: /Users/jcollyer/.rvm/rubies/ruby-1.9.3-p327/bin/ruby
  - EXECUTABLE DIRECTORY: /Users/jcollyer/.rvm/gems/ruby-1.9.3-p327/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-darwin-11
  - GEM PATHS:
     - /Users/jcollyer/.rvm/gems/ruby-1.9.3-p327
     - /Users/jcollyer/.rvm/gems/ruby-1.9.3-p327@global …
Run Code Online (Sandbox Code Playgroud)

ruby mysql ruby-on-rails heroku mysql2

2
推荐指数
1
解决办法
1088
查看次数

为什么我不能在Ruby On Rails上安装mysql2?

我不知道如何安装mysql2.

我正在使用:

  • Ruby On Rails 3.2.14
  • Ruby 2.0.0
  • Mac OS X 10.9("小牛队")

Installing mysql2 (0.3.13) 

An error occurred while installing mysql2 (0.3.13), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.3.13'` succeeds before bundling.

Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

    /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby extconf.rb 

*** 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.


Gem files …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails mysql2

2
推荐指数
1
解决办法
3246
查看次数

Rails 5和Ruby 2.3.3无法加载libmysql.dll

在为使用mysql数据库创建的应用程序启动服务器时,在控制台中记录下面的错误消息.

Failed to load libmysql.dll from C:\RailsInstaller\Ruby2.3.0\lib\ruby\gems\2.3.0\gems\mysql2-0.4.7-x86-mingw32\vendor\libmysql.dll
Run Code Online (Sandbox Code Playgroud)

环境细节:

Window 7 64 bit
Rails 5.0.4
ruby 2.3.3p222 (2016-11-21 revision 56859) [i386-mingw32]
mysqld  Ver 5.7.18 for Win64 on x86_64 (MySQL Community Server (GPL))
Run Code Online (Sandbox Code Playgroud)

来自mysql2 ruby​​ gem的安装后消息在控制台上收到以下消息.

You've installed the binary version of mysql2.
It was built using MySQL Connector/C version 6.1.10.
It's recommended to use the exact same version to avoid potential issues.

At the time of building this gem, the necessary DLL files were retrieved from:
http://cdn.mysql.com/Downloads/Connector-C/mysql-connector-c-6.1.10-win32.zip

This gem *includes* vendor/libmysql.dll …
Run Code Online (Sandbox Code Playgroud)

ruby mysql ruby-on-rails mysql2 ruby-on-rails-5

2
推荐指数
1
解决办法
5521
查看次数

ld:在安装mysql2 gem时找不到-lssl的库

我将项目的Gemfile中的mysql2版本从0.3.20更改为0.5.2。当我运行时bundle update mysql2,出现以下错误:

Fetching mysql2 0.5.2 (was 0.3.21)
Installing mysql2 0.5.2 (was 0.3.21) with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /Users/hirurg103/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/mysql2-0.5.2/ext/mysql2
/Users/hirurg103/.rbenv/versions/2.4.1/bin/ruby -I /Users/hirurg103/.rbenv/versions/2.4.1/lib/ruby/site_ruby/2.4.0 -r ./siteconf20190715-35076-13r30uh.rb extconf.rb
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for rb_wait_for_single_fd()... yes
-----
Using mysql_config at /usr/local/opt/mysql@5.6/bin/mysql_config
-----
checking for mysql.h... yes
checking for errmsg.h... yes
checking for SSL_MODE_DISABLED in mysql.h... no
checking for MYSQL_OPT_SSL_ENFORCE in mysql.h... no
checking for MYSQL.net.vio …
Run Code Online (Sandbox Code Playgroud)

failed-installation mysql2

2
推荐指数
1
解决办法
369
查看次数