首页主机资讯Ubuntu GitLab的数据库如何配置

Ubuntu GitLab的数据库如何配置

时间2026-01-21 16:37:06发布访客分类主机资讯浏览972
导读:Ubuntu 上 GitLab 数据库配置指南 一 推荐方案 使用内置 PostgreSQL 安装与启用数据库 安装 PostgreSQL 与 contrib 包: sudo apt update sudo apt install -...

Ubuntu 上 GitLab 数据库配置指南

一 推荐方案 使用内置 PostgreSQL

  • 安装与启用数据库
    • 安装 PostgreSQL 与 contrib 包:
      • sudo apt update
      • sudo apt install -y postgresql postgresql-contrib
    • 创建 GitLab 专用数据库用户与库(本地 Unix 域套接字认证):
      • sudo -u postgres createuser --pwprompt gitlab
      • sudo -u postgres createdb --owner=gitlab gitlabhq_production
  • 配置 GitLab 使用本地 PostgreSQL
    • 编辑 /etc/gitlab/gitlab.rb:
      • gitlab_rails[‘db_adapter’] = ‘postgresql’
      • gitlab_rails[‘db_encoding’] = ‘unicode’
      • gitlab_rails[‘db_database’] = ‘gitlabhq_production’
      • gitlab_rails[‘db_username’] = ‘gitlab’
      • gitlab_rails[‘db_password’] = ‘刚才设置的密码’
      • gitlab_rails[‘db_host’] = ‘127.0.0.1’
      • gitlab_rails[‘db_port’] = ‘5432’
    • 使配置生效:
      • sudo gitlab-ctl reconfigure
      • sudo gitlab-ctl restart
  • 说明
    • Omnibus 包默认自带并启用 PostgreSQL,上述做法与其默认策略一致,维护成本更低、兼容性更好。

二 使用外部 PostgreSQL

  • 在外部 PostgreSQL 服务器上准备库与账户(示例):
    • CREATE USER gitlab WITH PASSWORD ‘StrongPass!’;
    • CREATE DATABASE gitlabhq_production OWNER gitlab ENCODING ‘UTF8’ LC_COLLATE=‘en_US.UTF-8’ LC_CTYPE=‘en_US.UTF-8’;
    • GRANT ALL PRIVILEGES ON DATABASE gitlabhq_production TO gitlab;
  • 调整 /etc/gitlab/gitlab.rb(按需替换主机与端口):
    • gitlab_rails[‘db_adapter’] = ‘postgresql’
    • gitlab_rails[‘db_database’] = ‘gitlabhq_production’
    • gitlab_rails[‘db_username’] = ‘gitlab’
    • gitlab_rails[‘db_password’] = ‘StrongPass!’
    • gitlab_rails[‘db_host’] = ‘10.0.0.10’ # 外部数据库地址
    • gitlab_rails[‘db_port’] = ‘5432’
    • 如启用 SSL:gitlab_rails[‘db_sslmode’] = ‘require’
  • 使配置生效并重启:
    • sudo gitlab-ctl reconfigure
    • sudo gitlab-ctl restart
  • 网络与防火墙
    • 开放数据库服务器 5432/TCP,仅允许 GitLab 主机访问(例如通过 UFW 或云安全组策略)。

三 使用 MySQL MariaDB 的可选方案

  • 重要提示
    • GitLab 官方长期以 PostgreSQL 为主,MySQL/MariaDB 的支持在社区教程中常见,但在生产环境建议优先使用 PostgreSQL;如使用 MySQL,请充分测试并确认版本与驱动兼容。
  • 安装与准备数据库
    • 安装 MySQL/MariaDB 并安全初始化:
      • sudo apt install -y mysql-server
      • sudo mysql_secure_installation
    • 创建库与用户(使用 utf8mb4 以完整支持 Unicode):
      • CREATE DATABASE gitlabhq_production CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
      • CREATE USER ‘gitlab’@‘localhost’ IDENTIFIED BY ‘StrongPass!’;
      • GRANT ALL PRIVILEGES ON gitlabhq_production.* TO ‘gitlab’@‘localhost’;
      • FLUSH PRIVILEGES;
  • 配置 GitLab 使用 MySQL
    • 编辑 /etc/gitlab/gitlab.rb:
      • gitlab_rails[‘db_adapter’] = ‘mysql2’
      • gitlab_rails[‘db_encoding’] = ‘utf8mb4’
      • gitlab_rails[‘db_collation’] = ‘utf8mb4_unicode_ci’
      • gitlab_rails[‘db_database’] = ‘gitlabhq_production’
      • gitlab_rails[‘db_username’] = ‘gitlab’
      • gitlab_rails[‘db_password’] = ‘StrongPass!’
      • gitlab_rails[‘db_host’] = ‘localhost’
    • 使配置生效并重启:
      • sudo gitlab-ctl reconfigure
      • sudo gitlab-ctl restart
  • 说明
    • 需确保已安装 mysql2 驱动(Omnibus 通常已包含);字符集务必使用 utf8mb4,避免 emoji 等字符存储异常。

四 验证与常见问题

  • 验证数据库连接与应用状态
    • 查看服务状态:sudo gitlab-ctl status
    • 查看日志定位数据库问题:sudo gitlab-ctl tail
    • 进入 Rails 控制台检查数据库连接:sudo gitlab-rails console production
      • ActiveRecord::Base.connection.current_database
      • ActiveRecord::Base.connection.tables
  • 常见问题速查
    • 配置修改未生效:执行 sudo gitlab-ctl reconfigure
    • 数据库连接失败:核对用户名/密码/host/端口,确认数据库服务运行且防火墙放行
    • 字符集异常:确保库与连接均使用 utf8mb4/utf8mb4_unicode_ci
    • 外部数据库访问慢或不通:检查网络延迟、连接池与 SSL 配置

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: Ubuntu GitLab的数据库如何配置
本文地址: https://pptw.com/jishu/788827.html
Ubuntu上Zookeeper的Java环境如何配置 Ubuntu GitLab的API如何使用

游客 回复需填写必要信息