Linux中Oracle服务启动和停止脚本与开机自启动

时间:2021-11-08 20:55:34

在centos 6.3下安装完oracle 10g r2,重开机之后,你会发现oracle没有自行启动,这是正常的,因为在linux下安装oracle的确不会自行启动,必须要自行设定相关参数,首先先介绍一般而言如何启动oracle。

一、在linux下启动oracle

登录到centos,切换到oracle用户权限

?
1
# su – oracle

接着输入:

?
1
$ sqlplus "/as sysdba"

原本的画面会变为
sql>

接着请输入
sql> startup

就可以正常的启动数据库了。

Linux中Oracle服务启动和停止脚本与开机自启动

另外停止数据库的指令如下:

?
1
sql> shutdown immediate

二、检查oracle db监听器是否正常

回到终端机模式,输入:

?
1
$ lsnrctl status

检查看看监听器是否有启动

Linux中Oracle服务启动和停止脚本与开机自启动

如果没有启动,可以输入:

?
1
$ lsnrctl start

启动监听器

Linux中Oracle服务启动和停止脚本与开机自启动

?
1
sql> conn sys@orcl as sysdba

然后输入密码,sys以sysdba身份登入数据库。

Linux中Oracle服务启动和停止脚本与开机自启动

三、启动emctl

另外也可以发现http://localhost.localdomain:1158/em 目前是没有反应的,这边要另外启动,启动的指令如下:

?
1
$ emctl start dbconsole

这个指令运行时间较长,执行完的画面如下:

Linux中Oracle服务启动和停止脚本与开机自启动

手动启动oracle数据库完毕,下面创建系统自行启动oracle的脚本。

四、oracle启动&停止脚本

1. 修改oracle系统配置文件:/etc/oratab,只有这样,oracle 自带的dbstart和dbshut才能够发挥作用。

?
1
2
3
4
5
# vi /etc/oratab
orcl:/opt/oracle/102:y
 
# entries are of the form:
#  $oracle_sid:$oracle_home:<n|y>:

2. 在 /etc/init.d/ 下创建文件oracle,内容如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/sh
# chkconfig: 35 80 10
# description: oracle auto start-stop script.
 
#
# set ora_home to be equivalent to the $oracle_home
# from which you wish to execute dbstart and dbshut;
#
# set ora_owner to the user id of the owner of the
# oracle database in ora_home.
ora_home=/opt/oracle/102
ora_owner=oracle
if [ ! -f $ora_home/bin/dbstart ]
then
  echo "oracle startup: cannot start"
  exit
fi
case "$1" in
'start')
# start the oracle databases:
echo "starting oracle databases ... "
echo "-------------------------------------------------" >> /var/log/oracle
date +" %t %a %d : starting oracle databases as part of system up." >> /var/log/oracle
echo "-------------------------------------------------" >> /var/log/oracle
su - $ora_owner -c "$ora_home/bin/dbstart" >>/var/log/oracle
echo "done"
 
# start the listener:
echo "starting oracle listeners ... "
echo "-------------------------------------------------" >> /var/log/oracle
date +" %t %a %d : starting oracle listeners as part of system up." >> /var/log/oracle
echo "-------------------------------------------------" >> /var/log/oracle
su - $ora_owner -c "$ora_home/bin/lsnrctl start" >>/var/log/oracle
echo "done."
echo "-------------------------------------------------" >> /var/log/oracle
date +" %t %a %d : finished." >> /var/log/oracle
echo "-------------------------------------------------" >> /var/log/oracle
touch /var/lock/subsys/oracle
;;
 
'stop')
# stop the oracle listener:
echo "stoping oracle listeners ... "
echo "-------------------------------------------------" >> /var/log/oracle
date +" %t %a %d : stoping oracle listener as part of system down." >> /var/log/oracle
echo "-------------------------------------------------" >> /var/log/oracle
su - $ora_owner -c "$ora_home/bin/lsnrctl stop" >>/var/log/oracle
echo "done."
rm -f /var/lock/subsys/oracle
 
