Bootstrap

Linux云平台Oracle 12c安装与数据迁移

Linux云平台Oracle 12c安装与数据迁移

配置阿里云环境的操作系统参数

hosts 主机信息配置

echo "172.18.33.214 iZwz9f32ql5nimsjsa4verZ" >> /etc/hosts 
#这里的IP地址是云服务器的内外地址,可以使用ifconfig命令查看,不是你用ssh登录的公网IP。

配置英文环境变量

echo "export LANG=en_US" >> ~/.bash_profile 

创建 SWAP 交换页面空间

#使用下面命名查看服务器有没有swap空间,没有的需要新建
free -m   
#安装oracle数据库必须要有swap空间,不然在后面安装检查过程中回报错。
#使用dd命令,划分出swap空间,一般生产环境配置8G即可。
dd if=/dev/zero of=/home/swap bs=1M count=4096 
mkswap /home/swap 
swapon /home/swap 

#磁盘添加到fatab,开机自动挂载
vi /etc/fstab 
/home/swap swap swap default 0 0 

创建用户与目录

/usr/sbin/groupadd -g 60001 oinstall 
/usr/sbin/groupadd -g 60002 dba 
/usr/sbin/groupadd -g 60003 oper 
useradd -u 61001 -g oinstall -G dba,oper oracle 
id oracle 
passwd oracle 
#创建目录
mkdir /oracle 
mkdir /oradata 
mkdir -p /oracle/app/oracle/product/12.2.0/db_1 
mkdir -p /oracle/app/oraInventory 
chown -R oracle:oinstall /oracle 
chown -R oracle:oinstall /oradata 
chmod -R 775 /oracle 
chmod -R 775 /oradata 

安装相关 ORACLE 需要的依赖软件包

cd /etc/yum.repos.d

yum list |more

yum -y install autoconf 
yum -y install automake 
yum -y install binutils 
yum -y install binutils-devel 
yum -y install bison
yum -y install cpp 
yum -y install dos2unix 
yum -y install ftp 
yum -y install gcc 
yum -y install gcc-c++ 
yum -y install lrzsz 
yum -y install python-devel 
yum -y install compat-db* 
yum -y install compat-gcc-34 
yum -y install compat-gcc-34-c++ 
yum -y install compat-libcap1 
yum -y install compat-libstdc++-33 
yum -y install compat-libstdc++-33.i686 
yum -y install glibc-* 
yum -y install glibc-*.i686 
yum -y install libXpm-*.i686 
yum -y install libXp.so.6 
yum -y install libXt.so.6 
yum -y install libXtst.so.6 
yum -y install libXext 
yum -y install libXext.i686 
yum -y install libXtst 
yum -y install libXtst.i686 
yum -y install libX11 
yum -y install libX11.i686 
yum -y install libXau 
yum -y install libXau.i686 
yum -y install libxcb 
yum -y install libxcb.i686 
yum -y install libXi 
yum -y install libXi.i686 
yum -y install libgcc_s.so.1 
yum -y install libstdc++.i686 
yum -y install libstdc++-devel 
yum -y install libstdc++-devel.i686 
yum -y install libaio
yum -y install libaio.i686 
yum -y install libaio-devel 
yum -y install libaio-devel.i686 
yum -y install ksh 
yum -y install libXp 
yum -y install libaio-devel 
yum -y install numactl 
yum -y install numactl-devel 
yum -y install make -y 
yum -y install sysstat -y 
yum -y install unixODBC 
yum -y install unixODBC-devel 
yum -y install elfutils-libelf-devel-0.97 
yum -y install elfutils-libelf-devel 
yum -y install redhat-lsb-core 
yum -y install unzip 

修改操作系统相关参数

修改资源限制

vi /etc/security/limits.conf

#ORACLE SETTING 
oracle soft nproc 16384
oracle hard nproc 16384 
oracle soft nofile 16384
oracle hard nofile 65536 
oracle soft stack 10240 
oracle hard stack 32768
#oracle hard memlock 8192000 
#oracle soft memlock 8192000 --[锁页,8G就给6G,16G给12G]

