您的位置:首页 > 运维架构 > Tomcat

如何将liquibase部署到tomcat服务器上(使用postgresql数据库)

2015-11-11 14:09 1036 查看
Liquidbase作为一个数据库的自动更新软件,可以更好的在不同的工作者之间同步数据库的变化。在这里,以postgresql和tomcat作为例子,展示如何如何将liquibase部署到服务器上

安装tomcat postgresql liquibae

将postgresql的jdbc的jar包拷贝到tomcat的lib目录

配置tomcat的serverxml contextxml和webxml启动Liquibase的服务

创建dbchangelogxml并部署到tomcat服务器

table already exist错误

安装tomcat, postgresql, liquibae

本人使用的是:

apache-tomcat-8.0.28。安装路径:c:\apache-tomcat-8.0.28

PostgreSQL-9.3。安装路径是:c:\PostgreSQL\9.3\

liquibase-3.4.1。 安装路径是:c:\liquibase-3.4.1-bin\

设置好各种环境变量:

LIQUIBASE_HOME=c:\liquibase-3.4.1-bin

export PATH=%PATH%;%LIQUIBASE_HOME%;

将postgresql的jdbc的jar包拷贝到tomcat的lib目录

下载postgresql对应版本的jdbc。将jdbc拷贝到tomcat的lib目录下:

c:\apache-tomcat-8.0.28\lib\postgresql-9.3-1104-jdbc4.jar

配置tomcat的server.xml, context.xml和web.xml(启动Liquibase的服务)

这部分内容需要同学们自己去脑补JDNI。

首先,需要在server.xml里面配置postgresql数据库的连接池。

- 打开c:\apache-tomcat-8.0.28\conf\server.xml,找到GlobalNamingResources标签,在该标签下添加Resouce

<GlobalNamingResources>
...
<Resource
name="jdbc/postgres"
scope="Shareable"
type="javax.sql.DataSource"
url="jdbc:postgresql://127.0.0.1:5432/postgres"
driverClassName ="org.postgresql.Driver"
username="postgres"
password="postgres"
/>
</GlobalNamingResources>


resource中的Name,代表的是这个resource在JDNI中的资源位置。其他的标签则用于表明服务器如何建立jdbc链接时的参数

打开c:\apache-tomcat-8.0.28\conf\context.xml,添加resourceLink

<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
<ResourceLink name="jdbc/postgres" global="jdbc/postgres" type="javax.sql.DataSource"/>

</Context>


打开工程目录下的web.xml或者tomcat/conf目录下web.xml, 增加以下内容

<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/postgres</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<context-param>
<param-name>liquibase.changelog</param-name>
<param-value>${catalina.base}\conf\db.changelog.xml</param-value>
</context-param>

<context-param>
<param-name>liquibase.datasource</param-name>
<param-value>java:comp/env/jdbc/postgres</param-value>
</context-param>
<context-param>
<param-name>liquibase.contexts</param-name>
<param-value>jdbc/postgres</param-value>
</context-param>
<context-param>
<param-name>liquibase.onerror.fail</param-name>
<param-value>true</param-value>
</context-param>
<listener>
<listener-class>liquibase.integration.servlet.LiquibaseServletListener</listener-class>
</listener>


resource-ref标签,表明是对server.xml中定义的resource的引用。

res-ref-name标签,必须是server.xml中定义的Resource::name。

liquibase.changelog参数,对应的是liquibase的changelog file的位置。在这里,我把该文件部署在tomcat的conf目录中。

liquibase.datasource参数,对应的是数据库的JDNI链接地址。格式是:“java:comp/env/”加上dataresource name -> java:comp/env/jdbc/postgres

liquibase.contexts参数,对应的是liquibase需要的context,对应为context.xml中的resoucelink

创建db.changelog.xml,并部署到tomcat服务器

现在我们可以开始用liquibase管理数据库了。新建一个changelog, 这里,我把文件命名为db.changelog.xml (必须和web.xml中的liquibase.changelog参数一样)

<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.9 http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.9.xsd"> <changeSet id="1" author="LEX">

<createTable tableName="useraccount">
<column name="userid" type="varchar(20)">
<constraints primaryKey="true" />
</column>
<column name="accountstate" type="varchar(255)" />
</createTable>

</changeSet>

</databaseChangeLog>


然后运行tomcat. 我们可以看到liquibase通过changelog更新了postgres数据库



table already exist错误

如果你在使用liquibase更新数据库的时候碰到table already exist的错误提示,那极有可能是因为如下原因:

The table tracks each changeSet as a row, identified by a combination of the “id”, “author” and a “filename” column which stores the path to the changelog file.

Just adding new, more direct info on why the issue happens.

Liquibase DATABASECHANGELOG Documentation

Liquibase uses the DATABASECHANGELOG table to track which changeSets have been ran.

The table tracks each changeSet as a row, identified by a combination of the “id”, “author” and a “filename” column which stores the path to the changelog file.

Please note that the filename column stores the path to the changelog. This may be an absolute path or a relative path depending on how the changelog was passed to Liquibase. For best results, it should be a relative path.

If you move the database, as in the case of migrating, and the filename column contains full paths, Liquibase may not recognize the changeset from the DATABASECHANGELOG table.

如果你遇到错误:

table already exists

就证明其他程序用了同样的文件名(changelog),id名,author名,在同一个数据库上做过liquibase update操作。

解决这个问题的方法,是改变上面3个名字中的一个。或者新建一个数据库!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: