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

我在第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 来自终端的$ ps aux | grep rails

应该返回这样的东西:

 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