You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rails 7.1.2中使用Trilogy gem连接MySQL失败问题求助

Rails 7.1.2 使用Trilogy gem连接MySQL的问题及解决

我在Rails 7.1.2中将database.yml里的数据库适配器从mysql2改为trilogy后,遇到了连接MySQL的问题:

错误情况1:未添加Trilogy gem到Gemfile

Ruby控制台报错:

Error loading the 'trilogy' Active Record adapter. Missing a gem it depends on? trilogy is not part of the bundle. Add it to your Gemfile. (LoadError)

错误情况2:添加gem 'trilogy'后

出现连接失败错误:

/usr/share/rvm/gems/ruby-3.2.0/gems/activerecord-7.1.2/lib/active_record/connection_adapters/trilogy_adapter.rb:61:in `rescue in new_client': No such file or directory - trilogy_connect - unable to connect to /tmp/mysql.sock (ActiveRecord::ConnectionNotEstablished)
/usr/share/rvm/gems/ruby-3.2.0/gems/trilogy-2.6.0/lib/trilogy.rb:16:in `_initialize': No such file or directory - trilogy_connect - unable to connect to /tmp/mysql.sock (Trilogy::SyscallError::ENOENT)

初始database.yml配置

default: &default
  adapter: trilogy
  timeout: 5000
  username: user
  password: password

development:
  <<: *default
  database: development_db

解决办法

在默认配置中添加host: 127.0.0.1后,成功建立连接,修改后的配置如下:

default: &default
  adapter: trilogy
  host: 127.0.0.1
  timeout: 5000
  username: user
  password: password

内容的提问来源于stack exchange,提问作者user1929618

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 20:17:26