CentOS上快速安装Oracle服务器脚本

CentOS上快速安装Oracle服务器脚本

 

配置repos源

# cd /etc/yum.repos.d
# wget http://yum.oracle.com/public-yum-ol7.repo

官网指出了,将需要安装的版本 enable =1 ,所以要修改一下 .repo文件

 

下载RMP-GPG-KEY

  • wget http://public-yum.oracle.com/RPM-GPG-KEY-oracle-ol7 -O /etc/pki/rpm-gpg/RPM-GPG-KEY-oracle

oracle-rdbms-server-11gR2-preinstall包所干的事情

  • (1)自动安装oracle所需的RPM包
  • (2)自动创建oracle用户和group组
  • (3)自动配置/etc/sysctl.conf内核参数
  • (4)自动配置/etc/security/limits.conf参数
  • (5)关闭NUMA=OFF (关闭非一致内存访问)

 

安装必要软件

groupadd dba && useradd -m -G dba oracle && mkdir /u01 && chown oracle:dba /u01
yum install -y oracle-rdbms-server-11gR2-preinstall glibc-static wget unzip && yum clean all

 

保存脚本

mkdir -p /install
cd /install
wget https://raw.githubusercontent.com/MaksymBilenko/docker-oracle-ee-11g/master/oracle-11g-ee-base/install/oracle-11g-ee.rsp
wget https://github.com/MaksymBilenko/docker-oracle-ee-11g/blob/master/oracle-11g-ee-base/install/oracle_install.sh
wget https://raw.githubusercontent.com/MaksymBilenko/docker-oracle-ee-11g/master/entrypoint.sh

 

下载Oracle安装包

官网下载地址:https://www.oracle.com/technetwork/database/enterprise-edition/downloads/112010-linx8664soft-100572.html

# 请执行修改下载链接,AuthParam会自动过期
wget -q -O linux.x64_11gR2_database_1of2.zip https://download.oracle.com/otn/linux/oracle11g/R2/linux.x64_11gR2_database_1of2.zip?AuthParam=1561389203_9ccc19db849275e4a7d668f4607586ee

# 请执行修改下载链接,AuthParam会自动过期
wget -q -O linux.x64_11gR2_database_2of2.zip https://download.oracle.com/otn/linux/oracle11g/R2/linux.x64_11gR2_database_2of2.zip?AuthParam=1561389203_9ccc19db849275e4a7d668f4607586ee

 

执行脚本oracle_install.sh

#!/bin/bash

set -e

#Delete limits cause of docker limits issue
cat /etc/security/limits.conf | grep -v oracle | tee /etc/security/limits.conf

#此处请自行下载oracle的zip包
echo 'Downloading linux.x64_11gR2_database_1of2.zip'
wget -q -O linux.x64_11gR2_database_1of2.zip http://10.0.1.4:8080/linux.x64_11gR2_database_1of2.zip
#此处请自行下载oracle的zip包
echo 'Downloading linux.x64_11gR2_database_2of2.zip'
wget -q -O linux.x64_11gR2_database_2of2.zip http://10.0.1.4:8080/linux.x64_11gR2_database_2of2.zip
echo 'Unzipping'
unzip -q linux.x64_11gR2_database_1of2.zip
unzip -q linux.x64_11gR2_database_2of2.zip
rm -f linux*.zip

mv database /home/oracle/

su oracle -c 'cd /home/oracle/database && ./runInstaller -ignorePrereq -ignoreSysPrereqs -silent -responseFile /install/oracle-11g-ee.rsp -waitForCompletion 2>&1'
rm -rf /home/oracle/database

mv /u01/app/oracle/product /u01/app/oracle-product

#/u01/app/oraInventory/orainstRoot.sh
#/u01/app/oracle/product/11.2.0/EE/root.sh


#$ORACLE_HOME/bin/dbca -silent -createDatabase -templateName General_Purpose.dbc -gdbname EE.oracle.docker -sid EE -responseFile NO_VALUE -totalMemory 2048 -emConfiguration LOCAL  -sysPassword oracle -systemPassword oracle -dbsnmpPassword oracle

