gitlab使用多数据库

打印 上一主题 下一主题

主题 972|帖子 972|积分 2916

1. 说明

默认情况下,GitLab 使用一个单一的应用数据库,称为主数据库。为了扩展 GitLab,您可以将 GitLab 配置为使用多个应用数据库。
设置多个数据库后,GitLab 将使用第二个应用数据库用于 CI/CD 功能,称为 CI 数据库。我们并不清除将这两个数据库托管在单个 PostgreSQL 实例上。
主数据库和 CI 数据库中的所有表布局完全雷同。
• 当配置了多个数据库时,ci_pipelines 表会同时存在于主数据库和 CI 数据库中,但 GitLab 只会在 CI 数据库的 ci_pipelines 表中进行读写。
• 同样,projects 表存在于主数据库和 CI 数据库中,但 GitLab 只会在主数据库的 projects 表中进行读写。
• 对于某些表(如 loose_foreign_keys_deleted_records),GitLab 会在主数据库和 CI 数据库中同时进行读写。请参见开辟文档。
2. 配置

主要修改gitlab 的主配置文件(/etc/gitlab/gitlab.rb)
2.1 gitlab开放端口(gitlab 自带的数据库组件)

开放数据库的端口分两部操作
2.1.1 postgresql 的配置

数据库呆板的 IP 为10.100.10.53
  1. postgresql['listen_address'] = "0.0.0.0"
  2. postgresql['port'] = 5432
  3. postgresql['sql_user'] = "gitlab"
  4. postgresql['sql_user_password'] = '0873d907a1dbffa91bae86d744306549' #通过该命令生成 gitlab-ctl pg-password-md5 gitlab
  5. postgresql['md5_auth_cidr_addresses'] = %w(0.0.0.0/0)
  6. postgresql['trust_auth_cidr_addresses'] = %w(127.0.0.1/32)
  7. #postgresql['ssl'] = 'off'
  8. gitlab_rails['auto_migrate'] = false #如果是首次reconfigure可以不要,如果已经首次初始化了,必须要有此配置
复制代码
  1. gitlab-ctl reconfigure
  2. gitlab-ctl restart
  3. postgresql
复制代码
2.1.2 Rails 的配置

  1. gitlab_rails['db_host'] = '10.100.10.53'
  2. gitlab_rails['db_port'] = 5432
  3. gitlab_rails['db_database'] = "gitlabhq_production"
  4. gitlab_rails['db_username'] = "gitlab"
  5. gitlab_rails['db_password'] = "gitlab123"
复制代码
  1. gitlab-ctl reconfigure
复制代码
2.2 gitlab使用外部的数据库

2.2.1 外部的数据库必要的信息

  1. # 创建用户
  2. create user gitlab login password 'gitlab123';
  3. # 创建数据库
  4. create database gitlabhq_production owner=gitlab ENCODING = 'UTF8';
复制代码
  1. # 安装扩展
  2. ## 可以通过 "\dx" 或者 "select extname,extowner,extnamespace,extrelocatable,extversion from pg_extension;" 来查看gitlab自带数据库的扩展
  3. CREATE EXTENSION pg_trgm;
  4. CREATE EXTENSION btree_gist;
复制代码
2.2.2 gitlab主配置文件添加配置

  1. postgresql['enable'] = false
  2. gitlab_rails['db_adapter'] = 'postgresql' #固定配置
  3. gitlab_rails['db_encoding'] = 'utf8'
  4. gitlab_rails['db_database'] = 'gitlabhq_production'
  5. #gitlab_rails['db_pool'] = 10
  6. gitlab_rails['db_username'] = 'gitlab'
  7. gitlab_rails['db_password'] = 'gitlab123'
  8. gitlab_rails['db_host'] = '192.168.1.112'
  9. gitlab_rails['db_port'] = 15432
复制代码
  1. gitlab-ctl reconfigure
复制代码
3. gitlab配置使用多数据库(有ci相关数据的底子上)

主要修改gitlab 的主配置文件(/etc/gitlab/gitlab.rb)
3.1 场景1: main 和 ci 都使用自带的数据库pg

  1. # 开放自带的pg端口
  2. postgresql['listen_address'] = "0.0.0.0"
  3. postgresql['port'] = 5432
  4. postgresql['sql_user'] = "gitlab"
  5. postgresql['sql_user_password'] = '0873d907a1dbffa91bae86d744306549'
  6. postgresql['md5_auth_cidr_addresses'] = %w(0.0.0.0/0)
  7. postgresql['trust_auth_cidr_addresses'] = %w(127.0.0.1/32)
  8. #postgresql['ssl'] = 'off'
  9. gitlab_rails['auto_migrate'] = false
复制代码
  1. gitlab-ctl reconfigure
  2. gitlab-ctl restart
  3. postgresql
复制代码
rails 配置
  1. gitlab_rails['db_host'] = '10.100.10.53'
  2. gitlab_rails['db_port'] = 5432
  3. gitlab_rails['db_database'] = "gitlabhq_production"
  4. gitlab_rails['db_username'] = "gitlab"
  5. gitlab_rails['db_password'] = "gitlab123"
复制代码
  1. gitlab-ctl reconfigure
复制代码
配置CI库
  1. gitlab_rails['env'] = { 'GITLAB_ALLOW_SEPARATE_CI_DATABASE' => 'true' }
  2. gitlab_rails['databases']['ci']['enable'] = true
  3. gitlab_rails['databases']['ci']['db_database'] = 'gitlabhq_production_ci'
  4. gitlab_rails['databases']['ci']['db_host']  = '10.100.10.53'
  5. gitlab_rails['databases']['ci']['db_port'] = 5432
  6. gitlab_rails['databases']['ci']['db_username'] = 'gitlab'
  7. gitlab_rails['databases']['ci']['db_password'] = 'gitlab123'
  8. gitlab_rails['databases']['ci']['db_adapter'] = 'postgresql'
  9. gitlab_rails['databases']['ci']['db_encoding'] = 'UTF8'
