Debian GitLab数据库选择与配置指南
GitLab在Debian系统上的数据库选择需结合性能、稳定性及功能需求,主要有两种推荐方案:
PostgreSQL是GitLab的默认推荐数据库,以下是详细配置流程:
更新系统包并安装PostgreSQL及扩展组件:
sudo apt update
sudo apt install postgresql postgresql-contrib
切换至postgres用户,通过psql命令行工具创建数据库和用户,并授予权限:
sudo -u postgres psql
# 在psql中执行以下命令
CREATE DATABASE gitlabhq_production; # GitLab默认数据库名
CREATE USER gitlab WITH PASSWORD 'your_strong_password'; # 创建专用用户
GRANT ALL PRIVILEGES ON DATABASE gitlabhq_production TO gitlab; # 授权
\q # 退出psql
编辑PostgreSQL配置文件(路径:/etc/postgresql/<版本>/main/postgresql.conf),调整以下关键参数以提升性能:
max_connections = 100 # 根据并发用户数调整(推荐为并发数的2倍)
shared_buffers = 4GB # 占系统内存的25%-40%(如8GB内存设为2-3GB)
work_mem = 64MB # 提升复杂查询性能(如排序、聚合)
maintenance_work_mem = 256MB # 加速索引创建、表优化等操作
修改后重启PostgreSQL服务:
sudo systemctl restart postgresql
编辑GitLab主配置文件(/etc/gitlab/gitlab.rb),添加以下数据库配置:
gitlab_rails['db_adapter'] = 'postgresql' # 固定为postgresql
gitlab_rails['db_host'] = 'localhost' # 数据库服务器地址(本地为localhost)
gitlab_rails['db_port'] = 5432 # PostgreSQL默认端口
gitlab_rails['db_database'] = 'gitlabhq_production' # 数据库名
gitlab_rails['db_username'] = 'gitlab' # 数据库用户名
gitlab_rails['db_password'] = 'your_strong_password' # 数据库密码
保存配置文件后,执行以下命令使配置生效并重启GitLab服务:
sudo gitlab-ctl reconfigure # 应用配置
sudo gitlab-ctl restart # 重启GitLab
访问GitLab Web界面(如http://your_server_ip),若能正常登录并使用,则说明数据库连接成功。
若更熟悉MySQL生态,可按以下步骤配置:
更新系统包并安装MySQL Server:
sudo apt update
sudo apt install mysql-server
安装过程中会提示设置root密码,需牢记。
GitLab要求数据库使用utf8mb4字符集(支持emoji等特殊字符),需修改MySQL配置文件(/etc/mysql/mysql.conf.d/mysqld.cnf):
[mysqld]
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
[client]
default-character-set = utf8mb4
重启MySQL服务使配置生效:
sudo systemctl restart mysql
登录MySQL,执行以下SQL命令:
CREATE DATABASE gitlabhq_production CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'gitlab'@'localhost' IDENTIFIED BY 'your_strong_password';
GRANT ALL PRIVILEGES ON gitlabhq_production.* TO 'gitlab'@'localhost';
FLUSH PRIVILEGES;
EXIT;
编辑GitLab主配置文件(/etc/gitlab/gitlab.rb),添加以下数据库配置:
gitlab_rails['db_adapter'] = 'mysql2' # 必须为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'] = 'your_strong_password'
gitlab_rails['db_host'] = 'localhost'
执行以下命令应用配置并重启GitLab:
sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart
访问GitLab Web界面,确认能正常登录,即表示数据库配置成功。
安全性:
性能优化:
max_connections、shared_buffers);维护管理: