GitLab作为一款流行的DevOps平台,依赖数据库存储项目数据、用户信息及运行状态,正确配置数据库是保障GitLab稳定性与性能的核心步骤,以下为完整配置指南,适用于自托管场景。
GitLab官方支持 PostgreSQL(默认推荐)与 MySQL(仅限特定版本),建议优先选择PostgreSQL以兼容最新功能。
# Ubuntu/Debian sudo apt-get install -y postgresql postgresql-contrib # CentOS/RHEL sudo yum install -y postgresql-server postgresql-contrib
sudo -u postgres psql CREATE USER gitlab WITH PASSWORD 'your_strong_password'; CREATE DATABASE gitlabhq_production OWNER gitlab; q
/etc/gitlab/gitlab.rb
,取消注释并调整以下参数:postgresql['enable'] = false # 若使用外部数据库需禁用内置PostgreSQL gitlab_rails['db_adapter'] = "postgresql" gitlab_rails['db_encoding'] = "unicode" gitlab_rails['db_host'] = "127.0.0.1" gitlab_rails['db_port'] = "5432" gitlab_rails['db_username'] = "gitlab" gitlab_rails['db_password'] = "your_strong_password" gitlab_rails['db_database'] = "gitlabhq_production"
sudo gitlab-ctl reconfigure sudo gitlab-rake db:migrate
pg_hba.conf
中,仅允许GitLab服务器IP:host gitlabhq_production gitlab 192.168.1.100/32 md5
gitlab_rails['db_sslmode'] = "require"
pg_hba.conf
中权限配置 pg_stat_activity
监控活跃连接 shared_buffers
与work_mem
参数优化性能 sudo gitlab-rake gitlab:backup:create