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);維護管理: