安裝阿里雲 YUM 源html
https://opsx.alibaba.com/mirror?lang=zh-CNsql
1、安裝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數據庫
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
# 若未註冊可用個人,勿作修改 1161360442@qq.com Oracle123
yum -y localinstall /tmp/oracle-database-ee-18c-1.0-1.x86_64.rpm
2、建立和配置 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=1521 # ORACLE_DATA_LOCATION: Database oradata location # 數據存放位置 ORACLE_DATA_LOCATION=/opt/oracle/oradata # EM_EXPRESS_PORT: Oracle EM Express listener # Enterprise Manager 的偵聽端口 EM_EXPRESS_PORT=5500
2.建立 Oracle 數據庫實例
查看腳本express
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
#!/bin/bash # # chkconfig: 2345 80 05 # 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) != "0" ] then echo "You must be root user to run the configurations script. Login as root user and try again." exit 1 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=1 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=0 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 1 ] 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 2>&1 RETVAL=$? if [ $RETVAL -eq 0 ] 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 0 ] then echo "Oracle Database instance $ORACLE_SID started." fi else echo "The Oracle Database instance $ORACLE_SID is already started." exit 0 fi echo if [ $RETVAL -eq 0 ] && [ $RETVAL1 -eq 0 ] then return 0 else echo "Failed to start Oracle Net Listener using $ORACLE_HOME/bin/tnslsnr and Oracle Database using $ORACLE_HOME/bin/sqlplus." exit 1 fi } # To stop the DB stop() { check_for_configuration RETVAL=$? if [ $RETVAL -eq 1 ] 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 1 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 1 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 0 ] 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 2>&1 RETVAL1=$? if [ $RETVAL1 -eq 0 ] then echo "Oracle Net Listener stopped." fi fi echo if [ $RETVAL -eq 0 ] && [ $RETVAL1 -eq 0 ] then return 0 else echo "Failed to stop Oracle Net Listener using $ORACLE_HOME/bin/tnslsnr and Oracle Database using $ORACLE_HOME/bin/sqlplus." exit 1 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 0 ] 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 0 else echo "Database configuration failed." exit 1 fi } # Enh 27965939 - 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 2>&1 if [ "$configfile" = "" ] then return 1 fi return 0 } read_config_file() { if [ -f "$CONFIGURATION" ] then . "$CONFIGURATION" else echo "The Oracle Database is not configured. Unable to read the configuration file '$CONFIGURATION'" exit 1; fi } # Entry point to configure the DB configure() { check_for_configuration RETVAL=$? if [ $RETVAL -eq 0 ] then echo "Oracle Database instance $ORACLE_SID is already configured." exit 1 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 1; 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 1; 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 1 ;; esac exit 0
執行腳本vim
# 執行腳本以後將建立一個容器數據庫(ORCLCDB)和一個可插拔數據庫(ORCLPDB1),而且配置的默認監聽端口是1521。注意 /opt 目錄剩餘空間大小 /etc/init.d/oracledb_ORCLCDB-18c configure
查看 oratabbash
# 記錄每一個數據庫的信息,最後一個字母爲是否在系統啓動時啓動 cat /etc/oratab
3、使用
# 做用與 /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
相關命令oracle
# 監聽端口查看 netstat -nultp | grep -E '1521|5500' # 監聽 lsnrctl start lsnrctl stop lsnrctl stat # 數據庫 dbstart $ORACLE_HOME dbshut $ORACLE_HOME
鏈接到 oracle 數據庫app
# 切換到 oracle 用戶,root 用戶下沒法鏈接 su - oracle # 啓動監聽 lsnrctl start # 以 sysdba 身份登陸 sqlplus / as sysdba
修改數據庫密碼tcp
-- 啓動數據庫 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 加載ide
遠程鏈接至 Oracle 數據庫 (Navicat Premium)
4、添加開機自啓動
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