Liquibase----SQL格式通过update更新MySQL数据库
作者:redrose2100   类别:    日期:2022-05-09 02:15:04    阅读:1224 次   消耗积分:0 分

1 文件准备

因为操作MySQL数据库,因此,这里只需要将liquibase安装目录中的example文件中的sql文件夹拷贝出来即可,如:D:\ProgrameFile\liquibase-4.9.1\examples\sql

2 修改文件名

sql文件夹中只保留changelog.sql文件和liquibase.properties文件,其中changelog.sql由example-changelog.sql重命名而来

3 数据库准备

首先在要操作的MySQL数据库中创建demo数据库

4 编辑配置liquibase.properties文件

  1. # Enter the path for your changelog file.
  2. changeLogFile=changelog.sql
  3. #### Enter the Target database 'url' information ####
  4. liquibase.command.url=jdbc:mysql://xx.xx.xx.xx:3306/demo
  5. # Enter the username for your Target database.
  6. liquibase.command.username: root
  7. # Enter the password for your Target database.
  8. liquibase.command.password: xxxxxxxxx

5 编辑changelog.sql文件

如下,创建两个表person和company,然后向person表增加一列country

  1. --liquibase formatted sql
  2. --changeset redrose2100:1
  3. --comment: create table person
  4. create table person (
  5. id int primary key auto_increment not null,
  6. name varchar(50) not null,
  7. address1 varchar(50),
  8. address2 varchar(50),
  9. city varchar(30)
  10. )
  11. --rollback DROP TABLE person;
  12. --changeset redrose2100:2
  13. --comment: create table company
  14. create table company (
  15. id int primary key auto_increment not null,
  16. name varchar(50) not null,
  17. address1 varchar(50),
  18. address2 varchar(50),
  19. city varchar(30)
  20. )
  21. --rollback DROP TABLE company;
  22. --changeset redrose2100:3
  23. --comment: add column country to table person
  24. alter table person add column country varchar(2)
  25. --rollback ALTER TABLE person DROP COLUMN country;

6 打开cmd并进入到sql文件夹下

执行如下命令

  1. liquibase update

