安装阿里云 YUM 源
https://opsx.alibaba.com/mirror?lang=zh-CN
一、安装Oracle数据库
1.安装 Oracle 预安装 RPM
yum -y localinstall https://yum.oracle.com/repo/OracleLinux/OL7/latest/x86_64/getPackage/oracle-database-preinstall-18c-1.0-1.el7.x86_64.rpm
2.安装 Oracle Database RPM
https://www.oracle.com/technetwork/database/enterprise-edition/downloads/index.html
# 若未注册可用我的,勿做修改 1161360442@qq.com
Oracle123
yum -y localinstall /tmp/oracle-database-ee-18c-1.0-.x86_64.rpm
二、创建和配置 Oracle 数据库
1.查看配置
# 根据需要修改
cat /etc/sysconfig/oracledb_ORCLCDB-18c.conf #This is a configuration file to setup the Oracle Database.
#It is used when running '/etc/init.d/oracledb_ORCLCDB configure'.
#Please use this file to modify the default listener port and the
#Oracle data location. # LISTENER_PORT: Database listener
# 侦听器的侦听端口
LISTENER_PORT= # ORACLE_DATA_LOCATION: Database oradata location
# 数据存放位置
ORACLE_DATA_LOCATION=/opt/oracle/oradata # EM_EXPRESS_PORT: Oracle EM Express listener
# Enterprise Manager 的侦听端口
EM_EXPRESS_PORT=
2.创建 Oracle 数据库实例
查看脚本
#!/bin/bash
#
# chkconfig:
# Description: This script is responsible for taking care of configuring the Oracle Database and its associated services.
#
# processname: oracledb_ORCLCDB-18c
# Red Hat or SuSE config: /etc/sysconfig/oracledb_ORCLCDB-18c
# # Set path if path not set
case $PATH in
"") PATH=/bin:/usr/bin:/sbin:/etc
export PATH ;;
esac # Check if the root user is running this script
if [ $(id -u) != "" ]
then
echo "You must be root user to run the configurations script. Login as root user and try again."
exit
fi # Setting the required environment variables
export ORACLE_HOME=/opt/oracle/product/18c/dbhome_1 export ORACLE_VERSION=18c
export ORACLE_SID=ORCLCDB
export TEMPLATE_NAME=General_Purpose.dbc
export CHARSET=AL32UTF8
export PDB_NAME=ORCLPDB1
export LISTENER_NAME=LISTENER
export NUMBER_OF_PDBS=
export CREATE_AS_CDB=true # General exports and vars
export PATH=$ORACLE_HOME/bin:$PATH
LSNR=$ORACLE_HOME/bin/lsnrctl
SQLPLUS=$ORACLE_HOME/bin/sqlplus
DBCA=$ORACLE_HOME/bin/dbca
ORACLE_OWNER=oracle
RETVAL=
CONFIG_NAME="oracledb_$ORACLE_SID-$ORACLE_VERSION.conf"
CONFIGURATION="/etc/sysconfig/$CONFIG_NAME" # Commands
if [ -z "$SU" ];then SU=/bin/su; fi
if [ -z "$GREP" ]; then GREP=/usr/bin/grep; fi
if [ ! -f "$GREP" ]; then GREP=/bin/grep; fi # To start the DB
start()
{
check_for_configuration
RETVAL=$?
if [ $RETVAL -eq ]
then
echo "The Oracle Database is not configured. You must run '/etc/init.d/oracledb_$ORACLE_SID-$ORACLE_VERSION configure' as the root user to configure the database."
exit
fi
# Check if the DB is already started
pmon=`ps -ef | egrep pmon_$ORACLE_SID'\>' | $GREP -v grep`
if [ "$pmon" = "" ];
then # Unset the proxy env vars before calling sqlplus
unset_proxy_vars echo "Starting Oracle Net Listener."
$SU -s /bin/bash $ORACLE_OWNER -c "$LSNR start $LISTENER_NAME" > /dev/null >&
RETVAL=$?
if [ $RETVAL -eq ]
then
echo "Oracle Net Listener started."
fi echo "Starting Oracle Database instance $ORACLE_SID."
$SU -s /bin/bash $ORACLE_OWNER -c "$SQLPLUS -s /nolog << EOF
connect / as sysdba
startup
alter pluggable database all open
exit;
EOF" > /dev/null 2>&1
RETVAL1=$?
if [ $RETVAL1 -eq ]
then
echo "Oracle Database instance $ORACLE_SID started."
fi
else
echo "The Oracle Database instance $ORACLE_SID is already started."
exit
fi echo
if [ $RETVAL -eq ] && [ $RETVAL1 -eq ]
then
return
else
echo "Failed to start Oracle Net Listener using $ORACLE_HOME/bin/tnslsnr and Oracle Database using $ORACLE_HOME/bin/sqlplus."
exit
fi
} # To stop the DB
stop()
{
check_for_configuration
RETVAL=$?
if [ $RETVAL -eq ]
then
echo "The Oracle Database is not configured. You must run '/etc/init.d/oracledb_$ORACLE_SID-$ORACLE_VERSION configure' as the root user to configure the database."
exit
fi
# Check if the DB is already stopped
pmon=`ps -ef | egrep pmon_$ORACLE_SID'\>' | $GREP -v grep`
if [ "$pmon" = "" ]
then
echo "Oracle Database instance $ORACLE_SID is already stopped."
exit
else # Unset the proxy env vars before calling sqlplus
unset_proxy_vars echo "Shutting down Oracle Database instance $ORACLE_SID."
$SU -s /bin/bash $ORACLE_OWNER -c "$SQLPLUS -s /nolog << EOF
connect / as sysdba
shutdown immediate
exit;
EOF" > /dev/null 2>&1
RETVAL=$?
if [ $RETVAL -eq ]
then
echo "Oracle Database instance $ORACLE_SID shut down."
fi echo "Stopping Oracle Net Listener."
$SU -s /bin/bash $ORACLE_OWNER -c "$LSNR stop $LISTENER_NAME" > /dev/null >&
RETVAL1=$?
if [ $RETVAL1 -eq ]
then
echo "Oracle Net Listener stopped."
fi
fi echo
if [ $RETVAL -eq ] && [ $RETVAL1 -eq ]
then
return
else
echo "Failed to stop Oracle Net Listener using $ORACLE_HOME/bin/tnslsnr and Oracle Database using $ORACLE_HOME/bin/sqlplus."
exit
fi
} # To call DBCA to configure the DB
configure_perform()
{
# Unset the proxy env vars before calling dbca
unset_proxy_vars echo "Configuring Oracle Database $ORACLE_SID." $SU -s /bin/bash $ORACLE_OWNER -c "$DBCA -silent -createDatabase -gdbName $ORACLE_SID -templateName $TEMPLATE_NAME -characterSet $CHARSET -createAsContainerDatabase $CREATE_AS_CDB -numberOfPDBs $NUMBER_OF_PDBS -pdbName $PDB_NAME -createListener $LISTENER_NAME:$LISTENER_PORT -datafileDestination $ORACLE_DATA_LOCATION -sid $ORACLE_SID -autoGeneratePasswords -emConfiguration DBEXPRESS -emExpressPort $EM_EXPRESS_PORT" RETVAL=$? echo
if [ $RETVAL -eq ]
then
echo "Database configuration completed successfully. The passwords were auto generated, you must change them by connecting to the database using 'sqlplus / as sysdba' as the oracle user."
return
else
echo "Database configuration failed."
exit
fi
} # Enh - Unsets the proxy env variables
unset_proxy_vars()
{
if [ "$http_proxy" != "" ]
then
unset http_proxy
fi if [ "$HTTP_PROXY" != "" ]
then
unset HTTP_PROXY
fi if [ "$https_proxy" != "" ]
then
unset https_proxy
fi if [ "$HTTPS_PROXY" != "" ]
then
unset HTTPS_PROXY
fi
} # Check if the DB is already configured
check_for_configuration()
{
configfile=`$GREP --no-messages $ORACLE_SID:$ORACLE_HOME /etc/oratab` > /dev/null >&
if [ "$configfile" = "" ]
then
return
fi
return
} read_config_file()
{
if [ -f "$CONFIGURATION" ]
then
. "$CONFIGURATION"
else
echo "The Oracle Database is not configured. Unable to read the configuration file '$CONFIGURATION'"
exit ;
fi
} # Entry point to configure the DB
configure()
{
check_for_configuration
RETVAL=$?
if [ $RETVAL -eq ]
then
echo "Oracle Database instance $ORACLE_SID is already configured."
exit
fi
read_config_file
check_port_availability
check_em_express_port_availability
configure_perform
} check_port_availability()
{
port=`netstat -n --tcp --listen | $GREP :$LISTENER_PORT`
if [ "$port" != "" ]
then
echo "Port $LISTENER_PORT appears to be in use by another application. Specify a different port in the configuration file '$CONFIGURATION'"
exit ;
fi
} # Validation method to check for port availability for Oracle EM Express
check_em_express_port_availability()
{
port=`netstat -n --tcp --listen | $GREP :$EM_EXPRESS_PORT`
if [ "$port" != "" ]
then
echo "Port $EM_EXPRESS_PORT appears to be in use by another application. Specify a different Oracle EM Express port in the configuration file '$CONFIGURATION'"
exit ;
fi
} restart()
{
# Check if the DB is already stopped
pmon=`ps -ef | egrep pmon_$ORACLE_SID'\>' | $GREP -v grep`
if [ "$pmon" = "" ]
then
start
else
stop
start
fi
} case "$1" in
start)
start
;;
stop)
stop
;;
configure)
configure
;;
restart)
restart
;;
*)
echo $"Usage: $0 {start|stop|restart|configure}"
exit
;;
esac exit
执行脚本
# 执行脚本之后将创建一个容器数据库(ORCLCDB)和一个可插拔数据库(ORCLPDB1),并且配置的默认监听端口是1521。注意 /opt 目录剩余空间大小
/etc/init.d/oracledb_ORCLCDB-18c configure
查看 oratab
# 记录每个数据库的信息,最后一个字母为是否在系统启动时启动
cat /etc/oratab
三、使用
# 作用与 /etc/profile 一样
vim /etc/profile.d/oracle.sh export ORACLE_BASE=/opt/oracle/oradata
export ORACLE_HOME=/opt/oracle/product/18c/dbhome_1
export PATH=$ORACLE_HOME/bin:$PATH
export ORACLE_SID=ORCLCDB
export NLS_LANG=american_america.AL32UTF8 # 刷新环境变量
source /etc/profile.d/oracle.sh
相关命令
# 监听端口查看
netstat -nultp | grep -E '1521|5500' # 监听
lsnrctl start
lsnrctl stop
lsnrctl stat # 数据库
dbstart $ORACLE_HOME
dbshut $ORACLE_HOME
连接到 oracle 数据库
# 切换到 oracle 用户,root 用户下无法连接
su - oracle # 启动监听
lsnrctl start # 以 sysdba 身份登录
sqlplus / as sysdba
修改数据库密码
-- 启动数据库
startup; -- 配置 system 和 sys 账户的密码
alter user system identified by oracle;
alter user sys identified by oracle; -- 版本查询
select banner from sys.v_$version; -- 查看所有用户
select username from dba_users;
查看 em 界面,Oracle 18c 默认会开启 Enterprise Manager Database Express,修改完sys密码之后,就可以访问:https://IP:5500/em,忽略 https 证书错误,允许 Flash 加载
远程连接至 Oracle 数据库 (Navicat Premium)
四、添加开机自启动
1.新建环境参数
vim /etc/sysconfig/ORCLCDB.oracledb ORACLE_BASE=/opt/oracle/oradata
ORACLE_HOME=/opt/oracle/product/18c/dbhome_1
ORACLE_SID=ORCLCDB
2.新建监听服务
vim /usr/lib/systemd/system/ORCLCDB@lsnrctl.service [Unit]
Description=Oracle Net Listener
After=network.target [Service]
Type=forking
EnvironmentFile=/etc/sysconfig/ORCLCDB.oracledb
ExecStart=/opt/oracle/product/18c/dbhome_1/bin/lsnrctl start
ExecStop=/opt/oracle/product/18c/dbhome_1/bin/lsnrctl stop
User=oracle [Install]
WantedBy=multi-user.target
3.新建数据库服务
vim /usr/lib/systemd/system/ORCLCDB@oracledb.service [Unit]
Description=Oracle Database service
After=network.target lsnrctl.service [Service]
Type=forking
EnvironmentFile=/etc/sysconfig/ORCLCDB.oracledb
ExecStart=/opt/oracle/product/18c/dbhome_1/bin/dbstart $ORACLE_HOME
ExecStop=/opt/oracle/product/18c/dbhome_1/bin/dbshut $ORACLE_HOME
User=oracle [Install]
WantedBy=multi-user.target
4.设置服务自启动
# 刷新服务
systemctl daemon-reload # 设置开机自启动
systemctl enable ORCLCDB@lsnrctl ORCLCDB@oracledb
5.设置数据库自启动
# 此文件由 root.sh 创建
# 格式为:$ORACLE_SID:$ORACLE_HOME:<N|Y>
# 第一个和第二个字段分别是数据库的系统标识符和主目录。 第三个字段为是否跟随 dbstart 命令启动数据库实例
# 不允许有相同 $ORACLE_SID 的条目
vim /etc/oratab ORCLCDB:/opt/oracle/product/18c/dbhome_1:Y
https://blog.csdn.net/hanzheng260561728/article/details/88202571