前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >通过 Docker 实现国产数据库 OpenGauss 开发环境搭建

通过 Docker 实现国产数据库 OpenGauss 开发环境搭建

作者头像
耕耘实录
发布2024-05-24 15:29:18
3470
发布2024-05-24 15:29:18
举报
文章被收录于专栏:耕耘实录

通过 Docker 实现国产数据库 OpenGauss 开发环境搭建

一 前置准备

2.1 下载镜像
代码语言:javascript
复制
docker pull enmotech/opengauss:5.0.1

构建镜像的 Dockerfile,方便后期实现个性化定制:

代码语言:javascript
复制
FROM ubuntu:22.04 as builder

ARG TARGETARCH

WORKDIR /warehouse

RUN set -eux; \
    if [ "$TARGETARCH" = "arm64" ]; then \
        echo "deb http://mirror.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy main restricted universe multiverse" >/etc/apt/sources.list && \
        echo "deb http://mirror.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-updates main restricted universe multiverse" >>/etc/apt/sources.list && \
        echo "deb http://mirror.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-backports main restricted universe multiverse" >>/etc/apt/sources.list && \
        echo "deb http://mirror.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-security main restricted universe multiverse" >>/etc/apt/sources.list && \
        echo "deb http://mirror.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-proposed main restricted universe multiverse" >>/etc/apt/sources.list; \
    elif [ "$TARGETARCH" = "amd64" ]; then \
        echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse" >/etc/apt/sources.list && \
        echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse" >>/etc/apt/sources.list && \
        echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse" >>/etc/apt/sources.list && \
        echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse" >>/etc/apt/sources.list; \
    fi && \
    mkdir -p /warehouse/opengauss && \
    apt-get update && apt-get install --yes --no-install-recommends  \
    wget \
    bzip2 \
    ca-certificates && \
    if [ "$TARGETARCH" = "arm64" ]; then \
        wget --progress=bar:force:noscroll https://opengauss.obs.cn-south-1.myhuaweicloud.com/5.0.1/arm_2203/openGauss-5.0.1-openEuler-64bit-all.tar.gz -O /warehouse/openGauss.tar.bz2; \
    elif [ "$TARGETARCH" = "amd64" ]; then \
      wget --progress=bar:force:noscroll https://opengauss.obs.cn-south-1.myhuaweicloud.com/5.0.1/x86_openEuler_2203/openGauss-5.0.1-openEuler-64bit-all.tar.gz -O /warehouse/openGauss.tar.bz2; \
    fi && \
    tar xf /warehouse/openGauss.tar.bz2  && \
    tar xf openGauss-5.0.1-openEuler-64bit.tar.bz2 -C /warehouse/opengauss && \
    rm -f /warehouse/openGauss.tar.bz2 && chmod -R +rx /warehouse/opengauss



FROM ubuntu:22.04

ARG TARGETARCH

ENV LANG en_US.utf8
ENV GAUSSLOG /home/omm/pg_log
ENV PGDATA /var/lib/opengauss/data

