文章

CentOS7 二进制安装Oracle19c

官方文档

安装前准备

#修改 hostname
hostnamectl set-hostname oracle-db-19c
echo "172.16.22.171 oracle-db-19c" >> /etc/hosts
#关闭 selinux
sed -i 's/^SELINUX=.*/SELINUX=permissive/g' /etc/sysconfig/selinux
setenforce permissive
#同步时间
ntpdate ntp.aliyun.com
#安装依赖
yum install -y \
  bc \
  binutils \
  compat-libcap1 \
  compat-libstdc++-33 \
  elfutils-libelf \
  elfutils-libelf-devel \
  fontconfig-devel \
  glibc \
  glibc-devel \
  ksh \
  libaio \
  libaio-devel \
  libX11 \
  libXau \
  libXi \
  libXtst \
  libXrender \
  libXrender-devel \
  libgcc \
  libstdc++ \
  libstdc++-devel \
  libxcb \
  make \
  smartmontools \
  sysstat \
  net-tools \
  gcc \
  gcc-c++ \
  gcc-info \
  gcc-locale \
  gcc48 \
  gcc48-info \
  gcc48-locale \
  gcc48-c++ \
  unzip
#新建用户和组
groupadd -g oinstall
groupadd -g dba
groupadd -g oper
groupadd -g backupdba
groupadd -g dgdba
groupadd -g kmdba
groupadd -g racdba
useradd -g oinstall -G dba,oper,backupdba,dgdba,kmdba,racdba oracle
echo "oracle" | passwd oracle --stdin
#修改 oracle 用户安全限制
cat >/etc/security/limits.d/oracle.conf<<EOF
oracle   soft   nofile    1024
oracle   hard   nofile    65536
oracle   soft   nproc    16384
oracle   hard   nproc    16384
oracle   soft   stack    10240
oracle   hard   stack    32768
oracle   hard   memlock    134217728
oracle   soft   memlock    134217728
EOF
#调整内核
cat >/etc/sysctl.d/oracle.conf<<EOF
fs.file-max = 6815744
kernel.sem = 250 32000 100 128
kernel.shmmni = 4096
kernel.shmall = 1073741824
kernel.shmmax = 4398046511104
kernel.panic_on_oops = 1
net.core.rmem_default = 262144
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 1048576
net.ipv4.conf.all.rp_filter = 2
net.ipv4.conf.default.rp_filter = 2
fs.aio-max-nr = 1048576
net.ipv4.ip_local_port_range = 9000 65500
EOF
#重新加载参数
sysctl --system

内核参数调整官方说明

创建安装目录

#/u01 for the Oracle RDBMS and /u02 for the Oracle Databases
mkdir -p /u01/app/oracle/product/19.0.0/dbhome_1
mkdir -p /u02/oradata
chown -R oracle:oinstall /u01 /u02
chmod -R 775 /u01 /u02

修改 oracle 用户环境变量

su - oracle

cat >>~/.bash_profile<<'EOF'
# Oracle Settings
export TMP=/tmp
export TMPDIR=$TMP

export ORACLE_HOSTNAME=oracle-db-19c
export ORACLE_UNQNAME=cdb1
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=$ORACLE_BASE/product/19.0.0/dbhome_1
export ORA_INVENTORY=/u01/app/oraInventory
export ORACLE_SID=cdb1
export PDB_NAME=pdb1
export DATA_DIR=/u02/oradata
export PATH=$ORACLE_HOME/bin:$PATH
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib
export CLASSPATH=$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib
EOF

source ~/.bash_profile

安装 19c

su - oracle
#解压
unzip LINUX.X64_193000_db_home.zip -d $ORACLE_HOME
cd $ORACLE_HOME
#静默安装
./runInstaller -ignorePrereq -waitforcompletion -silent \
  oracle.install.option=INSTALL_DB_SWONLY \
  ORACLE_HOSTNAME=${ORACLE_HOSTNAME} \
  UNIX_GROUP_NAME=oinstall \
  INVENTORY_LOCATION=${ORA_INVENTORY} \
  ORACLE_HOME=${ORACLE_HOME} \
  ORACLE_BASE=${ORACLE_BASE} \
  oracle.install.db.InstallEdition=EE \
  oracle.install.db.OSDBA_GROUP=dba \
  oracle.install.db.OSBACKUPDBA_GROUP=backupdba \
  oracle.install.db.OSDGDBA_GROUP=dgdba \
  oracle.install.db.OSKMDBA_GROUP=kmdba \
  oracle.install.db.OSRACDBA_GROUP=racdba \
  SECURITY_UPDATES_VIA_MYORACLESUPPORT=false \
  DECLINE_SECURITY_UPDATES=true
  
