连接和使用vCenter Server嵌入式vPostgres数据库

发布于:2024-05-09 ⋅ 阅读:(23) ⋅ 点赞:(0)

vCenter Server 早期支持内嵌(embedded)和外部(external)数据库,内嵌数据库就是vPostgres,基于VMware Postgres数据库(PostgreSQL数据库),外部数据库用的多的是Oracle数据库和SQL Server数据库。因为早期使用内嵌的PostgreSQL数据库只能用于小型环境,比如仅支持几十台主机以及几百个虚拟机,所以一般大型的环境都使用外部数据库进行部署,这跟早期基于Platform Services Controller(PSC)内嵌和分离部署方式类似,而现在VMware只使用内嵌vPostgres数据库了,因为越往后面更新的版本功能性能越来越强,这与使用外部数据库的差距就不大了,后来PSC也仅支持内嵌部署,在简化产品构成的复杂性的同时也与产品本身更紧密集成了。

注意:进行数据库操作之前,说明已知晓风险并承担可能带来的后果。此操作不受VMware支持,请对vCenter Server做好备份和快照。

要连接到vPostgres数据库,需要先连接到vCenter Server的Shell命令行界面,所以需要先为vCenter启用SSH连接并使用root用户登录。其中,连接数据库需要用到psql命令,psql是vPostgres数据库自带的交互式命令行管理工具,该工具位于vCenter的以下目录中。

/opt/vmware/vpostgres/current/bin/psql

需要注意的是,在操作vPostgres数据库时,应该使用位于/opt/vmware/vpostgres/current/bin/中的psql二进制文件,而不是直接使用操作系统的psql二进制文件,因为这两者的版本存在一定区别。

/opt/vmware/vpostgres/current/bin/psql --version
psql --version

在连接使用vPostgres数据库之前,先来看一下psql命令工具的使用帮助。

root@vcenter [ ~ ]# /opt/vmware/vpostgres/current/bin/psql --help
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: "root")
  -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
      --csv                CSV (Comma-Separated Values) 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: "/var/run/vpostgres")
  -p, --port=PORT          database server port (default: "5432")
  -U, --username=USERNAME  database user name (default: "root")
  -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@lists.postgresql.org>.
PostgreSQL home page: <https://www.postgresql.org/>

psql命令后面可以跟多个选项,比如连接选项中,-h或--host指定连接的数据库主机,-p或--port指定连接数据库的端口号(默认5432),-U或--username指定连接数据库的用户名,-W或--password指定连接数据库的密码,在通用选项中,-d或--dbname指定连接的数据库名称。通过上述选项可以连接到某个数据库中并进入交互式命令行界面,如果不想进入交互式界面,可以使用-c或者--command直接在Shell中运行数据库指令,使用-l或--list选项列出所有可用的数据库名称,-f或--file选择可以执行本地目录中的一个脚本文件。还可以设定输入input和输出output选项以及输出output的格式,比如-b选项指定命令错误了输出,-q选项静默执行,-t选项仅输出结果等等。更多详细解释请看psql说明。

/opt/vmware/vpostgres/current/bin/psql -U postgres -l

默认情况下,vCenter Server内嵌式vPostgres数据库的用户名是 postgres,这是一个superuser具有超级管理员权限,通过本地登录无需要密码皆可连接,如果想通过外面其他客户端进行登录则还需要进行其他设置,后面会说。早期的vCenter Server版本还有一个用户 vc 也具有管理员权限,不过最新的版本中该用户依然存在不过无法进行数据库创建等操作。连接数据库后,我们可以使用\du或\du+或\dg或\dg+的psql基本命令查看数据库中的所有用户及其分配的角色。

/opt/vmware/vpostgres/current/bin/psql -U postgres -d VCDB

下图是vCenter Server 6.7版本中嵌入式vPostgres数据库中所有的用户。

下图是vCenter Server 8.0版本中嵌入式vPostgres数据库中所有的