复制代码
  1. gitlab-ctl reconfigure
复制代码
<可选> 迁徙表
  1. sudo gitlab-ctl start postgresql
  2. sudo -u gitlab-psql /opt/gitlab/embedded/bin/psql -h /var/opt/gitlab/postgresql -d template1 -c "CREATE DATABASE gitlabhq_production_ci OWNER gitlab;"
  3. sudo gitlab-rake db:schema:load:ci
复制代码
对 main 数据库中的 ci 表进行锁定写入,反之亦然
  1. sudo gitlab-ctl start postgresql
  2. sudo gitlab-rake gitlab:db:lock_writes
复制代码
  1. sudo gitlab-ctl restart
复制代码
  说明:
如果现有的 gitlabhq_production 已经有了ci的相关数据,那么就通过 制止puma/rails 服务,然后备份 gitlabhq_production 库,在将备份的sql写入到 gitlabhq_production_ci 库,确保 多数据库配置好之后,ci相关数据走 gitlabhq_production_ci 库后有数据库,下面有操作。
  3.2 场景2: main 和 ci 都使用外部的数据库pg

配置main库
  1. # 不启用gitlab自带的数据库
  2. postgresql['enable'] = false
  3. # main database
  4. gitlab_rails['db_database'] = 'gitlabhq_production'
  5. gitlab_rails['db_host'] = '192.168.1.112'
  6. gitlab_rails['db_port'] = 15432
  7. gitlab_rails['db_username'] = 'gitlab'
  8. gitlab_rails['db_password'] = 'gitlab123'
  9. gitlab_rails['db_adapter'] = 'postgresql'
  10. gitlab_rails['db_encoding'] = 'utf8'
  11. gitlab_rails['auto_migrate']= true
复制代码
  1. gitlab-ctl reconfigure
复制代码
配置CI库
  1. # ci database
  2. gitlab_rails['env'] = { 'GITLAB_ALLOW_SEPARATE_CI_DATABASE' => 'true' }
  3. gitlab_rails['databases']['ci']['enable'] = true
  4. gitlab_rails['databases']['ci']['db_database'] = 'gitlabhq_production_ci'
  5. gitlab_rails['databases']['ci']['db_host']  = '192.168.1.112'
  6. gitlab_rails['databases']['ci']['db_port'] = 15432
  7. gitlab_rails['databases']['ci']['db_username'] = 'gitlab'
  8. gitlab_rails['databases']['ci']['db_password'] = 'gitlab123'
  9. gitlab_rails['databases']['ci']['db_adapter'] = 'postgresql'
  10. gitlab_rails['databases']['ci']['db_encoding'] = 'utf8'
复制代码
  说明:
外部的数据库配置好上面的操作后,实行 gitlab-ctl reconfigure





会报错,但是 /var/opt/gitlab/gitlab-rails/etc/database.yml 实际已经渲染了配置,由于为了能实现 gitlab-ctl reconfigure





,可以通过备份恢复的方式将 gitlabhq_production 导入到 gitlabhq_production_ci 中 ,同时如许的好处就是 对于gitlabhq_production已经有了ci 相关的数据,导致有了ci库之后,走gitlabhq_production_ci 避免看不到之前ci相关的数据
  1. # 备份 gitlabhq_production
  2. /opt/gitlab/embedded/bin/pg_dump -h 10.100.10.53 -p 5432 -U gitlab -d gitlabhq_production -f database.sql
  3. # 恢复到 gitlabhq_production_ci
  4. /opt/gitlab/embedded/bin/psql -h 10.100.10.53 -p 5432 -U gitlab -d gitlabhq_production_ci -f database.sql
复制代码
  1. gitlab-ctl reconfigure
  2. # 重启服务gitlab-ctl restart
复制代码
4. 测试效果


5. gitlab配置使用多数据库(无ci相关数据的底子上)

同上述的配置方式,只是在reconfigure前添加环境变量,如
5.1 制止服务

  1. gitlab-ctl stop
复制代码
5.2 添加配置如下(/etc/gitlab/gitlab.rb)

  1. gitlab_rails['env'] = { 'GITLAB_ALLOW_SEPARATE_CI_DATABASE' => 'true' }
  2. gitlab_rails['databases']['ci']['enable'] = true
  3. gitlab_rails['databases']['ci']['db_database'] = 'gitlabhq_production_ci'
复制代码
  1. gitlab-ctl reconfigure
复制代码
5.3 ci库初始化

   重新配置极狐GitLab 应该会创建 gitlabhq_production_ci。如果没有,请手动创建 gitlabhq_production_ci。
  1. sudo gitlab-ctl start postgresql
  2. sudo -u gitlab-psql /opt/gitlab/embedded/bin/psql -h /var/opt/gitlab/postgresql -d template1 -c "CREATE DATABASE gitlabhq_production_ci OWNER gitlab;"
  3. export DISABLE_DATABASE_ENVIRONMENT_CHECK=1 && gitlab-rake db:schema:load:ci
复制代码
5.4 对 main 数据库中的 ci 表进行锁定写入,反之亦然

  1. gitlab-ctl start postgresql
  2. gitlab-rake gitlab:db:lock_writes
复制代码
5.5 重启GitLab

  1. gitlab-ctl restart
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

灌篮少年

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表