启动容器

首先确保 mysql 容器已经启动,详见 开发环境搭建

介绍

本小节介绍了如何初始化Choerodon 的数据库。

创建Mysql数据库

1.查看容器,确认存在容器名为mysql的容器。

2.用choerodon用户命令行登陆 mysql 容器,密码为 123456

  1. $ docker exec -ti mysql mysql -u choerodon -p

3.创建用户和数据库。

  1. /** init_user.sql */
  2. CREATE USER 'choerodon'@'%' IDENTIFIED BY "123456";
  3. CREATE DATABASE IF NOT EXISTS base_service DEFAULT CHARACTER SET utf8mb4;
  4. CREATE DATABASE IF NOT EXISTS manager_service DEFAULT CHARACTER SET utf8mb4;
  5. CREATE DATABASE IF NOT EXISTS asgard_service DEFAULT CHARACTER SET utf8mb4;
  6. CREATE DATABASE IF NOT EXISTS notify_service DEFAULT CHARACTER SET utf8mb4;
  7. GRANT ALL PRIVILEGES ON base_service.* TO choerodon@'%';\
  8. GRANT ALL PRIVILEGES ON manager_service.* TO choerodon@'%';\
  9. GRANT ALL PRIVILEGES ON asgard_service.* TO choerodon@'%';\
  10. GRANT ALL PRIVILEGES ON notify_service.* TO choerodon@'%';\
  11. FLUSH PRIVILEGES;

4.查看用户与数据库。

  1. mysql> select User from mysql.user;
  2. +---------------+
  3. | User |
  4. +---------------+
  5. | choerodon |
  6. | mysql.session |
  7. | mysql.sys |
  8. | root |
  9. +---------------+
  10. 4 rows in set (0.00 sec)
  11. mysql> show databases;
  12. +--------------------+
  13. | Database |
  14. +--------------------+
  15. | information_schema |
  16. | asgard_service |
  17. | base_service |
  18. | manager_service |
  19. | mysql |
  20. | notify_service |
  21. | performance_schema |
  22. | sys |
  23. | todo_service |
  24. +--------------------+
  25. 9 rows in set (0.00 sec)

初始化数据库

需初始化manager-servicebase_service两个数据库。

1.新建初始化数据库临时目录,并创建初始化脚本。

  1. $ mkdir -p tmp
  2. $ cd tmp
  3. $ touch init-local-database.sh

2.修改初始化脚本。

  1. #!/bin/bash
  2. # get manager-service
  3. git clone https://github.com/choerodon/manager-service.git manager-service
  4. mkdir -p manager/script
  5. cp -r ./manager-service/src/main/resources/script/db ./manager/script
  6. rm -rf ./manager-service
  7. # get base-service
  8. git clone https://github.com/choerodon/base_service.git base_service
  9. mkdir -p base/script
  10. cp -r ./base-service/src/main/resources/script/db ./base/script
  11. rm -rf ./base-service
  12. # get choerodon-tool-liquibase
  13. MAVEN_LOCAL_REPO=$(cd / && mvn help:evaluate -Dexpression=settings.localRepository -q -DforceStdout)
  14. TOOL_GROUP_ID=io.choerodon
  15. TOOL_ARTIFACT_ID=choerodon-tool-liquibase
  16. TOOL_VERSION=${1:-0.11.0.RELEASE}
  17. TOOL_JAR_PATH=${MAVEN_LOCAL_REPO}/${TOOL_GROUP_ID/\./\/}/${TOOL_ARTIFACT_ID}/${TOOL_VERSION}/${TOOL_ARTIFACT_ID}-${TOOL_VERSION}.jar
  18. mvn org.apache.maven.plugins:maven-dependency-plugin:get \
  19. -Dartifact=${TOOL_GROUP_ID}:${TOOL_ARTIFACT_ID}:${TOOL_VERSION} \
  20. -Dtransitive=false
  21. # init manager-service
  22. java -Dspring.datasource.url="jdbc:mysql://localhost:3306/manager_service?useUnicode=true&characterEncoding=utf-8&useSSL=false&useInformationSchema=true&remarks=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai" \
  23. -Dspring.datasource.username=choerodon \
  24. -Dspring.datasource.password=123456 \
  25. -Ddata.drop=false -Ddata.init=true \
  26. -Ddata.dir=./manager \
  27. -jar ${TOOL_JAR_PATH}
  28. # init base-service
  29. java -Dspring.datasource.url="jdbc:mysql://localhost:3306/base_service?useUnicode=true&characterEncoding=utf-8&useSSL=false&useInformationSchema=true&remarks=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai" \
  30. -Dspring.datasource.username=choerodon \
  31. -Dspring.datasource.password=123456 \
  32. -Ddata.drop=false -Ddata.init=true \
  33. -Ddata.dir=./base \
  34. -jar ${TOOL_JAR_PATH}

3.运行脚本。

  1. $ sh init-local-database.sh [version] #如未指定version,则默认使用tool version为0.11.0.

4.命令执行成功之后,刷新数据库,会出现初始化脚本中的表以及初始化数据。