简体   繁体   English

Capistrano:如何为PHP应用程序部署MySQL数据库?

[英]Capistrano: HowTo deploy MySQL database for a PHP application?

I am developing a PHP based application and using Capistrano to deploy it to my webserver. 我正在开发一个基于PHP的应用程序,并使用Capistrano将其部署到我的网络服务器。

Until now, I was not using Databases and hence, the deploy was running all fine. 到目前为止,我没有使用数据库,因此,部署运行良好。

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. 但是,现在我正在尝试使用MySQL数据库和这个应用程序,并且想知道,如果有可能部署数据库,以及使用Capistrano的远程服务器 - 这是为Rails的数据库完成的方式。

Regards 问候
Nikhil Gupta 尼基尔古普塔

I'm late to the party regarding this question but going to post anyway as this is a common question with few answers. 关于这个问题,我迟到了,但无论如何都要发帖,因为这是一个很少回答的常见问题。 I've had great success using Phing and Liquibase together, you can use Liquibase to roll database changes forward and backward in a highly formalized manner, and can even track your changes in version control. 我一起使用Phing和Liquibase取得了巨大的成功,您可以使用Liquibase以高度正式的方式向前和向后滚动数据库更改,甚至可以跟踪版本控制中的更改。

I've presented on this topic several times and posted my slides (HTML format) to GitHub: https://github.com/wjgilmore/Automating-Deployments-with-Phing--Capistrano-and-Liquibase 我已经多次介绍过这个主题并将我的幻灯片(HTML格式)发布到GitHub: https//github.com/wjgilmore/Automating-Deployments-with-Phing--Capistrano-and-Liquibase

Includes bonus material for deploying PHP websites using Capistrano. 包括使用Capistrano部署PHP网站的奖励材料。 :-) :-)

the whole magic of database deployment is a native functionality of RoR, you might want to mimic it to get the same results. 数据库部署的全部魔力是RoR的本机功能,您可能希望模仿它以获得相同的结果。

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. 您还需要存储已经执行的迁移的某个列表,rails使用数据库表,但是文件也可能对此有用。

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

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: 最重要的是保留迁移顺序,您应该使用连续数字或date_time命名迁移,因此ls -1v #{current_path}/migrations/*.sql示例输出如下所示:

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

the date_time entries use format YYYYmmdd_hhMMss_title.sql date_time条目使用格式YYYYmmdd_hhMMss_title.sql

As far as I know there are 3 fully automatic approaches to deploy database to the production server. 据我所知,有3种全自动方法将数据库部署到生产服务器。

  • Use liquibase , write (or generate ) changesets, which contain your migration code. 使用liquibase ,编写(或生成 )包含迁移代码的变更集。
  • Use event sourcing , with a relational read cache having the same structure as normally. 使用事件源 ,关系读缓存具有与正常相同的结构。 Send a schema dump with your releases. 使用您的版本发送架构转储。 Purge the whole read cache including tables. 清除整个读缓存,包括表。 Recreate the tables using the dump. 使用转储重新创建表。 Reinsert data in a single transaction using the event storage and the projections. 使用事件存储和投影在单个事务中重新插入数据。 Rollback is not so hard, you can dump your old read cache, or you can follow the same steps with the old release. 回滚并不是那么难,您可以转储旧的读取缓存,或者您可以使用旧版本执行相同的步骤。
  • Write your own migration script and sql for every release. 为每个版本编写自己的迁移脚本和sql。 Not recommended, because it is error prone. 不推荐,因为它容易出错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM