插件窝 干货文章 CentOS 7.x安装配置openGauss数据库

CentOS 7.x安装配置openGauss数据库

openGauss bash opt CentOS 816    来源:    2025-05-12

CentOS 7.x 安装配置 openGauss 数据库指南

一、准备工作

1. 系统要求

  • 操作系统:CentOS 7.6 或更高版本
  • 内存:至少 4GB(生产环境建议 8GB 以上)
  • 磁盘空间:至少 10GB 可用空间
  • 用户:使用 root 用户或具有 sudo 权限的用户

2. 关闭防火墙和SELinux(测试环境)

systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

3. 安装依赖包

yum install -y bzip2 net-tools python3 python3-devel gcc make \
               libaio-devel flex bison ncurses-devel glibc-devel \
               patch redhat-lsb-core readline-devel

二、下载 openGauss

1. 下载社区版

wget https://opengauss.obs.cn-south-1.myhuaweicloud.com/3.0.0/x86/openGauss-3.0.0-CentOS-64bit.tar.bz2

2. 解压安装包

tar -xjf openGauss-3.0.0-CentOS-64bit.tar.bz2 -C /opt

三、创建用户和组

1. 创建用户组和用户

groupadd dbgrp
useradd -g dbgrp omm
passwd omm  # 设置密码

2. 创建数据目录并授权

mkdir -p /opt/opengauss/data
chown -R omm:dbgrp /opt/opengauss
chmod -R 700 /opt/opengauss/data

四、安装 openGauss

1. 切换到 omm 用户

su - omm

2. 执行安装脚本

cd /opt/openGauss-3.0.0-CentOS-64bit
python3 setup.py -U omm -D /opt/opengauss/data -R /opt/opengauss/data

五、配置 openGauss

1. 修改配置文件

编辑 /opt/opengauss/data/postgresql.conf

listen_addresses = '*'  # 允许远程连接
port = 5432            # 默认端口
password_encryption_type = 0  # 使用md5加密

编辑 /opt/opengauss/data/pg_hba.conf,添加:

host    all             all             0.0.0.0/0               md5

2. 重启数据库

gs_ctl restart -D /opt/opengauss/data

六、初始化数据库

1. 连接到数据库

gsql -d postgres -p 5432 -r

2. 创建用户和数据库

CREATE USER testuser WITH PASSWORD 'Test@123';
CREATE DATABASE testdb OWNER testuser;
GRANT ALL PRIVILEGES ON DATABASE testdb TO testuser;

3. 退出数据库

\q

七、验证安装

1. 检查数据库状态

gs_ctl status -D /opt/opengauss/data

2. 测试连接

gsql -d testdb -U testuser -W Test@123 -h 127.0.0.1 -p 5432

八、设置开机自启

1. 创建服务文件

cat > /etc/systemd/system/opengauss.service <<EOF
[Unit]
Description=openGauss Database Server
After=network.target

[Service]
Type=forking
User=omm
Group=dbgrp
Environment=PGDATA=/opt/opengauss/data
ExecStart=/opt/openGauss-3.0.0-CentOS-64bit/bin/gs_ctl start -D \$PGDATA
ExecStop=/opt/openGauss-3.0.0-CentOS-64bit/bin/gs_ctl stop -D \$PGDATA
ExecReload=/opt/openGauss-3.0.0-CentOS-64bit/bin/gs_ctl restart -D \$PGDATA
TimeoutSec=300

[Install]
WantedBy=multi-user.target
EOF

2. 启用服务

systemctl daemon-reload
systemctl enable opengauss
systemctl start opengauss

常见问题解决

  1. 安装时报错 "libreadline.so.6: cannot open shared object file"

    ln -s /usr/lib64/libreadline.so.7 /usr/lib64/libreadline.so.6
    
  2. 连接时报错 "FATAL: Invalid username/password" 确保密码正确且使用了正确的加密方式,检查 postgresql.conf 中的 password_encryption_type 设置

  3. 远程连接失败 检查防火墙设置,确保 pg_hba.conf 配置正确,且 postgresql.conflisten_addresses 设置为 '*'

  4. 内存不足问题 可调整 /opt/opengauss/data/postgresql.conf 中的内存相关参数:

    shared_buffers = 512MB
    work_mem = 16MB
    maintenance_work_mem = 128MB
    

按照以上步骤操作后,您应该已在 CentOS 7.x 上成功安装并配置了 openGauss 数据库。