注意:脚本是保存在/install目录中的,请cd到此目录,以root权限执行脚本

 

设置Oracle参数

# vi ~/.bash_profile

export DBCA_TOTAL_MEMORY=4096
export WEB_CONSOLE="true"
export CHARACTER_SET=ZHS16GBK
export DB_NAME=ORCL
export DB_SID=ORCL
export DB_PASSWORD=ORCL123456

export ORACLE_SID=ORCL
export ORACLE_HOME=/u01/app/oracle/product/11.2.0/EE

PATH=$PATH:$ORACLE_HOME/bin

export PATH

#####保存完毕后,执行source ~/.bash_profile

# . ~/.bash_profile

 

执行entrypoint.sh脚本

#!/bin/bash
set -e

# Prevent owner issues on mounted folders
echo "Preparing oracle installer."
chown -R oracle:dba /u01/app/oracle
rm -f /u01/app/oracle/product
ln -s /u01/app/oracle-product /u01/app/oracle/product

#Run Oracle root scripts
echo "Running root scripts."
/u01/app/oraInventory/orainstRoot.sh 2>&1
echo | /u01/app/oracle/product/11.2.0/EE/root.sh 2>&1 || true

impdp () {
	set +e
	DUMP_FILE=$(basename "$1")
	DUMP_NAME=${DUMP_FILE%.dmp} 
	cat > /tmp/impdp.sql << EOL
-- Impdp User
CREATE USER IMPDP IDENTIFIED BY IMPDP;
ALTER USER IMPDP ACCOUNT UNLOCK;
GRANT dba TO IMPDP WITH ADMIN OPTION;
-- New Scheme User
create or replace directory IMPDP as '/docker-entrypoint-initdb.d';
create tablespace $DUMP_NAME datafile '/u01/app/oracle/oradata/$DUMP_NAME.dbf' size 1000M autoextend on next 100M maxsize unlimited;
create user $DUMP_NAME identified by $DUMP_NAME default tablespace $DUMP_NAME;
alter user $DUMP_NAME quota unlimited on $DUMP_NAME;
alter user $DUMP_NAME default role all;
grant all to $DUMP_NAME;
exit;
EOL

	su oracle -c "NLS_LANG=.$CHARACTER_SET $ORACLE_HOME/bin/sqlplus -S / as sysdba @/tmp/impdp.sql"
	su oracle -c "NLS_LANG=.$CHARACTER_SET $ORACLE_HOME/bin/impdp IMPDP/IMPDP directory=IMPDP dumpfile=$DUMP_FILE $IMPDP_OPTIONS"
	#Disable IMPDP user
	echo -e 'ALTER USER IMPDP ACCOUNT LOCK;\nexit;' | su oracle -c "NLS_LANG=.$CHARACTER_SET $ORACLE_HOME/bin/sqlplus -S / as sysdba"
	set -e
}