执行结果如下:

  1. D:\src\sql>liquibase update
  2. ####################################################
  3. ## _ _ _ _ ##
  4. ## | | (_) (_) | ##
  5. ## | | _ __ _ _ _ _| |__ __ _ ___ ___ ##
  6. ## | | | |/ _` | | | | | '_ \ / _` / __|/ _ \ ##
  7. ## | |___| | (_| | |_| | | |_) | (_| \__ \ __/ ##
  8. ## \_____/_|\__, |\__,_|_|_.__/ \__,_|___/\___| ##
  9. ## | | ##
  10. ## |_| ##
  11. ## ##
  12. ## Get documentation at docs.liquibase.com ##
  13. ## Get certified courses at learn.liquibase.com ##
  14. ## Free schema change activity reports at ##
  15. ## https://hub.liquibase.com ##
  16. ## ##
  17. ####################################################
  18. Starting Liquibase at 09:59:17 (version 4.9.1 #1978 built at 2022-03-28 19:39+0000)
  19. Liquibase Version: 4.9.1
  20. Liquibase Community 4.9.1 by Liquibase
  21. Do you want to see this operation's report in Liquibase Hub, which improves team collaboration?
  22. If so, enter your email. If not, enter [N] to no longer be prompted, or [S] to skip for now, but ask again next time [S]:
  23. N
  24. No operations will be reported. Simply add a liquibase.hub.apiKey setting to generate free deployment reports. Learn more at https://hub.liquibase.com
  25. * Updated properties file liquibase.properties to set liquibase.hub.mode=off
  26. Running Changeset: changelog.sql::1::redrose2100
  27. Running Changeset: changelog.sql::2::redrose2100
  28. Running Changeset: changelog.sql::3::redrose2100
  29. Liquibase command 'update' was executed successfully.
  30. D:\src\sql>

7 进入数据库查看

如下,可以查看到对应的数据库已经创建成功,person表中也已经存在country列

  1. mysql> use demo;
  2. Reading table information for completion of table and column names
  3. You can turn off this feature to get a quicker startup with -A
  4. Database changed
  5. mysql> show tables;
  6. +-----------------------+
  7. | Tables_in_demo |
  8. +-----------------------+
  9. | DATABASECHANGELOG |
  10. | DATABASECHANGELOGLOCK |
  11. | company |
  12. | person |
  13. +-----------------------+
  14. 4 rows in set (0.00 sec)
  15. mysql>mysql> desc person;
  16. +----------+-------------+------+-----+---------+----------------+
  17. | Field | Type | Null | Key | Default | Extra |
  18. +----------+-------------+------+-----+---------+----------------+
  19. | id | int(11) | NO | PRI | NULL | auto_increment |
  20. | name | varchar(50) | NO | | NULL | |
  21. | address1 | varchar(50) | YES | | NULL | |
  22. | address2 | varchar(50) | YES | | NULL | |
  23. | city | varchar(30) | YES | | NULL | |
  24. | country | varchar(2) | YES | | NULL | |
  25. +----------+-------------+------+-----+---------+----------------+
  26. 6 rows in set (0.00 sec)

8 通过命令行方式

这里可以不使用liquibase.properties文件,可以通过使用命令行参数来指定配置,如下:

  1. liquibase update --changelog-file=changelog.sql --url=jdbc:mysql://xx.xx.xx.xx:3306/demo --username=root --password=xxxxxxxx

如此时将changelog.sql文件增加以下修改内容,即向person表增加age一列

  1. --changeset redrose2100:4
  2. --comment: add column age to table person
  3. alter table person add column age int(3)
  4. --rollback ALTER TABLE person DROP COLUMN age;

执行回显如下:

  1. D:\src\sql>liquibase update --changelog-file=changelog.sql --url=jdbc:mysql://xx.xx.xx.xx:3306/demo --username=root --password=xxxxxxxx
  2. ####################################################
  3. ## _ _ _ _ ##
  4. ## | | (_) (_) | ##
  5. ## | | _ __ _ _ _ _| |__ __ _ ___ ___ ##
  6. ## | | | |/ _` | | | | | '_ \ / _` / __|/ _ \ ##
  7. ## | |___| | (_| | |_| | | |_) | (_| \__ \ __/ ##
  8. ## \_____/_|\__, |\__,_|_|_.__/ \__,_|___/\___| ##
  9. ## | | ##
  10. ## |_| ##
  11. ## ##
  12. ## Get documentation at docs.liquibase.com ##
  13. ## Get certified courses at learn.liquibase.com ##
  14. ## Free schema change activity reports at ##
  15. ## https://hub.liquibase.com ##
  16. ## ##
  17. ####################################################
  18. Starting Liquibase at 10:10:48 (version 4.9.1 #1978 built at 2022-03-28 19:39+0000)
  19. Liquibase Version: 4.9.1
  20. Liquibase Community 4.9.1 by Liquibase
  21. Do you want to see this operation's report in Liquibase Hub, which improves team collaboration?
  22. If so, enter your email. If not, enter [N] to no longer be prompted, or [S] to skip for now, but ask again next time [S]:
  23. N
  24. No operations will be reported. Simply add a liquibase.hub.apiKey setting to generate free deployment reports. Learn more at https://hub.liquibase.com
  25. * Updated properties file liquibase.properties to set liquibase.hub.mode=off
  26. Running Changeset: changelog.sql::4::redrose2100
  27. Liquibase command 'update' was executed successfully.
  28. D:\src\sql>

查看数据库中person表,已经存在age字段了

  1. mysql> desc person;
  2. +----------+-------------+------+-----+---------+----------------+
  3. | Field | Type | Null | Key | Default | Extra |
  4. +----------+-------------+------+-----+---------+----------------+
  5. | id | int(11) | NO | PRI | NULL | auto_increment |
  6. | name | varchar(50) | NO | | NULL | |
  7. | address1 | varchar(50) | YES | | NULL | |
  8. | address2 | varchar(50) | YES | | NULL | |
  9. | city | varchar(30) | YES | | NULL | |
  10. | country | varchar(2) | YES | | NULL | |
  11. | age | int(3) | YES | | NULL | |
  12. +----------+-------------+------+-----+---------+----------------+
  13. 7 rows in set (0.00 sec)
  14. mysql>

可能遇到的问题

  • 出现如下错误,因为没安装数据库驱动
    如下表示liquibase缺少mysql驱动
    1. Unexpected error running Liquibase: java.lang.RuntimeException: Cannot find database driver: com.mysql.jdbc.Driver

解决办法:
下载 mysql-connector-java-5.1.36.jar 包,具体版本根据数据库版本选择,然后放入liquibase的安装目录,这里如:D:\ProgrameFile\liquibase-4.9.1\lib

始终坚持开源开放共享精神,同时感谢您的充电鼓励和支持!
版权所有,转载本站文章请注明出处:redrose2100, http://blog.redrose2100.com/article/119
个人成就
  • 2022年 : 371 篇 
  • 2023年 : 211 篇 
  • 2024年 : 31 篇 
  • 2025年 : 0 篇 
  • 博客总数: 613 
  • 阅读总量: 668891 
测试开发技术全栈公众号
DevOps技术交流微信群