# stop the oracle database:
echo "stoping oracle databases ... "
echo "-------------------------------------------------" >> /var/log/oracle
date +" %t %a %d : stoping oracle databases as part of system down." >> /var/log/oracle
echo "-------------------------------------------------" >> /var/log/oracle
su - $ora_owner -c "$ora_home/bin/dbshut" >>/var/log/oracle
echo "done."
echo ""
echo "-------------------------------------------------" >> /var/log/oracle
date +" %t %a %d : finished." >> /var/log/oracle
echo "-------------------------------------------------" >> /var/log/oracle
;;
 
'restart')
$0 stop
$0 start
;;
esac

3. 改变文件权限

?
1
# chmod 755 /etc/init.d/oracle

4. 添加服务

?
1
# chkconfig --level 35 oracle on

5. 需要在关机或重启机器之前停止数据库,做一下操作

?
1
2
# ln -s /etc/init.d/oracle /etc/rc0.d/k01oracle  //关机
# ln -s /etc/init.d/oracle /etc/rc6.d/k01oracle  //重启

6. 使用方法

?
1
2
3
# service oracle start    //启动oracle
 # service oracle stop    //关闭oracle
 # service oracle restart   //重启oracle

7. 测试

a. 开机自启动

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
last login: mon nov 26 19:57:06 2012 from 10.0.0.145
[root@ors ~]# su - oracle
[oracle@ors ~]$ sqlplus "/as sysdba"
 
sql*plus: release 10.2.0.1.0 - production on mon nov 26 20:07:33 2012
 
copyright (c) 1982, 2005, oracle. all rights reserved.
 
 
connected to:
oracle database 10g enterprise edition release 10.2.0.1.0 - production
with the partitioning, olap and data mining options
 
sql> set linesize 300;
sql> set pagesize 30;
sql> select * from scott.emp;
 
   empno ename   job       mgr hiredate     sal    comm   deptno
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
smith   clerk      7902 17-dec-80    800          20
allen   salesman    7698 20-feb-81    1600    300     30
ward    salesman    7698 22-feb-81    1250    500     30
jones   manager     7839 02-apr-81    2975          20
martin   salesman    7698 28-sep-81    1250    1400     30
blake   manager     7839 01-may-81    2850          30
clark   manager     7839 09-jun-81    2450          10
scott   analyst     7566 19-apr-87    3000          20
king    president      17-nov-81    5000          10
turner   salesman    7698 08-sep-81    1500     0     30
adams   clerk      7788 23-may-87    1100          20
james   clerk      7698 03-dec-81    950          30
ford    analyst     7566 03-dec-81    3000          20
miller   clerk      7782 23-jan-82    1300          10
rows selected.
 
sql>

Linux中Oracle服务启动和停止脚本与开机自启动

b. service oracle stop

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
sql> disconnected from oracle database 10g enterprise edition release 10.2.0.1.0 - production
with the partitioning, olap and data mining options
[oracle@ors ~]$ logout
[root@ors ~]# service oracle stop
stoping oracle listeners ...
done.
stoping oracle databases ...
done.
 
[root@ors ~]# su - oracle
[oracle@ors ~]$ sqlplus "/as sysdba"
 
sql*plus: release 10.2.0.1.0 - production on mon nov 26 20:17:20 2012
 
copyright (c) 1982, 2005, oracle. all rights reserved.
 
connected to an idle instance.
 
sql> set linesize 300;
sql> set pagesize 30;
sql> select * from scott.emp;
select * from scott.emp
*
error at line 1:
ora-01034: oracle not available
 
 
sql>

Linux中Oracle服务启动和停止脚本与开机自启动

c. service oracle start

?
1
2
3
4
5
6
7
8
sql> disconnected
[oracle@ors ~]$ logout
[root@ors ~]# service oracle start
starting oracle databases ...
done
starting oracle listeners ...
done.
[root@ors ~]#

Linux中Oracle服务启动和停止脚本与开机自启动

d. service oracle restart

?
1
2
3
4
5
6
7
8
9
10
11
[root@ors ~]# service oracle restart
stoping oracle listeners ...
done.
stoping oracle databases ...
done.
 
starting oracle databases ...
done
starting oracle listeners ...
done.
[root@ors ~]#

Linux中Oracle服务启动和停止脚本与开机自启动

至此,oracle服务启动&停止脚本与开机自启动设置完毕。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/mchina/archive/2012/11/27/2782993.html