【问题标题】:ruby wont connect with MySQLruby 无法与 MySQL 连接
【发布时间】:2015-02-10 13:49:26
【问题描述】:

我是 ruby​​ on rails 的新手,我正在尝试让它与 MySQL 一起工作。我使用 brew install mysql 安装了 mysql 然后我安装了 gem mysql2 但没有任何效果。我已经解决了这里的所有问题,但似乎没有任何效果。这是我的 database.yml

    # MySQL.  Versions 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
      #
      default: &default
        adapter: mysql2
        encoding: utf8
        pool: 5
        username: root
        password:
        host: localhost

      development:
        <<: *default
        database: optimus_development

      # Warning: The database defined as "test" will be erased and
      # re-generated from your development database when you run "rake".
      # Do not set this db to the same as development or production.
      test:
        <<: *default
        database: optimus_test

      # As with config/secrets.yml, you never want to store sensitive information,
      # like your database password, in your source code. If your source code is
      # ever seen by anyone, they now have access to your database.
      #
      # Instead, provide the password as a unix environment variable when you boot
      # the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
      # for a full rundown on how to provide these environment variables in a
      # production deployment.
      #
      # On Heroku and other platform providers, you may have a full connection URL
      # available as an environment variable. For example:
      #
      #   DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase"
      #
      # You can use this database configuration with:
      #
      #   production:
      #     url: <%= ENV['DATABASE_URL'] %>
      #
      production:
        <<: *default
        database: optimus_production
        username: optimus
        password: <%= ENV['OPTIMUS_DATABASE_PASSWORD'] %>

这是我尝试在浏览器中运行时遇到的错误:

Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

我尝试将 localhost 更改为 127.0.0.1 但这不起作用。任何帮助将不胜感激

我在阅读下面可能的重复链接后启动了我的服务器,现在我收到了这条消息:

          Unknown database 'optimus_development'Run `$ bin/rake db:create db:migrate` to create your database

【问题讨论】:

  • 那么你得到了什么错误?
  • 请始终发布您遇到的错误。
  • 本地 MySQL 服务的一个常见问题是它们运行在文件不在默认预期位置的 Unix 套接字上,需要您在 database.yml 中添加 socket: /path/to/mysql.sock
  • 而且,你说你做了brew install mysql,但你真的启动了 MySQL 服务器,你能从命令行连接到它,没有 Rails 吗?
  • Michael Berkowski,我在哪里添加“socket:/path/to/mysql.sock”?

标签: mysql ruby-on-rails


【解决方案1】:

我想通了。我必须使用“mysql.server start”从终端运行 mysql 服务器。然后我不得不运行“bin/rake db:create db:migrate”,然后它就可以工作了

【讨论】:

    【解决方案2】:

    确保您的 MySQL 服务器已启动:

    Apple Menu > System Preferences > MySQL > Start MySQL Server
    

    【讨论】:

    • Mysql 甚至没有显示在系统首选项窗口中。这是因为我已经安装了 mamp 并且它干扰了 brew 安装的 mysql 吗?
    • 这仅在您使用带有 Preference Pane 扩展的 MySQL 安装程序时才相关。
    猜你喜欢
    • 2012-12-18
    • 1970-01-01
    • 2015-09-18
    • 2015-06-06
    • 1970-01-01
    • 2022-08-30
    • 1970-01-01
    • 2020-03-24
    • 2021-10-23
    相关资源
    最近更新 更多