Programing

Ruby on Rails 3 OSX에서 '/tmp/mysql.sock'소켓을 통해 로컬 MySQL 서버에 연결할 수 없습니다.

lottogame 2020. 9. 22. 21:00
반응형

Ruby on Rails 3 OSX에서 '/tmp/mysql.sock'소켓을 통해 로컬 MySQL 서버에 연결할 수 없습니다.


표준 Rails3 환경, 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가 있습니다.

rake db:migrate데이터베이스를 생성하기 위해 실행할 때 발생하는 오류 는 다음과 같습니다.

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

config / database.yml에는

development:
  adapter: mysql2
  host: localhost
  username: root
  password: xxxx
  database: xxxx

내가 놓친 간단한 것이 확실합니다.


먼저 소켓 파일을 찾으려면 :

mysqladmin variables | grep socket

나를 위해 이것은 다음을 제공합니다.

| socket                                            | /tmp/mysql.sock                                                                                                        |

그런 다음 다음 줄을 추가하십시오 config/database.yml.

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

그것을 발견!

레일을 로컬 소켓 대신 TCP / IP를 통해 연결하도록 host: localhostconfig / database.yml을 host: 127.0.0.1변경 합니다.

development:
  adapter: mysql2
  host: 127.0.0.1
  username: root
  password: xxxx
  database: xxxx

mysql 서버가 실행되고 있지 않을 수 있습니다. 다음은 서버를 시작하는 방법을 설명합니다. 이것은 mysql 다운로드와 함께 제공되는 README 파일에서 발췌 한 것입니다.

설치 후 터미널 창에서 다음 명령을 실행하여 MySQL을 시작할 수 있습니다. 이 작업을 수행하려면 관리자 권한이 있어야합니다.

시작 항목을 설치 한 경우 다음 명령을 사용하십시오.

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

시작 항목을 사용하지 않는 경우 다음 명령 시퀀스를 입력하십시오.

 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)

MySQL 서버를 시작하면 "/tmp/mysql.sock"이 자동으로 생성됩니다. 따라서 Rails 서버를 시작하기 전에이를 수행하는 것을 잊지 마십시오.


이 문제를 해결하기위한 옵션은 다음과 같습니다.

옵션 1 : 호스트를 127.0.0.1로 변경

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

옵션 2 : MySql 서버에 2 개의 연결이있는 것 같습니다. 소켓 파일 위치를 찾으려면 다음을 수행하십시오.

mysqladmin variables | grep socket

나를 위해 :

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!

또는

mysql --help 

PHP 애플리케이션 용 OS X 버전 10.9.5에 XAMPP를 설치했기 때문에이 오류가 발생합니다. 여기에서 기본 소켓 위치 중 하나를 선택하십시오.

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.


With my installation of MAMP on OSX the MySQL socket is located at /Applications/MAMP/tmp/mysql/mysql.sock. I used locate mysql.sock to find my MySQL socket location.

So my config/database.yml looks like:

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

If you are on Mac OSX,

The default location for the MySQL Unix socket is different on Mac OS X and Mac OS X Server depending on the installation type you chose

MySQL Unix Socket Locations on Mac OS X by Installation Type

  • Package Installer from MySQL ------------------/tmp/mysql.sock

  • Tarball from MySQL -------------------------------/tmp/mysql.sock

  • MySQL Bundled with Mac OS X Server -------/var/mysql/mysql.sock

So just change your database.yml in socket: /tmp/mysql.sock to point to the right place depending on what OS and installation type you are using


The default location for the MySQL socket on Mac OS X is /var/mysql/mysql.sock.


I have had the same problem, but none of the answers quite gave a step by step of what I needed to do. This error happens because your socket file has not been created yet. All you have to do is:

  1. Start you mysql server, so your /tem/mysql.sock is created, to do that you run: mysql.server start
  2. Once that is done, go to your config/database.yml and add the socket: /tmp/mysql.sock
  3. Run rake:dbmigrate once again and everything should workout fine

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


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 :)


You have problem with like this: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

Ans: $ sudo service mysql start


On my machine mysqld service stopped that's why it was giving me the same problem.

1:- Go to terminal and type

sudo service mysqld restart

This will restart the mysqld service and create a new sock file on the required location.


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

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

참고URL : https://stackoverflow.com/questions/5499035/ruby-on-rails-3-cant-connect-to-local-mysql-server-through-socket-tmp-mysql-s

반응형