–重新登录检查

ulimit -a

控制为用户分配的资源
echo "session required pam_limits.so" >> /etc/pam.d/login 
cat /etc/pam.d/login 
修改内核参数

vi /etc/sysctl.conf

#ORACLE SETTING 
fs.aio-max-nr = 1048576 
fs.file-max = 6815744 
kernel.shmmax = 1210612736 
kernel.shmall = 295560 
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 = 1048586 
kernel.panic_on_oops = 1 
#vm.nr_hugepages = 2500 
 
sysctl -p

–如果设置了 hugepages,还需要关闭透明页

vi /etc/rc.d/rc.local

if test -f /sys/kernel/mm/transparent_hugepage/enabled; then 
echo never > /sys/kernel/mm/transparent_hugepage/enabled 
fi 
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then 
echo never > /sys/kernel/mm/transparent_hugepage/defrag 
fi 

chmod +x /etc/rc.d/rc.local

配置安全

防火墙
systemctl status firewalld.service 
systemctl stop firewalld.service 
systemctl disable firewalld.service 
SELINUX(阿里云默认关闭)
vi /etc/selinux/config 
SELINUX=disabled 

setenforce 0 

设置 ORACLE 用户的环境变量

su - oracle

vi .bash_profile

PS1="[`whoami`@`hostname`:"'$PWD]$' 

export PS1
export TMP=/tmp 
export LANG=en_US 
export TMPDIR=$TMP 
export ORACLE_UNQNAME=hefeidb 
ORACLE_SID=hefeidb; export ORACLE_SID 
ORACLE_BASE=/oracle/app/oracle; export ORACLE_BASE 
ORACLE_HOME=$ORACLE_BASE/product/12.2.0/db_1; export ORACLE_HOME
ORACLE_TERM=xterm; export ORACLE_TERM 
NLS_DATE_FORMAT="yyyy-mm-dd HH24:MI:SS"; export NLS_DATE_FORMAT 
NLS_LANG=AMERICAN_AMERICA.ZHS16GBK;export NLS_LANG 
PATH=.:$PATH:$HOME/.local/bin:$HOME/bin:$ORACLE_HOME/bin; export PATH 
THREADS_FLAG=native; export THREADS_FLAG 
umask=022 
if [ $USER = "oracle" ]; then 
if [ $SHELL = "/bin/ksh" ]; then 
ulimit -p 16384 
ulimit -n 65536 
else 
ulimit -u 16384 -n 65536 
fi 
umask 022 
fi 

env |grep ORACLE

数据库软件安装与监听配置

安装数据库软件

su - oracle

cd /soft/database/

cp -r response /oracle/

[oracle@iZwz9f32ql5nimsjsa4verZ:/oracle/response]KaTeX parse error: Expected group after '^' at position 31: …rsp |grep -Ev "^̲#|^"

mv db_install.rsp db_install.rsp.bak

#vi db_install.rsp
oracle.install.responseFileVersion=/oracle/install/rspfmt_dbinstall_response_schema_v12.2.0
oracle.install.option=INSTALL_DB_SWONLY
UNIX_GROUP_NAME=oinstall
INVENTORY_LOCATION=/oracle/app/oraInventory
ORACLE_HOME=/oracle/app/oracle/product/12.2.0/db_1
ORACLE_BASE=/oracle/app/oracle
oracle.install.db.InstallEdition=EE
oracle.install.db.OSDBA_GROUP=dba
oracle.install.db.OSOPER_GROUP=oper
oracle.install.db.OSBACKUPDBA_GROUP=dba
oracle.install.db.OSDGDBA_GROUP=dba
oracle.install.db.OSKMDBA_GROUP=dba
oracle.install.db.OSRACDBA_GROUP=dba

#chmod 775 db_install.rsp
su - root 
vi /etc/oraInst.loc
inventory=/oracle/app/oraInventory
inst_group=oinstall
	
chown oracle:oinstall /etc/oraInst.loc

运行安装

