Ruby on Rails 3 Can't connect to local MySQL server through socket '/tmp/mysql.sock' on OSX

前端 未结 14 1515
情深已故
情深已故 2020-11-27 09:30

I have a standard Rails3 environment, RVM 1.2.9, Rails 3.0.5, Ruby 1.9.2p180, MySQL2 Gem 0.2.7, mysql-5.5.10-osx10.6-x86_64

Error I get when running rake db:m

相关标签:
14条回答
  • 2020-11-27 10:20

    Your mysql server may not be running. Below explains how to start the server. This is an excerpt from the README file that comes with the mysql download.

    After the installation, you can start up MySQL by running the following commands in a terminal window. You must have administrator privileges to perform this task.

    If you have installed the Startup Item, use this command:

     shell> sudo /Library/StartupItems/MySQLCOM/MySQLCOM start
     (ENTER YOUR PASSWORD, IF NECESSARY)
     (PRESS CONTROL-D OR ENTER "EXIT" TO EXIT THE SHELL)
    

    If you don't use the Startup Item, enter the following command sequence:

     shell> cd /usr/local/mysql
     shell> sudo ./bin/mysqld_safe
     (ENTER YOUR PASSWORD, IF NECESSARY)
     (PRESS CONTROL-Z)
     shell> bg
     (PRESS CONTROL-D OR ENTER "EXIT" TO EXIT THE SHELL)
    
    0 讨论(0)
  • 2020-11-27 10:21

    I found that the problem is that I only have a production environment. I do not have a development or test environment.

    By adding 'RAILS_ENV=production' to give the command

    bundle exec rake redmine:plugins:migrate RAILS_ENV=production
    

    it worked

    0 讨论(0)
  • 2020-11-27 10:21

    If you are running MYSQL through XAMPP or LAMPP on Ubuntu or other Linux, try:

    socket: /opt/lampp/var/mysql/mysql.sock

    0 讨论(0)
  • 2020-11-27 10:27

    If you are running MYSQL through XAMPP:

    1. Open XAMPP mysql configuration file (on OSX):

      /Applications/XAMPP/etc/my.cnf

    2. Copy the socket path:

      socket = /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock

    3. Open rails project's database configuration file: myproject/config/database.yml

    4. Add the socket config to the development database config:

    -->

    development:
      adapter: mysql2
      encoding: utf8
      reconnect: false
      database: difiuri_falcioni
      pool: 5
      username: root
      password:
      host: localhost
      socket: /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock
    
    1. Restart rails server

    Enjoy :)

    0 讨论(0)
  • 2020-11-27 10:31

    First, to find your socket file:

    mysqladmin variables | grep socket
    

    For me, this gives:

    | socket                                            | /tmp/mysql.sock                                                                                                        |
    

    Then, add a line to your config/database.yml:

    development:
      adapter: mysql2
      host: localhost
      username: root
      password: xxxx
      database: xxxx
      socket: /tmp/mysql.sock
    
    0 讨论(0)
  • 2020-11-27 10:34

    These are options to fix this problem:

    Option 1: change you host into 127.0.0.1

    staging:
      adapter: mysql2
      host: 127.0.0.1
      username: root
      password: xxxx
      database: xxxx
      socket: your-location-socket
    

    Option 2: It seems like you have 2 connections into you server MySql. To find your socket file location do this:

    mysqladmin variables | grep socket
    

    for me gives:

    mysqladmin: connect to server at 'localhost' failed
    error: 'Can't connect to local MySQL server through socket '/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock' (2)'
    Check that mysqld is running and that the socket: '/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock' exists!
    

    or

    mysql --help 
    

    I get this error because I installed XAMPP in my OS X Version 10.9.5 for PHP application. Choose one of the default socket location here.

    I choose for default rails apps:

    socket: /tmp/mysql.sock
    

    For my PHP apps, I install XAMPP so I set my socket here:

    socket: /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock
    

    OTHERS Socket Location in OS X

    For MAMPP:

    socket: /Applications/MAMP/tmp/mysql/mysql.sock
    

    For Package Installer from MySQL:

    socket: /tmp/mysql.sock
    

    For MySQL Bundled with Mac OS X Server:

    socket: /var/mysql/mysql.sock
    

    For Ubuntu:

    socket: /var/run/mysqld/mysql.sock
    

    Option 3: If all those setting doesn't work you can remove your socket location:

    staging:
      # socket: /var/run/mysqld/mysql.sock
    

    I hope this help you.

    0 讨论(0)
提交回复
热议问题