【问题标题】:rails3 mysql2 GEM does not seem to do TCP, only socketrails3 mysql2 GEM好像不做TCP,只做socket
【发布时间】:2011-03-31 01:07:27
【问题描述】:

我猜测的是,当我删除套接字参数时,mysql2 不会默认使用 TCP 连接协议。你们同意吗?有什么 GEM 建议吗?

我可以通过 mysql 命令行连接。所以是的,都是从 mysql 的角度配置的。

这行连接很好:“mysql -P 3406 -u userid -p --protocol=TCP -h localhost” 输入密码:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 77402
Server version: 5.0.77-log Source distribution

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+---------------------+
| Database            |
+---------------------+

数据库.yml

svc_development:
    adapter: mysql2
    encoding: utf8
    database: my_db
    username: userid
    password: apasswd
    host: localhost
    port: 3406  

此实例的端口是正确的。我通过 SSH 隧道“-L 3406:host:3306”连接到这个数据库 我看了mysql2的代码,它没有带协议参数。它只是使用套接字。

型号:looky_loo.rb

class LookyLoo < ActiveRecord::Base
    self.abstract_class = true
    establish_connection  "svc_development"
end

从 Rails 控制台

from script/rails:6:in `<main>'ruby-1.9.2-p136 :002 > LookyLoo.all
Mysql2::Error: Access denied for user 'userid'@'localhost' (using password: YES)
    from /Users/michael.pechner/.rvm/gems/ruby-1.9.2-p136@amqp/gems/mysql2-0.2.7/lib/mysql2/client.rb:42:in `connect'
    from /Users/michael.pechner/.rvm/gems/ruby-1.9.2-p136@amqp/gems/mysql2-0.2.7/lib/mysql2/client.rb:42:in `initialize'
    from /Users/michael.pechner/.rvm/gems/ruby-1.9.2-p136@amqp/gems/mysql2-0.2.7/lib/active_record/connection_adapters/mysql2_adapter.rb:14:in `new'
    from /Users/michael.pechner/.rvm/gems/ruby-1.9.2-p136@amqp/gems/mysql2-0.2.7/lib/active_record/connection_adapters/mysql2_adapter.rb:14:in `mysql2_connection'
    from /Users/michael.pechner/.rvm/gems/ruby-1.9.2-p136@amqp/gems/activerecord-3.0.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:229:in `new_connection'

【问题讨论】:

    标签: mysql ruby-on-rails-3 rubygems


    【解决方案1】:

    在 mysql 中 localhost 是“神奇的”,它意味着使用套接字。尝试改用 127.0.0.1。

    【讨论】:

    • 优秀。这行得通。现在的问题是我必须告诉模型表名不是复数。
    • @mpecher 只需在你的 AR 类中使用 set_table_name = "notpluralname"
    • 9 年后,这个答案完全帮助我摆脱困境!
    猜你喜欢
    • 1970-01-01
    • 2011-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多