doupapin172773 2013-03-19 17:12
浏览 84
已采纳

从Vagrant中的Chef Recipe运行PHP脚本时,MySQL无法正常工作

I've got a custom virtual machine base box that has PHP, Apache2 and MySQL pre-installed. So I can just boot up the virtual machine using this box and it wont need to install any of the above from Chef Cookbooks.

However, there is one Chef Cookbook I'm trying to install, but it's failing. The Cookbook I'm trying to install runs a PHP script, which connects and uses a MySQL db.

The reason it fails is because PHP cannot connect to the mysql server. I think it's because MySQL isn't started (or ready) at the time the script tries to connect. It only fails when I boot the VM. When I provision it (vagrant provision), it works. So that's why I think MySQL isn't ready to go when the PHP script attempts to connect.

The errors:

mysql_connect(): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in .....

And the code in the Chef Cookbook Recipe:

execute "doInstall" do
  cwd "/vagrant"
  command "php path/to/my/script.php"
end

Any ideas?

  • 写回答

1条回答 默认 最新

  • douliao7930 2013-03-19 17:27
    关注

    Maybe you can do a

    service "mysql" do
      action :start, :immediately
    end
    

    at the beginning of your recipe or simply do a heuristical

    execute "doInstall" do
      cwd "/vagrant"
      command "sleep 10; php path/to/my/script.php"
    end
    

    which seems a bit dirty to me but might solve your issue quickly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?