Rails 教程 (M.Hartl) 第 3 版,第 8 章,如何通过关闭流氓 `rails 控制台` 来解锁数据库?

Rails Tutorial (M.Hartl) 3rd Edition, Chapter 8, How to unlock a database by closing a rogue `rails console`?

我在 chapter 8.4.1,生成迁移后我尝试 运行:

$ bundle exec rake db:migrate

但是报错如下:

rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

SQLite3::BusyException: database is locked:

#followed by a long list of paths/filenames and line numbers...

我知道这可能是因为我没有正确退出 rails console --sandbox 会话就退出了终端。但是如何找到并关闭此 rails console 会话?

在您的终端控制台类型中(确保您位于 Rails 应用程序目录中):

$ ps aux

然后您应该会看到一个进程列表,其中类似于:

USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
ubuntu     49527  0.0  0.0 482924   620 ?        Sl   Jan29   0:00 rails_console --sandbox

记下 PID,在本例中为 49527,然后 运行 在命令行中输入以下内容:

$ kill 49527

现在 运行您的迁移 $ bundle exec rake db:migrate 应该可以工作了。

从终端尝试 运行 $ ps aux | grep rails

应该return像这样:

username   68924   0.0  1.5  3166032  62168   ??  S     8:49PM   0:00.16 rails_console    
username   68900   0.0  0.1  2472884   3420 s003  S+    8:48PM   0:00.62 /Users/username/.rbenv/versions/2.2.2/bin/ruby bin/rails c
username   68708   0.0  0.2  2535772   6396 s000  S+    8:20PM   0:03.16 /Users/username/.rbenv/versions/2.2.2/bin/ruby bin/rails s

由于这种情况下必须杀死进程,所以尝试用命令

强行杀死它

$ kill -9 PID