本文使用VirtualBox,在最小化安装的Oracle Linux 7.3中,安装一个单实例的Oracle Database 12c Release 2。目的是为Oracle Database 12c的学习和研究提供一个快速可靠的实验环境。
本文会以图形界面的方式安装数据库,如果想通过命令行的方式进行安装的话,请参考本站的另一篇文章《Oracle Linux 7 下静默安装 Oracle 12c》。
由于是使用虚拟环境,所以对于虚拟网络来说,只需要保证主机可以访问VM实例即可,这里主要设置Host-Only网络。注意在macOS系统中,默认的Host-Only网卡名称为vboxnet0,在Windows系统中的名称为VirtualBox Host-Only Ethernet Adapter。
VirtualBox默认使用192.168.56.1为主机地址,本文改成192.168.78.1,并设置网络掩码为255.255.255.0。
VirtualBox默认会为Host-Only网卡启动DHCP服务,讲主机作为DHCP服务器,为各个虚拟机实例自动分配IP。但是,现在我们需要固定IP,所以选择不启用DHCP。
虚拟机实例名称为ora12c,选择操作系统类型为Linux,版本为Oracle (64-bit)。为保证数据库的性能,设置内存大小为4GB(主机一共16GB的物理内存)。
设置虚拟硬盘大小为32GB,虚拟硬盘文件类型为默认的VDI,选择默认动态分配的方式,用多少分配多少物理硬盘空间。点击创建。
在上一步创建好的虚拟机实例上点击右键,选择设置,可以看到虚拟机实例的各个设置选择。这一步我们选择存储,为该实例挂载系统安装镜像。选择光驱控制器,点击右侧的光盘符按钮,选择选择一个虚拟光盘文件,在弹出窗口中定位到系统安装镜像,本文使用的是V83439401.OL73.iso文件。(可能你下载下来的文件名称是V83439401.iso)
第一块网卡的连接方式设置为仅主机(Host-Only)网络。
启用第二块网卡,并设置连接方式为网络地址转换(NAT)。这里特殊说明一下,如果你不需要从虚拟机实例访问外部网络的话,可以不启用第二块网卡,略过这一步也可以。
为了从访问主机中下载的数据库安装文件,需要为虚拟机实例设置共享目录,这样虚拟机实例就可以通过这个目录与主机方便的进行文件共享。本文把数据库安装文件放在主机中的/Users/kevin/VMs/install这个目录下了。
启动虚拟机实例,等待安装界面显示,选择NETWORK & HOST NAME,Host name设置为ora12c.example.com。点击名称为enp0s3的网卡,选择Automatically connect to this network when it is available以自动启用系统中的第一块网卡。
说几句闲话,从Oracle Linux 7开始,网卡命名不再遵从原来的eth0, eth1, …, ethN,而是使用了一种可预测的命名方法:一致网络设备命名,systemd使用这种方法根据固件、拓扑以及位置信息分配固定名称,好处是即使添加或者删除硬件也不发送变化,而且可以毫无影响地替换破坏的硬件。缺点是名称比起之前的ethX难读。虽然通过修改uded规则可以改回原来的名称方式,但是本文不会这么做,也不推荐。
新的名称也是有一定意义的,具体请参考了解可预期网络接口设备名称。
IP地址为手动设置:192.168.78.122,Netmask为:24,Gateway不填。使用自己作为DNS服务器,DNS Servers为192.168.78.122,Search domains为example.com。
如果你想从虚拟主机访问外部网络的话,就自动启用第二块网卡(NAT),勾选Automatically connect to this network when it is available。否则,这一步可以省略不做。
NAT类型的网卡,IP地址只能选择自动获取,而且为了保留第一块网卡设置好的DNS信息,这里选择Automatic(DHCP) addresses only。
设置系统时间为Asia/Shanghai timezone,保持默认的最小化安装Minimal Install。不启用KDUMP。选择好虚拟磁盘。开始安装过程。
在安装的过程中设置root用户密码为oracle1234。
系统重启后,以root用户登录到系统中,通过下面的命令查看网卡配置是否生效:
[root@ora12c ~]# ip l
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
link/ether 08:00:27:a6:47:ea brd ff:ff:ff:ff:ff:ff
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
link/ether 08:00:27:f3:03:4e brd ff:ff:ff:ff:ff:ff
主要看enp0s3和enp0s8是否有UP状态。一般来说都没有问题,这时可以使用 putty 或者 其他终端通过ssh远程登录到虚拟机中了。
$ ssh root@192.168.78.122
先查看一下防火墙的状态,发现是Active: active (running)。
[root@ora12c ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2017-04-11 14:15:38 CST; 6min ago
Docs: man:firewalld(1)
Main PID: 644 (firewalld)
CGroup: /system.slice/firewalld.service
└─644 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid
Apr 11 14:15:37 ora12c.example.com systemd[1]: Starting firewalld - dynamic ....
Apr 11 14:15:38 ora12c.example.com systemd[1]: Started firewalld - dynamic f....
Hint: Some lines were ellipsized, use -l to show in full.
[root@ora12c ~]#
停止防火墙:
[root@ora12c ~]# systemctl stop firewalld
再查看一下状态,发现是Active: inactive (dead),说明防火墙已经停止运行。
[root@ora12c ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Tue 2017-04-11 14:24:11 CST; 8s ago
Docs: man:firewalld(1)
Process: 644 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, status=0/SUCCESS)
Main PID: 644 (code=exited, status=0/SUCCESS)
Apr 11 14:15:37 ora12c.example.com systemd[1]: Starting firewalld - dynamic ....
Apr 11 14:15:38 ora12c.example.com systemd[1]: Started firewalld - dynamic f....
Apr 11 14:24:10 ora12c.example.com systemd[1]: Stopping firewalld - dynamic ....
Apr 11 14:24:11 ora12c.example.com systemd[1]: Stopped firewalld - dynamic f....
Hint: Some lines were ellipsized, use -l to show in full.
[root@ora12c ~]#
禁止开机自动启动防火墙:
[root@ora12c ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
确认开机启动已经无效:
[root@ora12c ~]# systemctl is-enabled firewalld
disabled
[root@ora12c ~]#
修改/etc/selinux/config,将SELINUX从enforcing改为disabled。
打开虚拟机实例的窗口,在菜单中选择Devices->Optical Drives->Choose Disk Image...打开刚才的系统安装光盘镜像。
登录到虚拟机实例中,挂载光盘到/media/cdrom:
[root@ora12c ~]# mkdir /media/cdrom
[root@ora12c ~]# mount -r /dev/cdrom /media/cdrom
备份原来的Yum源,配置新的本地Yum源:
[root@ora12c ~]# mkdir /etc/yum.repos.d/orig.repo.d
[root@ora12c ~]# mv /etc/yum.repos.d/public-yum-ol7.repo /etc/yum.repos.d/orig.repo.d/
[root@ora12c ~]# cat <<EOF>/etc/yum.repos.d/local.repo
> [Local]
> name=Oracle Linux 7.3 x86_64
> baseurl=file:///media/cdrom
> gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY
> enabled=1
> EOF
[root@ora12c ~]#
测试新Yum源:
[root@ora12c ~]# yum repolist
Loaded plugins: ulninfo
Local | 3.6 kB 00:00
(1/2): Local/group_gz | 134 kB 00:00
(2/2): Local/primary_db | 4.5 MB 00:00
repo id repo name status
Local Oracle Linux 7.3 x86_64 4,737
repolist: 4,737
[root@ora12c ~]#
[root@ora12c ~]# yum install -y oracle-database-server-12cR2-preinstall
[root@ora12c ~]# yum install tigervnc-server.x86_64 xclock xterm lsof bind bzip2 gcc kernel-uek-devel
[root@ora12c ~]# yum install metacity liberation-mono-fonts
先卸载安装镜像文件:
[root@ora12c ~]# umount /media/cdrom/
然后打开虚拟机实例的窗口,在菜单中选择Devices->Optical Drives->Remove disk from virtual drive彻底弹出安装系统关平镜像。
接着,继续在菜单中选择Devices->Insert Guest Additions CD Image...
登录到虚拟机实例中,挂载Guest Additions光盘镜像:
[root@ora12c ~]# mount -r /dev/cdrom /media/cdrom
开始安装:
[root@ora12c ~]# sh /media/cdrom/VBoxLinuxAdditions.run
Verifying archive integrity... All good.
Uncompressing VirtualBox 5.1.16 Guest Additions for Linux...........
VirtualBox Guest Additions installer
Copying additional installer modules ...
Installing additional modules ...
vboxadd.sh: Building Guest Additions kernel modules.
vboxadd.sh: Starting the VirtualBox Guest Additions.
Could not find the X.Org or XFree86 Window System, skipping.
[root@ora12c ~]#
这里不再配置DNS服务器(bind),而简单的配置一个静态的hosts文件,确保主机名永远都能被解析到。
[root@ora12c ~]# vi /etc/hosts
[root@ora12c ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
192.168.78.122 ora12c.example.com
[root@ora12c ~]#
执行下面的命令,修改/etc/pam.d/login,启用 limits:
sed -i -e '/session required pam_selinux.so open/i\
session required \/lib64\/security\/pam_limits.so\
session required pam_limits.so' /etc/pam.d/login
修改后的文件如下:
[root@ora12c ~]# cat /etc/pam.d/login
#%PAM-1.0
auth [user_unknown=ignore success=ok ignore=ignore default=bad] pam_securetty.so
auth substack system-auth
auth include postlogin
account required pam_nologin.so
account include system-auth
password include system-auth
# pam_selinux.so close should be the first session rule
session required pam_selinux.so close
session required pam_loginuid.so
session optional pam_console.so
# pam_selinux.so open should only be followed by sessions to be executed in the user context
session required /lib64/security/pam_limits.so
session required pam_limits.so
session required pam_selinux.so open
session required pam_namespace.so
session optional pam_keyinit.so force revoke
session include system-auth
session include postlogin
-session optional pam_ck_connector.so
[root@ora12c ~]#
修改oracle用户的密码为oracle1234。
[root@ora12c ~]# passwd oracle
Changing password for user oracle.
New password: oracle1234
BAD PASSWORD: The password contains the user name in some form
Retype new password: oracle1234
passwd: all authentication tokens updated successfully.
[root@ora12c ~]#
创建安装目录,并设置权限:
[root@ora12c ~]# mkdir -p /u01/app
[root@ora12c ~]# chown oracle:oinstall /u01/app
[root@ora12c ~]# usermod -G oinstall,dba,vboxsf oracle
[root@ora12c ~]# id oracle
uid=54321(oracle) gid=54321(oinstall) groups=54321(oinstall),54322(dba),995(vboxsf)
[root@ora12c ~]#
重启系统。
[root@ora12c ~]# su - oracle
[oracle@ora12c ~]$ vncserver :1
You will require a password to access your desktops.
Password: oracle1234
Verify: oracle1234
xauth: file /home/oracle/.Xauthority does not exist
New 'ora12c.example.com:1 (oracle)' desktop is ora12c.example.com:1
Creating default startup script /home/oracle/.vnc/xstartup
Starting applications specified in /home/oracle/.vnc/xstartup
Log file is /home/oracle/.vnc/ora12c.example.com:1.log
编辑自动生成的VNC配置文件,/home/oracle/.vnc/xstartup,在里面加入一行metacity &,确保VNC服务启动时先启动窗口管理器。
[oracle@ora12c ~]$ vncserver -kill :1
[oracle@ora12c ~]$ cat ~/.vnc/xstartup
#!/bin/sh
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
metacity &
exec /etc/X11/xinit/xinitrc
[oracle@ora12c ~]$ vncserver :1
使用VNC客户端来,登录到图像界面。本文使用的客户端为VNC Viewer:
打开VNC客户端,输入192.168.78.122:1:
先从共享目录中把数据库的安装文件拷贝到oracle用户的主目录中,并解压缩。
[oracle@ora12c ~]$ mkdir install
[oracle@ora12c ~]$ cp /media/sf_install/linuxx64_12201_database.zip install/
[oracle@ora12c ~]$ cd install/
[oracle@ora12c install]$ unzip linuxx64_12201_database.zip
以root执行脚本:
安装完成
[oracle@ora12c ~]$ /u01/app/oracle/product/12.2.0/dbhome_1/bin/dbca
先不创建创建监听器:
先不注册到EM中
为了方便记忆,全部使用一个密码:oracle1234
保存响应文件后,开始创建数据库
创建监听器
[root@ora12c ~]# /u01/app/oracle/product/12.2.0/dbhome_1/bin/netca
输入全局数据库(Global database name)名称orcl.example.com:
输入用户名system,密码oracle1234进行测试:
至此,数据库安装完成。
以oracle用户登录,在/home/oracle/.bash_profile中加入如下配置内容:
# Settings for Oracle Database
export TMP=/tmp
export TMPDIR=$TMP
export ORACLE_HOSTNAME=ora12c.example.com
export ORACLE_UNQNAME=orcl
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/jlib:$ORACLE_HOME/rdbms/jlib
以root用户,修改/etc/oratab的最后一行,将N改成Y,保证数据库在系统重启之后自动启动。
orcl:/u01/app/oracle/product/12.2.0/dbhome_1:Y
"/etc/oratab" 23L, 788C
参考官方安装文档,创建自启动脚本/etc/init.d/dbora,输入如下内容:
#! /bin/sh -x
#
# chkconfig: 2345 80 05
# description: Oracle auto start-stop script.
#
# Set ORACLE_HOME to be equivalent to the $ORACLE_HOME
# from which you wish to execute dbstart and dbshut;
#
# Set ORA_OWNER to the user id of the owner of the
# Oracle database in ORACLE_HOME.
ORA_HOME=/u01/app/oracle/product/12.2.0/dbhome_1
ORA_OWNER=oracle
case "$1" in
'start')
# Start the Oracle databases:
# The following command assumes that the oracle login
# will not prompt the user for any values
# Remove "&" if you don't want startup as a background process.
su - $ORA_OWNER -c "$ORA_HOME/bin/dbstart $ORA_HOME" &
touch /var/lock/subsys/dbora
;;
'stop')
# Stop the Oracle databases:
# The following command assumes that the oracle login
# will not prompt the user for any values
su - $ORA_OWNER -c "$ORA_HOME/bin/dbshut $ORA_HOME" &
rm -f /var/lock/subsys/dbora
;;
esac
修改启动脚本的权限:
# chgrp dba dbora
# chmod 750 dbora
将启动脚本注册为自启动服务:
# chkconfig --add dbora
# chkconfig dbora on
PDB Pluggable Database是12c中扛鼎的一个新特性, 但是对于CDB中的PDB,默认启动CDB时不会将所有的PDB带起来,这样我们就需要手动alter pluggable database ALL OPEN。解决方法就是使用SYS用户创建如下触发器:
[oracle@ora12c ~]$ sqlplus / as sysdba
CREATE TRIGGER open_all_pdbs
AFTER STARTUP
ON DATABASE
BEGIN
EXECUTE IMMEDIATE 'alter pluggable database all open';
END open_all_pdbs;
/
登录到数据库中,保证XDB已经安装好,通过exec DBMS_XDB_CONFIG.setHTTPPort(5500);开启EM Express,并使用默认端口5500.
[oracle@ora12c ~]$ sqlplus system@orcl
SQL*Plus: Release 12.2.0.1.0 Production on Wed Apr 12 15:37:15 2017
Copyright (c) 1982, 2016, Oracle. All rights reserved.
Enter password:
Last Successful login time: Wed Apr 12 2017 15:08:54 +08:00
Connected to:
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production
SQL> show parameter dispatchers
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
dispatchers string (PROTOCOL=TCP) (SERVICE=orclXD
B)
max_dispatchers integer
SQL> exec DBMS_XDB_CONFIG.setHTTPPort(5500);
PL/SQL procedure successfully completed.
查看监听器状态:
[oracle@ora12c ~]$ lsnrctl status
LSNRCTL for Linux: Version 12.2.0.1.0 - Production on 12-APR-2017 15:39:53
Copyright (c) 1991, 2016, Oracle. All rights reserved.
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=ora12c.example.com)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for Linux: Version 12.2.0.1.0 - Production
Start Date 12-APR-2017 14:58:34
Uptime 0 days 0 hr. 41 min. 19 sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Parameter File /u01/app/oracle/product/12.2.0/dbhome_1/network/admin/listener.ora
Listener Log File /u01/app/oracle/diag/tnslsnr/ora12c/listener/alert/log.xml
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=ora12c.example.com)(PORT=1521)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=ora12c.example.com)(PORT=5500))(Presentation=HTTP)(Session=RAW))
Services Summary...
Service "4ce29b4f34122507e0537a4ea8c0bfe9.example.com" has 1 instance(s).
Instance "orcl", status READY, has 1 handler(s) for this service...
Service "orcl.example.com" has 1 instance(s).
Instance "orcl", status READY, has 1 handler(s) for this service...
Service "orclXDB.example.com" has 1 instance(s).
Instance "orcl", status READY, has 1 handler(s) for this service...
Service "orclpdb.example.com" has 1 instance(s).
Instance "orcl", status READY, has 1 handler(s) for this service...
The command completed successfully
[oracle@ora12c ~]$
所有服务已经启动完成,可以使用http://192.178.78.122:5500/em来管理数据库了。注意,登录的时候使用用户名sys,密码oracle1234,不指定容器名,并选择以sysdba身份登录。
确认当前的数据库是CDB:
SQL> select name,cdb,con_id from v$database;
NAME CDB CON_ID
--------- --- ----------
ORCL YES 0
查看,并设置CDB的EM Express的HTTPS访问端口为5501:
SQL> select dbms_xdb_config.gethttpsport() from dual;
DBMS_XDB_CONFIG.GETHTTPSPORT()
------------------------------
0
SQL> exec dbms_xdb_config.sethttpsport(5501);
PL/SQL procedure successfully completed.
SQL> select dbms_xdb_config.gethttpsport() from dual;
DBMS_XDB_CONFIG.GETHTTPSPORT()
------------------------------
5501
SQL>
现在,CDB的用户就可以通过https://192.178.78.122:5501/em来访问EM Express了。
先切换到PDB中:
SQL> col name for a20;
SQL> select con_id,name,open_mode from v$pdbs;
CON_ID NAME OPEN_MODE
---------- -------------------- ----------
2 PDB$SEED READ ONLY
3 PDB READ WRITE
SQL> alter session set container=pdb;
Session altered.
SQL>
查看,并设置PDB的EM Express的HTTPS访问端口为5502:
SQL> exec dbms_xdb_config.sethttpsport(5502);
PL/SQL procedure successfully completed.
SQL> select dbms_xdb_config.gethttpsport() from dual;
DBMS_XDB_CONFIG.GETHTTPSPORT()
------------------------------
5502
SQL>
现在,PDB的用户就可以通过https://192.178.78.122:5502/em来访问EM Express了。这时,从登陆界面上就看不到那个容器名的输入框了。
普通用户想要访问EM Express,还需要以下权限:
譬如,对于PDB中的一个普通用户test01,可以通过下面的方式赋予访问EM Express的权限:
SQL> grant EM_EXPRESS_ALL to test01;
Grant succeeded.
SQL>