Home >Database >Mysql Tutorial >How to Fix 'Can't connect to local MySQL server through socket '/tmp/mysql.sock'' in Ruby on Rails 3 on OSX?

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

Barbara Streisand
Barbara StreisandOriginal
2024-11-11 00:07:03635browse

How to Fix

Connecting to MySQL with socket in Ruby on Rails 3 on OSX

When attempting to create a database through Ruby on Rails 3 using the rake db:migrate command, one might encounter the error: "Can't connect to local MySQL server through socket '/tmp/mysql.sock'". This error indicates an issue in establishing a connection between Rails and the MySQL server.

To resolve this issue, it is necessary to specify the socket file in the config/database.yml file:

  1. Find the socket file: Execute the following command to locate the socket file:
mysqladmin variables | grep socket
  1. Add the socket to config/database.yml: Open the config/database.yml file and add the following line under the "development" configuration:
socket: /tmp/mysql.sock

Here is an example of a complete config/database.yml configuration with the socket specified:

development:
  adapter: mysql2
  host: localhost
  username: root
  password: xxxx
  database: xxxx
  socket: /tmp/mysql.sock

Once these steps are completed, the connection to the MySQL server should be established successfully, and the rake db:migrate command should execute without any errors.

The above is the detailed content of How to Fix 'Can't connect to local MySQL server through socket '/tmp/mysql.sock'' in Ruby on Rails 3 on OSX?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn