1、linux 需要用 sqlplus 客戶端去連接oracle 數據庫,首先需要確認有沒有安裝:which sqlplus
2、如果沒有安裝就需要先安裝一下(百度)
3、配置環境變量:
vim /etc/profile
4、執行 source /etc/profile
5、whereis oracle #查看oracle 客戶端安裝路徑
6、 進入客戶端目錄
7、編輯配置文件:vim tnsnames.ora
ORCL =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = servicename)
)
)
8、編輯保存完成以后,可以用命令行測試是否連接成功
9、sqlplus username/password@ORCL
10、編寫shell腳本(我查詢的是表空間使用率,根據需求自行修改)
#! /bin/bash
sqlplus username/password@ORCL << EOF
set linesize 200
set pagesize 200
spool /home/tmp/zxh.log
select a.tablespace_name, total, free,(total-free) as usage from
(select tablespace_name, sum(bytes)/1024/1024 as total from dba_data_files group by tablespace_name) a,
(select tablespace_name, sum(bytes)/1024/1024 as free from dba_free_space group by tablespace_name) b
where a.tablespace_name = b.tablespace_name;
spool off
quit
EOF
遇到的問題:
1、INSERT -- W10: Warning: Changing a readonly file
su root
password:《輸入你的root密碼》
然后就切換到你的root用戶,就有權限修改一些readonly的文件了
2、source /home/oracle/.bash_profile //環境變量生效