原文出处:https://zhuanlan.zhihu.com/p/670286778?
安装环境
java -version openjdk version "1.8.0_242" OpenJDK Runtime Environment (build 1.8.0_242-b08) OpenJDK 64-Bit Server VM (build 25.242-b08, mixed mode) cat /etc/centos-release CentOS Linux release 7.8.2003 (Core)
一:安装运行
1.官网下载Oracle:进入官网下载rpm包
下载下来会有两个rpm包
将包上传到服务器目录 /tmp
2.安装依赖包
大部分依赖包已经集成在之前我们下载的rpm包里面,但还有一些依赖包仍然需要我们安装
yum -y install libstdc++-devel compat-libstdc++-33 ksh glibc-devel libaio-devel compat-libcap1
yum -y install 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 gcc-c++
cd /tmp
#先安装预安装包
yum -y localinstall oracle-database-preinstall-19c-1.0-1.el7.x86_64.rpm
3.建立用户
创建用户oracle
[root@localhost app]# useradd oracle
[root@localhost app]# passwd oracle
更改用户 oracle 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
创建oinstall组和dba组
[root@localhost app]# groupadd oinstall
[root@localhost app]# groupadd dba
设置用户oracle的主组为oinstall,次级组为dba
[root@localhost app]# usermod oracle -g oinstall -G dba
分别检查一下设置是否正确
[root@localhost app]# cat /etc/passwd [root@localhost app]# cat /etc/group
3.配置环境变量
su – oracle vi ~/.bash_profile export ORACLE_BASE=/opt/oracle export ORACLE_HOME=/opt/oracle/product/19c/dbhome_1 export PATH=$ORACLE_HOME/bin:$PATH export ORACLE_SID=ORCLCDB #配置立即生效 source ~/.bash_profile
4.安装Oracle
将介质传输到/tmp下 进行安装
cd /tmp
yum -y localinstall oracle-database-ee-19c-1.0-1.x86_64.rpm
su – root cd /etc/init.d/ #查看配置有没有问题 vi oracledb_ORCLCDB-19c #没问题执行脚本 ./oracledb_ORCLCDB-19c configure
5.运行并修改编码为UTF8
[root@HM311~]# sqlplus /as sysdba SQL> startup SQL> shutdown immediate; SQL> startup mount; SQL> alter system enable restricted session; SQL> alter system set job_queue_processes=0; SQL> alter database open; SQL> alter database character set internal_use AL32UTF8; SQL> shutdown immediate; SQL> startup
或者修改为:ZHS16GBK
SQL> shutdown immediate; SQL> startup mount; SQL> alter system enable restricted session; System altered. SQL> alter database open; Database altered. SQL> ALTER DATABASE character set INTERNAL_USE ZHS16GBK; Database altered. SQL> alter system disable restricted session; System altered. SQL> select userenv('language') from dual; USERENV('LANGUAGE') ---------------------------------------------------- AMERICAN_AMERICA.ZHS16GBK ----------------------------------------------------
修改初始密码
alter user sys identified by 123456; alter user system identified by 123456;
PLSQL ora配置(ODBC)
Oracle19C= (DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.*.1)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SID = ORCLCDB) ) )
二,Oracle文件调优
默认安装Oracle系统的文件在系统目录里/opt/oracle/product/***,这里得把数据库的数据存储重新挂载的一个新盘或者一个新的目录
1. 表空间迁移-修改系统表空间位置
1.1 查看表空间信息
可以将以下文件位置迁移到一个专用存储盘,我这里使用的是私有云搭建的专用存储服务器,挂载进来了一块大容量的数据盘,挂载目录为【/data】
#查询数据文件位置: select name from v$datafile; #查询日志文件位置: select * from v$logfile; #查询控制文件位置: select name from v$controlfile; #查看临时文件位置: select * from v$tempfile;
1.2 关闭数据库
su oracle oracle> sqlplus /as sysdba SQL>shutdown immediate;
1.3 复制system表空间对应数据文件去新路径
#创建新目录 CDB 文件 mkdir -p /data/oracle/oradata/ORCLCDB
#将以下文件copy到新目录
#Copy日志文件: [root@oracleyw ORCLCDB]# cp /opt/oracle/oradata/ORCLCDB/redo01.log /data/oracle/oradata/ORCLCDB/ [root@oracleyw ORCLCDB]# cp /opt/oracle/oradata/ORCLCDB/redo02.log /data/oracle/oradata/ORCLCDB/ [root@oracleyw ORCLCDB]# cp /opt/oracle/oradata/ORCLCDB/redo03.log /data/oracle/oradata/ORCLCDB/
#Copy数据文件: [root@oracleyw ORCLCDB]# cp /opt/oracle/oradata/ORCLCDB/sysaux01.dbf /data/oracle/oradata/ORCLCDB/ [root@oracleyw ORCLCDB]# cp /opt/oracle/oradata/ORCLCDB/undotbs01.dbf /data/oracle/oradata/ORCLCDB/ [root@oracleyw ORCLCDB]# cp /opt/oracle/oradata/ORCLCDB/users01.dbf /data/oracle/oradata/ORCLCDB/
#创建新目录 PDB 文件
mkdir -p /data/oracle/oradata/ORCLPDB1
cp /opt/oracle/oradata/ORCLCDB/ORCLPDB1/system01.dbf /data/oracle/oradata/ORCLPDB1/ cp /opt/oracle/oradata/ORCLCDB/ORCLPDB1/sysaux01.dbf /data/oracle/oradata/ORCLPDB1/ cp /opt/oracle/oradata/ORCLCDB/ORCLPDB1/undotbs01.dbf /data/oracle/oradata/ORCLPDB1/ cp /opt/oracle/oradata/ORCLCDB/ORCLPDB1/users01.dbf /data/oracle/oradata/ORCLPDB1/
1.4 给新复制的文件修改为原文件所属用户和用户组
#授权oracle账号读写权限
[root@oracleyw ORCLCDB]# chown -R oracle:dba /data/oracle/oradata/ORCLCDB [root@oracleyw ORCLCDB]# chown -R oracle:dba /data/oracle/oradata/ORCLPDB1
1.5 以mount启动数据库
SQL> startup mount
1.6 修改system表空间对应数据文件去新路径
SQL>
alter database rename file'/opt/oracle/oradata/ORCLCDB/redo01.log','/opt/oracle/oradata/ORCLCDB/redo02.log','/opt/oracle/oradata/ORCLCDB/redo03.log' to '/data/oracle/oradata/ORCLCDB/redo01.log','/data/oracle/oradata/ORCLCDB/redo02.log','/data/oracle/oradata/ORCLCDB/redo03.log'; alter database rename file'/opt/oracle/oradata/ORCLCDB/system01.dbf' to '/data/oracle/oradata/ORCLCDB/system01.dbf'; alter database rename file'/opt/oracle/oradata/ORCLCDB/sysaux01.dbf' to '/data/oracle/oradata/ORCLCDB/sysaux01.dbf'; alter database rename file'/opt/oracle/oradata/ORCLCDB/undotbs01.dbf' to '/data/oracle/oradata/ORCLCDB/undotbs01.dbf'; alter database rename file'/opt/oracle/oradata/ORCLCDB/users01.dbf' to '/data/oracle/oradata/ORCLCDB/users01.dbf';
2.1设置归档日志模式
#创建存储归档文件的目录 mkdir - p /data/oracle/archive chown -R oracle:dba /data/oracle/archive SQL> alter system set log_archive_dest_1 = 'location=/data/oracle/archive' scope=spfile;
shutdown immediate
startup mount
alter database archivelog;
alter database open;
archive log list;
2.2设置闪回数据功能
要使用闪回询查功能,首先需要检查闪回查询功能是否已经开启:
select * from v$version;
select flashback_on from v$database;
开启闪回查询前先设置参数(也可以不设置使用默认参数)
show parameter db_recovery
alter system set db_recovery_file_dest_size=20g;
alter system set db_recovery_file_dest=\'D:\Oracle\app\Administrator\flashback\' scope=both;
alter system set undo_retention=21600 scope=both;
show parameter db_recovery
注意:必须先设置db_recovery_file_dest_size后设置db_recovery_file_dest;
开启闪回查询 flashback
select log_mode,open_mode,flashback_on from v$database;
sql #需要开启归档日志才能使用 show parameter db_recovery #创建闪回数据目录 mkdir -p /data/oracle/flashback chown -R oracle:dba /data/oracle/flashback/ alter system set db_recovery_file_dest_size=32g; alter system set db_recovery_file_dest='/data/oracle/flashback/' scope=spfile; alter system set undo_retention=21600 scope=spfile; show parameter db_recovery alter database flashback on; select a.FLASHBACK_ON,a.* from v$database a;
使用:
查询指定时间数据:
SELECT * FROM t_p_plan AS OF TIMESTAMP TO_TIMESTAMP('2019-09-06 12:30:00', 'yyyy-mm-dd hh24:mi:ss');
回滚数据到指定时间:
ALTER TABLE t_p_plan ENABLE ROW MOVEMENT;
FLASHBACK TABLE t_p_plan TO TIMESTAMP TO_TIMESTAMP('2019-09-06 12:30:00','yyyy-mm-dd hh24:mi:ss');
ALTER TABLE t_p_plan DISABLE ROW MOVEMENT;
2.3 设置归档日志最大值:
#查看归档日期允许最大值 select space_limit/1024/1024/1024 as USE_G ,a.*from v$recovery_file_dest a;
# 修改为100GB alter system set db_recovery_file_dest_size='107374182400';
2.4.启动数据库
SQL> alter database open;
确认修改完成
select TABLESPACE_NAME,FILE_NAME from dba_data_files;
#查看归档位置 archive log list;
归档日志使用情况查询
使用sys用户登录数据库使用如下语句可以查询归档日志的使用情况及使用率。
[oracle@ESBDatabase ~]$ sqlplus / as sysdba SQL*Plus: Release 12.1.0.2.0 Production on Thu Jun 18 14:29:25 2020 Copyright (c) 1982, 2014, Oracle. All rights reserved. Connected to: Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options # 查询归档文件位置、大小信息 SQL> select * from v$recovery_file_dest; NAME SPACE_LIMIT SPACE_USED SPACE_RECLAIMABLE NUMBER_OF_FILES CON_ID ------------------------------------- --------------- ---------------- ----------------------------- ------------------------- ---------- /u01/app/fast_recovery_area 2.1475E+10 8498622464 0 381 0 # 查询归档日志文件使用情况 SQL> select * from v$flash_recovery_area_usage; FILE_TYPE PERCENT_SPACE_USED PERCENT_SPACE_RECLAIMABLE NUMBER_OF_FILES CON_ID ------------------------------ ------------------ ------------------------- --------------- ---------- CONTROL FILE 0 0 0 0 REDO LOG 0 0 0 0 ARCHIVED LOG 39.88 0 384 0 BACKUP PIECE 0 0 0 0 IMAGE COPY 0 0 0 0 FLASHBACK LOG 0 0 0 0 FOREIGN ARCHIVED LOG 0 0 0 0 AUXILIARY DATAFILE COPY 0 0 0 0 8 rows selected.
归档日志扩容及清理
归档日志扩容
通过sys用户登录后使用如下语句进行容量设置:
SQL> alter system set db_recovery_file_dest_size=20G;
归档日志清理
使用RMAN方式登录,并执行如下语句进行日志清理:
# 检查所有归档日志文件 RMAN> crosscheck archivelog all;
# 删除失效的归档日志文件 RMAN> delete noprompt expired archivelog all;
# 删除七天前的归档日志文件 RMAN> delete noprompt archivelog all completed befored 'sysdate-7';
归档日志定时清理
[oracle@localhost ~]$ vim /home/oracle/del_arch.sh
#!/bin/bash echo "----------------------------------------`date`---------------------------------------" source ~/.bash_profile rman target / <<EOF crosscheck archivelog all; delete noprompt expired archivelog all; delete noprompt archivelog all completed before 'sysdate-7'; EOF echo -e "\n" echo "------------------------------------ FINISHED ------------------------------------"
3.临时文件位置转移:
alter database tempfile '/opt/oracle/oradata/ORCLCDB/temp01.dbf' drop;
alter tablespace temp add tempfile '/data/oracle/oradata/ORCLCDB/temp01.dbf' size 100M reuse;
#查看结果 select * from v$tempfile; #
4.控制文件迁移
create pfile from spfile; #关闭数据库 shutdown immediate;
#移动到新目录 注意:需要切换到另一窗口用root用户操作 mv /opt/oracle/oradata/ORCLCDB/control01.ctl /data/oracle/oradata/ORCLCDB/control01.ctl mv /opt/oracle/oradata/ORCLCDB/control02.ctl /data/oracle/oradata/ORCLCDB/control02.ctl
#回到sql窗口 重新启动 startup
#查看是否迁移成功 show parameter control;
select * from v$controlfile
5. 确认修改后的表空间文件存储位置
select file_name from sys.dba_data_files;
一,配置开机启动服务
1.先在oracle账号下查找Oracle的lsnrctl监听服务路径
1.进入oracle账号:su - oracle
2.查找监听服务路径:whereis lsnrctl
二,创建Oracle 指令shell脚本
1.启动数据库实例的shell脚本
启动Oracle数据库的脚本为/data/oracle/scripts/dbstart.sh
sqlplus / as sysdba <<EOF startup; EOF
2. 启数据库实例的shell脚本
重启Oracle数据库的脚本为/data/oracle/scripts/dbshut.sh
sqlplus / as sysdba <<EOF shutdown immediate; startup; EOF
3.关闭数据库实例的shell脚本
关闭Oracle数据库的脚本为/data/oracle/app/oracle/dbshut
sqlplus / as sysdba <<EOF shutdown immediate; EOF
4.建好之后给三个文件授权
chmod +x /data/oracle/scripts/dbstart.sh chmod +x /data/oracle/scripts/dbrestart.sh chmod +x /data/oracle/scripts/dbshut.sh
5.配置两个启动服务 【oracle.service】和【lsnrctl.service】
cd /usr/lib/systemd/system/
oracle.service
[Unit] Description=Oracle RDBMS After=network.target [Service] Type=simple ExecStart=/usr/bin/su - oracle -c "/data/oracle/scripts/dbstart.sh >> /tmp/oracle.log" ExecReload=/usr/bin/su - oracle -c "/data/oracle/scripts/dbrestart.sh >> /tmp/oracle.log" ExecStop=/usr/bin/su - oracle -c "/data/oracle/scripts/dbshut.sh \>\> /tmp/oracle.log" RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
lsnrctl.service
[Unit] Description=Oracle lsnrctl After=network.target [Service] Type=simple ExecStart=/usr/bin/su - oracle -c "/opt/oracle/product/19c/dbhome_1/bin/lsnrctl start >> /tmp/lsnrctl.log" ExecReload=/usr/bin/su - oracle -c "/opt/oracle/product/19c/dbhome_1/bin/lsnrctl reload >> /tmp/lsnrctl.log" ExecStop=/usr/bin/su - oracle -c "/opt/oracle/product/19c/dbhome_1/bin/lsnrctl stop >> /tmp/lsnrctl.log" RemainAfterExit=yes [Install] WantedBy=multi-user.target
刷新服务配置文件
systemctl daemon-reload
三,测试 启动/停止/重启
在root账号下测试oracle和lsnrctl服务
1.直接先执行脚本检查是否有问题:
/usr/bin/su - oracle -c "/opt/oracle/product/19c/dbhome_1/bin/lsnrctl status >> /tmp/lsnrctl.log" /usr/bin/su - oracle -c "/data/oracle/scripts/dbshut.sh \>\> /tmp/oracle.log"
2.命令测试
systemctl start oracle # 启动oracle服务。 systemctl restart oracle # 重启oracle服务。 systemctl stop oracle # 关闭oracle服务。 systemctl start lsnrctl # 启动lsnrctl服务。 systemctl restart lsnrctl # 重启lsnrctl服务。 systemctl stop lsnrctl # 关闭lsnrctl服务。
3.把oracle和lsnrctl服务设置为开机/关机自启动/停止
systemctl enable oracle # 把Oracle实例服务设置为开机自启动。 systemctl enable lsnrctl # 把Oracle监听服务设置为开机自启动
4.reboot
开机重启看看(在关机之前请先执行systemctl stop oracle,避免Oracle异常关闭导致开机启动异常)
另外一种方法:
1.查看环境变量ORACLE_HOME是否设置
$ echo $ORACLE_HOME /u01/app/oracle/product/11.2.0/dbhome_1
2.修改bdstart 文件
vim /u01/app/oracle/product/11.2.0/dbhome_1/bin/dbstart 编辑dbstart,将ORACLE_HOME_LISTER=$1修改成ORACLE_HOME_LISTER=$ORACLE_HOME (相当于ORACLE_HOME_LISTNER=/u01/app/oracle/product/12.1.0/dbhome_1)
3、使用oracle用户编辑器编辑文件/etc/oratab
将“orcl:/u01/app/oracle/product/12.1.0/dbhome_1:N”, 改为“orcl:/u01/app/oracle/product/12.1.0/dbhome_1:Y”。
4、使用root编辑/etc/rc.d/rc.local启动文件,添加数据库启动脚本dbstart
末尾追加: su oracle -lc "/u01/app/oracle/product/12.1.0/dbhome_1/bin/dbstart" su oracle -lc "/u01/app/oracle/product/12.1.0/dbhome_1/bin/lsnrctl start"
说明:/u01/app/oracle/product/12.1.0/dbhome1为oracle的安装目录,要根据实际情况进行修改。
5、为文件/etc/rc.d/rc.local添加执行权限:
chmod +x /etc/rc.d/rc.local
备份方式使用RMAN(Recovery Manager),RMAN 是用于备份、还原和恢复 Oracle 数据库的工具。
RMAN 也是命令行工具,使用方式与导出工具/导入工具相同,不同的是EXP/EXPDP是导出逻辑备份,而 RMAN(rman backup)是物理备份。
一,RMAN方式备份
1.建立备份路径
mkdir /data/oracle/backup
#给oracle账号设置目录权限 chown -R oracle:dba /data/oracle/backup
2.(可选)CentOS访问Windows共享文件夹的方法
这里你可以将备份路径设置指定的存储位置,我使用另外一台备份服务器,所以这里给Centos挂载一个目录
mount -t cifs -o username="oracle",password="yourpwd",uid=oracle,gid=dba //192.168.0.0/Oracleyyback /data/oracle/backup
#删除挂载用命令: umount /myShare
#开机自动挂载 vi /etc/fstab //192.168.0.0/Oracleyyback /data/oracle/backup cifs defaults,username=oracle,password=yourpwd,uid=oracle 0 2
二,配置备份脚本
1.全量备份 备份所有schema,并删除最近7天的备份 , level0_backup.rman
run { backup incremental level 0 database format '/data/oracle/backup/rman/%T/backup_full_%d_%T_%s.bak'; delete noprompt archivelog all completed before 'sysdate-7'; } exit
2.增量备份 , 备份上一次备份后的时间节点开始到当前时间的差异数据 , level1_backup.rman
run { backup incremental level 1 database format '/data/oracle/backup/rman/%T/backup_full_%d_%T_%s.bak'; delete noprompt archivelog all completed before 'sysdate-7'; } exit
backup incremental level 说明:
backup incremental level 0 database;----0级增量备份,作为增量备份策略的基础
backup incremental level 1 cumulative database;----1级累积增量备份
backup incremental level 1 database;----1级差异增量备份
3.创建shell脚本 ,shell脚本用于定时执行rman脚本 ,level0_backup.sh
#!/bin/sh #export LANG=en_US source /home/oracle/.bash_profile mkdir -p /home/oracle/backup/rman/`date +%Y%m%d` rman target / cmdfile=/home/oracle/scripts/level0_backup.rman msglog=/home/oracle/backup/rman/`date +%Y%m%d`/`date +%Y%m%d_%H%M_0.log` exit
level1_backup.sh
#!/bin/sh #export LANG=en_US source /home/oracle/.bash_profile mkdir -p /home/oracle/backup/rman/`date +%Y%m%d` rman target / cmdfile=/home/oracle/scripts/level1_backup.rman msglog=/home/oracle/backup/rman/`date +%Y%m%d`/`date +%Y%m%d_%H%M_1.log` exit
如果开启了归档日志,记得定时清理 , 清理归档日志 , cleanup_archivelogs.sh
#!/bin/bash source /home/oracle/.bash_profile export ORACLE_HOME=/opt/oracle/product/19c/dbhome_1 export ORACLE_SID=ORCLCDB rman target / <<EOF delete force noprompt archivelog all completed before 'sysdate-1'; exit EOF
sysdate-1 :清理一天之前的归档日志
标签:opt,Centos,19.3,ORCLCDB,oracle,oradata,Oracle,data,alter From: https://www.cnblogs.com/chengxuyonghu/p/18096202