一、最小化安装 linux CentOS7
使用镜像包:CentOS-7-x86_64-Everything-1810.iso
手动IP则点击 configure
安装类型选择minimal最小化安装
等待安装;重启安装完成
Root登录安装几个必要的工具
yum -y update
yum -y install net-tools
yum -y install unzip
yum -y install nano
[root@oracle init.d]# uname -a
Linux oracle 3.10.0-1062.9.1.el7.x86_64 #1 SMP Fri Dec 6 15:49:49 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
[root@oracle init.d]# cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
[root@oracle init.d]# hostname
Oracle
重启
二、安装Oracle11GR2所需要的依赖包
一键安装命令:yum -y install gcc gcc-c++ make binutils compat-libstdc+±33 elfutils-libelf elfutils-libelf-devel elfutils-libelf-devel-static glibc glibc-common glibc-devel ksh libaio libaio-devel libgcc libstdc++ libstdc+±devel numactl-devel sysstat unixODBC unixODBC-devel kernelheaders pdksh pcre-devel readline
也可以一个个来安装:
执行:yum -y install gcc
安装成功
执行:yum -y install gcc-c++
安装成功
执行:yum -y install make
系统里已经安装了最新版
执行:yum -y install binutils
系统里已经安装了最新版
执行:yum -y install compat-libstdc+±33
安装成功
执行:yum -y install elfutils-libelf
系统里已经安装了最新版
执行:yum -y install elfutils-libelf-devel
安装成功
执行:yum -y install elfutils-libelf-devel-static
安装成功
执行:yum -y install glibc
安装成功
执行:yum -y install glibc-common
安装成功
执行:yum -y install glibc-devel
安装成功
执行:yum -y install ksh
安装成功
执行:yum -y install libaio
安装成功
执行:yum -y install libaio-devel
安装成功
执行:yum -y install libgcc
安装成功
执行:yum -y install libstdc++
安装成功
执行:yum -y install libstdc+±devel
安装成功
执行:yum -y install numactl-devel
安装成功
执行:yum -y install sysstat
安装成功
执行:yum -y install unixODBC
安装成功
执行:yum -y install unixODBC-devel
安装成功
执行:yum -y install kernel-headers
安装成功
执行:yum -y install pdksh
报错,原因:pdksh是一个老包了,新的oracle都使用ksh包
改执行:yum -y install ksh
安装成功
执行:yum -y install pcre-devel
安装成功
执行:yum -y install readline*
安装成功
完成;
三、配置安装、运行环境
创建用户组及文件夹
创建用户组:groupadd oinstall
创建用户组:groupadd dba
创建用户组:groupadd oper
创建用户并添加到用户组:useradd -g oinstall -G dba,oper oracle
设置密码(密码自己改):echo “123456” | passwd --stdin oracle
创建安装目录及权限
mkdir -p /data/u01/app/oracle/product/11.2.0/dbhome_1
mkdir /data/u01/app/oracle/{oradata,inventory,fast_recovery_area}
mkdir /data/u01/app/oracle/oradata/his
chown -R oracle:oinstall /data/u01/app/oracle
chmod -R 775 /data/u01/app/oracle
开启防火墙端口限制:
firewall-cmd --zone=public --add-port=1521/tcp --permanent
systemctl reload firewalld.service
firewall-cmd --state
有人建议关闭SELINUX:
nano /etc/selinux/config
SELINUX=disabled
官网下载oracle安装包:
linux.x64_11gR2_database_1of2.zip (oracle官网下载)linux.x64_11gR2_database_2of2.zip (oracle官网下载)
创建一个目录存放安装包:
mkdir -p /data/u01/software
上传安装包并把安装包移过去
mv linux.x64_11gR2_database_* /data/u01/software/
cd /data/u01/software
解压
unzip linux.x64_11gR2_database_1of2.zip
unzip linux.x64_11gR2_database_2of2.zip
修改内核文件: nano /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
按 o保存,x键退出;
执行:sysctl -p
使配置文件生效;
修改用户限制:nano /etc/security/limits.conf
添加内容:
oracle soft nproc 2047
oracle hard nproc 16384
oracle soft nofile 1024
oracle hard nofile 65536
oracle soft stack 10240
修改/etc/pam.d/login 文件:nano /etc/pam.d/login
添加内容:
session required /lib64/security/pam_limits.so
session required pam_limits.so
修改/etc/profile 文件:nano /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
设置oracle 用户环境变量
先切换到oracle用户:su - oracle
nano .bash_profile
添加内容:
export ORACLE_BASE=/data/u01/app/oracle
export ORACLE_HOME=/data/u01/app/oracle/product/11.2.0/dbhome_1
export ORACLE_SID=orcl
export ORACLE_UNQNAME=
O
R
A
C
L
E
S
I
D
e
x
p
o
r
t
P
A
T
H
=
ORACLE_SID export PATH=
ORACLESIDexportPATH=ORACLE_HOME/bin:$PATH
export NLS_LANG=american_america.AL32UTF8
执行:source .bash_profile
使变量马上生效;
修改静默安装响应文件,拷贝备份一份响应文件:
cp -R /data/u01/software/database/response/ .
cd response/
执行:nano db_install.rsp
修改内容:
oracle.install.option=INSTALL_DB_SWONLY
ORACLE_HOSTNAME=oracle(自己的主机名hostname)
UNIX_GROUP_NAME=oinstall
INVENTORY_LOCATION=/data/u01/app/oracle/inventory
SELECTED_LANGUAGES=en,zh_CN
ORACLE_HOME=/data/u01/app/oracle/product/11.2.0/dbhome_1
ORACLE_BASE=/data/u01/app/oracle
oracle.install.db.InstallEdition=EE
oracle.install.db.DBA_GROUP=dba
oracle.install.db.OPER_GROUP=dba
DECLINE_SECURITY_UPDATES=true
四、静默安装oracle
su - oracle
切换到解压的安装包目录:
cd /data/u01/software/database/
执行安装:
./runInstaller -silent -responseFile /home/oracle/response/db_install.rsp -ignorePrereq
已开始安装;等待安装成功,另起shell窗口查看进度;
tail -f /data/u01/app/oracle/inventory/logs/installActions2020-02-02_03-11-39PM.log
(文件名根据本机提示)
Root下运行脚本文件:
/data/u01/app/oracle/inventory/orainstRoot.sh
/data/u01/app/oracle/product/11.2.0/dbhome_1/root.sh
su - oracle
sqlplus / as sysdba
SQLPLUS 连接成功;
五、创建一个数据库实例;
退出SQLPLUS :quit
配置响应文件:nano /home/oracle/response/dbca.rsp
修改内容:
GDBNAME = “orcl”
SID = “orcl”
前面的 # 要删除,对应用户设置密码;
SYSPASSWORD = “输入密码”
SYSTEMPASSWORD = “输入密码”
SYSMANPASSWORD = “输入密码”
DBSNMPPASSWORD = “输入密码”
前面的 # 要删除,对应用户设置目录;
DATAFILEDESTINATION =/data/u01/app/oracle/oradata
RECOVERYAREADESTINATION=/data/u01/app/oracle/fast_recovery_area
前面的 # 要删除,设置字符集;
非常重要,设置字符集:CHARACTERSET = “AL32UTF8”
调大内存,2倍的物理内存大小
TOTALMEMORY = “2048”
检查配置
egrep -v “(#|$)” /home/oracle/response/dbca.rsp
安装创建:
dbca -silent -responseFile /home/oracle/response/dbca.rsp
完成
查看默认实例
env|grep ORACLE_UNQNAME
进入sqlplus 启动实例
startup
提示是实例已经起来了,无需再次启动。
可以shutdown后在startup
启动监听:
lsnrctl start
lsnrctl status
netstat -tnulp | grep 1521
开启日志归档模式:
sqlplus / as sysdba
>archive log list
>shutdown immediate;
>startup mount;
>alter database archivelog;
>alter system archive log start;
>archive log list
>alter database open;
六、创建HIS数据表
$ sqlplus / as sysdba
创建临时表空间
create temporary tablespace HIS_TEMP tempfile ‘/data/u01/app/oracle/oradata/his/his_temp.dbf’ size 128M autoextend on next 100M maxsize 1024M extent management local;
创建表空间
create tablespace HIS logging datafile ‘/data/u01/app/oracle/oradata/his/his.dbf’ size 128M autoextend on next 100M maxsize 1024M extent management local;
创建用户
create user his identified by 123456 default tablespace HIS temporary tablespace HIS_TEMP;
用户授权访问
grant connect,resource to his;
$ sqlplus / as sysdba
创建临时表空间
create temporary tablespace HISTEST_TEMP tempfile ‘/data/u01/app/oracle/oradata/his/histest_temp.dbf’ size 128M autoextend on next 100M maxsize 1024M extent management local;
创建表空间
create tablespace HISTEST logging datafile ‘/data/u01/app/oracle/oradata/his/histest.dbf’ size 128M autoextend on next 100M maxsize 1024M extent management local;
创建用户
create user histest identified by 123456 default tablespace HISTEST temporary tablespace HISTEST_TEMP;
用户授权访问
grant connect,resource to histest;
$ sqlplus / as sysdba
创建临时表空间
create temporary tablespace HISOPEN_TEMP tempfile ‘/data/u01/app/oracle/oradata/his/hisopen_temp.dbf’ size 128M autoextend on next 100M maxsize 1024M extent management local;
创建表空间
create tablespace HISOPEN logging datafile ‘/data/u01/app/oracle/oradata/his/hisopen.dbf’ size 128M autoextend on next 100M maxsize 1024M extent management local;
创建用户
create user hisopen identified by 123456 default tablespace HISOPEN temporary tablespace HISOPEN_TEMP;
用户授权访问
grant connect,resource to hisopen;
七、配置系统自启动
1、/etc/oratab由root.sh 脚本创建,在用DBCA创建实例时也会更新这个文件。当
O
R
A
C
L
E
S
I
D
:
ORACLE_SID:
ORACLESID:ORACLE_HOME: N|Y设置为Y时,允许实例自启动,当设置为N时,则不允许自启动。 这个文件里的配置仅仅起一个开关的作用,其并不会具体的执行启动和关闭,具体的操作由
O
R
A
C
L
E
H
O
M
E
/
b
i
n
/
d
b
s
t
a
r
t
和
d
b
s
h
u
t
脚
本
来
实
现
。
这
2
个
脚
本
在
执
行
时
会
检
查
/
e
t
c
/
o
r
a
t
a
b
文
件
里
的
配
置
,
为
Y
时
才
能
继
续
执
行
。
所
以
配
置
数
据
库
自
启
动
和
关
闭
的
步
骤
如
下
:
(
1
)
配
置
/
e
t
c
/
o
r
a
t
a
b
(
2
)
修
改
ORACLE_HOME/bin/dbstart和dbshut 脚本来实现。这2个脚本在执行时会检查/etc/oratab文件里的配置,为Y时才能继续执行。所以配置数据库自启动和关闭的步骤如下: (1)配置/etc/oratab (2)修改
ORACLEHOME/bin/dbstart和dbshut脚本来实现。这2个脚本在执行时会检查/etc/oratab文件里的配置,为Y时才能继续执行。所以配置数据库自启动和关闭的步骤如下:(1)配置/etc/oratab(2)修改ORACLE_HOME/bin/dbstart和dbshut,并将其添加到/etc/rc(.local) 文件中。
这里是使用oracle 自带的dbstart 和dbshut脚本,如果使用自己写的脚本来启动或关闭DB就不需要关心这个文件的设置了。
$ su
nano /etc/oratab
orcl:/data/u01/app/oracle/product/11.2.0/dbhome_1:Y
2、修改$ORACLE_HOME/bin/dbstart
将ORACLE_HOME_LISTNER=
1
修
改
为
O
R
A
C
L
E
H
O
M
E
L
I
S
T
N
E
R
=
1修改为ORACLE_HOME_LISTNER=
1修改为ORACLEHOMELISTNER=ORACLE_HOME
su - oracle
$ nano /data/u01/app/oracle/product/11.2.0/dbhome_1/bin/dbstart
# First argument is used to bring up Oracle Net Listener
#ORACLE_HOME_LISTNER=$1
ORACLE_HOME_LISTNER=$ORACLE_HOME
3、修改$ORACLE_HOME/bin/dbshut
su - oracle
$ nano /data/u01/app/oracle/product/11.2.0/dbhome_1/bin/dbshut
将ORACLE_HOME_LISTNER=
1
修
改
为
O
R
A
C
L
E
H
O
M
E
L
I
S
T
N
E
R
=
1修改为ORACLE_HOME_LISTNER=
1修改为ORACLEHOMELISTNER=ORACLE_HOME
# The this to bring down Oracle Net Listener
# ORACLE_HOME_LISTNER=$1
ORACLE_HOME_LISTNER=$ORACLE_HOME
4、建立启动脚本:
su
nano /etc/init.d/oracle
#!/bin/sh
# chkconfig: 345 61 61
# description: Oracle 11g R2 AutoRun Servimces
# /etc/init.d/oracle
# Run-level Startup script for the Oracle Instance, Listener, and
# Web Interface
export ORACLE_BASE="/data/u01/app/oracle"
export ORACLE_HOME=$ORACLE_BASE/product/11.2.0/dbhome_1
export ORACLE_SID="orcl"
export PATH="/data/u01/app/oracle/product/11.2.0/dbhome_1/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/oracle/.local/bin:/home/oracle/bin"
ORA_OWNR="oracle"
# if the executables do not exist -- display error
if [ ! -f $ORACLE_HOME/bin/dbstart -o ! -d $ORACLE_HOME ]
then
echo "Oracle startup: cannot start"
exit 1
fi
# depending on parameter -- startup, shutdown, restart
# of the instance and listener or usage display
case "$1" in
start)
# Oracle listener and instance startup
su $ORA_OWNR -lc $ORACLE_HOME/bin/dbstart
echo "Oracle Start Succesful!OK."
;;
stop)
# Oracle listener and instance shutdown
su $ORA_OWNR -lc $ORACLE_HOME/bin/dbshut
echo "Oracle Stop Succesful!OK."
;;
reload|restart)
$0 stop
$0 start
;;
*)
echo $"Usage: `basename $0` {start|stop|reload|reload}"
exit 1
esac
exit 0
给脚本加执行权限:
chmod 755 /etc/init.d/oracle
测试脚本是否有效:
/etc/init.d/oracle stop
Processing Database instance “orcl”: log file /data/u01/app/oracle/product/11.2.0/dbhome_1/shutdown.log
Oracle Stop Succesful!OK.
/etc/init.d/oracle start
Processing Database instance “orcl”: log file /data/u01/app/oracle/product/11.2.0/dbhome_1/startup.log
Oracle Start Succesful!OK.
5、加入自动启动行列
执行如下命令:
ln -s /etc/init.d/oracle /etc/rc2.d/S61oracle
ln -s /etc/init.d/oracle /etc/rc3.d/S61oracle
ln -s /etc/init.d/oracle /etc/rc4.d/S61oracle
ln -s /etc/init.d/oracle /etc/rc0.d/K61oracle
ln -s /etc/init.d/oracle /etc/rc6.d/K61oracle
chkconfig --level 234 oracle on
chkconfig --add oracle
chkconfig –list
6、进行测试
service oracle start
service oracle stop
service oracle restart
more /data/u01/app/oracle/product/11.2.0/dbhome_1/startup.log
八、数据库自动备份
–推荐RMAN方式,以下脚本为另一种方法
su – oracle
nano /data/u01/app/oracle/backup/backup.sh
#!/bin/sh
export ORACLE_BASE="/data/u01/appp/oracle"
export ORACLE_HOME="/data/u01/app/oracle/product/11.2.0/dbhome_1"
export ORACLE_SID="orcl"
export ORACLE_UNQNAME=$ORACLE_SID
export PATH=$ORACLE_HOME/bin:$PATH
export NLS_LANG=american_america.AL32UTF8
export DATA_DIR="/data/u01/app/oracle/backup/data"
export LOGS_DIR="/data/u01/app/oracle/backup/log"
export DELTIME=`date -d "30 days ago" +%Y%m%d`
export BAKUPTIME=`date +%Y%m%d%H%M%S`
mkdir -p $DATA_DIR
mkdir -p $LOGS_DIR
echo "Starting backup..."
echo "Backup file path $DATA_DIR/his_$BAKUPTIME.dmp"
exp his/123456 file=$DATA_DIR/his_$BAKUPTIME.dmp log=$LOGS_DIR/his_$BAKUPTIME.log
echo "Delete the file backups before 30 days..."
rm -rf $DATA_DIR/his_$DELTIME*.dmp
rm -rf $LOGS_DIR/his_$DELTIME*.log
echo "Delete the file backup successfully. "
echo "Backup completed."
chmod 700 /data/u01/app/oracle/backup/backup.sh
nano /etc/rc.local
/bin/systemctl start crond.service
# systemctl enable crond.service
# systemctl list-unit-files
# nano /etc/crontab
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
6 1 * * * oracle /data/u01/app/oracle/backup/backup.sh
# systemctl reload crond.service
# ll /data/u01/app/oracle/backup/log
# ll /data/u01/app/oracle/backup/data
九、数据库导回备份数据
1、给导入用户赋予dba 权限,切换非归档模式避免导回数据时产生大量归档日志,结束后切换回归档模式:
$ sqlplus / as sysdba
SQL> grant sysdba to his;
SQL> grant imp_full_database to his;
SQL> shutdown immediate;
SQL> startup mount;
SQL> alter database noarchivelog;
SQL > archive log list;
SQL > alter database open;
2、导回数据
$ imp his/123456 file=/home/oracle/his.dmp log=/home/oracle/his.log full=y ignore=y commit=y
3、收回权限
$ sqlplus / as sysdba
SQL> revoke sysdba from his;
SQL> revoke imp_full_database from his;
Revoke succeeded.
4、切换回归档模式:
SQL>shutdowm immediate;
SQL>startup mount;
SQL>>alter database archivelog;
SQL >archive log list
SQL >alter database open;
补充:10g以后版本推荐采用最新数据泵expdp/impdp方式:
1、原服务器上导出
mkdir –p /oradata/dump
chown oracle:oinstall /oradata/dump
sqlplus / as sysdba;
SQL>SELECT * FROM DBA_DIRECTORIES;
SQL>create or replace directory DUMP_DIR as ‘/oradata/dump’;
SQL>grant read,write on directory DUMP_DIR to his;
SQL>SELECT * FROM DBA_DIRECTORIES;
SQL>exit;
$ expdp his/123456 directory= DUMP_DIR dumpfile=his.dmp logfile=expdp.log full=y
Master table “HIS”.“SYS_EXPORT_FULL_01” successfully loaded/unloaded
Dump file set for HIS.SYS_EXPORT_FULL_01 is:
/oradata/dump/his.dmp
Job “HIS”.“SYS_EXPORT_FULL_01” successfully completed at 23:46:07
2、新服务器上导入
sqlplus / as sysdba;
SQL>SELECT * FROM DBA_DIRECTORIES;
SQL>grant read,write on directory DATA_PUMP_DIR to his;
SQL>exit;
impdp his/123456 directory= DATA_PUMP_DIR dumpfile=his.dmp logfile=his.log TABLE_EXISTS_ACTION=REPLACE full=y
参考:
https://blog.51cto.com/mflag/2300909
https://blog.csdn.net/u014642153/article/details/93133917