应用场景
在linux服务器上,需要使用oracle数据库,那么就需要安装oracle数据库服务器,然后进行数据库软件的使用。
1. 安装前环境准备
VMware Workstation 11搭建CentOS7虚拟机,CentOS7操作系统最小化安装,2G内存,20G磁盘空间,1核CPU,已配置好网络IP:192.168.206.135。已下载Oracle 11g Linux安装文件:linux.x64_11gR2_database_1of2.zip和linux.x64_11gR2_database_2of2.zip 通过Xmanager Xftp上传到虚拟机/tmp目录中。
2. 操作系统准备工作
(1).使用root用户登录操作系统,yum 安装unzip 软件,用来解压上传的Oracle安装文件。
[root@CentOS ~]# yum install unzip –y
解压Oracle 安装程序
[root@CentOS ~]# cd /tmp
[root@CentOS tmp]# unzip linux.x64_11gR2_database_1of2.zip && unzip linux.x64_11gR2_database_2of2.zip
等待解压完成后,会在/tmp目录下生产一个database文件夹,里面就Oracle 11g安装文件。
(2).在/etc/hosts文件中添加主机名
[root@CentOS tmp]# vim /etc/hosts
添加本地服务器IP CentOS
(3).关闭selinux
[root@CentOS tmp]# vim /etc/selinux/config
设置SELINUX=disabled
[root@CentOS tmp]# setenforce 0
(4).关闭防火墙
[root@CentOS tmp]# service iptables stop
[root@CentOS ~]# systemctl stop firewalld
[root@CentOS ~]# systemctl disable firewalld
(5).安装Oracle 11g依赖包,此步骤必须执行,如果在离线环境手动安装。
在线环境依赖包安装方式:
[root@CentOS tmp]# yum install yum install gcc make binutils gcc-c++ compat-libstdc++-33 elfutils-libelf-devel elfutils-libelf-devel-static ksh libaio libaio-devel numactl-devel sysstat unixODBC unixODBC-devel pcre-devel –y
离线环境依赖包安装方式:
离线环境依赖包下载:http://pan.baidu.com/s/1mh8Tjx2
# rpm -ivUh *.rpm --force --nodeps
(6).添加安装用户和用户组
[root@CentOS tmp]# groupadd oinstall
[root@CentOS tmp]# groupadd dba
[root@CentOS tmp]# useradd -g oinstall -G dba oracle
[root@CentOS tmp]# passwd oracle
[root@CentOS tmp]# id oracle
uid=1001(oracle) gid=1001(oinstall) 组=1001(oinstall),1002(dba)
(7).修改内核参数配置文件
[root@CentOS ~]# vim /etc/sysctl.conf
添加以下内容
fs.aio-max-nr = 1048576
fs.file-max = 6815744
kernel.shmall = 2097152
kernel.shmmax = 1073741824
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
[root@CentOS ~]# sysctl –p
其中kernel.shmmax = 1073741824为本机物理内存(2G)的一半,单位为byte。
(8).修改用户的限制文件
[root@CentOS ~]# vim /etc/security/limits.conf
添加以下内容
oracle soft nproc 2047
oracle hard nproc 16384
oracle soft nofile 1024
oracle hard nofile 65536
oracle soft stack 10240
(9).修改/etc/pam.d/login文件:
[root@CentOS ~]# vim /etc/pam.d/login
添加以下内容:
session required /lib64/security/pam_limits.so
session required pam_limits.so
(10).修改/etc/profile文件:
[root@CentOS ~]# vim /etc/profile
添加以下内容:
if [ $USER = "oracle" ]; then
if [ $SHELL = "/bin/ksh" ]; then
ulimit -p 16384
ulimit -n 65536
else
ulimit -u 16384 -n 65536
fi
fi
(11).创建安装目录和设置文件权限
[root@CentOS ~]# mkdir -p /u01/app/oracle/product/11.2.0
[root@CentOS ~]# mkdir /u01/app/oracle/oradata
[root@CentOS ~]# mkdir /u01/app/oracle/inventory
[root@CentOS ~]# mkdir /u01/app/oracle/fast_recovery_area
[root@CentOS ~]# chown -R oracle:oinstall /u01/app/oracle
[root@CentOS ~]# chmod -R 775 /u01/app/oracle
(12).设置oracle用户环境变量
[root@CentOS ~]# su - oracle
[oracle@CentOS ~]$ vim .bash_profile
添加如下内容:
ORACLE_BASE=/u01/app/oracle
ORACLE_HOME=$ORACLE_BASE/product/11.2.0
ORACLE_SID=orcl
PATH=$PATH:$ORACLE_HOME/bin
export ORACLE_BASE ORACLE_HOME ORACLE_SID PATH
(13).编辑静默安装响应文件
[oracle@CentOS ~]$ cp -R /tmp/database/response/ .
[oracle@CentOS ~]$ cd response/
[oracle@CentOS response]$ vim db_install.rsp
需要设置的选项如下:
oracle.install.option=INSTALL_DB_SWONLY
ORACLE_HOSTNAME=CentOS
UNIX_GROUP_NAME=oinstall
INVENTORY_LOCATION=/u01/app/oracle/inventory
SELECTED_LANGUAGES=en,zh_CN
ORACLE_HOME=/u01/app/oracle/product/11.2.0
ORACLE_BASE=/u01/app/oracle
oracle.install.db.InstallEdition=EE
oracle.install.db.DBA_GROUP=dba
oracle.install.db.OPER_GROUP=dba
DECLINE_SECURITY_UPDATES=true
3. 根据响应文件静默安装Oracle 11g
[oracle@CentOS response]$ cd /tmp/database/
[oracle@CentOS database]$ ./runInstaller -silent –responseFile /home/oracle/response/db_install.rsp –ignorePrereq
开始Oracle在后台静默安装。安装过程中,如果提示[WARNING]不必理会,此时安装程序仍在后台进行,如果出现[FATAL],则安装程序已经停止了。
可以在以下位置找到本次安装会话的日志:
/u01/app/oracle/inventory/logs/installActions2015-06-08_04-00-25PM.log
可以切换终端执行top命令查看后台进程一直是在安装的,/u01目录也在不断增大, 当出现以下提示时,代表安装成功:
按照要求执行脚本。
打开终端,以root身份登录,执行脚本:
[root@CentOS ~]# sh /u01/app/oracle/inventory/orainstRoot.sh
[root@CentOS ~]# sh /u01/app/oracle/product/11.2.0/root.sh
4. 以静默方式配置监听
重新使用oracle用户登录
[oracle@CentOS ~]$ netca /silent /responsefile /home/oracle/response/netca.rsp
成功运行后,在/u01/app/oracle/product/11.2.0/network/admin/中生成listener.ora和sqlnet.ora
通过netstat命令可以查看1521端口正在监听。
Yum安装netstat软件,软件包是在net-tools中。
[root@CentOS ~]# yum install net-tools
[root@CentOS ~]# netstat -tnulp | grep 1521
5. 以静默方式建立新库,同时也建立一个对应的实例。
[oracle@CentOS ~]$ vim /home/oracle/response/dbca.rsp
设置以下参数:
GDBNAME = "orcl"
SID = "orcl"
SYSPASSWORD = "oracle"
SYSTEMPASSWORD = "oracle"
SYSMANPASSWORD = "oracle"
DBSNMPPASSWORD = "oracle"
DATAFILEDESTINATION =/u01/app/oracle/oradata
RECOVERYAREADESTINATION=/u01/app/oracle/fast_recovery_area
CHARACTERSET = "ZHS16GBK"
TOTALMEMORY = "1638"
其中TOTALMEMORY = “1638” 为1638MB,物理内存2G*80%。
进行静默配置:
[oracle@CentOS ~]$ dbca -silent -responseFile /home/oracle/response/dbca.rsp
建库后进行实例进程检查:
[oracle@CentOS ~]$ ps -ef | grep ora_ | grep -v grep
查看监听状态:
[oracle@CentOS ~]$ lsnrctl status
登录查看实例状态:
[oracle@CentOS ~]$ sqlplus / as sysdba
SQL> select status from v$instance;
6. Oracle开机自启动设置
1.修改/u01/app/oracle/product/11.2.0/bin/dbstart
[oracle@CentOS ~]$ vim /u01/app/oracle/product/11.2.0/bin/dbstart
将ORACLE_HOME_LISTNER=$1修改为ORACLE_HOME_LISTNER=$ORACLE_HOME
2.修改/u01/app/oracle/product/11.2.0/bin/dbshut
[oracle@CentOS ~]$ vim /u01/app/oracle/product/11.2.0/bin/dbshut
将ORACLE_HOME_LISTNER=$1修改为ORACLE_HOME_LISTNER=$ORACLE_HOME
3.修改/etc/oratab文件
[oracle@CentOS ~]$ vim /etc/oratab
将orcl:/u01/app/oracle/product/11.2.0:N中最后的N改为Y,成为orcl:/u01/app/oracle/product/11.2.0:Y
4.输入命令dbshut和dbstart测试
[oracle@CentOS ~]$ dbshut
Oracle监听停止,进程消失。
[oracle@CentOS ~]$ dbstart
Oracle监听启动,进程启动。
5. 切换到root账户建立自启动脚本
[oracle@CentOS ~]$ su -
[root@CentOS ~]# vim /etc/rc.d/init.d/oracle
添加以下内容(有些值如ORACLE_HOME和ORACLE_USER等根据实际情况可以修改):
#!/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=/u01/app/oracle/product/11.2.0
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
;;
#'status')
# echo $0 started.
# else
# echo $0 stopped.
# fi
# ;;
#*)
# echo "Usage: $0 [start|stop|status]"
# exit 1
esac
exit 0
6. 修改/etc/d/oracle服务文件权限
[root@CentOS init.d]# chmod 755 /etc/init.d/oracle
7. 设置为开机启动
[root@CentOS ~]# chkconfig oracle on
8. 进行service oracle start/stop/restart测试
9. 需要在关机或重启机器之前停止数据库,做以下操作
ln -s /etc/init.d/oracle /etc/rc0.d/K01oracle //关机
ln -s /etc/init.d/oracle /etc/rc6.d/K01oracle //重启
Reboot重启查看Oracle监听和实例进程均能自动启动。