操作系统:CentOS-7-x86_64-DVD-1804.iso
内存:8G
磁盘:128G
- Oracle安装包下载:https://www.oracle.com/technetwork/database/enterprise-edition/downloads/112010-linx8664soft-100572.html
- 依赖包下载:https://pkgs.org/
#--检查是否开启
# systemctl status iptables.service
[comuser@localhost ~]$ systemctl status iptables.service
Unit iptables.service could not be found.
#--重启后生效
#--开启
# systemctl enable iptables.service
#--关闭 (== 当前操作建议使用 ==)
# systemctl disable iptables.service
#--即时生效,重启后失效
#--开启
# systemctl start iptables.service
#--关闭
# systemctl stop iptables.service
# vim /etc/selinux/config
#--原内容
SELINUX=enforcing
#--修改后
SELINUX=disabled
https://docs.oracle.com/cd/E11882_01/install.112/e47689/pre_install.htm#LADBI1111
# rpm -ivh kernel-headers-*.x86_64.rpm
# rpm -ivh zlib-devel-*.x86_64.rpm
# rpm -Uvh libgcc-*.x86_64.rpm libgomp-*.x86_64.rpm binutils-*.x86_64.rpm libstdc++-*.x86_64.rpm
# rpm -Uvh nspr-*.x86_64.rpm nss-softokn-freebl-*.x86_64.rpm nss-util-*.x86_64.rpm
# rpm -Uvh glibc-common-*.x86_64.rpm glibc-*.x86_64.rpm elfutils-libs-*.x86_64.rpm elfutils-*.x86_64.rpm elfutils-libelf-*.x86_64.rpm
# rpm -Uvh sysstat-*.x86_64.rpm lm_sensors-libs-*.x86_64.rpm
# rpm -Uvh libxcb-*.x86_64.rpm libxcb-devel-*.x86_64.rpm
# rpm -ivh libX11-common-*.noarch.rpm
# rpm -Uvh libX11-*.x86_64.rpm libX11-devel-*.x86_64.rpm
# rpm -ivh elfutils-libelf-devel-*.x86_64.rpm
# rpm -ivh libmpc-*.x86_64.rpm
# rpm -ivh libstdc++-devel-*.x86_64.rpm cpp-*.x86_64.rpm
# rpm -ivh gcc-*.x86_64.rpm gcc-c++-*.x86_64.rpm
# rpm -ivh glibc-devel-*.x86_64.rpm glibc-headers-*.x86_64.rpm
# rpm -ivh nss-softokn-freebl-*.i686.rpm glibc-*.i686.rpm glibc-devel-*.i686.rpm
# rpm -ivh unixODBC-*.x86_64.rpm unixODBC-devel-*.x86_64.rpm
# rpm -ivh libtool-ltdl-*.x86_64.rpm libXext-*.x86_64.rpm mpfr-*.x86_64.rpm libXau-*.x86_64.rpm
# rpm -ivh libXp-*.x86_64.rpm compat-libstdc++-*.x86_64.rpm libaio-devel-*.x86_64.rpm pdksh-*.x86_64.rpm
oinstall
# groupadd oinstall
dba
# groupadd dba
orauser
,并加入用户组oinstall
和dba
# useradd -g oinstall -g dba -m orauser
orauser
是否创建成功# groups orauser
# id orauser
orauser
的密码,用于CentOS 登录使用# passwd orauser
# vim /etc/sudoers
root ALL=(ALL) ALL
orauser ALL=(ALL) ALL
/etc/sysctl.conf
文件# vim /etc/sysctl.conf
net.ipv4.icmp_echo_ignore_broadcasts = 1
net.ipv4.conf.all.rp_filter = 1
#--设置最大打开文件数
fs.file-max = 6815744
fs.aio-max-nr = 1048576
#--共享内存的总量,8G内存设置:2097152*4k/1024/1024
kernel.shmall = 2097152
#--最大共享内存的段大小
kernel.shmmax = 2147483648
#--整个系统共享内存端的最大数
kernel.shmmni = 4096
kernel.sem = 250 32000 100 128
#--可使用的IPv4端口范围
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
# sysctl -p
对 orauser 用户设置限制,提高软件运行性能
/etc/security/limits.conf
文件# vim /etc/security/limits.conf
orauser soft nproc 2047
orauser hard nproc 16384
orauser soft nofile 1024
orauser hard nofile 65536
由于 Oracle 11g 发行时,CentOS7并未发行,所以,需要修改linux发行版本,应对Oracle安装时的检测
编辑 /etc/redhat-release
文件
# vim /etc/redhat-release
#--原文件内容
CentOS Linux release 7.5.1804 (Core)
#--修改后文件内容
redhat-7
#--创建文件夹
# mkdir /opt/oracle11g
# mkdir /opt/oracle11g/oracle
# mkdir /opt/oracle11g/oraInventory
# mkdir /opt/oracle11g/inspkg
#-- 修改目录及子目录所有文件的拥有者
# chown -R orauser:oinstall /opt/oracle11g
/home/orauser/.bash_profile
文件$ vim /home/orauser/.bash_profile
#--oracle数据库安装目录
export ORACLE_BASE=/opt/oracle11g/oracle
#--oracle数据库路径
export ORACLE_HOME=$ORACLE_BASE/product/11.2.0/db_1
#--oracle启动数据库实例名
export ORACLE_SID=orcl
#--xterm窗口模式安装
export ORACLE_TERM=xterm
#--添加系统环境变量
export PATH=$ORACLE_HOME/bin:/usr/sbin:$PATH
#--添加系统环境变量
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib
#--防止安装过程出现乱码
export LANG=en_US
#--设置Oracle客户端字符集,必须与Oracle安装时设置的字符集保持一致
export NLS_LANG=AMERICAN_AMERICA.ZHS16GBK
# 解决窗口异常的问题(小条边框或不完整导致不能点击yes|no)
export JAVA_FONTS=/usr/share/fonts/zh_CN/TrueType
$ source .bash_profile
# unzip linux.x64_11gR2_database_1of2.zip -d /opt/oracle11g/inspkg/
# unzip linux.x64_11gR2_database_2of2.zip -d /opt/oracle11g/inspkg/
$ cd /opt/oracle11g/inspkg/database
$ ./runInstaller -ignoreSysPrereqs
1.跳过邮箱设置
2.选择 Create and configure a database
3.选择 Desktop Class
4.确认安装路径
dba
sys
的密码5.确认 oraInventory 目录
6.勾选 Ignore all 复选框
7.确认配置信息后,点击 finish 按钮(如果Oracle安装过程有出错,可以直接忽略,点击 Continue )
#--输出当前实例
$ echo $ORACLE_SID
#--多个实例时,指定当前实例
$ export ORACLE_SID=orcl
#--启动监听
$ lsnrctl start
#--查看监听状态
$ lsnrctl status
#--启动sqlplus
$ sqlplus /nolog
#--dba登录
SQL> conn / as sysdba
#--启动当前实例
SQL> startup
#--检查当前实例是否启动成功(出现一排的ora_XXX_实例名,说明启动成功)
$ ps -ef|grep ora_
1)配置 /etc/oratab
文件(如果没有,新建即可)
# vim /etc/oratab
orcl:/opt/oracle11g/oracle/product/11.2.0/db_1:N
orcl:/opt/oracle11g/oracle/product/11.2.0/db_1:Y
2)配置 /etc/rc.d/rc.local
文件
# vim /etc/rc.d/rc.local
su orauser -lc "/opt/oracle11g/oracle/product/11.2.0/db_1/bin/lsnrctl start"
su orauser -lc /opt/oracle11g/oracle/product/11.2.0/db_1/bin/dbstart
rc.local
的可执行权限# chmod +x /etc/rc.d/rc.local
#--停止监听
$ lsnrctl stop
#--查看监听状态
$ lsnrctl status
#--查看指定端口状态
$ netstat -an | grep 15215
#--启动监听
$ lsnrctl start
listener.ora
监听配置文件#--端口由 1521 改为 15215 | $ORACLE_HOME需要配置环境变量
$ vim $ORACLE_HOME/network/admin/listener.ora
#--原配置
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
)
)
#--修改后
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 15215))
)
)
#--启动sqlplus
$ sqlplus /nolog
#--dba登录
SQL> conn / as sysdba
#--查询 local_listener 属性的Value是否为空字符串
SQL> show parameter local_listener;
#--修改 local_listener 属性
SQL> alter system set local_listener='(address = (protocol = tcp)(host = localhost)(port = 15215))';
#--多个实例时,切换到需要操作的实例
$ export ORACLE_SID=orcl
#--进入sqlplus控制台
$ sqlplus /nolog
#--以管理员身份登录
SQL> conn / as sysdba
#--停止数据库实例
SQL> shutdown abort
#--启动实例
SQL> startup
#--退出sqlplus
SQL> exit
oracle 默认表空间 system
oracle 默认临时表空间 temp
创建表空间
#--登录sqlplus,查询数据文件位置
SQL> select name from v$datafile;
#--创建临时表空间,temp_orcl 表示临时表空间名称
SQL> create temporary tablespace temp_orcl tempfile '/opt/oracle11g/oradata/orcl/temp_orcl.dbf' size 512M autoextend on next 128M maxsize unlimited extent management local;
#--创建表空间,db_orcl 表示表空间名称
SQL> create tablespace db_orcl logging datafile '/opt/oracle11g/oradata/orcl/db_orcl.dbf' size 512M autoextend on next 128M maxsize unlimited extent management local;
用户名
,identified后面接密码
,db_orcl表示表空间,temp_orcl表示临时表空间)#--创建用户
SQL> create user testuser identified by test123 account unlock default tablespace db_orcl temporary tablespace temp_orcl;
#--级联删除,cascade表示如果用户testuser下有其它对象,也会一并删除(强制删除)
SQL> drop user testuser cascade
#--修改用户testuser的密码
SQL> alter user testuser identified by 123456;
#--把connect,resources角色赋予用户testuser
SQL> grant connect,resource to testuser;
#--把dba角色赋予用户testuser
SQL> grant dba to testuser;
#--撤销角色
SQL> revoke connect,resource to testuser
#-- 用户=testuser,密码=test123,监听端口=15215
$ imp testuser/test123@localhost:15215/db_orcl file=/home/orauser/upload/test.dmp full=y
SQL> select 'drop table '||table_name||';' from cat where table_type='TABLE';
建议使用Oracle用户(具有sudo权限)
查询当前数据库字符集,避免备份后乱码情况(选其一)
SQL> select * from nls_database_parameters t where t.parameter='NLS_CHARACTERSET';
SQL> select * from v$nls_parameters where parameter='NLS_CHARACTERSET';
#--创建数据备份目录,`-p`=创建多级目录
# mkdir -p /opt/oracle11g/backup
#--创建备份脚本目录
# mkdir -p /opt/oracle11g/backup/shell
#--创建导出数据日志目录
# mkdir -p /opt/oracle11g/backup/logs
# vim /opt/oracle11g/backup/shell/orabak.sh
orabak.sh
脚本内容如下:
1. Oracle安装路径、备份路径、Oracle用户和密码、实例名等信息需要根据情况进行修改
2. 注释的脚本是,每次备份数据库时自动删除30天之前的数据库文件
3. 脚本中使用 NLS_LANG
设置导出数据库文件字符集
#!/bin/sh
export ORACLE_BASE=/opt/oracle11g
export ORACLE_HOME=$ORACLE_BASE/product/11.2.0/db_1
export ORACLE_SID=orcl
export PATH=$PATH:$HOME/bin:$ORACLE_HOME/bin
export DATA_DIR=/opt/oracle11g/backup
export LOGS_DIR=/opt/oracle11g/backup/log
#export DELTIME=`date -d "30 days ago" +%Y%m%d`
export BAKUPTIME=`date +%Y%m%d%H%M%S`
export NLS_LANG=AMERICAN_AMERICA.ZHS16GBK
mkdir -p $DATA_DIR
mkdir -p $LOGS_DIR
echo "Starting bakup..."
echo "Bakup file path $DATA_DIR/$BAKUPTIME.dmp"
exp testuser/123456@localhost/orcl file=$DATA_DIR/ORCL$BAKUPTIME.dmp log=$LOGS_DIR/ORCL$BAKUPTIME.log
#echo "Delete the file bakup before 30 days..."
#rm -rf $DATA_DIR/ORCL$DELTIME*.dmp
#rm -rf $LOGS_DIR/ORCL$DELTIME*.log
#echo "Delete the file bakup successfully. "
echo "Bakup completed."
#--修改脚本文件权限
# chmod 777 dbbak.sh
#--测试脚本
# cd /opt/oracle11g/backup/shell
# ./orabak.sh
# vim /etc/crontab
配置内容如下:
1. 配置内容格式: 时间 + 执行操作的系统用户 + 脚本文件的全路径
2. 下面配置表示凌晨1点执行数据库备份脚本
00 1 * * * orauser /opt/oracle11g/backup/shell/orabak.sh
ORA-01157: cannot identify/lock data file 6 - see DBWR trace file
ORA-01110: data file 6: ‘/opt/oracle11g/oracle/oradata/nhswksh/db_nhswksh.dbf’
#--多个实例时,切换到需要操作的实例
$ export ORACLE_SID=orcl
$ sqlplus /nolog
SQL> conn / as sysdba
SQL> alter database datafile 6 offline drop;
SQL> alter database open;
SQL> select ts#,file#,name from v$datafile;
SQL> select tablespace_name,status from dba_tablespaces;
#-- 表空间名称:temp_orcl
SQL> drop tablespace temp_orcl including contents and datafiles;
SQL> shutdown immediate
SQL> startup