Ruby on Rails 3 无法通过 OSX 上的套接字“/tmp/mysql.sock”连接到本地 MySQL 服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5499035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 19:21:40  来源:igfitidea点击:

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

mysqlruby-on-rails-3macos

提问by Jamie Buchanan

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

我有一个标准的 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

Error I get when running rake db:migrateto create database is:

运行rake db:migrate创建数据库时出现的错误是:

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

config/database.yml has

config/database.yml 有

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

sure it's something simple I'm missing.

当然这是我遗漏的一些简单的东西。

回答by Tobias Cohen

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:

然后,在您的config/database.yml:

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

回答by Jamie Buchanan

Found it!

找到了!

Change host: localhostin config/database.yml to host: 127.0.0.1to make rails connect over TCP/IP instead of local socket.

host: localhostconfig/database.yml更改host: 127.0.0.1为使 rails 通过 TCP/IP 而不是本地套接字连接。

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

回答by suga_shane

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.

您的 mysql 服务器可能没有运行。下面解释如何启动服务器。这是 mysql 下载附带的 README 文件的摘录。

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.

安装后,您可以通过在终端窗口中运行以下命令来启动 MySQL。您必须具有管理员权限才能执行此任务。

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)

回答by Magne

"/tmp/mysql.sock" will be created automatically when you start the MySQL server. So remember to do that before starting the rails server.

“/tmp/mysql.sock”将在您启动 MySQL 服务器时自动创建。所以请记住在启动 rails 服务器之前这样做。

回答by akbarbin

These are options to fix this problem:

这些是解决此问题的选项:

Option 1:change you host into 127.0.0.1

选项 1:将您的主机更改为 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:

选项 2:您似乎有 2 个连接到服务器 MySql。要找到您的套接字文件位置,请执行以下操作:

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.

我收到此错误是因为我在 OS X 版本 10.9.5 中为 PHP 应用程序安装了 XAMPP。在此处选择默认套接字位置之一。

I choose for default rails apps:

我选择默认 Rails 应用程序:

socket: /tmp/mysql.sock

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

对于我的 PHP 应用程序,我安装了 XAMPP,所以我在这里设置了我的套接字:

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

OTHERS Socket Location in OS X

OS X 中的 OTHERS 套接字位置

For MAMPP:

对于 MAMPP:

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

For Package Installer from MySQL:

对于 MySQL 的软件包安装程序:

socket: /tmp/mysql.sock

For MySQL Bundled with Mac OS X Server:

对于与 Mac OS X 服务器捆绑的 MySQL:

socket: /var/mysql/mysql.sock

For Ubuntu:

对于 Ubuntu:

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

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

选项 3:如果所有这些设置都不起作用,您可以删除您的套接字位置:

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

I hope this help you.

我希望这对你有帮助。

回答by Brad Denver

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

我在 OSX 上安装 MAMP 后,MySQL 套接字位于/Applications/MAMP/tmp/mysql/mysql.sock. 我曾经locate mysql.sock找到我的 MySQL 套接字位置。

So my config/database.ymllooks like:

所以我的config/database.yml样子:

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

回答by Musili

If you are on Mac OSX,

如果您使用的是 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 套接字的默认位置在 Mac OS X 和 Mac OS X Server 上有所不同,具体取决于您选择的安装类型

MySQL Unix Socket Locations on Mac OS X by Installation Type

按安装类型划分的 Mac OS X 上的 MySQL Unix 套接字位置

  • Package Installer from MySQL ------------------/tmp/mysql.sock
  • 来自 MySQL 的软件包安装程序 ------------------/tmp/mysql.sock


  • Tarball from MySQL -------------------------------/tmp/mysql.sock
  • 来自 MySQL 的压缩包 -------------------------------/tmp/mysql.sock


  • MySQL Bundled with Mac OS X Server -------/var/mysql/mysql.sock
  • 与 Mac OS X 服务器捆绑的 MySQL -------/var/mysql/mysql.sock

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

因此,只要改变你的database.ymlsocket: /tmp/mysql.sock以指向正确的地方这取决于操作系统和安装类型您正在使用

回答by morgant

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

Mac OS X 上 MySQL 套接字的默认位置是/var/mysql/mysql.sock.

回答by luizParreira

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 /tmp/mysql.sockis created, to do that you run: mysql server start
  2. Once that is done, go to your app directory end edit the config/database.ymlfile and add/edit the socket: /tmp/mysql.sockentry
  3. Run rake:dbmigrateonce again and everything should workout fine
  1. 启动你的 mysql 服务器,这样你/tmp/mysql.sock的创建就完成了,你运行:mysql server start
  2. 完成后,转到您的应用程序目录结束编辑config/database.yml文件并添加/编辑socket: /tmp/mysql.sock条目
  3. 再跑rake:dbmigrate一次,一切都会好起来的

回答by Binni Bharadiya

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

你有这样的问题:Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

Ans: $ sudo service mysql start

Ans: $ sudo service mysql start