监控Postgres数据库

本文主要探讨了如何有效地监控Postgres数据库,通过官方文档解析监控关键指标,确保数据库的稳定性和性能。
摘要由CSDN通过智能技术生成

官方文档中有关监控的章节:

 
监控软件的罗列文档:
 
其中选择安装了powa。这个可以实时监控数据库,但是没有系统cpu和内存图表。
大多数带有系统监控的,都是通过插件实现的,要多安装和配置一些东西。
 
powa的官方网站:
 
下面是安装powa的步骤,Centos系统。
 
1、下载解压powa
在官网下载代码,现在是2.0版,这个版本需要postgresql是9.4以上版本。
我的pg是9.3版,所以下载的1.2.1版本
 
wget https://github.com/dalibo/powa/archive/REL_1_2_1.zip
unzip powa-REL_1_2_1.zip
下载并解压
注:如果没有unzip命令,通过yum安装。(yum install unzip)
 
2、安装postgresql数据库9.3版本
这里不细述了。网上资料很多
 
3、安装powa
cd ./powa-REL_1_2_1
make
 
一切正常出现类似如下信息
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -g -fpic -I. -I. -I/home/thomas/postgresql/postgresql-9.3.4/include/server -I/home/thomas/postgresql/postgresql-9.3.4/include/internal -D_GNU_SOURCE -I/usr/include/libxml2   -c -o powa.o powa.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -g -fpic -L/home/thomas/postgresql/postgresql-9.3.4/lib -Wl,--as-needed -Wl,-rpath,'/home/thomas/postgresql/postgresql-9.3.4/lib',--enable-new-dtags  -shared -o powa.so powa.o
 
安装
make install
 
4、创建powa用的数据库
进入postgresql执行下列命令
$>psql
psql (9.3.5)
Type "help" for help.
postgres=# create database powa;
CREATE DATABASE
postgres=# \c powa
You are now connected to database "powa" as user "postgres".
powa=# create extension pg_stat_statements ;
CREATE EXTENSION
powa=# create extension btree_gist ;
CREATE EXTENSION
powa=# create extension powa;
CREATE EXTENSION
powa=# \dt
                        List of relations
  Schema |              Name               | Type  |  Owner
--------+---------------------------------+-------+----------
  public | powa_functions                  | table | postgres
  public | powa_last_aggregation           | table | postgres
  public | powa_last_purge                 | table | postgres
  public | powa_statements                 | table | postgres
  public | powa_statements_history         | table | postgres
  public | powa_statements_history_current | table | postgres
(6 rows)
 
 
5、修改postgresql配置
修改postgressql.conf文件
shared_preload_libraries = 'powa,pg_stat_statements'# (change requires restart)
....track_io_timing= on
 
允许本地用户连接powa数据库,在数据库配置文件pg_hba.conf中添加
host powa all 127.0.0.1/32 md5
重启数据库
service postgresql-9.3 restart
或者
pg_ctl restart -m f
 
至此服务端检测部分安装完成,下面是客户端查看部分。
 
6、安全性
注意客户端的连接安全性。有以下几点可以考虑:
·检查pg_hba.conf文件
·不要允许用户通过网络连接powa
·不要允许用户通过网络连接数据库
·以https方式连接powa
·使用SSL连接powa的GUI和Postgresql
·拒绝非保护的连接
 
7、需要的前提程序
·Perl 5.10
·Perl DBI and DBD-Pg modules
·Mojolicious 4.75 and later (or A CGI/Perl webserver)
以上版本是测试过的,更老的版本可能可以运行。
 
A)安装Perl
一句安装(各系统不同):yum install perl
或者手动安装:
下载你需要的版本,下载地址:http://www.activestate.com/activeperl/downloads
% tar zxf ActivePerl-5.18.4.1804-i686-linux-64int-glibc-2.5-298913.tar.gz
% cd ActivePerl-5.18.4.1804-i686-linux-64int-glibc-2.5-298913
% ./install.sh
 
安装过程中回答几个问题,最重要的就是安装目录
Did you read the LICENSE.txt file? [no] yes
Do you agree to the ActivePerl is covered by the ActiveState Community License.? [no] yes

Enter top level directory for install? [/opt/ActivePerl-5.18] /opt/ActivePerl-5.18

Install HTML documentation [yes] yes

Proceed? [yes] yes
 
设置环境变量
# cd /etc/profile.d
# vi perl.sh
 
#set perl environment
#set perl environment
PERL5LIB=/opt/ActivePerl-5.18/lib:/opt/ActivePerl-5.18/site/lib
PATH=/opt/ActivePerl-5.18/bin:/opt/ActivePerl-5.18/site/bin:$PATH
export PERL5LIB  PATH
 
# source perl.sh
 
初始化CPAN
cpan
cpan>o conf init urllist
让他自动配置就好了
sites for you? (This means connecting to the Internet) [yes] yes
最后提交
cpan>o conf commit
 
B)安装DBI
DBI一般都同时安装了,查看是否安装
cpan
cpan>m DBI
 
C)安装DBD-Pg
cpan
cpan>install Bundle::DBD::Pg
 
D)安装Mojolicious
下载地址:http://backpan.perl.org/authors/id/S/SR/SRI/Mojolicious-4.75.tar.gz
tar zxf Mojolicious-4.75
cd Mojolicious-4.75
perl Makefile.PL PREFIX=/path/to/powa/mojo
make
make install
 
8、配置powa
 
cd /path/to/powa/ui/
cp powa.conf-dist powa.conf
vi powa.conf
修改"servers"里的内容,数据库的IP端口什么的,可以增加服务器子节点
 
9、启动powa
/path/to/powa/mojo/bin/morbo /path/to/powa/ui/script/powa
 
10、登陆
http://ip:3000
登陆用户密码是数据库的用户密码
 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果存在数据库连接泄漏的情况,即连接没有正确关闭导致连接池中的连接被耗尽,可以尝试以下方法来解决问题: 1. 检查代码:仔细检查应用程序中的代码,确保在每个数据库连接使用完毕后都进行了正确的关闭操作。通常使用 try-catch-finally 块来确保连接在任何情况下都能够被关闭。例如: ```java Connection conn = null; try { // 获取数据库连接 conn = dataSource.getConnection(); // 执行数据库操作 // ... } catch (SQLException e) { // 处理异常 } finally { // 关闭数据库连接 if (conn != null) { try { conn.close(); } catch (SQLException e) { // 处理异常 } } } ``` 2. 使用连接池管理工具:确保正确使用连接池管理工具,如 Apache Commons DBCP、HikariCP 等。这些工具可以自动管理连接的获取和释放,避免手动管理连接时出现错误。 3. 检查连接池配置:确保连接池的配置参数正确设置。特别注意连接池的最大连接数和最小空闲连接数的设置。如果这些参数设置不合理,可能导致连接被耗尽或过多空闲连接占用资源。 4. 监控连接池状态:使用连接池提供的监控功能,如 HikariCP 的监控指标、管理界面等,可以帮助你了解连接池的使用情况,及时发现连接泄漏问题。 5. 定期检查连接状态:可以编写定时任务或使用连接池提供的连接状态检查功能,定期检查连接是否被正确释放。如果发现连接没有被关闭,可以手动关闭连接或重新初始化连接池以释放泄漏的连接。 6. 配置连接超时时间:在连接池的配置中设置适当的连接超时时间,确保空闲连接在一定时间内没有被使用时能够自动关闭。 通过以上方法,可以有效地解决数据库连接泄漏问题,避免连接被耗尽,保证应用程序的稳定性和性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值