Ruby on Rails - 地址已在使用中 - 为“0.0.0.0”端口 3000 (Errno::EADDRINUSE) 绑定 (2)

Posted

技术标签:

【中文标题】Ruby on Rails - 地址已在使用中 - 为“0.0.0.0”端口 3000 (Errno::EADDRINUSE) 绑定 (2)【英文标题】:Ruby on Rails - Address already in use - bind(2) for "0.0.0.0" port 3000 (Errno::EADDRINUSE) 【发布时间】:2018-02-20 23:55:38 【问题描述】:

当我尝试从本地计算机启动 Rails 服务器时,我收到以下错误消息。

/Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/puma-3.9.1/lib/puma/binder.rb:269:in `initialize': Address already in use - bind(2) for "0.0.0.0" port 3000 (Errno::EADDRINUSE)
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/puma-3.9.1/lib/puma/binder.rb:269:in `new'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/puma-3.9.1/lib/puma/binder.rb:269:in `add_tcp_listener'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/puma-3.9.1/lib/puma/binder.rb:105:in `block in parse'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/puma-3.9.1/lib/puma/binder.rb:88:in `each'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/puma-3.9.1/lib/puma/binder.rb:88:in `parse'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/puma-3.9.1/lib/puma/runner.rb:144:in `load_and_bind'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/puma-3.9.1/lib/puma/cluster.rb:391:in `run'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/puma-3.9.1/lib/puma/launcher.rb:174:in `run'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/puma-3.9.1/lib/rack/handler/puma.rb:69:in `run'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/rack-2.0.3/lib/rack/server.rb:297:in `start'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/railties-5.0.3/lib/rails/commands/server.rb:104:in `start'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/railties-5.0.3/lib/rails/commands/commands_tasks.rb:90:in `block in server'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/railties-5.0.3/lib/rails/commands/commands_tasks.rb:85:in `tap'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/railties-5.0.3/lib/rails/commands/commands_tasks.rb:85:in `server'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/railties-5.0.3/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/railties-5.0.3/lib/rails/commands.rb:18:in `<top (required)>'
from /Users/zachdobbs/sample_app/bin/rails:9:in `require'
from /Users/zachdobbs/sample_app/bin/rails:9:in `<top (required)>'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `load'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `call'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/spring-2.0.2/lib/spring/client/command.rb:7:in `call'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/spring-2.0.2/lib/spring/client.rb:30:in `run'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/spring-2.0.2/bin/spring:49:in `<top (required)>'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `load'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `<top (required)>'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:68:in `require'
from /Users/zachdobbs/.rbenv/versions/2.4.0/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:68:in `require'
from /Users/zachdobbs/sample_app/bin/spring:15:in `<top (required)>'
from bin/rails:3:in `load'
from bin/rails:3:in `<main>'

这个错误是突然发生的,我不确定它的原因是什么。当我在网上查看时,我注意到问题可能是在端口 3000 上运行的另一个服务,但是当我尝试运行 'lsof -i tcp:3000' 时,我没有得到任何结果。

在 puma.rb 中,我定义。

port        ENV.fetch("PORT")  3000 
rackup      DefaultRackup
port        ENV['PORT']     || 3000
environment ENV['RACK_ENV'] || 'development'

当我将端口值更改为 8080 之类的值时,服务器将运行。我对为什么服务器不会在端口 3000 上启动感到困惑。我尝试运行的每个命令都显示没有在该端口上运行任何服务。

【问题讨论】:

您是否尝试以 root 身份运行 lsof -i tcp:3000?如果没有,您应该使用sudo 进行操作。原因可能是该进程正在以其他用户身份运行。试试sudo lsof -i :3000 | grep LISTEN @MaksimKalmykov 作为 sudo 运行仍然没有给我任何结果。 在浏览器上运行localhost:3000会返回什么? @sa77 'Safari 无法连接到服务器' 【参考方案1】:

我知道这是一篇很老的帖子,但我必须回答以防其他人像我一样来到这里。

当您在 puma 配置中声明 portbind 命令时,它将尝试绑定到所有这些端口或地址。

在这种情况下,您已指定 port 两次,因此它会尝试绑定两者,而第二次将失败。

【讨论】:

这是正确的。使用 puma 时,它使用默认端口 3000。当我们指定相同的端口时,它会抛出此错误。【参考方案2】:

尝试以下方法:

> sudo ps ax | grep rails
#1338 pts/1    Sl+    0:06 /usr/bin/ruby2.3 bin/rails c
#1707 pts/0    Rl+    0:07 /usr/bin/ruby2.3 bin/rails s

> sudo kill -9 1707

> rails s

【讨论】:

执行sudo ps ax | grep rails 给我一个结果:1559 s000 S+ 0:00.00 grep rails 并尝试执行sudo kill -9 1559 导致kill: 1559: No such process 不,您不应该终止 grep rails,您必须终止以 bin/rails s 结尾的进程 ID。我在运行 rails 控制台时也提到了rails c 啊,我明白了。问题仍然存在,但是没有显示任何服务器实例,并且端口 3000 似乎未使用,所以我仍然对服务器无法启动的原因感到困惑

以上是关于Ruby on Rails - 地址已在使用中 - 为“0.0.0.0”端口 3000 (Errno::EADDRINUSE) 绑定 (2)的主要内容,如果未能解决你的问题,请参考以下文章

如何在 ruby​​ on rails 中显示 vimeo 和 youtube 嵌入链接的缩略图?

从 ruby​​ on rails 重定向到 angular 给 preflight 的响应是无效错误

Ruby on Rails - 如何在 Rails 方法中使用 HTML 坐标?

Ruby on Rails全栈课程5.2 项目上线--在云服务器上配置Ruby On Rails环境

Ruby on Rails 学习之路

Ruby on Rails,如何确定请求是由机器人还是搜索引擎蜘蛛制作的?