case "$1" in
	'')
		#Check for mounted database files
		if [ "$(ls -A /u01/app/oracle/oradata)" ]; then
			echo "found files in /u01/app/oracle/oradata Using them instead of initial database"
			echo "EE:$ORACLE_HOME:N" >> /etc/oratab
			chown oracle:dba /etc/oratab
			chown 664 /etc/oratab
			rm -rf /u01/app/oracle-product/11.2.0/EE/dbs
			ln -s /u01/app/oracle/dbs /u01/app/oracle-product/11.2.0/EE/dbs
			#Startup Database
			su oracle -c "/u01/app/oracle/product/11.2.0/EE/bin/tnslsnr &"
			su oracle -c 'echo startup\; | $ORACLE_HOME/bin/sqlplus -S / as sysdba'
		else
			echo "Database not initialized. Initializing database."
			export IMPORT_FROM_VOLUME=true

			if [ -z "$CHARACTER_SET" ]; then
				export CHARACTER_SET="AL32UTF8"
			fi

			#printf "Setting up:\nprocesses=$processes\nsessions=$sessions\ntransactions=$transactions\n"

			mv /u01/app/oracle-product/11.2.0/EE/dbs /u01/app/oracle/dbs
			ln -s /u01/app/oracle/dbs /u01/app/oracle-product/11.2.0/EE/dbs

			echo "Starting tnslsnr"
			su oracle -c "/u01/app/oracle/product/11.2.0/EE/bin/tnslsnr &"
			#create DB for SID: EE
			echo "Running initialization by dbca"
			su oracle -c "$ORACLE_HOME/bin/dbca -silent -createDatabase -templateName General_Purpose.dbc -gdbname EE.oracle.docker -sid EE -responseFile NO_VALUE -characterSet $CHARACTER_SET -totalMemory $DBCA_TOTAL_MEMORY -emConfiguration LOCAL -dbsnmpPassword oracle -sysPassword oracle -systemPassword oracle"
			
			# echo "Configuring Apex console"
			# cd $ORACLE_HOME/apex
			# su oracle -c 'echo -e "0Racle$\n8080" | $ORACLE_HOME/bin/sqlplus -S / as sysdba @apxconf > /dev/null'
			# su oracle -c 'echo -e "${ORACLE_HOME}\n\n" | $ORACLE_HOME/bin/sqlplus -S / as sysdba @apex_epg_config_core.sql > /dev/null'
			# su oracle -c 'echo -e "ALTER USER ANONYMOUS ACCOUNT UNLOCK;" | $ORACLE_HOME/bin/sqlplus -S / as sysdba > /dev/null'
			# echo "Database initialized. Please visit http://#containeer:8080/em http://#containeer:8080/apex for extra configuration if needed"
		fi

		if [ $WEB_CONSOLE == "true" ]; then
			echo 'Starting web management console'
			su oracle -c 'echo EXEC DBMS_XDB.sethttpport\(8080\)\; | $ORACLE_HOME/bin/sqlplus -S / as sysdba'
		else
			echo 'Disabling web management console'
			su oracle -c 'echo EXEC DBMS_XDB.sethttpport\(0\)\; | $ORACLE_HOME/bin/sqlplus -S / as sysdba'
		fi

		if [ $IMPORT_FROM_VOLUME ]; then
			echo "Starting import from '/docker-entrypoint-initdb.d':"

			for f in /docker-entrypoint-initdb.d/*; do
				echo "found file /docker-entrypoint-initdb.d/$f"
				case "$f" in
					*.sh)     echo "[IMPORT] $0: running $f"; . "$f" ;;
					*.sql)    echo "[IMPORT] $0: running $f"; echo "exit" | su oracle -c "NLS_LANG=.$CHARACTER_SET $ORACLE_HOME/bin/sqlplus -S / as sysdba @$f"; echo ;;
					*.dmp)    echo "[IMPORT] $0: running $f"; impdp $f ;;
					*)        echo "[IMPORT] $0: ignoring $f" ;;
				esac
				echo
			done

			echo "Import finished"
			echo
		else
			echo "[IMPORT] Not a first start, SKIPPING Import from Volume '/docker-entrypoint-initdb.d'"
			echo "[IMPORT] If you want to enable import at any state - add 'IMPORT_FROM_VOLUME=true' variable"
			echo
		fi

		echo "Database ready to use. Enjoy! ;)"

		##
		## Workaround for graceful shutdown.
		##
		while [ "$END" == '' ]; do
			sleep 1
			trap "su oracle -c 'echo shutdown immediate\; | $ORACLE_HOME/bin/sqlplus -S / as sysdba'" INT TERM
		done
		;;

	*)
		echo "Database is not configured. Please run '/entrypoint.sh' if needed."
		exec "$@"
		;;
esac

 

如果没有发生异常的话,到此就完工结束了

CentOS上快速安装Oracle服务器脚本

上一篇:Oracle RAC添加节点


下一篇:linux下图形化安装oracle数据适合初学者