su - oracle 
cd /soft/database 
./runInstaller -silent -force -noconfig -responseFile /oracle/response/db_install.rsp

#-silent 静默安装不做任何提示
#--force 不是空目录不给安装,加这个就是安装
#-noconfig 不运行监听配置
#-responseFile 响应文件,绝对路径

跑 sh 脚本 --用root用户执行

image-20241117215652076

配置监听

[oracle@iZwz9f32ql5nimsjsa4verZ:/oracle/response]$cat netca.rsp |grep -Ev "^#|^$" 
[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 /oracle/response/netca.rsp

lsnrctl status 查看监听,正常。

数据库的创建

静默创建 ORACLE 数据库

dbca 
[oracle@iZwz9f32ql5nimsjsa4verZ:/oracle/response]$cat dbca.rsp |grep -Ev "^#|^$"

responseFileVersion=/oracle/assistants/rspfmt_dbca_response_schema_v12.2.0 
gdbName= 
sid= 
databaseConfigType= 
RACOneNodeServiceName= 
policyManaged= 
createServerPool= 
serverPoolName= 
cardinality= 
force= 
pqPoolName= 
pqCardinality= 
createAsContainerDatabase= 
numberOfPDBs= 
pdbName= 
useLocalUndoForPDBs= 
pdbAdminPassword= 
nodelist= 
templateName= 
sysPassword= 
systemPassword= 
oracleHomeUserPassword= 
emConfiguration= 
emExpressPort=5500 
runCVUChecks= 
dbsnmpPassword= 
omsHost= 
omsPort= 
emUser= 
emPassword= 
dvConfiguration= 
dvUserName= 
dvUserPassword= 
dvAccountManagerName= 
dvAccountManagerPassword=
olsConfiguration= 
datafileJarLocation= 
datafileDestination= 
recoveryAreaDestination= 
storageType= 
diskGroupName= 
asmsnmpPassword= 
recoveryGroupName= 
characterSet= 
nationalCharacterSet= 
registerWithDirService= 
dirServiceUserName= 
dirServicePassword= 
walletPassword= 
listeners= 
variablesFile= 
variables= 
initParams= 
sampleSchema= 
memoryPercentage= 
databaseType= 
automaticMemoryManagement= 
totalMemory= 
方式01.通过响应文件创建
dbca -silent -createDatabase -responseFile /oracle/response/dbca.rsp 
方式02.通过手工指定参数创建
dbca -silent -createDatabase -templateName General_Purpose.dbc -gdbName hefeidb -sid hefeidb -databaseConfigType SI -responseFile NO_VALUE -sysPassword oracle -systemPassword oracle -characterSet ZHS16GBK -memoryPercentage 30 -emConfiguration LOCAL

image-20241117220056386

oracle@iZwz9f32ql5nimsjsa4verZ:/oracle/response]$dbca -help 
Usage: dbca [<flag>] [<command> <option>] 
Following are the possible flags: 
-help 
-contextId <RAC | SI> 
-ignorePreReqs - Ignore prerequisite checks for current operation. 
-ignorePrereqFailure - Ignore all prerequisites failures. 
-silent - This flag allows you to carry on configuration in silent mode. 
-<command> 
Following are the possible commands: 
-addInstance - Command to Add an instance to an admin managed cluster database. 
-configureDatabase - Command to Configure a database. 
-configurePluggableDatabase - Command to Configure a pluggable database. 
-createCloneTemplate - Command to Create a clone template from an existing database. 
-createDatabase - Command to Create a database. 
-createDuplicateDB - Command to Duplicate a database. 
-createPluggableDatabase - Command to Create a pluggable database. 
-createTemplateFromDB - Command to Create a template from an existing database. 
-deleteDatabase - Command to Delete a database. 
-deleteInstance - Command to Delete an instance from an admin managed cluster database. 
-deletePluggableDatabase - Command to Delete a pluggable database. 
-executePrereqs - Command to execute prerequisite checks. 
-generateScripts - Command to Generate scripts which can be used to create database. 
-unplugDatabase - Command to Unplug a pluggable database. 

