【AntDB数据库】AntDB数据库服务启停(一)

数据库服务启停

集群启停

psql到adbmgr上,可以对集群进行启停操作。

停止集群:

stop all;

停止agent:

stop agent all;

启动agent:

start agent password 'xxxxxx' ;

启动集群:

start all;

执行完命令后,可以执行monitor all命令查看各个节点的状态, monitor agent all查看agent状态。

“running”代表节点是启动状态,“not running”代表节点是停止状态。

monitor all;
monitor agent all;

集群连接

连接说明

在集群搭建完成之后,就可以登录数据库进行相应的操作。默认可以使用与操作系统同名的用户(比如antdb)登录postgres数据库。与操作系统用户同名的用户默认为超级用户(superuser)。

**注意:**集群版中,客户端登录的时候,需要指定gtmcoord或者coord的连接端口,而非adbmgr的端口,这点需要特别注意。

在首次通过客户端连接的时候,可能会出现如下报错:

FATAL:  no pg_hba.conf entry for host "10.21.28.35", user "adb01", database "postgres", SSL off

原因是:在节点的hba中没有开放访问IP。此时登录adbmgr执行下述命令即可:

add hba coordinator all("host all all 10.21.28.0 24 trust");
add hba gtmcoord all("host all all 10.21.28.0 24 trust");

示例中的IP根据实际情况进行修改。

如果执行报错,通过\h add hba可查看帮助信息。

执行成功后,重新尝试登录。

使用psql连接

在集群主机上,默认安装了psql客户端,可以直接使用。如果需要在集群之外的主机上通过psql登录,则需要安装对应的postgres客户端。

通过psql --help命令可以得到psql的帮助信息:

psql is the PostgreSQL interactive terminal.

Usage:
  psql [OPTION]... [DBNAME [USERNAME]]

General options:
  -c, --command=COMMAND    run only single command (SQL or internal) and exit
  -d, --dbname=DBNAME      database name to connect to (default: "danghb")
  -f, --file=FILENAME      execute commands from file, then exit
  -l, --list               list available databases, then exit
  -v, --set=, --variable=NAME=VALUE
                           set psql variable NAME to VALUE
                           (e.g., -v ON_ERROR_STOP=1)
  -V, --version            output version information, then exit
  -X, --no-psqlrc          do not read startup file (~/.psqlrc)
  -1 ("one"), --single-transaction
                           execute as a single transaction (if non-interactive)
  -?, --help[=options]     show this help, then exit
      --help=commands      list backslash commands, then exit
      --help=variables     list special variables, then exit

Input and output options:
  -a, --echo-all           echo all input from script
  -b, --echo-errors        echo failed commands
  -e, --echo-queries       echo commands sent to server
  -E, --echo-hidden        display queries that internal commands generate
  -L, --log-file=FILENAME  send session log to file
  -n, --no-readline        disable enhanced command line editing (readline)
  -o, --output=FILENAME    send query results to file (or |pipe)
  -q, --quiet              run quietly (no messages, only query output)
  -s, --single-step        single-step mode (confirm each query)
  -S, --single-line        single-line mode (end of line terminates SQL command)

Output format options:
  -A, --no-align           unaligned table output mode
  -F, --field-separator=STRING
                           field separator for unaligned output (default: "|")
  -H, --html               HTML table output mode
  -P, --pset=VAR[=ARG]     set printing option VAR to ARG (see \pset command)
  -R, --record-separator=STRING
                           record separator for unaligned output (default: newline)
  -t, --tuples-only        print rows only
  -T, --table-attr=TEXT    set HTML table tag attributes (e.g., width, border)
  -x, --expanded           turn on expanded table output
  -z, --field-separator-zero
                           set field separator for unaligned output to zero byte
  -0, --record-separator-zero
                           set record separator for unaligned output to zero byte

Connection options:
  -h, --host=HOSTNAME      database server host or socket directory (default: "local socket")
  -p, --port=PORT          database server port (default: "5432")
  -U, --username=USERNAME  database user name (default: "danghb")
  -w, --no-password        never prompt for password
  -W, --password           force password prompt (should happen automatically)

For more information, type "\?" (for internal commands) or "\help" (for SQL
commands) from within psql, or consult the psql section in the PostgreSQL
documentation.

Report bugs to <[email protected]>.

