欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Oracle 数据库连接失败问题

程序员文章站 2022-05-01 18:14:30
...
环境上安装的是oracle11g,其实oracle的安装是比较容易的,winA装server, winB装client, 最终目的winA能连上winB,并能查询数据和操作,但是由于我的环境装了两个数据库server, 一个监听1521, 一个监听1522,而客户端只有一个,长久时间没用,已经不记得当时client连的是哪一个server,一阵乱搞,越弄越瞎,结果用了半天才把环境恢复,不得不把一些弄清楚的概念记一下,以免下次遇到不至于这么手足无措

安装完后需要配置连接,首先从server开始
1 配置监听服务
Oracle 数据库连接失败问题
这个选项里面可以配置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

Oracle 数据库连接失败问题

验证:
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的范畴

client安装好后需要配置net-manager,配置连接哪个数据库,哪个服务器信息

Oracle 数据库连接失败问题

增加服务名(一般与连接的数据库一样,其实随意,就是一个名字)
Oracle 数据库连接失败问题
Oracle 数据库连接失败问题
Oracle 数据库连接失败问题
填写服务器的地址以及服务器对这个数据库开启的监听端口,默认1521,也有可能是其它的端口
Oracle 数据库连接失败问题
填写服务器上想要连的数据库
Oracle 数据库连接失败问题
设置完测试失败,因为账号不对,默认用scott/tiger进行测试
Oracle 数据库连接失败问题
更改成之前在服务器上建数据库时用的密码登录 system/password,测试成功
Oracle 数据库连接失败问题
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/aaa@qq.com

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/aaa@qq.com
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/aaa@qq.com
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/aaa@qq.com
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/aaa@qq.com
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/aaa@qq.com as sysdba               #terry查看jerry建的表
Connected.
SQL> select * from jerry.info;

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


#注意的地方
1 client连数据库时格式是   sqlplus username/aaa@qq.com             
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文件,概念有混





转载于:https://my.oschina.net/hding/blog/547521