Postgresqlデータベース監視実験07-PostgreSQLのインストールと監視

Postgresqlデータベース監視実験07-PostgreSQLのインストールと監視

環境

マシン:10.176.140.72
モバイルクラウドの
plat-ecloud01-mgmt-monitor04 monitor04

オペレーティングシステム:
CentOS Linuxリリース7.3.1611(コア)

InfluxDBバージョン:
influxdb-1.7.9

テレグラフバージョン:
telegraf-1.12.6

grafanaバージョン本:
grafana-4.3.1

postgresqlバージョン:
postgresql12-12.1

postgresqlをインストールする

参考:公式サイト文書

最新のリポジトリをダウンロードします。

[root@plat-ecloud01-mgmt-monitor04 ~]# yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm -y

クライアント、サーバー、追加モジュール、拡張パッケージをインストールします。

[root@plat-ecloud01-mgmt-monitor04 ~]# yum install postgresql12 postgresql12-server postgresql12-contrib postgresql12-devel -y

postgresqlを設定する

1.ディレクトリを作成します

データファイルとアーカイブログを格納するディレクトリを作成します。

[root@plat-ecloud01-mgmt-monitor04 ~]# mkdir -p /opt/data/pgdata
[root@plat-ecloud01-mgmt-monitor04 ~]# mkdir -p /opt/data/archlog
[root@plat-ecloud01-mgmt-monitor04 ~]# chown -R postgres:postgres /opt/data

2.環境変数を構成する

postgresユーザー環境変数を変更します。

[root@plat-ecloud01-mgmt-monitor04 ~]# su - postgres
-bash-4.2$ vim ./.bash_profile

[ -f /etc/profile ] && source /etc/profile
export PGPORT=9999
export PGUSER= postgres
export PGDATA=/opt/data/pgdata
export LANG=en_US.utf8

export PGHOME=/usr/pgsql-12/bin
export LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib
export DATE=`date +"%Y%m%d%H%M"`
export PATH=$PGHOME/bin:$PATH:.
export MANPATH=$PGHOME/share/man:$MANPATH
alias rm='rm -i'
alias ll='ls -lh'

# If you want to customize your settings,
# Use the file below. This is not overridden
# by the RPMS.
[ -f /var/lib/pgsql/.pgsql_profile ] && source /var/lib/pgsql/.pgsql_profile

環境変数の変更を有効にします。

-bash-4.2$ source ./.bash_profile

3.データベースを初期化する

postgresユーザーを使用して初期化コマンドを実行し、文字セットをUTF8として指定し、データディレクトリを/ opt / data / pgdata /として指定します

-bash-4.2$ /usr/pgsql-12/bin/initdb -E UTF8 --locale=C -D /opt/data/pgdata/
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "C".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /opt/data/pgdata ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Asia/Shanghai
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    /usr/pgsql-12/bin/pg_ctl -D /opt/data/pgdata/ -l logfile start

4.データベース構成を変更する

構成ファイルを変更します。

-bash-4.2$ vim /opt/data/pgdata/postgresql.conf 

data_directory = '/opt/data/pgdata'
hba_file = '/opt/data/pgdata/pg_hba.conf'
ident_file = '/opt/data/pgdata/pg_ident.conf'
listen_addresses = '*'
port = 9999
max_connections = 1000
superuser_reserved_connections = 5
shared_buffers = 3GB
wal_level = replica
fsync = on
synchronous_commit = on
archive_mode = on
archive_command = 'test ! -f /opt/data/archlog//backup_in_progress && (test ! -f /opt/data/archlog/%f && cp %p /opt/data/archlog/%f)'
'test ! -f /opt/data/archlog//backup_in_progress && (test ! -f /opt/data/archlog/%f && cp %p /opt/data/archlog/%f)'
max_wal_senders = 10
wal_keep_segments = 32
hot_standby = on
hot_standby_feedback = on

hbaファイルを変更します。

-bash-4.2$ vim /opt/data/pgdata/pg_hba.conf

host all all 0.0.0.0/0 md5
host replication all 0.0.0.0/0 trust

5.データベースを起動します

postgresユーザーを使用してデータベースを起動します

-bash-4.2$ /usr/pgsql-12/bin/pg_ctl start -D /opt/data/pgdata/
waiting for server to start....2019-12-13 10:50:39.277 CST [12881] LOG:  starting PostgreSQL 12.1 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39), 64-bit
2019-12-13 10:50:39.278 CST [12881] LOG:  listening on IPv4 address "0.0.0.0", port 9999
2019-12-13 10:50:39.278 CST [12881] LOG:  listening on IPv6 address "::", port 9999
2019-12-13 10:50:39.281 CST [12881] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.9999"
2019-12-13 10:50:39.287 CST [12881] LOG:  listening on Unix socket "/tmp/.s.PGSQL.9999"
2019-12-13 10:50:39.464 CST [12881] LOG:  redirecting log output to logging collector process
2019-12-13 10:50:39.464 CST [12881] HINT:  Future log output will appear in directory "log".
 done
server started

6.パスワードを設定する

postgresユーザーを使用してデータベースを入力し、postgresパスワードをpostgresに変更します。

-bash-4.2$ psql -p 9999
psql (12.1)
Type "help" for help.

postgres=# alter user postgres with password 'postgres';
ALTER ROLE

監視構成

1.テストデータを作成する

ビルドライブラリテスト

