Search code examples
mysqlruby-on-railsmina

Mina Deploy fails connecting to MySQL during rake db:migrate


I am new to using Mina for a Rails 4 app deployment. It is a very simple application using MySQL. The server is Ubuntu 14.04 on Digital Ocean. I have MySQL installed on the server and can login with the mysql CLI on the server using the credentials that are in my database.yml. However, when Mina is trying to run a rake db:migrate, it fails with the following:

-----> Migrating database
   $ RAILS_ENV="staging" bundle exec rake db:migrate
   rake aborted!
   Mysql2::Error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysql.sock' (2)

I verified the socket location in /etc/mysqld/my.cnf is /var/run/mysqld/mysql.sock and this matches the database.yml configuration as well.

Why?


Solution

  • These are steps to fix your problem:

    Step 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
    

    Step 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
    

    For details information ruby-on-rails-3-cant-connect-to-local-mysql-server-through-socket-tmp-mysql-s

    Step 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.