如何将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错误
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%;
c:\apache-tomcat-8.0.28\lib\postgresql-9.3-1104-jdbc4.jar
首先,需要在server.xml里面配置postgresql数据库的连接池。
- 打开c:\apache-tomcat-8.0.28\conf\server.xml,找到GlobalNamingResources标签,在该标签下添加Resouce
resource中的Name,代表的是这个resource在JDNI中的资源位置。其他的标签则用于表明服务器如何建立jdbc链接时的参数
–打开c:\apache-tomcat-8.0.28\conf\context.xml,添加resourceLink
打开工程目录下的web.xml或者tomcat/conf目录下web.xml, 增加以下内容
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
然后运行tomcat. 我们可以看到liquibase通过changelog更新了postgres数据库
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个名字中的一个。或者新建一个数据库!
安装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个名字中的一个。或者新建一个数据库!
相关文章推荐
- Nginx代理Tomcat
- Apache+mod_jk+Tomcat负载均衡服务器配置
- MyEclipse通过TomCat创建一个简单的Web后台供Android使用
- Windows下配置TOMCAT的FIPS模式
- Eclipse部署Maven web项目到tomcat服务器时,没有将lib下的jar复制过去的解决办法
- jvm实例,tomcat容器,spring容器,在内存中的关系
- MyEclipse + Tomcat 热部署问题
- Myeclipse中tomcat所应用的JDK设置
- Tomcat和Solr的整合
- An internal error occurred during: "Launching ZMS on MyEclipse Tomcat". java.lang.NullPointerExcepti
- android客户端访问服务端tomcat
- Could not publish server configuration for Tomcat v6.0 Server at localhost
- java;jsp;tomcat;mysql;hibernate;编码中文乱码全面解决方案
- CentOS 6.3安装配置Tomcat-7
- 服务器端tomcat,debug调试
- Tomcat项目编译路径下的classes文件夹为空
- tomcat源码阅读(二) Digester方法详解
- tomcat源码阅读(一) 入口
- eclipse+tomcat+jvm,刚刚参加工作的一些简单问题
- 利用awstats分析tomcat日志