Asql 工具介绍

Asql 是一个用于执行psql命令的工具,在运维工作中,不仅要经常切换不同的数据库,还经常会使用大量的SQL语句。在工作中我们或多或少都积累了一些自身的SQL脚本,但由于SQL语句中可能会带有变量,原SQL的psql不太好处理。所以ASQL就出来了。 就是它是一个依托于psql的执行文件。我们可以提前配置好连接串,和准备好SQL文件就可以。下面我们来演示一下吧。看看它有什么功能,如果你有更好的想法或者需求,也可以联系我更新完善 ,其期待大家一起完善相应的SQL脚本。

1.工能介绍

  • 可以直接执行psql 原始命令如\l ,在asql中执行 \l
  • 支持切换数据库,快捷键 c 或 \c
  • 支持直接执行.sql 文件
  • 支持sql 文件中带参数
  • 可以快速选择其它数据库

2.准备工作

准备环境变更,除了要求有权限运行psql外,还需要设置一个SQLDIR目录。 同时我们提前编写好.dblist文件,需要连接的数据库。

2.1环境准备

# 1.环境变量,如果有设置,会在进入的时候提示使输入路径
export  SQLDIR=/home/postgres/dba

# 2.设置连接串, 格式需要按照要求写
cat  .dblist
# pg
业务数据库: s2db:  psql -U user02 -w user02 -h 192.168.1.55 -p 5432 -d s2db
监控数据库: zcloud :  psql -U user01 -w user01 -h 192.168.1.55 -p 5432 -d zcloud

#3.执行权限
chmod u+x    asql

2.2SQL脚本

这里我就用getpar.sql 和 db_connect.sql 这2个SQL脚本,getpar.sql 是带有参数&parname

#参数查询 
postgres@s2ahumysqlpg01-> cat getpar.sql
\echo 数据库参数
select  name,setting,unit,boot_val,reset_val from pg_settings where name like  '&parname%' ;

# 连接数统计
postgres@s2ahumysqlpg01-> cat db_connect.sql

\echo 数据库连接数查看
select max_conn, max_conn-now_conn as resi_conn, now_conn from (select setting::int8 as max_conn,(select count(*) from pg_stat_activity) as now_conn from pg_settings where name = 'max_connections') t;

3.使用示例

3.1 选择数据库

postgres@s2ahumysqlpg01-> ./asql
******************************************* 
******************************************* 
******************************************* 
******************************************* 
*********      欢迎使用ASQL       ********* 
******************************************* 
******************************************* 
******************************************* 
******************************************* 

     1  # pg
     2  业务数据库: s2db:  psql -U user02 -w user02 -h 192.168.1.55 -p 5432 -d s2db
     3  监控数据库: zcloud :  psql -U user01 -w user01 -h 192.168.1.55 -p 5432 -d zcloud

请输入数据库NO:2
你将要在 192.168.1.55:s2db 数据库上执行操作
192.168.1.55:user02@s2db>

3.2 执行psql原使命

192.168.1.55:user02@s2db>\\du
                                             List of roles
    Role name     |                         Attributes                         |       Member of       
------------------+------------------------------------------------------------+-----------------------
 app1             |                                                            | {}
 app2             |                                                            | {}
 asher            |                                                            | {}
 barman           | Superuser, Create role, Create DB                          | {}
 huyi             |                                                            | {}
 huyi2            |                                                            | {}
 postgres         | Superuser, Create role, Create DB, Replication, Bypass RLS | {pg_stat_scan_tables}
 repluser         | Replication                                                | {}
 streaming_barman | Replication                                                | {}
 test             |                                                            | {}
 user01           | Superuser                                                  | {}
 user02           | Superuser                                                  | {}

3.3执行sql文件不带参数

192.168.1.55:user02@s2db>db_connect.sql
数据库连接数查看
 max_conn | resi_conn | now_conn 
----------+-----------+----------
      100 |        90 |       10
(1 row)

3.4执行sql文件带参数

192.168.1.55:user02@s2db>getpar.sql
Please input &parname :max_conne      
输入的参数值是: max_conne
数据库参数
      name       | setting | unit | boot_val | reset_val 
-----------------+---------+------+----------+-----------
 max_connections | 100     |      | 100      | 100
(1 row)

3.5 切换dbname

192.168.1.55:user01@zcloud>c
                                   List of databases
   Name    |  Owner   | Encoding | Collate |    Ctype    |      Access privileges       
-----------+----------+----------+---------+-------------+------------------------------
 db01      | user01   | UTF8     | C       | zh_CN.UTF-8 | 
 db02      | user02   | UTF8     | C       | zh_CN.UTF-8 | 
 huyidb1   | postgres | UTF8     | C       | C           | 
 huyidb2   | postgres | UTF8     | C       | C           | 
 postgres  | postgres | UTF8     | C       | C           | 
 s2db      | postgres | UTF8     | C       | C           | 
 template0 | postgres | UTF8     | C       | C           | =c/postgres                 +
           |          |          |         |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | C       | C           | =c/postgres                 +
           |          |          |         |             | postgres=CTc/postgres
 test      | postgres | UTF8     | C       | C           | 
 testdb    | postgres | UTF8     | C       | C           | 
 zcloud    | postgres | UTF8     | C       | C           | =Tc/postgres                +
           |          |          |         |             | postgres=CTc/postgres       +
           |          |          |         |             | zcloud_repl=CTc/postgres    +
           |          |          |         |             | zcloud_bk=CTc/postgres      +
           |          |          |         |             | zcloud_ha=CTc/postgres      +
           |          |          |         |             | zcloud_monitor=CTc/postgres +
           |          |          |         |             | zcloud_platform=CTc/postgres
(11 rows)

请输入你要切换的数据库:huyidb2
请输入要切换的用户:user02
请输入你密码:user02
 你的数据库是: user02@huyidb2 
192.168.1.55:user02@huyidb2>

3.5 查看帮助

192.168.1.55:user02@s2db>h
press  d  list db connect
press  c  switch dbname 
press  s  list sqlhelp
press  l  list sql file in the sqldir
press  r  reset SQLDIR
press  h  list help
You can directly enter the SQL statement to execute
You can execute the sql file, files that must end with   .sql 
if you want execute pg command like \d,  please add '\', like this  \\d 
if you want exit,  please enter : ctrl + c ora  Q  or  q  


192.168.1.55:user02@s2db>
192.168.1.55:user02@s2db>
192.168.1.55:user02@s2db>q
postgres@s2ahumysqlpg01-> 

4.下载连接

墨天轮下载: https://www.modb.pro/download/483370





posted @ 2022-03-10 12:52  www.cqdba.cn  阅读(417)  评论(0编辑  收藏  举报