postgres=# create database test ;
CREATE DATABASE

テーブルを作成してデータを挿入します。

postgres=# \c test
You are now connected to database "test" as user "postgres".
test=# select i,'t:'||i as t into t from generate_series(1,10000000) as i;
SELECT 10000000

テーブルサイズを見る:

test=# select pg_size_pretty(pg_relation_size('t'));
 pg_size_pretty 
----------------
 422 MB
(1 row)

ライブラリのサイズを表示:

test=# select schemaname,round(sum(pg_total_relation_size(schemaname||'.'||tablename))/1024/1024) "Mb" from pg_tables  group by 1;
     schemaname     | Mb  
--------------------+-----
 public             | 422
 pg_catalog         |   8
 information_schema |   0

2.テレグラフを構成する

参照ドキュメント:公式ドキュメント
データベースに関連する構成をテレグラフに追加する必要があります。

[root@plat-ecloud01-mgmt-monitor04 ~]# vim /etc/telegraf/telegraf.conf

 # Read metrics from one or many postgresql servers
 [[inputs.postgresql]]
   ## specify address via a url matching:
   ##   postgres://[pqgotest[:password]]@localhost[/dbname]\
   ##       ?sslmode=[disable|verify-ca|verify-full]
   ## or a simple string:
   ##   host=localhost user=pqotest password=... sslmode=... dbname=app_production
   ##
   ## All connection parameters are optional.
   ##
   ## Without the dbname parameter, the driver will default to a database
   ## with the same name as the user. This dbname is just for instantiating a
   ## connection with the server and doesn't restrict the databases we are trying
   ## to grab metrics for.
   ##
   # address = "host=localhost user=postgres sslmode=disable"
   address = "postgres://postgres:postgres@localhost:9999"  #数据库地址,格式参见前面介绍
   ## A custom name for the database that will be used as the "server" tag in the
   ## measurement output. If not specified, a default one generated from
   ## the connection address is used.
   # outputaddress = "db01"

   ## connection configuration.
   ## maxlifetime - specify the maximum lifetime of a connection.
   ## default is forever (0s)
   max_lifetime = "0s"

   ## A  list of databases to explicitly ignore.  If not specified, metrics for all
   ## databases are gathered.  Do NOT use with the 'databases' option.
   # ignored_databases = ["postgres", "template0", "template1"]
   ignored_databases = ["postgres", "template0", "template1"]  #忽略括号内数据库
   ## A list of databases to pull metrics about. If not specified, metrics for all
   ## databases are gathered.  Do NOT use with the 'ignored_databases' option.
   # databases = ["app_production", "testing"]
   # databases = ["test"] #只监控括号内数据库不能与ignored_databases同用

テレグラフを再起動します。

[root@plat-ecloud01-mgmt-monitor04 ~]# systemctl restart telegraf

3.検証

influxdbのtelegrafライブラリを確認し、postgresqlという名前の新しい測定値が作成されたことを確認します。

[root@plat-ecloud01-mgmt-monitor04 ~]# influx -precision rfc3339 -database telegraf -execute 'select * from postgresql limit 3'
name: postgresql
time                 blk_read_time blk_write_time blks_hit blks_read buffers_alloc buffers_backend buffers_backend_fsync buffers_checkpoint buffers_clean checkpoint_sync_time checkpoint_write_time checkpoints_req checkpoints_timed conflicts datid datname  db       deadlocks host                         maxwritten_clean numbackends server                                             temp_bytes temp_files tup_deleted tup_fetched tup_inserted tup_returned tup_updated xact_commit xact_rollback
----                 ------------- -------------- -------- --------- ------------- --------------- --------------------- ------------------ ------------- -------------------- --------------------- --------------- ----------------- --------- ----- -------  --       --------- ----                         ---------------- ----------- ------                                             ---------- ---------- ----------- ----------- ------------ ------------ ----------- ----------- -------------
2019-12-13T06:46:20Z 0             0              20586    254       3241          135511          0                     535                0             12                   149643                3               45                0         14185 postgres postgres 0         plat-ecloud01-mgmt-monitor04 0                0           dbname=test host=localhost port=9999 user=postgres 0          0          0           4182        0            290465       0           478         0
2019-12-13T06:46:20Z 0             0              25572    83830                                                                                                                                                                       0         16384 test     test     0         plat-ecloud01-mgmt-monitor04                  1           dbname=test host=localhost port=9999 user=postgres 141400000  2          68          5629        10100113     287196       3           471         1
2019-12-13T06:46:30Z 0             0              20663    254       3333          135511          0                     535                0             12                   149643                3               45                0         14185 postgres postgres 0         plat-ecloud01-mgmt-monitor04 0                0           dbname=test host=localhost port=9999 user=postgres 0          0          0           4193        0            291704       0           480         0

データが正常にフェッチされ、influxdbのように保存されていることを確認します

4.配置grafana

公式ウェブサイトで利用可能なテンプレートを直接検索:公式テンプレートアドレス

ここではPostgres Overview-more infoテンプレートを使用しています。IDは7626です。
ここに画像の説明を挿入
インポート方法については、postgresqlデータベース監視実験05-Grafanaインストール構成を参照してください。

grafanaをインポートすると、次のように表示されます。
ここに画像の説明を挿入

元の記事を136件公開 58のように 360,000以上を訪問

おすすめ

転載: blog.csdn.net/sunbocong/article/details/103527674