#根据提示切换 root 运行脚本
/u01/app/oraInventory/orainstRoot.sh
/u01/app/oracle/product/19.0.0/dbhome_1/root.sh
#切换 oracle 用户启动监听
lsnrctl start
#创建 Oracle 19c Multitenant 数据库
dbca -silent -createDatabase \
  -templateName General_Purpose.dbc \
  -gdbname ${ORACLE_SID} -sid  ${ORACLE_SID} \
  -responseFile NO_VALUE \
  -characterSet AL32UTF8 \
  -sysPassword XXy#sys2023 \
  -systemPassword XXy#system2023 \
  -createAsContainerDatabase true \
  -numberOfPDBs 1 \
  -pdbName ${PDB_NAME} \
  -pdbAdminPassword XXy#pdb2023 \
  -databaseType MULTIPURPOSE \
  -totalMemory 3072 \
  -storageType FS \
  -datafileDestination "${DATA_DIR}" \
  -redoLogFileSize 50 \
  -emConfiguration NONE \
  -ignorePreReqs
# 手动配置sga和pga
-automaticMemoryManagement false \
-initParams sga_target=2048M,pga_aggregate_target=1024M \
# 自动
-automaticMemoryManagement true \
-initParams MEMORY_TARGET=3072M MEMORY_MAX_TARGET=3072M

DBCA使用可参考官方文档

连接数据库

--Oracle Managed File (OMF)以简化数据库和数据库文件的创建
ALTER SYSTEM SET DB_CREATE_FILE_DEST='/u02/oradata' SCOPE=BOTH;
--自启PDB
ALTER PLUGGABLE DATABASE PDB1 SAVE STATE;

如果使用数据库账户和密码连接报ORA-12154错误,则在$ORACLE_HOME/admin/network/下添加tnsnames.ora文件

PDB1 =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = oracle-db-19c)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = PDB1)
      (SERVER = DEDICATED)
    )
  )

创建 systemd 托管

#修改 /etc/oratab
sed -i 's/:N$/:Y/g' /etc/oratab
#创建ORCLCDB.oracledb环境变量文件
cat >/etc/sysconfig/ORCLCDB.oracledb<<EOF
ORACLE_BASE=/u01/app/oracle
ORACLE_HOME=/u01/app/oracle/product/19.0.0/dbhome_1
ORACLE_SID=cdb1
EOF
#创建listener service
cat >/usr/lib/systemd/system/ORCLCDB@lsnrctl.service<<EOF
[Unit]
Description=Oracle Net Listener
After=network.target

[Service]
Type=forking
EnvironmentFile=/etc/sysconfig/ORCLCDB.oracledb
ExecStart=/u01/app/oracle/product/19.0.0/dbhome_1/bin/lsnrctl start
ExecStop=/u01/app/oracle/product/19.0.0/dbhome_1/bin/lsnrctl stop
User=oracle

[Install]
WantedBy=multi-user.target
EOF
#创建database service
cat >/usr/lib/systemd/system/ORCLCDB@oracledb.service<<'EOF'
[Unit]
Description=Oracle Database service
After=network.target ORCLCDB@lsnrctl.service

[Service]
Type=forking
EnvironmentFile=/etc/sysconfig/ORCLCDB.oracledb
ExecStart=/u01/app/oracle/product/19.0.0/dbhome_1/bin/dbstart $ORACLE_HOME
ExecStop=/u01/app/oracle/product/19.0.0/dbhome_1/bin/dbshut $ORACLE_HOME
User=oracle
TimeoutSec=300s

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable ORCLCDB@lsnrctl ORCLCDB@oracledb

rlwrap工具安装

实现在SqlPlus 命令回退,历史命令以及翻页功能。

yum -y install readline python36 perl-Data-Dumper perl-File-Slurp
wget https://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/r/rlwrap-0.45.2-2.el7.x86_64.rpm
rpm -Uvh rlwrap-0.45.2-2.el7.x86_64.rpm
#修改 oracle 用户环境变量追加
alias sqlplus='rlwrap sqlplus'
alias rman='rlwrap rman'
License:  CC BY 4.0