Oracle 數據庫鏈接失敗問題

環境上安裝的是oracle11g,其實oracle的安裝是比較容易的,winA裝server, winB裝client, 最終目的winA能連上winB,並能查詢數據和操做,可是因爲個人環境裝了兩個數據庫server, 一個監聽1521, 一個監聽1522,而客戶端只有一個,長久時間沒用,已經不記得當時client連的是哪個server,一陣亂搞,越弄越瞎,結果用了半天才把環境恢復,不得不把一些弄清楚的概念記一下,以避免下次遇到不至於這麼手足無措

安裝完後須要配置鏈接,首先從server開始
1 配置監聽服務

這個選項裏面能夠配置listener文件,也能夠自定義監聽端口,其配置生效文件在
C:\app\Administrator\product\11.2.0\dbhome_1\NETWORK\ADMIN\listener.ora
# listener.ora Network Configuration File: C:\app\Administrator\product\11.2.0\dbhome_1\network\admin\listener.ora
# Generated by Oracle configuration tools.
#listene_2是新建的測試鏈接,通常就是默認的listener,1521的那個
LISTENER_2 =                                                           #這是我建的新的鏈接,端口是1531做測試
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = server-ip)(PORT = 1531))      #HOST直接用IP,域名,計算機名都是出錯的地方
    )
  )

ADR_BASE_LISTENER_2 = C:\app\Administrator

SID_LIST_LISTENER =
  (SID_LIST =
    (SID_DESC =
      (SID_NAME = CLRExtProc)
      (ORACLE_HOME = C:\app\Administrator\product\11.2.0\dbhome_1)
      (PROGRAM = extproc)
      (ENVS = "EXTPROC_DLLS=ONLY:C:\app\Administrator\product\11.2.0\dbhome_1\bin\oraclr11.dll")
    )
  )

LISTENER =                                                          #這個是原始建的listener文件,1521,通常就用這個監聽
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
    )
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = server-ip)(PORT = 1521))
    )
  )

ADR_BASE_LISTENER = C:\app\Administrator
從這個配置文件能夠看出oracle是把全部的listen配置統一寫在一個文件中,開啓服務時,一塊兒開啓的
服務器只須要listener.ora文件便可,切記修改完後,必定要把監聽服務重啓
-----------------------------------------------------------------------------------
C:\Documents and Settings\Administrator>lsnrctl stop
-----------------------------------------------------------------------------------
C:\Documents and Settings\Administrator>lsnrctl start
----------------------------------------------------------------------------------
也能夠在"個人電腦->右擊manage->調出service

驗證:
C:\Documents and Settings\Administrator>netstat -ano

Active Connections

  Proto  Local Address          Foreign Address        State           PID
  TCP    0.0.0.0:135            0.0.0.0:0              LISTENING       1060
  TCP    0.0.0.0:445            0.0.0.0:0              LISTENING       4
  TCP    0.0.0.0:1025           0.0.0.0:0              LISTENING       1512
  TCP    0.0.0.0:1531           0.0.0.0:0              LISTENING       3472
  TCP    0.0.0.0:2596           0.0.0.0:0              LISTENING       2848
  TCP    0.0.0.0:2661           0.0.0.0:0              LISTENING       3420
  TCP    0.0.0.0:3389           0.0.0.0:0              LISTENING       1916
  TCP    10.8.116.232:139       0.0.0.0:0              LISTENING       4
  TCP    10.8.116.232:1521      0.0.0.0:0              LISTENING       3588
  TCP    10.8.116.232:1522      0.0.0.0:0              LISTENING       1112
  TCP    10.8.116.232:1522      10.8.116.232:2662      ESTABLISHED     1112
  TCP    10.8.116.232:1531      10.8.95.177:1141       ESTABLISHED     3472
  TCP    10.8.116.232:1531      10.8.116.232:2660      ESTABLISHED     3472

若是服務器本機須要自測,則要須要配置tnsname.ora文件或利用oracle提供的net-manager工具,其實這是client的範疇 python

client安裝好後須要配置net-manager,配置鏈接哪一個數據庫,哪一個服務器信息 sql

增長服務名(通常與鏈接的數據庫同樣,其實隨意,就是一個名字)



填寫服務器的地址以及服務器對這個數據庫開啓的監聽端口,默認1521,也有多是其它的端口

填寫服務器上想要連的數據庫

設置完測試失敗,由於帳號不對,默認用scott/tiger進行測試

更改爲以前在服務器上建數據庫時用的密碼登陸 system/password,測試成功

