通过sqlplus可以连接数据库根据用户权限进行数据或者设定操作,这篇文章介绍一下常用的连接方式。
环境准备
使用oracle的精简版创建docker方式的demo环境,详细可参看:
方式1(本机): / as sysdba
在oracle服务器,可以直接通过操作系统权限认证,使用sysdba方式登陆,前提是你可以登入服务器,并且拥有此权限。
oracle@e871d42341c0:~$ id uid=1000(oracle) gid=1000(dba) groups=1000(dba) oracle@e871d42341c0:~$ sqlplus / as sysdba sql*plus: release 11.2.0.2.0 production on sun oct 21 08:20:51 2018 copyright (c) 1982, 2011, oracle. all rights reserved. connected to: oracle database 11g express edition release 11.2.0.2.0 - 64bit production sql> show user user is "sys" sql>
方式2(本机): sqlplus 用户名/密码
在本机除了sysdba,还可以通过用户名和密码的方式登陆进来
oracle@e871d42341c0:~$ sqlplus system/liumiao123 sql*plus: release 11.2.0.2.0 production on sun oct 21 08:21:27 2018 copyright (c) 1982, 2011, oracle. all rights reserved. connected to: oracle database 11g express edition release 11.2.0.2.0 - 64bit production sql> show user user is "system" sql>
方式3: 通过tnsname方式
通过tns设定,保证联通性的情况下使用 sqlplus 用户名/密码@oracle实例名 的方式进行连接。
确认tns连接通畅
oracle@e871d42341c0:~$ tnsping xe tns ping utility for linux: version 11.2.0.2.0 - production on 21-oct-2018 10:32:55 copyright (c) 1997, 2011, oracle. all rights reserved. used parameter files: used tnsnames adapter to resolve the alias attempting to contact (description = (address = (protocol = tcp)(host = e871d42341c0)(port = 1521)) (connect_data = (server = dedicated) (service_name = xe))) ok (0 msec) oracle@e871d42341c0:~$
确认oracle的监听进程正常启动
oracle@e871d42341c0:~$ ps -ef |grep lsnr |grep -v grep oracle 27 1 0 oct16 ? 00:00:28 /u01/app/oracle/product/11.2.0/xe/bin/tnslsnr listener -inherit oracle@e871d42341c0:~$
连接
oracle@e871d42341c0:~$ sqlplus system/liumiao123@xe sql*plus: release 11.2.0.2.0 production on sun oct 21 10:34:04 2018 copyright (c) 1982, 2011, oracle. all rights reserved. connected to: oracle database 11g express edition release 11.2.0.2.0 - 64bit production sql> show user user is "system" sql>
方式4: ip和port的方式定位
还可通过ip和port的方式定位oracle实例进行连接:sqlplus 用户名/密码@//ip地址或者hostname:端口号/oracle实例名
# netstat -tunlp |grep 1521 tcp6 0 0 :::1521 :::* listen - # ip ad |grep 172.17 inet 172.17.0.2/16 scope global eth0 # sqlplus system/abcd1234@//172.17.0.2:1521/xe sql*plus: release 11.2.0.2.0 production on sun oct 21 10:37:31 2018 copyright (c) 1982, 2011, oracle. all rights reserved. connected to: oracle database 11g express edition release 11.2.0.2.0 - 64bit production sql>
方法5: 使用nolog 和 connect实现连接
准确的来说,这种方式和方式2/方式3/方式4没有本质区别,无非就是用户名/密码以及实例名的信息的写法不同而已,详细如下:
# sqlplus /nolog sql*plus: release 11.2.0.2.0 production on sun oct 21 11:19:50 2018 copyright (c) 1982, 2011, oracle. all rights reserved. sql> connect system/liumiao123 connected. sql> connect system/liumiao123@xe connected. sql> connect system/liumiao123@//172.17.0.2:1521/xe connected. sql> show user user is "system" sql>
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对www.887551.com的支持。如果你想了解更多相关内容请查看下面相关链接