1. 程式人生 > 其它 >openGauss2.0安裝教程

openGauss2.0安裝教程

緣起:本人有緣參加了華為智慧資料庫課程,因在完成資料庫課設設計的第一步,定義表的時候,意外發現openGauss不支援外來鍵約束。無奈下,想通過各種方式(儲存過程、觸發器)來實現外來鍵約束,在搜尋資料途中,發現openGauss2.0開始支援外來鍵約束,故有了這篇文章。

本教程需要的環境為openEuler aarch64,其他版本可稍做參考。

注意:安裝openEuler2.0前需要安裝libnsl軟體包,否則會報錯。

配置yum源

首先我們對剛租下來的伺服器先配置yum

mkdir /etc/yum.repos.d/bak
mv /etc/yum.repos.d/*.repo  /etc/yum.repos.d/bak/
wget -O /etc/yum.repos.d/openEuler_aarch64.repo https://repo.huaweicloud.com/repository/conf/openeuler_aarch64.repo
yum clean all

如果出現出錯可以在華為映象找到適合自己的映象

安裝依賴包

yum install -y bzip2 libaio-devel flex bison ncurses-devel glibc-devel patch readline-devel libnsl

關閉安全設定

## 關閉防火牆 
systemctl status firewalld
systemctl disable firewalld.service 
systemctl stop firewalld.service

 ## 關閉SELinux
sed -i '/SELINUX=/d' /etc/selinux/config 
echo "SELINUX=disabled" >> /etc/selinux/config 
cat /etc/selinux/config|grep -v ^#|grep -v '^$'

建立普通使用者和目錄,並授權

groupadd -g 1001 dbgrp
useradd -u 2001 -g dbgrp omm
mkdir -p /opt/software/openGauss
chown -R omm:dbgrp /opt

下載解壓安裝單機openGauss

su - omm
cd /opt/software/openGauss/
wget https://opengauss.obs.cn-south-1.myhuaweicloud.com/2.0.0/arm/openGauss-2.0.0-openEuler-64bit.tar.bz2
tar -jxf openGauss-2.0.0-openEuler-64bit.tar.bz2

## 一鍵式指令碼安裝
cd /opt/software/openGauss/simpleInstall/
sh install.sh -w Bigdata@123  -p 26000

說明: - -w:初始化資料庫密碼(gs_initdb指定),安全需要必須設定。 - -p:指定的openGauss埠號, 如不指定,預設為5432。 - -h|–help 列印使用說明。 - 安裝後,資料庫的名稱為sgnode。 - 安裝後,資料庫目錄安裝路徑/opt/software/openGauss/data/single_node,其中/opt/software/openGauss為解壓包路徑,data/single_node為新建立的資料庫節點目錄。

注意,由於我們的openEuler為鯤鵬架構,aarch64,因此我們下載openeuler_aarch64版本

安裝過程中會提示是否建立demo資料庫,這裡隨意,我輸入了yes

到這一步恭喜你,基本安裝成功了

You can start or stop the database server using:
    gs_ctl start|stop|restart -D $GAUSSHOME/data/single_node -Z single_node

以上的提示是我們日後開啟、關閉、重啟資料時輸入的命令。

配置資料庫遠端連線

我們進入資料庫後,首先需要建立一個database和一個使用者供我們遠端連線使用。

輸入以下命令進入資料庫

gsql -d postgres -p 26000 -r

建立資料庫和使用者

create database test;
create user chris password 'bigdata@123';
# 為了避免不必要的麻煩,建議給使用者賦予所有許可權
GRANT ALL PRIVILEGES TO chris;

配置遠端連線

關閉防火牆

配置遠端連線需要關閉防火牆,但是如果按照本教程進行,可以省略這一步

此操作須在root下進行

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

切換omm使用者

su - omm

配置pg_hba.conf

vim /opt/software/openGauss/data/single_node/pg_hba.conf

pg_hba.conf中新增以下內容

host    all             all             0.0.0.0/0               md5

配置postgresql.conf

vim /opt/software/openGauss/data/single_node/postgresql.conf

我們在檔案中找到相應的行,對其取消註釋或修改

listen_addresses = '*'
local_bind_address = '0.0.0.0'
password_encryption_type = 0

重啟資料庫

# 重啟資料庫
gs_ctl restart -D $GAUSSHOME/data/single_node -Z single_node

修改使用者密碼

登入資料庫須切換omm使用者,在omm使用者下

gsql -p 26000 -d test -U chris -W bigdata@123 -r

在資料庫的命令列輸入以下命令修改密碼

test=> ALTER USER chris identified by 'admin@123' replace 'bigdata@123';

提示用了MD5進行加密,成功。

左上角連線選擇PostgreSQL連線

按測試連線,來測試是否能成功連線

:openGauss外來鍵約束語法

drop table if exists employee;
create table employee(
	e_id char(10) not null primary key,	
	e_name varchar(50) not null
)

drop table if exists company;
create table company(
	c__id char(10) not null primary key,
	e_id char(10) not null references employee(e_id),
	c_name varchar(50) not null
)

Python 遠端連線

python遠端連線使用的是psycopg2

如果電腦沒有安裝psycopg2,可以才命令列使用pip install psycopg2來安裝。

使用方法

import psycopg2
conn = psycopg2.connect(dbname="test",
                            user="chris",
                            password="admin@123",
                            host="xxx.xxx.xxx.xxx",
                            port="26000")
conn.set_client_encoding('utf8')
cur = conn.cursor()

# 建立表
cur.execute('''CREATE TABLE COMPANY
       (ID INT PRIMARY KEY     NOT NULL,
       NAME           TEXT    NOT NULL,
       AGE            INT     NOT NULL,
       ADDRESS        CHAR(50),
       SALARY         REAL);''')
conn.commit()

# 插入資料
cur.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
      VALUES (1, 'Paul', 32, 'California', 20000.00 )");
cur.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
      VALUES (2, 'Allen', 25, 'Texas', 15000.00 )");
cur.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
      VALUES (3, 'Teddy', 23, 'Norway', 20000.00 )");
cur.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
      VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 )");
conn.commit()

# 資料查詢
cur.execute("SELECT id, name, address, salary  from COMPANY")
rows = cur.fetchall()
for row in rows:
   print("ID = ", row[0])
   print("NAME = ", row[1])
   print("ADDRESS = ", row[2])
   print("SALARY = ", row[3])
    
conn.close()