Oracle11g RAC+DG搭建
项目环境准备
3.1虚拟机配置
- 版本选择
注意Linux操作系统。此次项目我选择的版本是Oracle Enterprise Linux 5.4
- 内存的设置
本人电脑物理内存8G,由于此次实验要开三台虚拟机,基于集群两台机需要的内存较大,故每台给2G,单实例做standby的给1.5G。
- 添加选择网卡类型
使用桥接方式容易引发IP冲突,所以我选择的是Host Only方式,避免IP冲突。
两个网卡使用分配:
NAT:作Public IP
Host Only:作Private IP
注意主机和虚拟机的防火墙要关闭,达到互信。
- 分配磁盘空间
在分配磁盘空间的时候不要立即分配,避免占用实际空间大小,可以分配给其他进程。这里我分配50G给根分区,选择将虚拟磁盘存储为单个文件,性能较好。
- 添加共享磁盘
添加3个共享磁盘,分别为asm1,asm2,asm3.(注意:共享磁盘不能建为本地磁盘),也就是说SCSI设置为1:n,而不是0:n。模式选择独立永久,立即分配磁盘,存储为单个文件方式。每个磁盘均给3G,用于存储数据库相关文件。
由上可知,做RAC需要一块本地硬盘,两块网卡,3个共享磁盘及OEL5.4的安装配置。
3.2 安装OEL5.4操作系统
- 这里不要全部选择,sdb,sdc,sdd三个作裸设备,格式sda即可。防止其他三个盘全部被系统占用。
- 主机名:rac1.example.com
eth0:192.168.23.100/255.255.255.0(public)
eth1:192.168.21.10/255.255.255.0(private)
gateway:192.168.23.1
- 禁用防火墙和SELinux
Oracle不推荐私有网络使用iptables.
- 安装VMTOOLS
让鼠标脱离虚拟机,便于虚拟机和主机的文件复制。
3.3配置操作系统
- 搭建yum仓库,安装所需要的包。
[root@rac1~]vi /etc/yum.repo.d/server
[server]
name=oel5.4
baseurl=file:///mnt/Server
gpgcheck=0
enable=1
[root@rac1~]yum install -y oracle-validate*
[root@rac1~]yum install -y *asm*
- 配置host文件
- 添加用户和组
[root@rac1 ~]# userdel -r grid
userdel: user grid does not exist
[root@rac1 ~]# userdel -r oracle
[root@rac1 ~]# groupdel oinstall
[root@rac1 ~]# groupdel dba
[root@rac1 ~]# groupdel asmadmin
groupdel: group asmadmin does not exist
[root@rac1 ~]# groupdel oper
groupdel: group oper does not exist
[root@rac1 ~]# groupdel oper
groupdel: group oper does not exist
[root@rac1 ~]# groupdel asmdba
groupdel: group asmdba does not exist
[root@rac1 ~]# groupdel asmoper
groupdel: group asmoper does not exist
[root@rac1 ~]# groupadd -g 1000 oinstall
[root@rac1 ~]# groupadd -g 1100 asmadmin
[root@rac1 ~]# groupadd -g 1200 dba
[root@rac1 ~]# groupadd -g 1201 oper
[root@rac1 ~]# groupadd -g 1300 asmdba
[root@rac1 ~]# groupadd -g 1301 asmoper
[root@rac1 ~]# useradd -u 1100 -g oinstall -G asmadmin,asmdba,asmoper grid
[root@rac1 ~]# useradd -u 1101 -g oinstall -G dba,oper,asmdba oracle
[root@rac1 ~]# passwd oracle
Changing password for user oracle.
New UNIX password:
BAD PASSWORD: it is based on a dictionary word
Retype new UNIX password:
passwd: all authentication tokens updated successfully.
[root@rac1 ~]# passwd grid
- 修改配置用户环境变量
grid用户
export TMP=/tmp
export TMPDIR=$TMP
export ORACLE_SID=+ASM1
export ORACLE_BASE=/u01/app/grid
export ORACLE_HOME=/u01/app/11.2.0/grid
export NLS_DATE_FORMAT="yyyy-mm-dd HH24:MI:SS"
export PATH=$ORACLE_HOME/bin:$PATH
if [ $USER = "oracle" ] || [ $USER = "grid" ]; then
if [ $SHELL = "/bin/ksh" ]; then
ulimit -p 16384
ulimit -n 65536
else
ulimit -u 16384 -n 65536
fi
umask 022
fi
Oracle 用户
export TMP=/tmp
export TMPDIR=$TMP
export ORACLE_SID=racdb1
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=/u01/app/oracle/product/11.2.0/db_1
export NLS_DATE_FORMAT="yyyy-mm-dd HH24:MI:SS"
export PATH=$ORACLE_HOME/bin:$PATH
if [ $USER = "oracle" ] || [ $USER = "grid" ]; then
if [ $SHELL = "/bin/ksh" ]; then
ulimit -p 16384
ulimit -n 65536
else
ulimit -u 16384 -n 65536
fi
umask 022
Fi
- 修改系统参数(用户限制)
[root@rac1 ~]# vi /etc/security/limits.conf
grid soft nproc 2047
grid hard nproc 16384
grid soft nofile 1024
grid hard nofile 65536
oracle soft nproc 2047
oracle hard nproc 16384
oracle soft nofile 1024
oracle hard nofile 65536
- 创建相关目录
[root@rac1 ~]# mkdir -p /u01/app/grid/11.2.0
[root@rac1 ~]# chown -R grid:oinstall /u01/
[root@rac1 ~]# mkdir -p /u01/app/oracle
[root@rac1 ~]# chown -R oracle:oinstall /u01/app/oracle
3.4创建第二个节点
- 修改虚拟机配置文件以创建共享磁盘
到E:\RAC\rac1目录下找到rac1.vmx文件,记事本打开,进行修改
- 创建第二台主机
关闭rac1,复制所有rac1 虚拟机文件,放置文件夹rac2 中。
打开rac2,选择“我已复制”。
1) 将rac2 的两块网卡删除,重新添加;或者重新给定mac 地址,并给予正确的IP。
eth0:192.168.23.200/255.255.255.0(public)
eth1:192.168.21.20/255.255.255.0(private)
gateway:192.168.23.1
注意:两台机器都要有默认网关。
2) 修改rac2 的主机名为rac2.example.com(/etc/sysconfig/network)
3) 修改rac2 中oracle 用户换进变量的ORACLE_SID 为racdb2,grid用户的ORACLE_SID为+ASM2。
4)再执行[root@rac1 ~]# hostname rac2.example.com
注意:配置完以后重启rac2,确保主机名生效。
3.5安装配置Grid软件
- 对磁盘进行分区
[root@rac1 ~]#fdisk /dev/sdb
[root@rac1 ~]#fdisk /dev/sdc
[root@rac1 ~]#fdisk /dev/sdd
‘n’----’p’----’1’----’w’
- 配置oracleasm(两个节点执行rac1,rac2)
/etc/init.d/oracleasm configure
Configuring the Oracle ASM library driver.
This will configure the on-boot properties of the Oracle ASM library
driver. The following questions will determine whether the driver is
loaded on boot and what permissions it will have. The current values
will be shown in brackets ('[]'). Hitting <ENTER> without typing an
answer will keep that current value. Ctrl-C will abort.
Default user to own the driver interface []: grid
Default group to own the driver interface []: oinstall
Start Oracle ASM library driver on boot (y/n) [n]: y
Scan for Oracle ASM disks on boot (y/n) [y]: y
Writing Oracle ASM library driver configuration: done
Initializing the Oracle ASMLib driver: [ OK ]
Scanning the system for Oracle ASMLib disks: [ OK ]
- 创建asm磁盘(rac1执行)
[root@rac1 ~]# oracleasm createdisk VOL1 /dev/sdb1
Writing disk header: done
Instantiating disk: done
[root@rac1 ~]# oracleasm createdisk VOL2 /dev/sdc1
Writing disk header: done
Instantiating disk: done
[root@rac1 ~]# oracleasm createdisk VOL3 /dev/sdd1
Writing disk header: done
Instantiating disk: done
[root@rac1 ~]# /etc/init.d/oracleasm listdisks
VOL1
VOL2
VOL3
- 扫描生成rac1创建的asm磁盘(rac2执行)
[root@rac2 ~]# oracleasm scandisks
Reloading disk partitions: done
Cleaning any stale ASM disks...
Scanning system for ASM disks...
[root@rac2 ~]# /etc/init.d/oracleasm listdisks
VOL1
VOL2
VOL3
- 两个节点停止ntp服务
/etc/init.d/ntpd stop
/etc/init.d/ntpd status
chkconfig ntpd off
rm -rf /etc/ntp.conf
- 安装Grid
拷贝grid压缩软件赋权解压后执行安装。(rac1执行)
[grid@rac1 grid]$ ./runInstaller
注意顺序(rac2,rac1)root用户
注意顺序,所有脚本都在root用户执行。
跑第二个脚本成功输出结果:
/u01/app/11.2.0/grid/root.sh
Changing permissions of /u01/app/oraInventory.
Adding read,write permissions for group.
Removing read,write,execute permissions for world.
Changing groupname of /u01/app/oraInventory to oinstall.
The execution of the script is complete.
You have mail in /var/spool/mail/root
[root@rac1 ~]# /u01/app/11.2.0/grid/root.sh
Performing root user operation for Oracle 11g
The following environment variables are set as:
ORACLE_OWNER= grid
ORACLE_HOME= /u01/app/11.2.0/grid
Enter the full pathname of the local bin directory: [/usr/local/bin]:
Copying dbhome to /usr/local/bin ...
Copying oraenv to /usr/local/bin ...
Copying coraenv to /usr/local/bin ...
..........................................................................................
...........................................................................................
.........................................................................................
## STATE File Universal Id File Name Disk group
-- ----- ----------------- --------- ---------
1. ONLINE 6f12fa9ffe274fc3bfa64e25d3e270de (/dev/oracleasm/disks/VOL1) [DATA]
Located 1 voting disk(s).
CRS-2672: Attempting to start 'ora.asm' on 'rac1'
CRS-2676: Start of 'ora.asm' on 'rac1' succeeded
CRS-2672: Attempting to start 'ora.DATA.dg' on 'rac1'
CRS-2676: Start of 'ora.DATA.dg' on 'rac1' succeeded
CRS-2672: Attempting to start 'ora.registry.acfs' on 'rac1'
CRS-2676: Start of 'ora.registry.acfs' on 'rac1' succeeded
Configure Oracle Grid Infrastructure for a Cluster ... succeeded
这是由于没有配置DNS服务导致的,可以忽略。
- 检测安装结果
3.6创建ASM磁盘组
[grid@rac1 ~]$ asmca
3.7安装数据库
3.7.1安装数据库软件
[oracle@rac1 database]$ ./runInstaller
注意先后顺序
成功输出结果
/u01/app/oracle/product/11.2.0/db_1/root.sh
Performing root user operation for Oracle 11g
The following environment variables are set as:
ORACLE_OWNER= oracle
ORACLE_HOME= /u01/app/oracle/product/11.2.0/db_1
Enter the full pathname of the local bin directory: [/usr/local/bin]:
The contents of "dbhome" have not changed. No need to overwrite.
The contents of "oraenv" have not changed. No need to overwrite.
The contents of "coraenv" have not changed. No need to overwrite.
Entries will be added to the /etc/oratab file as needed by
Database Configuration Assistant when a database is created
Finished running generic part of root script.
Now product-specific root actions will be performed.
Finished product-specific root actions.
You have new mail in /var/spool/mail/root
3.7.2 配置监听
3.7.3创建数据库
[oracle@rac1 admin]$ dbca
[grid@rac1 grid]$ srvctl config database -d racdb
Database unique name: racdb
Database name: racdb
Oracle home: /u01/app/oracle/product/11.2.0/db_1
Oracle user: oracle
Spfile: +DBDATA/racdb/spfileracdb.ora
Domain:
Start options: open
Stop options: immediate
Database role: PRIMARY
Management policy: AUTOMATIC
Server pools: racdb
Database instances: racdb1,racdb2
Disk Groups: DBDATA,DATA,RECOVER
Mount point paths:
Services:
Type: RAC
Database is administrator managed
[grid@rac1 grid]$
四、项目实现
4,配置RAC+Data Guard
4.1创建第三台机rac3
- 主机名:rac3.example.com
IP:192.168.23.155/255.255.255.0
Gateway:192.168.23.1
- 禁用防火墙和SELinux
- 安装VMTOOLS
让鼠标脱离虚拟机,便于虚拟机和主机的文件复制。
- 搭建yum仓库,安装所需要的包。
[root@rac1~]vi /etc/yum.repo.d/server
[server]
name=oel5.4
baseurl=file:///mnt/Server
gpgcheck=0
enable=1
[root@rac1~]yum install -y oracle-validate*
- 编辑/etc/hosts文件
- 用户及环境变量配置
- 安装数据库软件
4.2检查环境
- 归档模式
SYS@racdb1>select log_mode from v$database;
LOG_MODE
------------
ARCHIVELOG
- 参数设置
SYS@racdb1>alter database force logging;
4.3配置监听
- Tnsnames.ora文件配置,三台机
- Listener.ora(备库主机racdg)
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = racdg)
(ORACLE_HOME = /u01/app/oracle/product/11.2.0/db_1)
(SID_NAME = racdg)
)
)
LISTENER =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = rac3.example.com)(PORT = 1521))
)
ADR_BASE_LISTENER = /u01/app/oracle
4.4文件准备
- 口令文件准备
[oracle@rac1 dbs]$ scp orapwracdb1 rac3:$ORACLE_HOME/dbs/orapwracdg
- 参数文件准备
主库添加以下参数
#primary
DB_UNIQUE_NAME=racdb
LOG_ARCHIVE_CONFIG='DG_CONFIG=(racdb,racdg)'
LOG_ARCHIVE_DEST_1=
'LOCATION=+RECOVER
VALID_FOR=(ALL_LOGFILES,ALL_ROLES)
DB_UNIQUE_NAME=racdb'
LOG_ARCHIVE_DEST_2=
'SERVICE=racdg ASYNC
VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE)
DB_UNIQUE_NAME=racdg'
LOG_ARCHIVE_DEST_STATE_1=ENABLE
LOG_ARCHIVE_DEST_STATE_2=defer
LOG_ARCHIVE_FORMAT=%t_%s_%r.arc
#standby
FAL_SERVER=racdg
racdb1.fal_client=racdb1
racdb2.fal_client=racdb2
DB_FILE_NAME_CONVERT='/disk1/racdg/datafile','+DBDATA/racdb/datafile'
LOG_FILE_NAME_CONVERT=
'/disk2/racdg/logfile','+DBDATA/racdb/onlinelog'
STANDBY_FILE_MANAGEMENT=AUTO
备库参数
racdg.__db_cache_size=138412032
racdg.__java_pool_size=4194304
racdg.__large_pool_size=4194304
racdg.__pga_aggregate_target=209715200
racdg.__sga_target=314572800
racdg.__shared_io_pool_size=0
racdg.__shared_pool_size=159383552
racdg.__streams_pool_size=0
*.audit_file_dest='/u01/app/oracle/admin/racdg/adump'
*.audit_trail='db'
*.compatible='11.2.0.0.0'
*.control_files='/disk3/racdg/controlfile/control01.ctl'
*.db_block_size=8192
*.db_create_file_dest='/disk3/racdg/fast'
*.db_domain=''
*.db_name='racdb'
*.diagnostic_dest='/u01/app/oracle'
*.log_archive_format='%t_%s_%r.dbf'
*.memory_target=524288000
*.open_cursors=300
*.processes=150
*.remote_login_passwordfile='exclusive'
racdg.undo_tablespace='UNDOTBS1'
#primary
DB_UNIQUE_NAME=racdg
LOG_ARCHIVE_CONFIG='DG_CONFIG=(racdg,racdb)'
LOG_ARCHIVE_DEST_1=
'LOCATION=/disk4/racdg/archivelog
VALID_FOR=(ALL_LOGFILES,ALL_ROLES)
DB_UNIQUE_NAME=racdg'
LOG_ARCHIVE_DEST_2=
'SERVICE=racdb1 ASYNC
VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE)
DB_UNIQUE_NAME=racdb'
LOG_ARCHIVE_FORMAT=%t_%s_%r.arc
#standby
FAL_SERVER=racdb1,racdb2
fal_client='racdg
DB_FILE_NAME_CONVERT='+DBDATA/racdb/datafile','/disk1/racdg/datafile'
LOG_FILE_NAME_CONVERT=
'+DBDATA/racdb/onlinelog', '/disk2/racdg/logfile'
STANDBY_FILE_MANAGEMENT=AUTO
- 创建standby redo
SYS@racdb1>alter database add standby logfile thread 1 group 5 size 50m;
SYS@racdb1>>alter database add standby logfile thread 1 group 6 size 50m;
SYS@racdb1>alter database add standby logfile thread 1 group 7 size 50m;
SYS@racdb1>alter database add standby logfile thread 2 group 8 size 50m;
SYS@racdb1>alter database add standby logfile thread 2 group 9 size 50m;
SYS@racdb1>alter database add standby logfile thread 2 group 10 size 50m;
- 数据文件和控制文件(rman备份复制)
主库备份rman target
RMAN> backup format '/home/oracle/backup/bk_%u' current controlfile for standby;
RMAN>backup format '/home/oracle/backup/bkk_%u' database plus archivelog;
[oracle@rac1 backup]$ scp * rac3:/home/oracle/backup
备库端复制
[oracle@rac3 backup]$ rman target sys/oracle@racdb1 auxiliary /
RMAN> duplicate target database for standby nofilenamecheck;
4.5测试及应用
- 查看两库的日志传送情况和状态
- 测试表空间和表数据
SYS@racdb1>create tablespace test datafile '/disk5/racdb1/test.dbf' size 20m;
SYS@racdb1>create table testdat(id number,name varchar2(20)) tablespace test;
SYS@racdb1>alter system switch logfile;
SYS@racdg>alter database recover managed standby database disconnect from session;
SYS@racdg>desc testdat;
Name Null? Type
----------------------------------------- --------
ID NUMBER
NAME VARCHAR2(20)
SYS@racdb1>insert into testdat values(41,'Leader.Zhang');
SYS@racdb1>commit;
SYS@racdb1>alter system switch logfile;
SYS@racdg>select * from testdat;
ID NAME
---------- --------------------
41 Leader.Zhang
4.6切换
- 从主库切换到备库(主库执行)
SYS@racdb1>alter database commit to switchover to physical standby;
SYS@racdb1>shutdown immediate
SYS@racdb1>startup mount
SYS@racdb1>alter database recover managed standby database disconnect from session;
SYS@racdb1>select database_role,switchover_status from v$database;
DATABASE_ROLE SWITCHOVER_STATUS
---------------- --------------------
PHYSICAL STANDBY NOT ALLOWED
- 从备库切换到主库
SYS@racdg>alter database commit to switchover to primary;
SYS@racdg>shutdown immediate
SYS@racdg>startup
SYS@racdg>select database_role,switchover_status from v$database;
DATABASE_ROLE SWITCHOVER_STATUS
---------------- --------------------
PRIMARY RESOLVABLE GAP
SYS@racdg>alter system switch logfile;
SYS@racdg>select database_role,switchover_status from v$database;
DATABASE_ROLE SWITCHOVER_STATUS
---------------- --------------------
PRIMARY TO STANDBY
注意关机开机顺序:主库先关后起,备库后关先起。