从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?
我有一个预装了PHP,Apache2和MySQL的自定义虚拟机基本框。 所以我可以使用这个盒子启动虚拟机,它不需要安装Chef Cookbooks中的任何一个。 p>
但是,我正在尝试安装一本Chef Cookbook,但它失败了。 我正在尝试安装的Cookbook运行一个PHP脚本,它连接并使用MySQL数据库。 p>
它失败的原因是因为PHP无法连接到mysql服务器。 我认为这是因为MySQL在脚本尝试连接时没有启动(或准备好)。 它只在我启动VM时失败。 当我提供它(流浪汉提供),它的工作原理。 因此,当PHP脚本尝试连接时,我认为MySQL还没准备好。 p>
错误: p>
mysql_connect():无法通过socket '/ var / run / mysqld / mysqld.sock'(2)连接到本地MySQL服务器..... p> blockquote> \ n
主厨食谱中的代码: p>
执行“doInstall”do cwd“/ vagrant” 命令“php path / to / my / script.php“ end code> pre>
有什么想法吗? p> div>
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.