net-manager作這麼多的事,只是爲了生成一個tnsname.ora文件
#C:\app\Administrator\product\11.2.0\dbhome_1\NETWORK\ADMIN\listener.ora
# tnsnames.ora Network Configuration File: C:\app\Administrator\product\11.2.0\client_1\NETWORK\ADMIN\tnsnames.ora
# Generated by Oracle configuration tools.
剛剛的步聚新建了ORCL的鏈接信息,數據庫orcl, 服務器地址10.8.116.232, 監聽端口1521
ORCL =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 10.8.116.232)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = orcl)
    )
  )
因爲服務器有兩個數據庫,能夠直接編寫一個鏈接服務器dh數據庫的信息放在同一文件中,進行保存,若是服務器只有orcl,則不須要下面的這些信息
DH =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 10.8.116.232)(PORT = 1531))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = dh)
    )
  )
--------------------------------------------------------------------------------
 Microsoft Windows [Version 5.2.3790]
(C) Copyright 1985-2003 Microsoft Corp.

C:\Documents and Settings\Administrator>sqlplus system/password@dh

SQL*Plus: Release 11.2.0.1.0 Production on Fri Dec 18 18:32:13 2015

Copyright (c) 1982, 2010, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> select sysdate from dual;

SYSDATE
---------
18-DEC-15

SQL> discon
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Pr
oduction
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> conn terry/password@orcl
Connected.
SQL>

SQL基本操做: 數據庫

client啓動sqlplus
C:\Documents and Settings\Administrator>sqlplus /nolog

SQL*Plus: Release 11.2.0.1.0 Production on Fri Dec 18 18:51:33 2015

Copyright (c) 1982, 2010, Oracle.  All rights reserved.
鏈接
SQL> conn terry/password@orcl
Connected.
用戶和權限
建立用戶
create user 用戶名 identified by 口令 [account lock| unlock]
 SQL> create user orcl identified by password;                            #一行寫完

User created.
SQL> create user jerry                                                    #分行寫完
  2  identified by password
  3  account unlock;

User created.
受權: grant 角色|權限 to 用戶
SQL> grant connect to orcl;                                              #orcl有鏈接和查看的權利,但沒有寫的權利

Grant succeeded.

SQL> grant connect to jerry;

Grant succeeded.

SQL> grant resource to jerry;                                            #jerry有操做數據庫的權利

Grant succeeded.
建表
SQL> conn orcl/password@orcl
Connected.
SQL> select sysdate from dual;

SYSDATE
---------
18-DEC-15
SQL> create table scores
  2  (id number,
  3  term varchar2(2));
create table scores
*
ERROR at line 1:
ORA-01031: insufficient privileges


SQL> discon
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Pr
oduction
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> conn jerry/password@orcl
Connected.
SQL> create table scores
  2  (
  3  id number,
  4  term varchar2(2)
  5  );

Table created.
更改密碼   alter user 用戶名 identified by 新密碼
更改鎖定狀態 alter user 用戶名 account lock|unlock

查詢: select *|列名| from 表名 where 條件 order by 列名

SQL> select id from scores;

        ID
----------
    187186


插入: insert into 表名(列名1, 列名2 ..)values(值1,值2)
SQL> insert into scores values
  2  (187186,'dh');

1 row created.
更新: update 表名 set 列名1=值, 列名2=值.....where 條件
SQL> update scores set
  2  id=911,term='zj';

1 row updated.

SQL> select * from scores;

        ID TE
---------- --
       911 zj


刪除: delete from 表名 where 條件

SQL> delete from scores;

1 row deleted.


SQL> select * from scores;

no rows selected

SQL> commit;                                          #只有提交了,才真正改了數據庫中的表,不然其它用戶仍是見不到的val

Commit complete.

SQL> conn terry/password@orcl as sysdba               #terry查看jerry建的表
Connected.
SQL> select * from jerry.info;

NAME          SCORE
-------- ----------
terry        187186


#注意的地方
1 client連數據庫時格式是   sqlplus username/password@database             
client端database是必需有的,可是在服務器本地測試則能夠不加,當服務器只有惟一數據庫時
2 client因爲我默認1521,但我服務器上有兩個數據庫,用的那一個不是1521致使tns adaptor出錯
tnsname.ora的服務器地址,監聽端口,數據庫沒填對
3 監聽服務沒有起來,致使 TNS no listener
4 server更改完listener,沒有把監聽服務重啓,沒生效
5 scott/tiger帳戶被鎖,密碼過時,致使連不上
6 client只須要tnsname.ora文件, server只須要listener.ora文件,概念有混
相關文章
相關標籤/搜索