Ruby on Rails - 地址已被使用 - bind(2) 用于“0.0.0.0”端口 3000 (Errno::EADDRINUSE)

Ruby on Rails - Address already in use - bind(2) for "0.0.0.0" port 3000 (Errno::EADDRINUSE)

当我尝试从我的本地计算机启动我的 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 上的另一个服务 运行ning 上,但是当我尝试 运行 'lsof -i tcp:3000' 时,我没有得到任何结果。

在puma.rb内,我定义。

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

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

尝试以下操作:

> 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

我知道这是一个非常古老的 post 但我不得不回答以防其他人像我一样到达这里。

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

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