数据库后期配置与启停

180 天密码过期问题

alter profile default limit PASSWORD_LIFE_TIME unlimited; 

关闭审计 (19c以后是自动关闭的)

show parameter audit;
alter system set audit_trail=none scope=spfile; 

相关参数修改

show parameter memory
如果设置了大内存页,要把这个memory_max_target关闭,单独设置sga和pga
show parameter sga;
show parameter pga;
alter system set sga_max_size=750M scope=spfile; 
alter system set sga_target=750M scope=spfile; 
alter system set pga_aggregate_target=200M scope=spfile; 
alter system set memory_target=0 scope=spfile; 
alter system set memory_max_target=0 scope=spfile; 
alter system set processes=1000 scope=spfile; 
alter system set open_cursors=1000 scope=spfile; 

EM 管理器

–默认访问:https://ip:5500/em、http://ip:5501/em

–打开 EM

exec dbms_xdb_config.sethttpsport(5500); 
exec dbms_xdb_config.sethttpport(5501); 

–关闭 EM

exec dbms_xdb_config.sethttpsport(0); 
exec dbms_xdb_config.sethttpport(0); 

–检查 EM

select dbms_xdb_config.gethttpsport from dual; 
select dbms_xdb_config.gethttpport from dual; 

启停数据库操作

#停止数据库 
su - oracle 
lsnrctl stop 
sqlplus "/as sysdba" 
shutdown immediate; 
exit 

#启动数据库

su - oracle 
sqlplus "/as sysdba" 
startup 
exit 
lsnrctl start 
#如果遇到这种情况,解决: 
SQL> startup 
ORA-00821: Specified value of sga_target 552M is too small, needs to be at least 736M 
ORA-01078: failure in processing system parameters 
pfile 解决。 
create pfile='/oracle/pfile.ora' from spfile;
vi /oralce/pfile.ora

create spfile from pfile='/oracle/pfile.ora';

数据库库迁移至云平台的过程

  • 思路:

    • 迁移的方法:toad+expdp/impdp,按用户导出。

    • 风险评估:磁盘空间的大小,字符集和表空间的大小,不要导入一半数据导入失败。

    • 临时表空间大小,尽量设置大点。

    • undo表空间尽量也设置大点。

  • 准备:

    • 目标库表空间创建。

    • 用户创建。

    • 关闭两个库的job和统计信息。

  • 验证

    • 导入的行数和对象的总数
    • 日志有没有什么报错信息。
  • 后期

    • 编译无效对象。
    • 数据库的序列是否有问题。
    • job、物化视图、还有dblink要检查好。
    • 统计信息收集一遍。
    • 测试业务。

用toad工具,导出创建表空间,创建数据文件的语句,到目标库执行

image-20241117220635414

image-20241117220643935

image-20241117220651635

image-20241117220658990

[oracle@VM-0-17-centos:/home/oracle]$vi create_tab.sql
[oracle@VM-0-17-centos:/home/oracle]$chomd 777 create_tab.sql
[oracle@VM-0-17-centos:/home/oracle]$nohup sqlplus "/as sysdba" @create_tab.sql & 

用impdp导入,表空间其实是自动建立的,但是考虑到表空间的路径问题,一定要手动先把表空间创建好。

源库导出

–【导出前,停止job】

select job,schema_user from dba_jobs; --查询有没有job
select sid,job from dba_jobs_running;  --查询有没有运行的job,有的话删掉
execute dbms_auto_task_admin.disable;  --如果是晚上的话,关闭统计信息 
create directory hefeibak_dir as '/soft'; 
grant read,write on directory hefeibak_dir to system; 
expdp system/oracle directory=hefeibak_dir dumpfile=expdp_u_hefei_scott.dmp logfile=expdp_u_hefei_scott.log schemas=hefei,scott
--parallel=2 cluster=N 
##【如果数据量大,parallel并行开多个,不能大于cpu核数,如果是集群的话,加cluster=】
execute dbms_auto_task_admin.enable; #开启统计信息

