一、安装数据库前准备工作
1.1 安装源
在线的或离线的,最好能有在线安装源
# 阿里云在线镜像
curl -o etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum makecache fast
1.2 禁用透明HugePages
Oracle建议在开始安装之前禁用“透明HugePages”
透明的HugePages可能会在运行时导致内存分配延迟。为避免性能问题,Oracle建议您在所有Oracle数据库服务器上禁用“透明HugePages”。Oracle建议您改用标准HugePages来增强性能。
执行如下命令禁用
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag
echo 'echo never > sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.d/rc.local
echo 'echo never > sys/kernel/mm/transparent_hugepage/defrag' >> /etc/rc.d/rc.local
chmod +x etc/rc.d/rc.local
cat etc/rc.d/rc.local
1.3 交换空间
比如增加交换空间:16GB
mkdir -p /var/lib/swap
dd if=/dev/zero of=/var/lib/swap/swapfile bs=1M count=16384
chmod 600 /var/lib/swap/swapfile
mkswap /var/lib/swap/swapfile
swapon /var/lib/swap/swapfile
echo -e '/var/lib/swap/swapfile\tswap\tswap\tdefaults\t0 0' >> etc/fstab
查看当前交换空间是否生效:swapon -s
1.4 软件包需求
yum install -y bc binutils compat-libcap1 compat-libstdc++-33 glibc glibc-devel ksh libaio libaio-devel libgcc libstdc++ libstdc++-devel libxcb libX11 libXau libXi libXtst libXrender libXrender-devel make smartmontools sysstat
yum install -y unzip net-tools
1.5 用户和组
Extended Oracle Database groups:
OSBACKUPDBA (backupdba)
OSDGDBA (dgdba)
OSKMDBA (kmdba)
groupadd -g 54321 oinstall
groupadd -g 54322 dba
groupadd -g 54323 oper
groupadd -g 54324 backupdba
groupadd -g 54325 dgdba
groupadd -g 54326 kmdba
groupadd -g 54327 asmdba
groupadd -g 54328 asmoper
groupadd -g 54329 asmadmin
groupadd -g 54330 racdba
useradd -m -u 54321 -g oinstall -G dba,oper,backupdba,dgdba,kmdba,asmdba,racdba oracle
echo "oracle:password" | chpasswd
1.6 系统内核参数优化
新建/etc/sysctl.d/97-oracle-database-sysctl.conf文件,增加以下内容:
cat >> /etc/sysctl.d/97-oracle-database-sysctl.conf << EOF
fs.aio-max-nr = 1048576
fs.file-max = 6815744
kernel.shmall = 2097152
kernel.shmmax = 4294967295
kernel.shmmni = 4096
kernel.sem = 250 32000 100 128
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
EOF
重新启动计算机,或运行sysctl --system
以使/etc/sysctl.d/97-oracle-database-sysctl.conf文件更改在活动的内核内存中可用
kernel.shmmax值的计算:此值一般为物理内存的一半
4294967295/1024/1024/1024=4G
可以通过下面的脚本来计算shmall
和shmmax
的值
#!/bin/bash
# http://archives.postgresql.org/pgsql-admin/2010-05/msg00285.php
# Output lines suitable for sysctl configuration based
# on total amount of RAM on the system. The output
# will allow up to 50% of physical memory to be allocated
# into shared memory.
# On Linux, you can use it as follows (as root):
#
# ./shmsetup >> etc/sysctl.conf
# sysctl -p
# Early FreeBSD versions do not support the sysconf interface
# used here. The exact version where this works hasn't
# been confirmed yet.
page_size=`getconf PAGE_SIZE`
phys_pages=`getconf _PHYS_PAGES`
if [ -z "$page_size" ]; then
echo Error: cannot determine page size
exit 1
fi
if [ -z "$phys_pages" ]; then
echo Error: cannot determine number of memory pages
exit 2
fi
shmall=`expr $phys_pages 2`
shmmax=`expr $shmall \* $page_size`
echo \# Maximum shared segment size in bytes
echo kernel.shmmax = $shmmax
echo \# Maximum number of shared memory segments in pages
echo kernel.shmall = $shmall
1.7 配置oracle安装所有者的shell 限制
cp /etc/security/limits.conf etc/security/limits.conf.bak`date +%Y%m%d`
cat >> /etc/security/limits.conf << EOF
#oracle shell limit
oracle soft nproc 2047
oracle hard nproc 16384
oracle soft nofile 1024
oracle hard nofile 65536
oracle hard stack 10240
EOF
1.8 配置Oracle用户环境变量
cp home/oracle/.bash_profile home/oracle/.bash_profile.bak`date +%Y%m%d`
cat >> home/oracle/.bash_profile << EOF
#Oracle Settings
export ORACLE_BASE=/u01/app/oracle;
export ORACLE_HOME=\$ORACLE_BASE/product/12.2.0/dbhome_1/;
export ORACLE_SID=orcl;
export PATH=\$ORACLE_HOME/bin:\$PATH;
export LD_LIBRARY_PATH=\$ORACLE_HOME/lib:/lib:/usr/lib;
export CLASSPATH=\$ORACLE_HOME/JRE:\$ORACLE_HOME/jlib:\$ORACLE_HOME/rdbms/jlib;
export ORACLE_OWNER=oracle
export ORACLE_UNQNAME=\$ORACLE_SID
EOF
source home/oracle/.bash_profile
1.9 创建相关目录
初始化数据分区并挂载
parted /dev/sdb mklabel gpt mkpart primary xfs 0% 100%
mkfs.xfs /dev/sdb1
mkdir /u01
echo -e "UUID=$(lsblk -f | grep sdb1 | awk '{print $NF}')\t/u01\txfs\tdefaults\t0 0" >> etc/fstab
mount -a
lsblk -f
1.10 创建 Oracle 安装目录
mkdir -p /u01/app/oraInventory
mkdir -p /u01/app/oracle
mkdir -p u01/app/oracle/product/12.2.0/dbhome_1/
chown -R oracle:oinstall u01/app/oraInventory
chown -R oracle:oinstall u01/app/oracle
chown -R oracle:oinstall u01/app/oracle/product/12.2.0/dbhome_1/
1.11 oracle安装包准备
上传并解压安装包
cd /opt/
unzip linuxx64_12201_database.zip
修改安装目录权限
chown -R oracle:oinstall /opt/database/
ll -d opt/database/
二、数据库安装过程
2.1 oracle 用户登录
安装过程使用oracle用户登录操作
su - oracle
2.2 创建应答文件
mkdir /u01/app/oracle/etc/
cp /opt/database/response/* u01/app/oracle/etc/
cd /u01/app/oracle/etc/
预处理应答文件(可选)
删除注释行(以#开头) 和 沒有內容的空行(^$)
cd u01/app/oracle/etc/
sed -i 's/^#.*$//g' *.rsp
sed -i '/^$/d' *.rsp
2.3 静默安装oracle软件
编辑db_install.rsp文件
将需要修改的部分写入db_install.config中:
cat >> db_install.config << EOF
oracle.install.option=INSTALL_DB_SWONLY
UNIX_GROUP_NAME=oinstall
INVENTORY_LOCATION=/u01/app/oraInventory
ORACLE_HOME=/u01/app/oracle/product/12.2.0/dbhome_1/
ORACLE_BASE=/u01/app/oracle
oracle.install.db.InstallEdition=EE
oracle.install.db.OSDBA_GROUP=dba
oracle.install.db.OSOPER_GROUP=oper
oracle.install.db.OSBACKUPDBA_GROUP=backupdba
oracle.install.db.OSDGDBA_GROUP=dgdba
oracle.install.db.OSKMDBA_GROUP=kmdba
oracle.install.db.OSRACDBA_GROUP=racdba
oracle.install.db.config.starterdb.type=GENERAL_PURPOSE
oracle.install.db.config.starterdb.globalDBName=orcl # 根据实际修改
oracle.install.db.config.starterdb.SID=orcl # 根据实际修改
oracle.install.db.config.starterdb.characterSet=AL32UTF8 # 根据实际修改
oracle.install.db.config.starterdb.memoryLimit=25600 # 根据实际修改 指定数据库的总内存分配。值(以MB为单位)应该至少为256 MB,并且不应该超过系统上可用的物理内存总量。25600大概是32GB的80%
oracle.install.db.config.starterdb.password.ALL=password # 根据实际修改
DECLINE_SECURITY_UPDATES=true
EOF
快速修改
for i in $(cat db_install.config);do
key=$(echo $i | awk -F= '{print $1}');
value=$(echo $i | awk -F= '{print $2}');
sed -i "s#$key=#&$value#g" db_install.rsp;
done
sed 的 / 换成 # 在原因是配置中的路径有 / 字符
执行静默数据库软件安装命令
cd /opt/database/
#./runInstaller -force -silent -noconfig -responseFile /u01/app/oracle/etc/db_install.rsp
./runInstaller -silent -ignorePrereq -responseFile /u01/app/oracle/etc/db_install.rsp # 跳过前期检查
tail -f runInstaller命令输出的日志文件,可以监控安装的过程。
执行安装命令后会提示使用root用户执行两个脚本文件,安装完成;
安装不成功时删除/u01/app/oraInventory和/u01/app/oracle/product/12.2.0/dbhome_1/目录的内容,再重启执行安装命令
rm -rf /u01/app/oraInventory/*
rm -rf /u01/app/oracle/product/12.2.0/dbhome_1/*
2.4 静默安装监听
编辑netca.rsp文件(保持默认)
# cd /u01/app/oracle/etc/
# egrep -v '^#|^$' netca.rsp
[GENERAL]
RESPONSEFILE_VERSION="12.2"
CREATE_TYPE="CUSTOM"
[oracle.net.ca]
INSTALLED_COMPONENTS={"server","net8","javavm"}
INSTALL_TYPE=""typical""
LISTENER_NUMBER=1
LISTENER_NAMES={"LISTENER"}
LISTENER_PROTOCOLS={"TCP;1521"}
LISTENER_START=""LISTENER""
NAMING_METHODS={"TNSNAMES","ONAMES","HOSTNAME"}
NSN_NUMBER=1
NSN_NAMES={"EXTPROC_CONNECTION_DATA"}
NSN_SERVICE={"PLSExtProc"}
NSN_PROTOCOLS={"TCP;HOSTNAME;1521"}
可根据需要修改监听端口
执行静默创建命令
netca -silent -responsefile /u01/app/oracle/etc/netca.rsp
2.5 静默创建数据库
编辑dbca.rsp文件
将需要修改的部份写入dbca.config中:
cd /u01/app/oracle/etc/
cat >> dbca.config << EOF
gdbName=orcl
sid=orcl
sysPassword=password
systemPassword=password
dbsnmpPassword=password
characterSet=AL32UTF8
EOF
快速修改
for i in $(cat dbca.config);do
key=$(echo $i | awk -F= '{print $1}');
value=$(echo $i | awk -F= '{print $2}');
sed -i "s#$key=#&$value#g" dbca.rsp;
done
sed 的 / 换成 # 在原因是配置中的路径有 / 字符
执行静默安装数据库命令
dbca -silent -createDatabase -templateName General_Purpose.dbc -responseFile /u01/app/oracle/etc/dbca.rsp
三、配置开机启动
编辑 /etc/oratab
将
GDSTI:/u01/app/oracle/product/12.2.0/dbhome_1:N
修改为
GDSTI:/u01/app/oracle/product/12.2.0/dbhome_1:Y
sed -i '/dbhome_1/s/N/Y/g' /etc/oratab
编辑
/u01/app/oracle/product/12.2.0/dbhome_1/bin/dbstart
/u01/app/oracle/product/12.2.0/dbhome_1/bin/dbshut
文件,将
ORACLE_HOME_LISTNER=$1
修改为
ORACLE_HOME_LISTNER=$ORACLE_HOME
cd /u01/app/oracle/product/12.2.0/dbhome_1/bin/
sed -i 's/ORACLE_HOME_LISTNER=$1/ORACLE_HOME_LISTNER=$ORACLE_HOME/g' dbstart
sed -i 's/ORACLE_HOME_LISTNER=$1/ORACLE_HOME_LISTNER=$ORACLE_HOME/g' dbshut
数据库启动命令:dbstart
数据库关闭命令:dbshut
四、查询命令
4.1 查看监听状态
su - oracle
lsnrctl status
# 关闭监听 lsnrctl stop
# 启动监听 lsnrctl start
4.2 登陆查看实例
sqlplus / as sysdba
# 启动数据库:startup
# 关闭数据库:shutdown immediate
# 查看实例状态
SQL> select status from v$instance;
STATUS
------------
OPEN
# 查看数据库版本信息
SQL> set line 1000
SQL> select * from v$version;
BANNER CON_ID
-------------------------------------------------------------------------------- ----------
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production 0
PL/SQL Release 12.2.0.1.0 - Production 0
CORE 12.2.0.1.0 Production 0
TNS for Linux: Version 12.2.0.1.0 - Production 0
NLSRTL Version 12.2.0.1.0 - Production 0
五、附录
5.1 防火墙配置
#永久打开1521/TCP端口
firewall-cmd --permanent --add-port=1521/tcp
firewall-cmd --reload
#查看防火墙,添加的端口也可以看到
firewall-cmd --list-all
5.2 乱码问题
操作系统安装时的安装语言是简体中文
CRT会话语言是UTF8
oracle使用的字符集是AL32UTF8
shell 客户端登录乱码
[oracle@db01 bin]$ sqlplus / as sysdba;
SQL*Plus: Release 12.2.0.1.0 Production on Fri May 27 10:27:34 2021
Copyright (c) 1982, 2016, Oracle. All rights reserved.
???: # 这个地方乱码
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production
SQL>
解决方法:
export NLS_LANG="SIMPLIFIED CHINESE_CHINA.AL32UTF8"
echo 'export NLS_LANG="SIMPLIFIED CHINESE_CHINA.AL32UTF8"' >> /home/oracle/.bash_profile
再将登录
[oracle@db01 bin]$ sqlplus / as sysdba;
SQL*Plus: Release 12.2.0.1.0 Production on 星期五 5月 27 10:34:18 2021
Copyright (c) 1982, 2016, Oracle. All rights reserved.
连接到:
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production
SQL>
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