RUN set -eux; \
    apt-get update && apt-get install --yes --no-install-recommends \
    libc6 \
    ncat \
    gosu \
    bzip2 \
    procps \
    locales \
    iputils-ping \
    libaio-dev \
    libkeyutils-dev \
    libreadline-dev; \
    apt-get clean; \
    rm -rf /var/lib/apt/lists/* /tmp/*; \
    groupadd -g 70 omm; \
    useradd -u 70 -g omm -m -s /bin/bash omm; \
    mkdir -p /var/lib/opengauss; \
    mkdir -p /var/run/opengauss; \
    mkdir /docker-entrypoint-initdb.d; \
    chown omm:omm /var/lib/opengauss /home/omm /var/run/opengauss /docker-entrypoint-initdb.d; \
    echo "export GAUSSLOG=/home/omm/pg_log" >> /home/omm/.profile; \
    echo "export GAUSSHOME=/usr/local/opengauss" >> /home/omm/.profile; \
    echo "export PGDATA=/var/lib/opengauss/data" >> /home/omm/.profile; \
    echo "export PATH=\$GAUSSHOME/bin:\$PATH " >> /home/omm/.profile; \
    echo "export LD_LIBRARY_PATH=\$GAUSSHOME/lib:\$LD_LIBRARY_PATH" >> /home/omm/.profile; \
    locale-gen en_US.UTF-8 && \
    if [ "$TARGETARCH" = "arm64" ];then \
        ln -s /lib/aarch64-linux-gnu/libreadline.so.8.0 /lib/aarch64-linux-gnu/libreadline.so.7; \
    elif [ "$TARGETARCH" = "amd64" ];then \
        ln -s /lib/x86_64-linux-gnu/libreadline.so.8.0 /lib/x86_64-linux-gnu/libreadline.so.7; \
    fi


COPY entrypoint.sh /usr/local/bin/
COPY --chown=omm:omm --from=builder /warehouse/opengauss /usr/local/opengauss
RUN chmod 755 /usr/local/bin/entrypoint.sh && ln -s /usr/local/bin/entrypoint.sh /

ENTRYPOINT ["entrypoint.sh"]
EXPOSE 5432
CMD ["gaussdb"]

docker-entrypoinrt.sh 内容:

代码语言:javascript
复制
#!/usr/bin/env bash
set -Eeo pipefail

# usage: file_env VAR [DEFAULT]
#    ie: file_env 'XYZ_DB_PASSWORD' 'example'
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
#  "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)

export GAUSSHOME=/usr/local/opengauss
export PATH=$GAUSSHOME/bin:$PATH
export LD_LIBRARY_PATH=$GAUSSHOME/lib:$LD_LIBRARY_PATH
export LANG=en_US.UTF-8

file_env() {
        local var="$1"
        local fileVar="${var}_FILE"
        local def="${2:-}"
        if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
                echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
                exit 1
        fi
        local val="$def"
        if [ "${!var:-}" ]; then
                val="${!var}"
        elif [ "${!fileVar:-}" ]; then
                val="$(< "${!fileVar}")"
        fi
        export "$var"="$val"
        unset "$fileVar"
}

# check to see if this file is being run or sourced from another script
_is_sourced() {
        [ "${#FUNCNAME[@]}" -ge 2 ] \
                && [ "${FUNCNAME[0]}" = '_is_sourced' ] \
                && [ "${FUNCNAME[1]}" = 'source' ]
}

# used to create initial opengauss directories and if run as root, ensure ownership belong to the omm user
docker_create_db_directories() {
        local user
        user="$(id -u)"

        mkdir -p "$PGDATA"
        chmod 700 "$PGDATA"

        # ignore failure since it will be fine when using the image provided directory;
        mkdir -p /var/run/opengauss || :
        chmod 775 /var/run/opengauss || :

        # Create the transaction log directory before initdb is run so the directory is owned by the correct user
        if [ -n "$POSTGRES_INITDB_XLOGDIR" ]; then
                mkdir -p "$POSTGRES_INITDB_XLOGDIR"
                if [ "$user" = '0' ]; then
                        find "$POSTGRES_INITDB_XLOGDIR" \! -user postgres -exec chown postgres '{}' +
                fi
                chmod 700 "$POSTGRES_INITDB_XLOGDIR"
        fi

        # allow the container to be started with `--user`
        if [ "$user" = '0' ]; then
                find "$PGDATA" \! -user omm -exec chown omm '{}' +
                find /var/run/opengauss \! -user omm -exec chown omm '{}' +
        fi
}

# initialize empty PGDATA directory with new database via 'initdb'
# arguments to `initdb` can be passed via POSTGRES_INITDB_ARGS or as arguments to this function
# `initdb` automatically creates the "postgres", "template0", and "template1" dbnames
# this is also where the database user is created, specified by `GS_USER` env
docker_init_database_dir() {
        # "initdb" is particular about the current user existing in "/etc/passwd", so we use "nss_wrapper" to fake that if necessary
        if ! getent passwd "$(id -u)" &> /dev/null && [ -e /usr/lib/libnss_wrapper.so ]; then
                export LD_PRELOAD='/usr/lib/libnss_wrapper.so'
                NSS_WRAPPER_GROUP="$(mktemp)" && export NSS_WRAPPER_GROUP
                NSS_WRAPPER_PASSWD="$(mktemp)" && export NSS_WRAPPER_PASSWD
                echo "postgres:x:$(id -u):$(id -g):PostgreSQL:$PGDATA:/bin/false" > "$NSS_WRAPPER_PASSWD"
                echo "postgres:x:$(id -g):" > "$NSS_WRAPPER_GROUP"
        fi

        if [ -n "$POSTGRES_INITDB_XLOGDIR" ]; then
                set -- --xlogdir "$POSTGRES_INITDB_XLOGDIR" "$@"
        fi

        cmdbase="gs_initdb --pwfile=<(echo $GS_PASSWORD)"

        if [ -n "$GS_NODENAME" ]; then
                cmdbase="$cmdbase --nodename=$GS_NODENAME"
        else
                cmdbase="$cmdbase --nodename=gaussdb"
        fi

        if [ -n "$ENCODING" ]; then
                cmdbase="$cmdbase --encoding=$ENCODING"
        else
                cmdbase="$cmdbase --encoding=UTF-8"
        fi

        if [ -n "$LOCALE" ]; then
                cmdbase="$cmdbase --locale=$LOCALE"
        else
                cmdbase="$cmdbase --no-locale"
        fi

        if [ -n "$DBCOMPATIBILITY" ]; then
                cmdbase="$cmdbase --dbcompatibility=$DBCOMPATIBILITY"
        else
                cmdbase="$cmdbase --dbcompatibility=PG"
        fi

        cmdbase="$cmdbase -D $PGDATA"

        eval "$cmdbase"

        # unset/cleanup "nss_wrapper" bits
        if [ "${LD_PRELOAD:-}" = '/usr/lib/libnss_wrapper.so' ]; then
                rm -f "$NSS_WRAPPER_PASSWD" "$NSS_WRAPPER_GROUP"
                unset LD_PRELOAD NSS_WRAPPER_PASSWD NSS_WRAPPER_GROUP
        fi
}

# print large warning if GS_PASSWORD is long
# error if both GS_PASSWORD is empty and GS_HOST_AUTH_METHOD is not 'trust'
# print large warning if GS_HOST_AUTH_METHOD is set to 'trust'
# assumes database is not set up, ie: [ -z "$DATABASE_ALREADY_EXISTS" ]
docker_verify_minimum_env() {
        # check password first so we can output the warning before postgres
        # messes it up
        if [[ "$GS_PASSWORD" =~ ^(.{8,}).*$ ]] && [[ "$GS_PASSWORD" =~ ^(.*[a-z]+).*$ ]] && [[ "$GS_PASSWORD" =~ ^(.*[A-Z]).*$ ]] && [[ "$GS_PASSWORD" =~ ^(.*[0-9]).*$ ]] && [[ "$GS_PASSWORD" =~ ^(.*[#?!@$%^&*-]).*$ ]]; then
                cat >&2 <<- 'EOWARN'

                        Message: The supplied GS_PASSWORD is meet requirements.

EOWARN
        else
                cat >&2 <<- 'EOWARN'

                        Error: The supplied GS_PASSWORD is not meet requirements.
                        Please Check if the password contains uppercase, lowercase, numbers, special characters, and password length(8).
                        At least one uppercase, lowercase, numeric, special character.
                        Example: Enmo@123
EOWARN
                exit 1
        fi
        if [ -z "$GS_PASSWORD" ] && [ 'trust' != "$GS_HOST_AUTH_METHOD" ]; then
                # The - option suppresses leading tabs but *not* spaces. :)
                cat >&2 <<- 'EOE'
                        Error: Database is uninitialized and superuser password is not specified.
                               You must specify GS_PASSWORD to a non-empty value for the
                               superuser. For example, "-e GS_PASSWORD=password" on "docker run".

                               You may also use "GS_HOST_AUTH_METHOD=trust" to allow all
                               connections without a password. This is *not* recommended.

EOE
                exit 1
        fi
        if [ 'trust' = "$GS_HOST_AUTH_METHOD" ]; then
                cat >&2 <<- 'EOWARN'
                        ********************************************************************************
                        WARNING: GS_HOST_AUTH_METHOD has been set to "trust". This will allow
                                 anyone with access to the opengauss port to access your database without
                                 a password, even if GS_PASSWORD is set.
                                 It is not recommended to use GS_HOST_AUTH_METHOD=trust. Replace
                                 it with "-e GS_PASSWORD=password" instead to set a password in
                                 "docker run".
                        ********************************************************************************
EOWARN
        fi
}

# usage: docker_process_init_files [file [file [...]]]
#    ie: docker_process_init_files /always-initdb.d/*
# process initializer files, based on file extensions and permissions
docker_process_init_files() {
        # gsql here for backwards compatiblilty "${gsql[@]}"
        gsql=(docker_process_sql)

        echo
        local f
        for f; do
                case "$f" in
                        *.sh)
                                if [ -x "$f" ]; then
                                        echo "$0: running $f"
                                        "$f"
                                else
                                        echo "$0: sourcing $f"
                                        . "$f"
                                fi
                                ;;
                        *.sql)    echo "$0: running $f"; docker_process_sql -f "$f"; echo ;;
                        *.sql.gz) echo "$0: running $f"; gunzip -c "$f" | docker_process_sql; echo ;;
                        *.sql.xz) echo "$0: running $f"; xzcat "$f" | docker_process_sql; echo ;;
                        *)        echo "$0: ignoring $f" ;;
                esac
                echo
        done
}

# Execute sql script, passed via stdin (or -f flag of pqsl)
# usage: docker_process_sql [gsql-cli-args]
#    ie: docker_process_sql --dbname=mydb <<<'INSERT ...'
#    ie: docker_process_sql -f my-file.sql
#    ie: docker_process_sql <my-file.sql
docker_process_sql() {
        local query_runner=(gsql -v ON_ERROR_STOP=1 --username "$GS_USER" --password "$GS_PASSWORD")
        if [ -n "$GS_DB" ]; then
                query_runner+=(--dbname "$GS_DB")
        fi

        echo "Execute SQL: ${query_runner[@]} $@"
        "${query_runner[@]}" "$@"
}

# create initial database
# uses environment variables for input: GS_DB
docker_setup_db() {
        echo "GS_DB = $GS_DB"
        if [ "$GS_DB" != 'postgres' ]; then
                GS_DB= docker_process_sql --dbname postgres --set db="$GS_DB" --set passwd="$GS_PASSWORD" <<- 'EOSQL'
                        CREATE DATABASE :"db" ;
                        create user gaussdb with login password :"passwd" ;
                        grant all privileges to gaussdb;

EOSQL
                echo
        fi
}

docker_setup_user() {
        if [ -n "$GS_USERNAME" ]; then
                GS_DB= docker_process_sql --dbname postgres --set db="$GS_DB" --set passwd="$GS_PASSWORD" --set user="$GS_USERNAME" <<- 'EOSQL'
                        create user :"user" with login password :"passwd" ;
EOSQL
        else
                echo " default user is gaussdb"
        fi
}

docker_setup_rep_user() {
        if [ -n "$SERVER_MODE" ] && [ "$SERVER_MODE" = "primary" ]; then
                GS_DB= docker_process_sql --dbname postgres --set passwd="RepUser@2020" --set user="repuser" <<- 'EOSQL'
                        create user :"user" SYSADMIN REPLICATION password :"passwd" ;
EOSQL
        else
                echo " default no repuser created"
        fi
}

# Loads various settings that are used elsewhere in the script
# This should be called before any other functions
docker_setup_env() {
        export GS_USER=omm
        file_env 'GS_PASSWORD' 'Enmo@123'

        # file_env 'GS_USER' 'omm'
        file_env 'GS_DB' "$GS_USER"
        file_env 'POSTGRES_INITDB_ARGS'
        # default authentication method is md5
        : "${GS_HOST_AUTH_METHOD:=md5}"

        declare -g DATABASE_ALREADY_EXISTS
        # look specifically for OG_VERSION, as it is expected in the DB dir
        if [ -s "$PGDATA/PG_VERSION" ]; then
                DATABASE_ALREADY_EXISTS='true'
        fi
}

# append parameter to mot.conf
opengauss_setup_mot_conf() {
        echo "enable_numa = false" > "$PGDATA/mot.conf"
}

# append parameter to pg_hba.conf
opengauss_setup_hba_conf() {
        {
                echo
                if [ 'trust' = "$GS_HOST_AUTH_METHOD" ]; then
                        echo '# warning trust is enabled for all connections'
                fi
                echo "host all all 0.0.0.0/0 $GS_HOST_AUTH_METHOD"
                echo "host replication gaussdb 0.0.0.0/0 md5"
                if [ -n "$SERVER_MODE" ]; then
                        echo "host replication repuser $OG_SUBNET trust"
                fi
        } >> "$PGDATA/pg_hba.conf"
}

# append parameter to postgres.conf
opengauss_setup_postgresql_conf() {
        {
                echo
                if [ -n "$GS_PORT" ]; then
                        echo "port = $GS_PORT"
                        echo "wal_level = logical"
                        echo "password_encryption_type = 1"
                else
                        echo '# use default port 5432'
                        echo "wal_level = logical"
                        echo "password_encryption_type = 1"
                fi

                if [ -n "$SERVER_MODE" ]; then
                        echo "most_available_sync = on"
                        echo "listen_addresses = '0.0.0.0'"
                        echo "pgxc_node_name = '$NODE_NAME'"
                        echo "remote_read_mode = non_authentication"
                        echo -e "$REPL_CONN_INFO"
                        if [ -n "$SYNCHRONOUS_STANDBY_NAMES" ]; then
                                echo "synchronous_standby_names=$SYNCHRONOUS_STANDBY_NAMES"
                        fi
                else
                        echo "listen_addresses = '0.0.0.0'"
                fi

                if [ -n "$OTHER_PG_CONF" ]; then
                        echo -e "$OTHER_PG_CONF"
                fi

        } >> "$PGDATA/postgresql.conf"
}

docker_slave_full_backup() {
        gs_ctl build -D "$PGDATA" -b full
}

# stop postgresql server after done setting up user and running scripts
docker_temp_server_stop() {
        PGUSER="${PGUSER:-$GS_USER}" gs_ctl -D "$PGDATA" -m fast -w stop
}

# start socket-only postgresql server for setting up or running scripts
# all arguments will be passed along as arguments to `postgres` (via pg_ctl)
docker_temp_server_start() {
        if [ "$1" = 'gaussdb' ]; then
                shift
        fi

        # internal start of server in order to allow setup using gsql client
        # does not listen on external TCP/IP and waits until start finishes
        set -- "$@" -c listen_addresses='127.0.0.1' -p "${PGPORT:-5432}"

        PGUSER="${PGUSER:-$GS_USER}" gs_ctl -D "$PGDATA" -o "$(printf '%q ' "$@")" -w start
}

# check arguments for an option that would cause opengauss to stop
# return true if there is one

_opengauss_want_help() {
        local arg
        count=1
        for arg; do
                case "$arg" in
                        # postgres --help | grep 'then exit'
                        # leaving out -C on purpose since it always fails and is unhelpful:
                        # postgres: could not access the server configuration file "/var/lib/postgresql/data/postgresql.conf": No such file or directory
                        -'?' | --help | --describe-config | -V | --version)
                                return 0
                                ;;
                esac
                if [ "$arg" == "-M" ]; then
                        SERVER_MODE=${@:$count+1:1}
                        echo "openGauss DB SERVER_MODE = $SERVER_MODE"
                        shift
                fi
                count=$(($count + 1))
        done
        return 1
}

_main() {
        # if first arg looks like a flag, assume we want to run postgres server
        if [ "${1:0:1}" = '-' ]; then
                set -- gaussdb "$@"
        fi

        if [ "$1" = 'gaussdb' ] && ! _opengauss_want_help "$@"; then
                docker_setup_env
                # setup data directories and permissions (when run as root)
                docker_create_db_directories
                if [ "$(id -u)" = '0' ]; then
                        # then restart script as postgres user
                        exec gosu omm "$BASH_SOURCE" "$@"
                fi

                # only run initialization on an empty data directory
                if [ -z "$DATABASE_ALREADY_EXISTS" ]; then
                        docker_verify_minimum_env

                        # check dir permissions to reduce likelihood of half-initialized database
                        ls /docker-entrypoint-initdb.d/ > /dev/null

                        docker_init_database_dir
                        opengauss_setup_hba_conf
                        opengauss_setup_postgresql_conf
                        opengauss_setup_mot_conf

                        # PGPASSWORD is required for gsql when authentication is required for 'local' connections via pg_hba.conf and is otherwise harmless
                        # e.g. when '--auth=md5' or '--auth-local=md5' is used in POSTGRES_INITDB_ARGS
                        export PGPASSWORD="${PGPASSWORD:-$GS_PASSWORD}"
                        docker_temp_server_start "$@"
                        if [ -z "$SERVER_MODE" ] || [ "$SERVER_MODE" = "primary" ]; then
                                docker_setup_db
                                docker_setup_user
                                docker_setup_rep_user
                                docker_process_init_files /docker-entrypoint-initdb.d/*
                        fi

                        if [ -n "$SERVER_MODE" ] && [ "$SERVER_MODE" != "primary" ]; then
                                docker_slave_full_backup
                        fi
                        docker_temp_server_stop
                        unset PGPASSWORD

                        echo
                        echo 'openGauss  init process complete; ready for start up.'
                        echo
                else
                        echo
                        echo 'openGauss Database directory appears to contain a database; Skipping initialization'
                        echo
                fi
        fi
        exec "$@"
}

if ! _is_sourced; then
        _main "$@"
fi
2.2 环境变量配置说明

GS_PASSWORD:必须设置该参数。该参数值不能为空或者不定义。该参数设置了 openGauss 数据库的超级用户 omm 以及测试用户 gaussdb 的密码。openGauss 安装时默认会创建 omm 超级用户,该用户名暂时无法修改。测试用户 gaussdb 是在 entrypoint.sh 中自定义创建的用户。openGauss 镜像配置了本地信任机制,因此在容器内连接数据库无需密码,但是如果要从容器外部(其它主机或者其它容器)连接则必须要输入密码。openGauss 的密码有复杂度要求,需要:密码长度 8 个字符及以上,必须同时包含英文字母大小写,数字,以及特殊符号。

GS_NODENAME:指定数据库节点名称 默认为 gaussdb。

GS_USERNAME:指定数据库连接用户名 默认为 gaussdb。

GS_PORT:指定数据库端口,默认为 5432。

2.3 启动容器

启动命令如下:

代码语言:javascript
复制
docker run -d --name opengauss \
           --privileged=true \
           --restart=always \
           -v /home/ivan/data/opengauss:/var/lib/opengauss  \
           -u root \
           -p 15432:5432 \
           -e GS_NODENAME=gaussdb \
           -e GS_USERNAME=gaussdb \
           -e GS_PASSWORD='C*x#1a2b' \
           enmotech/opengauss:5.0.1
2.4 使用 gsql 连接数据库

示例如下:

代码语言:javascript
复制
gsql -d "host=127.0.0.1 port=5432 dbname=gaussdb user=omm password='C*x#1a2b'"
2.5 建库建表

示例如下:

代码语言:javascript
复制
CREATE USER IF NOT EXISTS cmams WITH PASSWORD 'ghaF&fZugC9y';
CREATE DATABASE IF NOT EXISTS cmams ENCODING 'UTF8' OWNER cmams;
CREATE SCHEMA IF NOT EXISTS cmams;
GRANT CREATE,USAGE ON SCHEMA cmams TO cmams;
GRANT SELECT,INSERT,UPDATE,DELETE ON ALL TABLES IN SCHEMA public TO cmams;
GRANT USAGE,SELECT ON ALL SEQUENCES IN SCHEMA cmams TO cmams;
DROP TABLE IF EXISTS cmams.cmams_status;
CREATE TABLE cmams_status
(
    cmams_id               varchar(18) not null constraint cmams_status_pk primary key,
    cmams_province         varchar(20) not null,
    cmams_concurrency      integer   not null,
    cmams_successful_count integer   not null,
    cmams_failure_count    integer   not null,
    cmams_queue_length     integer   not null,
    cmams_update_time      timestamp   not null
);

COMMENT ON COLUMN cmams_status.cmams_id IS '主键ID';
COMMENT ON COLUMN cmams_status.cmams_province IS '省份';
COMMENT ON COLUMN cmams_status.cmams_concurrency IS '当前并发数';
COMMENT ON COLUMN cmams_status.cmams_successful_count IS '成功调用次数';
COMMENT ON COLUMN cmams_status.cmams_failure_count IS '调用失败次数';
COMMENT ON COLUMN cmams_status.cmams_queue_length IS 'Redis队列长度';
COMMENT ON COLUMN cmams_status.cmams_update_time IS '数据更新时间';
2.6 Maven 项目引入相关驱动
代码语言:javascript
复制
<dependency>
    <groupId>org.opengauss</groupId>
    <artifactId>opengauss-jdbc</artifactId>
    <version>5.1.0-og</version>
    <scope>provided</scope>
</dependency>
2.7 主从集群搭建

按需修改并执行以下脚本:

代码语言:javascript
复制
#!/bin/bash -e
# Parameters
#!/bin/bash

#set OG_SUBNET,GS_PASSWORD,MASTER_IP,SLAVE_1_IP,MASTER_HOST_PORT,MASTER_LOCAL_PORT,SLAVE_1_HOST_PORT,SLAVE_1_LOCAL_PORT,MASTER_NODENAME,SLAVE_NODENAME

read -p "Please input OG_SUBNET (容器所在网段) [172.11.0.0/24]: " OG_SUBNET
OG_SUBNET=${OG_SUBNET:-172.11.0.0/24}
echo "OG_SUBNET set $OG_SUBNET"

read -p "Please input GS_PASSWORD (定义数据库密码)[Enmo@123]: " GS_PASSWORD
GS_PASSWORD=${GS_PASSWORD:-Enmo@123}
echo "GS_PASSWORD set $GS_PASSWORD"

read -p "Please input MASTER_IP (主库IP)[172.11.0.101]: " MASTER_IP
MASTER_IP=${MASTER_IP:-172.11.0.101}
echo "MASTER_IP set $MASTER_IP"

read -p "Please input SLAVE_1_IP (备库IP)[172.11.0.102]: " SLAVE_1_IP
SLAVE_1_IP=${SLAVE_1_IP:-172.11.0.102}
echo "SLAVE_1_IP set $SLAVE_1_IP"

read -p "Please input MASTER_HOST_PORT (主库数据库服务端口)[5432]: " MASTER_HOST_PORT
MASTER_HOST_PORT=${MASTER_HOST_PORT:-5432}
echo "MASTER_HOST_PORT set $MASTER_HOST_PORT"

read -p "Please input MASTER_LOCAL_PORT (主库通信端口)[5434]: " MASTER_LOCAL_PORT
MASTER_LOCAL_PORT=${MASTER_LOCAL_PORT:-5434}
echo "MASTER_LOCAL_PORT set $MASTER_LOCAL_PORT"

read -p "Please input SLAVE_1_HOST_PORT (备库数据库服务端口)[6432]: " SLAVE_1_HOST_PORT
SLAVE_1_HOST_PORT=${SLAVE_1_HOST_PORT:-6432}
echo "SLAVE_1_HOST_PORT set $SLAVE_1_HOST_PORT"

read -p "Please input SLAVE_1_LOCAL_PORT (备库通信端口)[6434]: " SLAVE_1_LOCAL_PORT
SLAVE_1_LOCAL_PORT=${SLAVE_1_LOCAL_PORT:-6434}
echo "SLAVE_1_LOCAL_PORT set $SLAVE_1_LOCAL_PORT"

read -p "Please input MASTER_NODENAME [opengauss_master]: " MASTER_NODENAME
MASTER_NODENAME=${MASTER_NODENAME:-opengauss_master}
echo "MASTER_NODENAME set $MASTER_NODENAME"

read -p "Please input SLAVE_NODENAME [opengauss_slave1]: " SLAVE_NODENAME
SLAVE_NODENAME=${SLAVE_NODENAME:-opengauss_slave1}
echo "SLAVE_NODENAME set $SLAVE_NODENAME"

read -p "Please input openGauss VERSION [1.1.0]: " VERSION
VERSION=${VERSION:-1.1.0}
echo "openGauss VERSION set $VERSION"

echo "starting  "

docker network create --subnet=$OG_SUBNET opengaussnetwork \
|| {
  echo ""
  echo "ERROR: OpenGauss Database Network was NOT successfully created."
  echo "HINT: opengaussnetwork Maybe Already Exsist Please Execute 'docker network rm opengaussnetwork' "
  exit 1
}
echo "OpenGauss Database Network Created."

docker run --network opengaussnetwork --ip $MASTER_IP --privileged=true \
--name $MASTER_NODENAME -h $MASTER_NODENAME -p $MASTER_HOST_PORT:$MASTER_HOST_PORT -d \
-e GS_PORT=$MASTER_HOST_PORT \
-e OG_SUBNET=$OG_SUBNET \
-e GS_PASSWORD=$GS_PASSWORD \
-e NODE_NAME=$MASTER_NODENAME \
-e REPL_CONN_INFO="replconninfo1 = 'localhost=$MASTER_IP localport=$MASTER_LOCAL_PORT localservice=$MASTER_HOST_PORT remotehost=$SLAVE_1_IP remoteport=$SLAVE_1_LOCAL_PORT remoteservice=$SLAVE_1_HOST_PORT'\n" \
enmotech/opengauss:$VERSION -M primary \
|| {
  echo ""
  echo "ERROR: OpenGauss Database Master Docker Container was NOT successfully created."
  exit 1
}
echo "OpenGauss Database Master Docker Container created."

sleep 30s

docker run --network opengaussnetwork --ip $SLAVE_1_IP --privileged=true \
--name $SLAVE_NODENAME -h $SLAVE_NODENAME -p $SLAVE_1_HOST_PORT:$SLAVE_1_HOST_PORT -d \
-e GS_PORT=$SLAVE_1_HOST_PORT \
-e OG_SUBNET=$OG_SUBNET \
-e GS_PASSWORD=$GS_PASSWORD \
-e NODE_NAME=$SLAVE_NODENAME \
-e REPL_CONN_INFO="replconninfo1 = 'localhost=$SLAVE_1_IP localport=$SLAVE_1_LOCAL_PORT localservice=$SLAVE_1_HOST_PORT remotehost=$MASTER_IP remoteport=$MASTER_LOCAL_PORT remoteservice=$MASTER_HOST_PORT'\n" \
enmotech/opengauss:$VERSION -M standby \
|| {
  echo ""
  echo "ERROR: OpenGauss Database Slave1 Docker Container was NOT successfully created."
  exit 1
}
echo "OpenGauss Database Slave1 Docker Container created."
2.8 多节点URL配置

示例如下:

代码语言:javascript
复制
local.url=jdbc:opengauss://127.0.0.1:20001,127.0.0.2:20001/localtest?useUnicode=true&characterEncoding=utf8&useSSL=false&targetServerType=master&batchMode=off

三 参考资料

  1. 云和墨恩官方 OpenGauss GitHub 5.0.1 镜像构建相关内容
  2. 云和墨恩官方 OpenGauss 镜像
  3. OpenGauss 官方支持包和工具下载
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-03-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 通过 Docker 实现国产数据库 OpenGauss 开发环境搭建
    • 一 前置准备
      • 2.1 下载镜像
      • 2.2 环境变量配置说明
      • 2.3 启动容器
      • 2.4 使用 gsql 连接数据库
      • 2.5 建库建表
      • 2.6 Maven 项目引入相关驱动
      • 2.7 主从集群搭建
      • 2.8 多节点URL配置
    • 三 参考资料
    相关产品与服务
    数据库
    云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档