目标库导入

alter database datafile '/oracle/app/oracle/oradata/hefeidb/users01.dbf' resize 500M; --以前库的数据有一部分再users表空间里面,所以加大
execute dbms_auto_task_admin.disable;  --如果是晚上的话,关闭统计信息 
create directory hefeibak_dir as '/soft'; 
grant read,write on directory hefeibak_dir to system; 
impdp system/oracle directory=hefeibak_dir dumpfile=expdp_u_hefei_scott.dmp logfile=impdp_u_hefei_scott.log --parallel=2 
##建一个脚本放在后台运行导入,怕网络不稳定,中断:nohup ./impdp.sh $
#查看日志:tail -100f nohup.out

验证

–日志也没问题

–数据表的行数

select count(*) from hefei.hefei_MEMBER; 
--对象数量 --[两个数据库上面执行,比较下]
select object_type s_object_type,count(*) from dba_objects where owner='hefei' group by object_type ; 
select object_type t_object_type,count(*) from dba_objects where owner='SCOTT' group by object_type ; 

select * from dba_objects where status <> 'VALID' and owner='hefei'; 
select * from dba_objects where status <> 'VALID' and owner='SCOTT'; 
select count(*) from dba_objects where status <> 'VALID' and owner='hefei';  --无效对象数量,也要对上
select count(*) from dba_objects where status <> 'VALID' and owner='SCOTT';  --无效对象数量,也要对上
dblink 两个库一起查询;
select s.s_object_type, s.s_count, t.t_object_type, t.t_count 
from (select object_type s_object_type, count(*) s_count 
from dba_objects@db01 
where owner = 'hefei01' 
group by object_type) s, 
(select object_type t_object_type, count(*) t_count 
from dba_objects 
where owner = 'hefei01' 
group by object_type) t;
编译无效对象
\# su - oracle 
$ sqlplus / as sysdba 
SQL> @$ORACLE_HOME/rdbms/admin/utlrp.sql 
序列问题

如果有序列要处理 --在导入的语句里面加这个选项,排除序列

exclude=SEQUENCE 

可以用以下脚本来实现 : --创建序列

#tts_create_seq.sql 

script from the source database. Use this script to reset the proper starting value for 
sequences on the target database. 
cr_tts_create_seq.sql 
set heading off feedback off trimspool on escape off 
set long 1000 linesize 1000 pagesize 0 
col SEQDDL format A300 
--spool tts_create_seq.sql 
prompt /* ========================= */ 
prompt /* Drop and create sequences */ 
prompt /* ========================= */ 
select regexp_replace( 
dbms_metadata.get_ddl('SEQUENCE',sequence_name,sequence_owner), 
'^.*(CREATE SEQUENCE.*CYCLE).*$', 
'DROP SEQUENCE "'||sequence_owner||'"."'||sequence_name 
||'";'||chr(10)||'\1;') SEQDDL 
from dba_sequences 
where sequence_owner not in 
(select name 
from system.logstdby$skip_support
where action=0) 
; 
spool off 
#手工发起统计信息(按用户)    --表导进去最好收集统计信息,不然性能有问题
hefeidb01:/dmptempfs/statdir > more stat.sh 
date 
sqlplus -s /nolog <<EOS 
connect /as sysdba 
spool /tmp/stat.log 
exec dbms_stats.gather_schema_stats( - 
ownname => 'hefei', - 
options => 'GATHER AUTO', - 
estimate_percent => dbms_stats.auto_sample_size, - 
method_opt => 'for all columns size repeat', - 
degree => 8 - 
) 
/ 
spool off 
exit 
EOS 
date 

hefeidb01:/dmptempfs/statdir > nohup sh stat.sh & 
#手工发起统计信息(全库) 
stats.sql: 
	begin 
	dbms_stats.gather_database_stats; 
	end; 
	/
	
nohup sqlplus "/as sysdba" @ stats.sql & 
--启用数据库自动维护任务 
SQL> execute DBMS_AUTO_TASK_ADMIN.ENABLE; 

;