2012-02-05 50 views
5

Tôi đã chạy gem install mysql2, thêm tệp lib vào thư mục bin khi nó nói và mysql2 nằm trong tệp gem. Chuyện gì thế?Không thể kết nối với MySQL trong Ruby on Rails

# MySQL. Versions 4.1 and 5.0 are recommended. 
# 
# Install the MYSQL driver 
# gem install mysql2 
# 
# Ensure the MySQL gem is defined in your Gemfile 
# gem 'mysql2' 
# 
# And be sure to use new-style password hashing: 
# http://dev.mysql.com/doc/refman/5.0/en/old-client.html 
development: 
    adapter: mysql2 
    encoding: utf8 
    reconnect: false 
    database: dealers 
    pool: 5 
    username: fredric 
    password: test 
    host: localhost 

Mysql2::Error (Access denied for user 'fredric'@'localhost' (using password: YES)): 
    mysql2-0.3.11-x86 (mingw32) lib/mysql2/client.rb:44:in `connect' 
    mysql2-0.3.11-x86 (mingw32) lib/mysql2/client.rb:44:in `initialize' 
    activerecord (3.2.1) lib/active_record/connection_adapters/mysql2_adapter.rb:16:in `new' 
    activerecord (3.2.1) lib/active_record/connection_adapters/mysql2_adapter.rb:16:in `mysql2_connection' 
    activerecord (3.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:277:in `new_connection' 
    activerecord (3.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:287:in `checkout_new_connection' 
    activerecord (3.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:235:in `block (2 levels) in checkout' 
    activerecord (3.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:230:in `loop' 
    activerecord (3.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:230:in `block in checkout' 
    C:/Ruby193/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize' 
    activerecord (3.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:229:in `checkout' 
    activerecord (3.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:95:in `connection' 
    activerecord (3.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:374:in `retrieve_connection' 
    activerecord (3.2.1) lib/active_record/connection_adapters/abstract/connection_specification.rb:168:in `retrieve_connection' 
    activerecord (3.2.1) lib/active_record/connection_adapters/abstract/connection_specification.rb:142:in `connection' 
    activerecord (3.2.1) lib/active_record/query_cache.rb:67:in `rescue in call' 
    activerecord (3.2.1) lib/active_record/query_cache.rb:61:in `call' 
    activerecord (3.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:443:in `call' 
    actionpack (3.2.1) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call' 
    activesupport (3.2.1) lib/active_support/callbacks.rb:405:in `_run__874471498__call__280582312__callbacks' 
    activesupport (3.2.1) lib/active_support/callbacks.rb:405:in `__run_callback' 
    activesupport (3.2.1) lib/active_support/callbacks.rb:385:in `_run_call_callbacks' 
    activesupport (3.2.1) lib/active_support/callbacks.rb:81:in `run_callbacks' 
    actionpack (3.2.1) lib/action_dispatch/middleware/callbacks.rb:27:in `call' 
    actionpack (3.2.1) lib/action_dispatch/middleware/reloader.rb:65:in `call' 
    actionpack (3.2.1) lib/action_dispatch/middleware/remote_ip.rb:31:in `call' 
    actionpack (3.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call' 
    actionpack (3.2.1) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call' 
    railties (3.2.1) lib/rails/rack/logger.rb:26:in `call_app' 
    railties (3.2.1) lib/rails/rack/logger.rb:16:in `call' 
    actionpack (3.2.1) lib/action_dispatch/middleware/request_id.rb:22:in `call' 
    rack (1.4.1) lib/rack/methodoverride.rb:21:in `call' 
    rack (1.4.1) lib/rack/runtime.rb:17:in `call' 
    activesupport (3.2.1) lib/active_support/cache/strategy/local_cache.rb:72:in `call' 
    rack (1.4.1) lib/rack/lock.rb:15:in `call' 
    actionpack (3.2.1) lib/action_dispatch/middleware/static.rb:53:in `call' 
    railties (3.2.1) lib/rails/engine.rb:479:in `call' 
    railties (3.2.1) lib/rails/application.rb:220:in `call' 
    rack (1.4.1) lib/rack/content_length.rb:14:in `call' 
    railties (3.2.1) lib/rails/rack/log_tailer.rb:14:in `call' 
    rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service' 
    C:/Ruby193/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service' 
    C:/Ruby193/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run' 
    C:/Ruby193/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread' 


    Rendered C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/middleware/templates/rescues/_trace.erb (3.0ms) 
    Rendered C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (39.0ms) 
    Rendered C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (108.0ms) 
[2012-02-05 20:30:54] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true 

Trả lời

3

Dường như vấn đề kiểm soát truy cập do cài đặt PRIVILEGES của bạn. Hãy nhớ rằng localhost là trường hợp đặc biệt không được bao gồm bởi % khi cấp quyền vì nó sẽ kết nối qua ổ cắm UNIX cục bộ chứ không phải cổng socket 3306 TCP.

Bạn nên kiểm tra xem bạn có thể kết nối từ cùng một máy ứng dụng đang chạy trên bằng công cụ mysql dòng lệnh:

mysql --user=frederic --password=test dealers 

Bạn sẽ nhận được các lỗi tương tự như trình điều khiển Mysql2 vào thời điểm này.

Nói chung này được cố định bằng cách cập nhật các khoản tài trợ của bạn:

GRANT ALL PRIVILEGES ON dealers.* to `frederic`@`localhost` IDENTIFIED BY 'test' 
+0

Cảm ơn bạn! Tôi đã phải thay đổi localhost thay vì sử dụng 127.0.0.1 và nó hoạt động! – LuckyLuke

+0

Và bằng cách này tadman: sự khác biệt là gì? – LuckyLuke

+0

Đó là một số tính năng kiểm soát truy cập của MySQL. 'localhost' là một bí danh cho" socket UNIX cục bộ "và không bị nhầm lẫn với những gì mọi người khác gọi là 127.0.0.1. Bạn có thể muốn chỉ định 'socket:/var/lib/mysql/mysqld.sock' trong cấu hình của bạn, hoặc bất cứ điều gì thực sự là, để phân biệt rõ ràng hơn. Bạn có thể tìm hiểu thiết lập socket của bạn nên sử dụng 'SHOW VARIABLES LIKE 'socket'' là cách máy chủ MySQL của bạn hiện được cấu hình. – tadman