Capistrano:如何为PHP应用程序部署MySQL数据库? [英] Capistrano: HowTo deploy MySQL database for a PHP application?
问题描述
我正在开发一个基于PHP的应用程序,并使用Capistrano将其部署到我的Web服务器上.
I am developing a PHP based application and using Capistrano to deploy it to my webserver.
直到现在,我还没有使用数据库,因此部署运行得很好.
Until now, I was not using Databases and hence, the deploy was running all fine.
但是,现在我正在尝试与此应用程序一起使用MySQL数据库,并且想知道是否有可能在Capistrano的远程服务器上以及在远程服务器上部署数据库-这是为Rails的数据库完成的方式.
However, now I am trying to use a MySQL database as well with this application and was wondering, if there is a possibility of deploying database, as well on remote server with Capistrano - the way it is done for Rails' databases.
致谢
尼基尔·古普塔(Nikhil Gupta)
Regards
Nikhil Gupta
推荐答案
数据库部署的全部魔力是RoR的本机功能,您可能希望模仿它以获得相同的结果.
the whole magic of database deployment is a native functionality of RoR, you might want to mimic it to get the same results.
您将需要准备用于迁移数据库的脚本,因此不要使用一个脚本,数据库的每次更改都需要一个新脚本.您还需要在某个位置存储已执行的迁移的列表,rails为此使用数据库表,但是文件也可能对此有用.
You will need to prepare scripts for migrating your database, so do not use one script, each change of database requires a new script. You need also to store somewhere list of the already performed migrations, rails uses database table for this, but an file might be good for this too.
您可能想尝试以下代码:
You might want to try with this code:
set :mysql_params, "-u user -ppassword"
set :mysql_db_name, "database_name"
after :deploy, :migrate
desc "migrate database on server"
task :migrate do
run "touch #{shared_path}/migration.list ;
ls -1v #{current_path}/sql/*.sql 2>/dev/null > #{shared_path}/migration.available;
diff #{shared_path}/migration.available #{shared_path}/migration.list | awk \"/^</ {print \\$2}\" | while read f ;
do echo \"migrating $(basename $f)\"; mysql #{mysql_params} #{mysql_db_name} < $f && echo $f >> #{shared_path}/migration.list ; done;
rm -f #{shared_path}/migration.available"
end
after "deploy:setup", :create_db
desc "create database on server"
task :create_db do
run "mysql #{mysql_params} -e \"CREATE DATABASE #{mysql_db_name}\""
end
最重要的是要保留迁移顺序,您应该使用连续的数字或date_time来命名迁移,因此ls -1v #{current_path}/migrations/*.sql
的示例输出如下所示:
and most important to preserve order of migrations you should name your migrations with consecutive numbers or date_time, so example output of ls -1v #{current_path}/migrations/*.sql
would look like:
0001_create_database.sql
0002_create_user_table.sql
0003_add_password_to_users.sql
20101205_141534_add_admin_user.sql
20110108_090712_create_post_table.sql
20110210_165609_create_comment_table.sql
date_time条目使用格式YYYYmmdd_hhMMss_title.sql
the date_time entries use format YYYYmmdd_hhMMss_title.sql
这篇关于Capistrano:如何为PHP应用程序部署MySQL数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!