6

What I am surmising is that mysql2 does not default to a TCP connection protocol when I remove the socket parameter. Do you guys agree? Any GEM suggestions?

I can connect via the mysql command line. So yep all configured from a mysql point of view.

This line connects nicely: "mysql -P 3406 -u userid -p --protocol=TCP -h localhost" Enter password:

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            |
+---------------------+

database.yml

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

The port is correct for this instance. I am connecting to this database via SSH tunnel "-L 3406:host:3306" I looked at themysql2 code, it does not take a protocol parameter. It just uses socket.

Model: looky_loo.rb

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

From Rails console

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'

1 Answer 1

32

In mysql localhost is "magic" it means use the socket. Try using 127.0.0.1 instead.

5
  • Excellent. This worked. Now the issue is that I have to tell the model the table name is not plural.
    – mpechner
    Mar 31, 2011 at 2:41
  • @mpecher just use set_table_name = "notpluralname" in your AR class
    – Wes
    Mar 31, 2011 at 4:21
  • 9 years later and this answer totally helped me get unstuck!
    – subelsky
    Jun 18, 2020 at 19:45
  • strange, but 127.0.0.1 is not working for me Apr 8 at 9:46
  • UPD: the issue was in the port. We should use port on local machine for SSH tunnel different than 3306. In this case mysql will try to use socket even if we specify 127.0.0.1 Apr 8 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.