Oracle单实例安装环境一键配置脚本(CentOS6 + 11gR2 )

这是自己曾经写的一个oracle 11gR2在CentOS6 x86_64服务器上,一键配置安装环境的脚本,能快速完成安装前环境的配置。

具体完成以下工作:

  • 备份系统配置文件,以防出错
  • 添加oracle用户和用户组
  • 创建安装目录
  • 关闭selinux
  • 在.bash_profile中修改环境变量
  • 修改sysctl.conf文件
  • 修改limits.conf文件
  • 修改PAM的login文件
  • 安装必要的依赖包

使用注意事项:

  • root的用户执行,chmod +x oraclePreInstCheck.sh
  • ./oraclePreInstCheck.sh运行后,请仔细阅读说明,再决定是否使用该脚本
  • 该脚本默认参数适用于2核4G内存的环境,你可以根据需要修改kernelset()部分
  • 执行完后,你检查一下你的安装目录及权限(默认/db/oracle
  • 该脚本会有提示输入的地方,请不要挑战它的健壮性,比如输入安装根目录时,不要带入空格
  • 脚本只需执行一次,修改系统参数如sysctl.conf之前,都有备份成xxx.ora_bak
  • 确保可以通过yum方式安装软件包(使用挂载DVD镜像或联网)
  • 建议结合tee将执行过程记录在日志文件中,./oraclePreInstCheck.sh | tee oraclePreInstCheck.log

oraclePreInstCheck.sh:

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/bin/bash
# author zhouxiao
# date 2014-03-07
# description oracle 11g R2 for linux 6.0+ x86_64 安装辅助脚本
#定义常量
SYSCTL=/etc/sysctl.conf
LIMITS=/etc/security/limits.conf
PAM=/etc/pam.d/login
PROFILE=/etc/profile
BASH_PROFILE=/home/oracle/.bash_profile
#循环变量
i=1
#定义显示颜色
#颜色定义 信息(33黄色) 警示(31红色) 过程(36浅蓝)
usage()
{
echo "Scripts: initialize the required env settings for Oracle 11gR2 installation on Linux 6.0+ x86_64"
echo "Make sure you have prepared conditions list bellow:"
echo -e " \n\e[1;33m yum, hosts, user oracle's passwd, oralce SID, DISPLAY location \e[0m"
echo "The Script will backup config files with .ora_bak in case failure "
echo "The Script will set the following change:"
echo " - add user oracle and group oinstall/dba/oper, etc."
echo " - make directory ORACLE_HOME and change owner"
echo " - modify oracle .bash_profile"
echo " - modify /etc/sysctl.conf kernel parameters like shmall/shmmax"
echo " - modify /etc/security/limits.conf "
echo " - install necessary packages like libgcc/libaio/unixODBC, etc."
echo " - IF anything goes wrong, you need to recover ora_bak files manually."
echo -e "\n\e[1;33m Continue? (y/n [n]): \e[0m"
read singal
if [ $singal != "y" ]; then
exit 0
else
echo "God Bless you! Settings started."
echo ""
echo ""
fi
}
#判断执行用户是否root
isroot()
{
if [ $USER != "root" ];then
echo -e "\n\e[1;31m the user must be root,and now you user is $USER,please su to root. \e[0m"
exit4
else
echo -e "\n\e[1;36m check root ... OK! \e[0m"
fi
}
#挂在光盘到/mnt/cdrom目录下
mount_cdrom()
{
echo -e "\n\e[1;31m please insert RHEL to CDROM,press any key ...\e[0m"
read -n 1
if [ -d /mnt/cdrom ];then
mount -t auto -o ro /dev/cdrom /mnt/cdrom
else
mkdir -p /mnt/cdrom
mount -t auto -o ro /dev/cdrom /mnt/cdrom
fi
if [ $? -eq 0 ];then
echo -e "\n\e[1;36m CDROM mount on /mnt/cdrom ... OK! \e[0m"
fi
}
#设置yum本地光盘源
yum_repo()
{
rm -rf /etc/yum.repos.d/* && cat <<EOF >> /etc/yum.repos.d/Server.repo
[Server]
name=MyRPM
baseurl=file:///mnt/cdrom/Server
enabled=1
gpgkey=file:///mnt/cdrom/RPM-GPG-KEY-redhat-release
EOF
if [ $? -eq 0 ];then
echo -e "\n\e[1;36m /etc/yum.repos.d/Server.repo ... OK! \e[0m"
fi
}
#添加oracle用户,添加oracle用户所属组oinstall及附加组dba
ouseradd()
{
if [[ `grep "oracle" /etc/passwd` != "" ]];then
userdel -r oracle
fi
if [[ `grep "oinstall" /etc/group` = "" ]];then
groupadd -g 501 oinstall
fi
if [[ `grep "dba" /etc/group` = "" ]];then
groupadd -g 502 dba
groupadd -g 503 oper
groupadd -g 504 asmadmin
groupadd -g 506 asmdba
groupadd -g 505 asmoper
fi
useradd oracle -g oinstall -G dba,asmdba,oper && echo $1 |passwd oracle --stdin
if [ $? -eq 0 ];then
echo -e "\n\e[1;36m oracle's password updated successfully --- OK! \e[0m"
else
echo -e "\n\e[1;31m oracle's password set faild. --- NO!\e[0m"
fi
}
#检查oracle所需软件包并安装
packagecheck()
{
for package in binutils compat-libcap1 compat-libstdc++-33 compat-libstdc++-33*i686 gcc gcc-c++ glibc glibc-2*i686 glibc-devel glibc-devel-2*i686 glibc-headers-2.* libgcc libgcc-4*i686 libstdc++ libstdc++-4*i686 libstdc++-devel libstdc++-devel-4*i686 libaio-0* libaio-0*i686 libaio-devel libaio-devel-0*i686 unixODBC unixODBC-2*i686 unixODBC-devel unixODBC-devel-2*i686 make sysstat ksh
do
rpm -q $package 2> /dev/null
if [ $? != 0 ];then
yum -y install $package 2> /dev/null
echo -e "\n\e[1;36m $package is installed ... OK! \e[0m"
fi
done
}
#安装桌面套件 X Window System / Desktop
xdesk()
{
LANG=C yum -y groupinstall "X Window System" "Desktop"
if [ $? -eq 0 ];then
echo -e "\n\e[1;36m $package is already installed ... OK! \e[0m"
fi
}
# 设置内核参数
# shmall 物理内存<8G =2097152 >8G MemTotal/4kb
# shmmax 物理内存<8G = 4294967296 >8G 16GMemTotal = 10G=10*1024^3
kernelset()
{
cp $SYSCTL{,.ora_bak} && cat <<EOF >>$SYSCTL
fs.aio-max-nr = 1048576
fs.file-max = 6815744
kernel.shmall = 2097152
kernel.shmmax = 4294967296
kernel.shmmni = 4096
kernel.sem = 250 32000 100 128
net.ipv4.ip_local_port_range = 9000 65500
net.core.rmem_default = 262144
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 1048576
EOF
if [ $? -eq 0 ];then
echo -e "\n\e[1;36m kernel parameters updated successfully --- OK! \e[0m"
fi
sysctl -p
}
#设置oracle资源限制
oralimit()
{
cp $LIMITS{,.ora_bak} && cat <<EOF >> $LIMITS
oracle soft nproc 2047
oracle hard nproc 16384
oracle soft nofile 1024
oracle hard nofile 65536
oracle soft stack 10240
EOF
if [ $? -eq 0 ];then
echo -e "\n\e[1;36m $LIMITS updated successfully ... OK! \e[0m"
fi
cat $LIMITS | grep '^o'
}
#设置login文件
setlogin()
{
cp $PAM{,.ora_bak} && cat <<EOF >>$PAM
session required pam_limits.so
EOF
if [ $? -eq 0 ];then
echo -e "\n\e[1;36m $PAM updated successfully ... OK! \e[0m"
fi
}
#设置profile文件
setprofile()
{
cp $PROFILE{,.ora_bak} && cat <<EOF >>$PROFILE
if [ $USER = "oracle" ];then
if [ $SHELL = "/bin/ksh" ];then
ulimit -p 16384
ulimit -n 65536
else
ulimit -u 16384 -n 65536
fi
fi
EOF
if [ $? -eq 0 ];then
echo -e "\n\e[1;36m $PROFILE updated successfully ... OK! \e[0m"
fi
}
#设置oracle的profile文件
setbash_profile()
{
cp $BASH_PROFILE{,.ora_bak} && cat <<EOF >> $BASH_PROFILE
umask 022
#oracle settings
TMP=/tmp; export TMP
TMPDIR=\$TMP; export TMPDIR
ORACLE_BASE=$1/oracle
ORACLE_HOME=\$ORACLE_BASE/product/11.2.0/db_1
ORACLE_SID=$2
PATH=\$ORACLE_HOME/bin/:\$PATH
LANG=en_US.UTF-8
ORACLE_TERM=xterm
export ORACLE_BASE ORACLE_HOME ORACLE_SID ORACLE_TERM
LD_LIBRARY_PATH=\$ORACLE_HOME/lib:/lib:/usr/lib; export LD_LIBRARY_PATH
CLASSPATH=\$ORACLE_HOME/jre:\$ORACLE_HOME/jlib:\$ORACLE_HOME/rdbms/jlib
export CLASSPATH
EOF
if [ $? -eq 0 ];then
echo -e "\n\e[1;36m $BASH_PROFILE updated successfully ... OK! \e[0m"
fi
su - oracle -c source $BASH_PROFILE
}
#系统环境检查
oscheck()
{
#查看内存大小是否大于1G
echo -e "\n check MEM Size ..."
if [ `cat /proc/meminfo | grep MemTotal | awk '{print $2}'` -lt 1048576 ];then
echo -e "\n\e[1;33m Memory Small \e[0m"
exit 1
else
echo -e "\n\e[1;36m Memory checked PASS \e[0m"
fi
#查看tmp空间大小
echo -e "\n check tmpfs Size ..."
cp /etc/fstab{,.ora_bak}
while true;do
if [ `df | awk '/tmpfs/ {print $2}'` -lt 1048576 ];then
echo -e "\n\e[1;33m tmpfs Smaill \e[0m"
sed -i '/tmpfs/s/defaults/defaults,size=1G/' /etc/fstab && mount -o remount /dev/shm
if [ $? != 0 ];then
i=i+1
if [ $i -eq 3 ];then
echo -e "\n\e[1;31m set tmpfs faild. \e[0m"
exit 3
fi
else
echo -e "\n\e[1;36 tmpfs updated successfully. \e[0m"
break
fi
else
echo -e "\n\e[1;36m tmpfs checked PASS \e[0m"
break
fi
done
}
usage
#停止防火墙IPTABLES
service iptables stop
#chkconfig iptables off
#关闭SELINUX
cp /etc/selinux/config{,.ora_bak} && sed -i '/SELINUX/s/enforcing/disabled/;/SELINUX/s/permissive/disabled/' /etc/selinux/config
setenforce 0
#执行以上函数
isroot
#oscheck
#yum_repo
#mount_cdrom
packagecheck
#xdesk
kernelset
oralimit
setlogin
setprofile
echo -e "\n\e[1;33m please input oracle's user passwd: \e[0m"
read oraclepw
ouseradd $oraclepw
echo -e "\n\e[1;33m please input oracle install PATH (default /db) \e[0m"
read oraclepath
if [ -z $oraclepath ];then
oraclepath=/db
fi
if [ ! -x "$oraclepath" ];then
mkdir -p "$oraclepath"
chown oracle.oinstall $oraclepath
echo -e "\n\e[1;36 $oraclepath created. \e[0m"
fi
echo -e "\n\e[1;33m please input oracle_sid, just for env (default TSDB) \e[0m"
read orasid
if [ -z orasid ];then
orasid=TSDB
fi
setbash_profile $oraclepath $orasid
mkdir -p $oraclepath/oracle/product/11.2.0/db_1 && chown -R oracle:oinstall $oraclepath && chmod -R 755 $oraclepath
unset i
echo -e "\n\e[1;33m please input where to display the X Window (default 127.0.0.1:0.0) \e[0m"
read xdpy
if [ -z $xdpy ];then
xdpy=127.0.0.1:0.0
fi
su - oracle -c export DISPLAY=$xdpy && host +
echo -e "\n\e[1;35m Oracle install pre-setting finish! && please run oracle installer as user oracle \e[0m"