启用 GoCD 使用 MySQL
注意:MySQL 的支持已在 GoCD 中添加
20.5.0
,尽管已经完成了一轮基本的迁移测试,但功能测试套件并未定期在构建管道中针对 MySQL 运行。这是在迁移到 MySQL 时需要注意的一点。H2 和 PostgreSQL 在 GoCD 的构建管道中经过了彻底测试。
第 1 步:安装 MySQL 服务器
要将 MySQL 数据库与 GoCD 一起使用,需要一个外部 MySQL 数据库服务器来托管 GoCD 服务器的数据库。请参阅MySQL 安装文档,以根据您的环境安装最新版本的 MySQL 数据库服务器。GoCD 支持 MySQL 8.0 版本。
注意:GoCD 需要支持大小写不敏感的标识符,在类 Unix 系统上至少需要执行此操作在安装 MySQL 之前!以下来自 MySQL 文档的页面可能有用:
第 2 步:创建一个空数据库
启动 MySQL 服务器后,可以使用命令行中的mysql
或mysqladmin
工具(MySQL 提供)创建一个空数据库。请参阅创建数据库文档以设置数据库。
CREATE DATABASE gocd;
CREATE USER 'gocd_user'@'localhost' IDENTIFIED BY 'password';
GRANT ALL ON gocd.* TO 'gocd_user'@'localhost';
GRANT SUPER ON *.* TO 'gocd_user'@'localhost';
注意:您需要首次添加SUPER
权限,因为会创建触发器。MySQL 不允许在没有 SUPER 权限的情况下创建该触发器,并且如果未提供权限,将报错误 1419。首次启动后可以撤销此权限。
第 3 步:配置 GoCD 与 MySQL 连接详细信息
需要在 GoCD 的配置目录中创建一个名为db.properties
的属性文件(config/
)。GoCD 的配置目录位置因操作系统而异。请参考GoCD 服务器安装文档以了解 GoCD 服务器配置目录的位置。
该文件应包含有关数据库服务器的信息,以便 GoCD 服务器可以连接到它。
查看GoCD 数据库配置属性以了解可以在db.properties
.
连接到 MySQL 数据库的示例属性文件:
db.driver=com.mysql.cj.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/gocd
db.user=gocd_user
db.password=password
第4步:启动 GoCD 服务器
查看管理GoCD服务器进程以启动您的 GoCD 服务器。
排查问题
MySQL:标识符大小写敏感性
如果您正在使用 MySQL,则可能会在 GoCD 服务器日志中看到类似以下的消息:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.commons.dbcp2.BasicDataSource]: Factory method 'getDataSource' threw exception; nested exception is java.sql.SQLException: Unable to migrate the database
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
... 73 common frames omitted
Caused by: java.sql.SQLException: Unable to migrate the database
at com.thoughtworks.go.server.database.migration.DatabaseMigrator.migrate(DatabaseMigrator.java:68)
at com.thoughtworks.go.server.database.Database.getDataSource(Database.java:63)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
... 74 common frames omitted
Caused by: liquibase.exception.MigrationFailedException: Migration failed for change set db-migration-scripts/initial/create-trigger.xml::107::gocd(generated):
Reason: liquibase.exception.DatabaseException: Table 'gocd.buildStateTransitions' doesn't exist [Failed SQL: (1146) CREATE TRIGGER lastTransitionedTimeUpdate
AFTER INSERT ON buildStateTransitions
FOR EACH ROW
BEGIN
UPDATE stages SET lastTransitionedTime = NEW.statechangetime WHERE stages.id = NEW.stageid;
END]
at liquibase.changelog.ChangeSet.execute(ChangeSet.java:646)
at liquibase.changelog.visitor.UpdateVisitor.visit(UpdateVisitor.java:53)
at liquibase.changelog.ChangeLogIterator.run(ChangeLogIterator.java:83)
at liquibase.Liquibase.update(Liquibase.java:202)
at liquibase.Liquibase.update(Liquibase.java:179)
at liquibase.Liquibase.update(Liquibase.java:175)
at com.thoughtworks.go.server.database.migration.DatabaseMigrator.migrate(DatabaseMigrator.java:54)
... 80 common frames omitted
Caused by: liquibase.exception.DatabaseException: Table 'gocd.buildStateTransitions' doesn't exist [Failed SQL: (1146) CREATE TRIGGER lastTransitionedTimeUpdate
AFTER INSERT ON buildStateTransitions
FOR EACH ROW
BEGIN
UPDATE stages SET lastTransitionedTime = NEW.statechangetime WHERE stages.id = NEW.stageid;
END]
at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:402)
at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:59)
at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:131)
at liquibase.database.AbstractJdbcDatabase.execute(AbstractJdbcDatabase.java:1276)
at liquibase.database.AbstractJdbcDatabase.executeStatements(AbstractJdbcDatabase.java:1258)
at liquibase.changelog.ChangeSet.execute(ChangeSet.java:609)
... 86 common frames omitted
Caused by: java.sql.SQLSyntaxErrorException: Table 'gocd.buildStateTransitions' doesn't exist
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.StatementImpl.executeInternal(StatementImpl.java:764)
at com.mysql.cj.jdbc.StatementImpl.execute(StatementImpl.java:648)
at org.apache.commons.dbcp2.DelegatingStatement.execute(DelegatingStatement.java:194)
at org.apache.commons.dbcp2.DelegatingStatement.execute(DelegatingStatement.java:194)
at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:398)
... 91 common frames omitted
如果您看到此消息,最可能的原因是您的 MySQL 实例启用了大小写敏感标识符。GoCD 需要大小写不敏感的标识符,您需要更改 MySQL 实例以启用这一点。请注意,根据文档,一旦初始化了 MySQL 实例,就无法更改该lower_case_table_names
变量。您可能需要重新创建实例。