2011-11-25 76 views
0

我正在開發Rails v2.3應用程序。並使用MySQL v5.1。停止啓動數據庫

我需要停止在我rake任務之一開始 mysql數據庫,但我不知道如何實現這個東西,可能有人幫助我嗎?

namespace :db do 
    task :some_db_task => :environment do 
     #shut down mysql 
     ... 
     #start mysql 
    end 
end 

順便說一句,我開發上Ubuntu的機。

----------------------- update --------------------- -------

我試過exec "/etc/init.d/mysql stop",我得到了以下錯誤消息:

Rather than invoking init scripts through /etc/init.d, use the service(8) 
utility, e.g. service mysql stop 

Since the script you are attempting to invoke has been converted to an 
Upstart job, you may also use the stop(8) utility, e.g. stop mysql 
stop: Rejected send message, 1 matched rules; type="method_call", sender=":1.78" (uid=1000 pid=6331 comm="stop) interface="com.ubuntu.Upstart0_6.Job" member="Stop" error name="(unset)" requested_reply=0 destination="com.ubuntu.Upstart" (uid=0 pid=1 comm="/sbin/init")) 

我試圖/etc/init.d/mysql stop還命令行上,相同的錯誤加薪,

通過遵循錯誤消息的說明,然後我嘗試service mysql stop,我收到了一個新的錯誤消息:

stop: Rejected send message, 1 matched rules; type="method_call", sender=":1.79" (uid=1000 pid=6343 comm="stop) interface="com.ubuntu.Upstart0_6.Job" member="Stop" error name="(unset)" requested_reply=0 destination="com.ubuntu.Upstart" (uid=0 pid=1 comm="/sbin/init")) 

爲什麼我不能停止mysql?

---------------更新2 -------------------------

我意識到,即使上面提到的錯誤消息顯示,sudo /etc/init.d/mysql stop實際上完成其作業。 MySQL 通過此命令得到停止,同時消息提升。

回答

0

我寧願使用系統(「命令/etc/init.d/mysqld停止」),這樣你就可以確保數據庫已採空。 system()在exec啓動後臺線程時等待命令執行。

@Update您可能需要「sudo的服務mysql的停止」如果你正在使用Ubuntu的清醒或更新

+1

請檢查我的更新,現在的問題似乎我無法停止mysql ... – Mellon

+0

看到我的更新^^ – Michael

+0

我試圖更新運行「sudo服務mysql停止」,現在我收到錯誤消息「停止:未知實例:」 – Mellon

-1

您可以使用紅寶石exec方法如下

exec "/etc/rc.d/init.d/mysqld start" #start mySql 
exec "/etc/rc.d/init.d/mysqld stop" #stop mySql 
+0

你好,請檢查我的更新,我似乎無法停止MySQL的,爲什麼呢? – Mellon

+0

沒有sudo它不起作用,如果用戶不是以root用戶身份登錄的 – Mellon

+0

它完全取決於你的命令,我給了使用exce方法運行shell命令的想法。 –

0

也許類似下面的執行shell命令?

namespace :db do 
    task :start do 
    exec "sudo /etc/init.d/mysqld start" 
    end 

    task :stop do 
    exec "sudo /etc/init.d/mysqld stop" 
    end 
end 
+0

嗨,請檢查我的更新,我似乎無法阻止mysql,爲什麼? – Mellon