其中我们需要我们重点关注的是:

  • -h 指定数据库服务器的主机名称或者IP
  • -p 指定数据库服务的端口
  • -d 指定需要连接的数据库名称
  • -U 指定需要连接的数据库用户名

一个完整的示例如下:

psql -h 192.168.1.1 -p 5432  -d postgres -U antdb

如果服务端对连接客户端设置了trust认证,则可以直接登录,如果设置了md5认证,则会提示输入密码:

Password for user antdb: 

按照提示输入密码即可。

登录成功提示符:

psql (4.0.0 cbabf9feaf based on PG 10.7)
Type "help" for help.

postgres=# 

可以通过更换-h-p的参数值来连接集群中不同的coord节点。

集群中各个coord节点提供的功能都是一样的,后续操作登录任意coord节点均可。

应用程序接口

用户可以使用标准的数据库应用程序接口(如ODBC和JDBC),开发基于AntDB的应用程序。

API 下载地址
JDBC https://jdbc.postgresql.org/download.html
ODBC psqlodbc - PostgreSQL ODBC driver PostgreSQL: File Browser

Java 连接

Java连接使用JDBC驱动,如果用户不需要使用Oracle语法,则直接下载PostgreSQL自带的Java驱动即可。

如果需要在jdbc串中使用Oracle语法,则需要使用AntDB提供的jdbc驱动,可以让交付人员提供。

驱动类为:

driver=org.postgresql.Driver

连接串格式如下:

conn=jdbc:postgresql://10.21.20.256:11010/postgres?binaryTransfer=False&forceBinary=False&reWriteBatchedInserts=true

如果要使用Oracle语法,则连接串增加语法参数grammar=oracle

conn=jdbc:postgresql://10.21.20.256:11010/postgres?binaryTransfer=False&forceBinary=False&reWriteBatchedInserts=true&grammar=oracle

如果是xml配置文件,则需要将& 替换为 &amp;:

conn=jdbc:postgresql://10.21.20.256:11010/postgres?binaryTransfer=False&amp;forceBinary=False&amp;reWriteBatchedInserts=true&amp;grammar=oracle

可以配置多个地址防止节点发生切换后数据库不可访问,在 jdbc 串中配置如下:

jdbc:postgresql://10.21.20.256:6432,10.21.20.257:6432,10.21.20.258:6432/postgres?targetServerType=master&binaryTransfer=False&forceBinary=False&grammar=oracle

也可以配置多个地址进行负载均衡,在 jdbc 串中配置如下:

jdbc:postgresql://10.21.20.256:6432,10.21.20.257:6432,10.21.20.258:6432/postgres?targetServerType=master&loadBalanceHosts=true&binaryTransfer=False&forceBinary=False&grammar=oracle

其中:

  • 10.21.20.256为AntDB中coordinator的地址。
  • 11010为AntDB中coordinator的端口。
  • postgres指定了需要连接的数据库名称。
  • targetServerType 指定了只连 master 节点,即可读可写的节点。会按照给定的顺序去进行连接,第一个连不上或者不是 master 节点的时候,去连第二个,以此类推。
  • loadBalanceHosts 从给定的连接信息中随机选择节点进行连接,达到负载均衡的目的。

更多的连接参数可以参考:https://jdbc.postgresql.org/documentation/head/connect.html

开发测试人员在进行连接的时候,可以咨询DBA或者系统管理员获取以上信息。

Python 连接

Python连接AntDB使用连接postgres的python驱动即可。 python连接postgres的驱动也有好几个,具体可看:https://wiki.postgresql.org/wiki/Python 本例中我们选择流行度比较高的psycopg2。

文档地址:http://initd.org/psycopg/docs/,py2、py3均可使用。

安装psycopg2

在centos下,直接执行:

yum install python-psycopg2

如果是内网环境,且没有yum源,可以去github上下载源码进行安装,源码地址:GitHub - psycopg/psycopg2: PostgreSQL database adapter for the Python programming language,按照readme的说明操作即可。

安装好psycopg2之后,进行测试:

python -c "import psycopg2"

返回空,表示安装成功。

AntDB数据库始于2008年,在运营商的核心系统上,为全国24个省份的10亿多用户提供在线服务,具备高性能、弹性扩展、高可靠等产品特性,峰值每秒可处理百万笔电信核心交易,保障系统持续稳定运行近十年,并在通信、金融、交通、能源、物联网等行业成功商用落地。

猜你喜欢

转载自blog.csdn.net/weixin_44518445/article/details/131300992
今日推荐