vCenter Server 早期支持內嵌(embedded)和外部(external)資料庫,內嵌資料庫就是vPostgres,基於VMware Postgres資料庫(PostgreSQL資料庫),外部資料庫用的多的是Oracle資料庫和SQL Server資料庫。因為早期使用內嵌的Postgr ...
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 <[email protected]>.
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資料庫中所有的用戶。
查看vPostgres資料庫的配置文件,vCenter Server預設的資料庫實例是VCDB,資料庫的存放位置是/storage/db/vpostgres。JDBC地址以及PGUSER_PASSWORD密碼後面通過外部客戶端連接的時候會用到。
cat /etc/vmware-vpx/embedded_db.cfg
cat /etc/vmware-vpx/vcdb.properties
使用psql工具連接到vPostgres資料庫以後,進入互動式命令行界面,可以使用help查看幫助。
/opt/vmware/vpostgres/current/bin/psql -U postgres -d VCDB
使用\?可以查看 psql 基本命令。
VCDB=# \?
General
\copyright show PostgreSQL usage and distribution terms
\crosstabview [COLUMNS] execute query and display results in crosstab
\errverbose show most recent error message at maximum verbosity
\g [(OPTIONS)] [FILE] execute query (and send results to file or |pipe);
\g with no arguments is equivalent to a semicolon
\gdesc describe result of query, without executing it
\gexec execute query, then execute each value in its result
\gset [PREFIX] execute query and store results in psql variables
\gx [(OPTIONS)] [FILE] as \g, but forces expanded output mode
\q quit psql
\watch [SEC] execute query every SEC seconds
Help
\? [commands] show help on backslash commands
\? options show help on psql command-line options
\? variables show help on special variables
\h [NAME] help on syntax of SQL commands, * for all commands
Query Buffer
\e [FILE] [LINE] edit the query buffer (or file) with external editor
\ef [FUNCNAME [LINE]] edit function definition with external editor
\ev [VIEWNAME [LINE]] edit view definition with external editor
\p show the contents of the query buffer
\r reset (clear) the query buffer
\s [FILE] display history or save it to file
\w FILE write query buffer to file
Input/Output
\copy ... perform SQL COPY with data stream to the client host
\echo [-n] [STRING] write string to standard output (-n for no newline)
\i FILE execute commands from file
\ir FILE as \i, but relative to location of current script
\o [FILE] send all query results to file or |pipe
\qecho [-n] [STRING] write string to \o output stream (-n for no newline)
\warn [-n] [STRING] write string to standard error (-n for no newline)
Conditional
\if EXPR begin conditional block
\elif EXPR alternative within current conditional block
\else final alternative within current conditional block
\endif end conditional block
Informational
(options: S = show system objects, + = additional detail)
\d[S+] list tables, views, and sequences
\d[S+] NAME describe table, view, sequence, or index
\da[S] [PATTERN] list aggregates
\dA[+] [PATTERN] list access methods
\dAc[+] [AMPTRN [TYPEPTRN]] list operator classes
\dAf[+] [AMPTRN [TYPEPTRN]] list operator families
\dAo[+] [AMPTRN [OPFPTRN]] list operators of operator families
\dAp[+] [AMPTRN [OPFPTRN]] list support functions of operator families
\db[+] [PATTERN] list tablespaces
\dc[S+] [PATTERN] list conversions
\dC[+] [PATTERN] list casts
\dd[S] [PATTERN] show object descriptions not displayed elsewhere
\dD[S+] [PATTERN] list domains
\ddp [PATTERN] list default privileges
\dE[S+] [PATTERN] list foreign tables
\des[+] [PATTERN] list foreign servers
\det[+] [PATTERN] list foreign tables
\deu[+] [PATTERN] list user mappings
\dew[+] [PATTERN] list foreign-data wrappers
\df[anptw][S+] [FUNCPTRN [TYPEPTRN ...]]
list [only agg/normal/procedure/trigger/window] functions
\dF[+] [PATTERN] list text search configurations
\dFd[+] [PATTERN] list text search dictionaries
\dFp[+] [PATTERN] list text search parsers
\dFt[+] [PATTERN] list text search templates
\dg[S+] [PATTERN] list roles
\di[S+] [PATTERN] list indexes
\dl list large objects, same as \lo_list
\dL[S+] [PATTERN] list procedural languages
\dm[S+] [PATTERN] list materialized views
\dn[S+] [PATTERN] list schemas
\do[S+] [OPPTRN [TYPEPTRN [TYPEPTRN]]]
list operators
\dO[S+] [PATTERN] list collations
\dp [PATTERN] list table, view, and sequence access privileges
\dP[itn+] [PATTERN] list [only index/table] partitioned relations [n=nested]
\drds [ROLEPTRN [DBPTRN]] list per-database role settings
\dRp[+] [PATTERN] list replication publications
\dRs[+] [PATTERN] list replication subscriptions
\ds[S+] [PATTERN] list sequences
\dt[S+] [PATTERN] list tables
\dT[S+] [PATTERN] list data types
\du[S+] [PATTERN] list roles
\dv[S+] [PATTERN] list views
\dx[+] [PATTERN] list extensions
\dX [PATTERN] list extended statistics
\dy[+] [PATTERN] list event triggers
\l[+] [PATTERN] list databases
\sf[+] FUNCNAME show a function's definition
\sv[+] VIEWNAME show a view's definition
\z [PATTERN] same as \dp
Formatting
\a toggle between unaligned and aligned output mode
\C [STRING] set table title, or unset if none
\f [STRING] show or set field separator for unaligned query output
\H toggle HTML output mode (currently off)
\pset [NAME [VALUE]] set table output option
(border|columns|csv_fieldsep|expanded|fieldsep|
fieldsep_zero|footer|format|linestyle|null|
numericlocale|pager|pager_min_lines|recordsep|
recordsep_zero|tableattr|title|tuples_only|
unicode_border_linestyle|unicode_column_linestyle|
unicode_header_linestyle)
\t [on|off] show only rows (currently off)
\T [STRING] set HTML <table> tag attributes, or unset if none
\x [on|off|auto] toggle expanded output (currently off)
Connection
\c[onnect] {[DBNAME|- USER|- HOST|- PORT|-] | conninfo}
connect to new database (currently "VCDB")
\conninfo display information about current connection
\encoding [ENCODING] show or set client encoding
\password [USERNAME] securely change the password for a user
Operating System
\cd [DIR] change the current working directory
\setenv NAME [VALUE] set or unset environment variable
\timing [on|off] toggle timing of commands (currently off)
\! [COMMAND] execute command in shell or start interactive shell
Variables
\prompt [TEXT] NAME prompt user to set internal variable
\set [NAME [VALUE]] set internal variable, or list all if no parameters
\unset NAME unset (delete) internal variable
Large Objects
\lo_export LOBOID FILE
\lo_import FILE [COMMENT]
\lo_list
\lo_unlink LOBOID large object operations
VCDB=#
使用\h可以查看支持的標準 SQL 命令。
VCDB=# \h
Available help:
ABORT CHECKPOINT CREATE USER DROP TRIGGER
ALTER AGGREGATE CLOSE CREATE USER MAPPING DROP TYPE
ALTER COLLATION CLUSTER CREATE VIEW DROP USER
ALTER CONVERSION COMMENT DEALLOCATE DROP USER MAPPING
ALTER DATABASE COMMIT DECLARE DROP VIEW
ALTER DEFAULT PRIVILEGES COMMIT PREPARED DELETE END
ALTER DOMAIN COPY DISCARD EXECUTE
ALTER EVENT TRIGGER CREATE ACCESS METHOD DO EXPLAIN
ALTER EXTENSION CREATE AGGREGATE DROP ACCESS METHOD FETCH
ALTER FOREIGN DATA WRAPPER CREATE CAST DROP AGGREGATE GRANT
ALTER FOREIGN TABLE CREATE COLLATION DROP CAST IMPORT FOREIGN SCHEMA
ALTER FUNCTION CREATE CONVERSION DROP COLLATION INSERT
ALTER GROUP CREATE DATABASE DROP CONVERSION LISTEN
ALTER INDEX CREATE DOMAIN DROP DATABASE LOAD
ALTER LANGUAGE CREATE EVENT TRIGGER DROP DOMAIN LOCK
ALTER LARGE OBJECT CREATE EXTENSION DROP EVENT TRIGGER MOVE
ALTER MATERIALIZED VIEW CREATE FOREIGN DATA WRAPPER DROP EXTENSION NOTIFY
ALTER OPERATOR CREATE FOREIGN TABLE DROP FOREIGN DATA WRAPPER PREPARE
ALTER OPERATOR CLASS CREATE FUNCTION DROP FOREIGN TABLE PREPARE TRANSACTION
ALTER OPERATOR FAMILY CREATE GROUP DROP FUNCTION REASSIGN OWNED
ALTER POLICY CREATE INDEX DROP GROUP REFRESH MATERIALIZED VIEW
ALTER PROCEDURE CREATE LANGUAGE DROP INDEX REINDEX
ALTER PUBLICATION CREATE MATERIALIZED VIEW DROP LANGUAGE RELEASE SAVEPOINT
ALTER ROLE CREATE OPERATOR DROP MATERIALIZED VIEW RESET
ALTER ROUTINE CREATE OPERATOR CLASS DROP OPERATOR REVOKE
ALTER RULE CREATE OPERATOR FAMILY DROP OPERATOR CLASS ROLLBACK
ALTER SCHEMA CREATE POLICY DROP OPERATOR FAMILY ROLLBACK PREPARED
ALTER SEQUENCE CREATE PROCEDURE DROP OWNED ROLLBACK TO SAVEPOINT
ALTER SERVER CREATE PUBLICATION DROP POLICY SAVEPOINT
ALTER STATISTICS CREATE ROLE DROP PROCEDURE SECURITY LABEL
ALTER SUBSCRIPTION CREATE RULE DROP PUBLICATION SELECT
ALTER SYSTEM CREATE SCHEMA DROP ROLE SELECT INTO
ALTER TABLE CREATE SEQUENCE DROP ROUTINE SET
ALTER TABLESPACE CREATE SERVER DROP RULE SET CONSTRAINTS
ALTER TEXT SEARCH CONFIGURATION CREATE STATISTICS DROP SCHEMA SET ROLE
ALTER TEXT SEARCH DICTIONARY CREATE SUBSCRIPTION DROP SEQUENCE SET SESSION AUTHORIZATION
ALTER TEXT SEARCH PARSER CREATE TABLE DROP SERVER SET TRANSACTION
ALTER TEXT SEARCH TEMPLATE CREATE TABLE AS DROP STATISTICS SHOW
ALTER TRIGGER CREATE TABLESPACE DROP SUBSCRIPTION START TRANSACTION
ALTER TYPE CREATE TEXT SEARCH CONFIGURATION DROP TABLE TABLE
ALTER USER CREATE TEXT SEARCH DICTIONARY DROP TABLESPACE TRUNCATE
ALTER USER MAPPING CREATE TEXT SEARCH PARSER DROP TEXT SEARCH CONFIGURATION UNLISTEN
ALTER VIEW CREATE TEXT SEARCH TEMPLATE DROP TEXT SEARCH DICTIONARY UPDATE
ANALYZE CREATE TRANSFORM DROP TEXT SEARCH PARSER VACUUM
BEGIN CREATE TRIGGER DROP TEXT SEARCH TEMPLATE VALUES
CALL CREATE TYPE DROP TRANSFORM WITH
VCDB=#
通常情況下,psql基本命令和標準的sql命令可以配合使用。使用\q可以退出 psql 互動式命令行界面,下麵看一下常用的psql基本命令。更多詳細解釋請看psql說明。
\conninfo 查看當前連接信息。
\c postgres 連接到其他資料庫。
\encoding 查看當前連接編碼。
\l或\l+ 查看所有資料庫。
\du或\du+ 查看所有用戶及分配的角色(上面說過)。
\drds 查看角色的設置。
\dn+ 查看所有schema模式。
\d+ 查看所有對象信息,包含table、view、sequence等。\dt+ 查看table,\dv+ 查看view,\ds+ 查看sequence。
\db+ 查看表空間。
\dA+ 查看訪問的方法。
\a 開啟或關閉輸出對齊模式。上面所有的預設輸出都是對齊模式,unaligned後,以 \l 為例,如下所示。
重新aligned以後,再查看資料庫,所有列都對齊了。
有的時候,當查詢一個比較大的表時,顯示的內容展示不太友好,比如\l+查看資料庫,有些內容超出第二行了。
\x 開啟expanded顯示後,這樣就方便我們進行查看了,若要關閉再次執行\x即可。
\t 僅顯示輸出的結果,不顯示標題。
關於PostgreSQL標準的SQL命令語法這裡就不展開說明瞭,詳細可以查看PostgreSQL教程,下麵舉幾個例子。
查看vCenter資料庫版本,表名可以tab補全。
SELECT * FROM vpx_version;
查看表空間占用最大的前十個。
SELECT nspname || '.' || relname AS "table",pg_size_pretty(pg_total_relation_size(C.oid)) AS "total_size" FROM pg_class C LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace) WHERE nspname NOT IN ('pg_catalog', 'information_schema') AND C.relkind <> 'i' AND nspname !~ '^pg_toast' ORDER BY pg_total_relation_size(C.oid) DESC LIMIT 10;
查看用戶登錄事件。vCenter 所有事件列表。
SELECT event_type,create_time,username,vm_name FROM vc.vpxv_events where event_type like '%UserLoginSessionEvent%';
在/opt/vmware/vpostgres/current/bin/目錄下除了psql二進位文件以外,還有其他二進位文件工具,關於這些工具的使用和說明可以在這裡查看。關於vCenter Server vPostgres資料庫的備份和還原,可以參考KB 2091961知識庫文章中的方法。
上述方式連接和使用vPostgres資料庫都是基於psql命令行,其實也支持使用圖形化客戶端工具進行管理。可以使用 pgAdmin 或者 DBeaver 客戶端圖形工具來管理vPostgres資料庫。下麵就以DBeaver社區版客戶端為例,瞭解如何連接和使用vPostgres資料庫。
預設情況下,vCenter Server資料庫只能進行本地連接,不過如果要從外部進行連接也是可以的。
需要在vCenter Server上將vPostgres資料庫的訪問埠(5432)從iptables防火牆上放開(accept)。
iptables -I INPUT 1 -p tcp --dport 5432 -j ACCEPT
配置vPostgres資料庫允許來自所有遠程主機的連接並要求postgres用戶的密碼,配置文件位於/storage/db/vpostgres/pg_hba.conf。
echo "host all postgres 0.0.0.0/0 md5" >> /storage/db/vpostgres/pg_hba.conf
重新載入vPostgres配置以應用對pg_hba.conf的更改。
/opt/vmware/vpostgres/current/bin/psql -U postgres -c "SELECT pg_reload_conf();"
查看postgres用戶的密碼。
cat /etc/vmware-vpx/embedded_db.cfg
安裝好DBeaver工具後,下麵配置連接vPostgres資料庫。
先配置用於連接PostgreSQL資料庫的驅動,展開導航欄中的“資料庫”,點擊“驅動管理器”,找到PostgreSQL資料庫並編輯。
在設置的最下麵有一行網址,點擊下載PostgreSQL標準驅動,保存至客戶端所在的本地文件夾中。
點擊“庫”,將下麵存在驅動全部刪除,點擊“添加文件”將剛剛下載的驅動加入到庫中。點擊確定完成驅動的添加。
展開導航欄中的“資料庫”,點擊“新建資料庫連接”,找到PostgreSQL資料庫並點擊下一步。
設置PostgreSQL資料庫連接,配置主機(vCenter)和資料庫(VCDB),還有用戶名(postgres)和密碼,點擊測試連接。
展開VCDB資料庫,查看所有的schema模式。
在vc schema模式下,vpx_alarm表中查看vCenter所有告警列表。
vpx_datastore表中查看vCenter所有數據存儲。
vpx_dvs表中查看vCenter所有分散式交換機。
vpx_network表中查看vCenter所有標準交換機。
vpx_vm表中查看vCenter所有虛擬機。
參考:
- Connect and work with the vCSA Embedded vPostgres Database
- Manage vCSA Embedded vPostgres Database with pgAdmin
- Run pgAdmin in a Docker container on the vCenter Server Appliance