暂无图片
系统重启后如何重新启动数据库服务
我来答
分享
关耳
2023-09-27
系统重启后如何重新启动数据库服务

系统重启后如何重新启动数据库服务

我来答
添加附件
收藏
分享
问题补充
1条回答
默认
最新
EricOmer

数据库服务启停

集群启停

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 <pgsql-bugs@postgresql.org>.
复制

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

  • -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下载地址
JDBChttps://jdbc.postgresql.org/download.html
ODBChttps://odbc.postgresql.org/ https://www.postgresql.org/ftp/odbc/versions/

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上下载源码进行安装,源码地址:https://github.com/psycopg/psycopg2,按照readme的说明操作即可。

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

python -c "import psycopg2"
复制

返回空,表示安装成功。

暂无图片 评论
暂无图片 有用 0
打赏 0
回答交流
Markdown


请输入正文
提交
相关推荐
求助,antdb数据库datanode节点宕机后怎么恢复?
回答 1
datanode节点所在主机宕机后,repmgrd后台进程会自动切换到备节点。在主机恢复后,需要将down掉的节点重新添加到集群。如果down掉的是master节点,则只能以slave的身份重回集群。
以下哪个视图能查看数据库进程的当前活动相关的信息()?
回答 1
pgstatactivity以下是这些视图的简要说明:pgstatreplication:这个视图提供了关于复制的状态信息,包括Slots和WAL流的信息,用于监控复制过程。pgstatalltabl
antdb数据库连接时,出现“FATAL: database “cz” does not exist”。
回答 1
解决方式未指定数据库实例名。adb需要指定d选项或在环境变量添加PGDATABASExxxadbdshcrmpxxxUxxx原因说明adb连接时,若不指定数据库名称,则以下述顺序优先选择:环境变量$P
什么是数据库?
回答 3
相比Excel,多了并发处理~
antdb数据库使用ora2pg导出分区表时,仍以继承表的方式实现,而非最新的内置分区表方式实现,这该怎么解决?
回答 1
解决方式配置文件添加下述2个新增的配置项。其中adbVERSION支持10/11,按实际情况配置即可。adbSUPPORTSPARTITION1adbVERSION11原因说明工具调整,新增了几个配置
antdb数据库连接时,为什么显示“adb_ctl: could not open PID file “/ssd/adb40sy/data/cd/adb_hba.conf/postmaster.pid”: Not a directory”。
回答 1
解决方式通过adbctlreload重新加载配置文件时,只需要指定D,即数据库的数据目录,勿需指定到具体的配置文件。程序会自动重新加载改动后的配置信息。以下给出一个示例说明:–错误的方式adbctlD
国产数据库选哪个比较好?
回答 1
每个数据库都有各自的优势与独特的竞争力,需根据需求类型来进行挑选AntDB数据库始于2008年,在运营商的核心系统上,为全国24个省份的10亿多用户提供在线服务,具备高性能、弹性扩展、高可靠等产品特性
对maintenance_work_mem参数的描述
回答 1
选A,数据库的维护操作使用的内存大小
antdb数据库显示“LOG: number of page slots needed (X) exceeds max_fsm_pages (Y)”。
回答 1
解决方式maxfsmpages最大自由空间映射不足。建议增加maxfsmpages的同时进行VACUUMFULL原因说明maxfsmpages最大自由空间映射不足
antdb数据库数据备份提示“WAL archive: FAILED (please make sure WAL shipping is setup)”该怎么处理?
回答 1
执行barmancheck命令时,返回上述报错信息barmanc/aifs01/users/antdb/barman/conf/datanode0.confcheckdatanode0下面给出一个完整