通过Vagrant访问Oracle 18c数据库

版权声明:本文为Buddy Yuan原创文章,未经允许不得转载。原文地址:http://www.dboracle.com/archivers/通过Vagrant访问Oracle 18c数据库.html

在上一篇文章中,我写了使用VAGRANT 快速安装ORACLE DATABASE 18C。在这篇文章中,我们将学习如何使用Vagrant访问Oracle 18c数据库。首先我们需要检查VM的状态。使用上篇文章用到的Vagrant global-status命令。

PS G:\VM DATA\vagrant-boxes\OracleDatabase\18.3.0> vagrant global-status
id       name               provider   state   directory
----------------------------------------------------------------------------------------------
b1be299  oracle-18c-vagrant virtualbox running G:/VM DATA/vagrant-boxes/OracleDatabase/18.3.0

The above shows information about all known Vagrant environments
on this machine. This data is cached and may not be completely
up-to-date (use "vagrant global-status --prune" to prune invalid
entries). To interact with any of the machines, you can go to that
directory and run Vagrant, or you can use the ID directly with
Vagrant commands from any directory. For example:
"vagrant destroy 1a2b3c4d"

接下来我们可以打开Virtual Box虚拟机看一下状态。通过上一篇文章我们自动创建了一个叫oracle-18c-vagrant的虚拟机。

然后我们可以使用vagrant ssh命令登陆进主机。

PS G:\VM DATA\vagrant-boxes\OracleDatabase\18.3.0> vagrant ssh
Welcome to Oracle Linux Server release 7.5 (GNU/Linux 4.14.35-1818.0.9.el7uek.x86_64)
The Oracle Linux End-User License Agreement can be viewed here:
    * /usr/share/eula/eula.en_US
For additional packages, updates, documentation and community help, see:
    * http://yum.oracle.com/
[vagrant@oracle-18c-vagrant ~]$ sudo su - oracle
Last login: Mon Oct  1 23:20:44 +08 2018
[oracle@oracle-18c-vagrant ~]$

切换到Oracle用户之后,我们就可以执行一系列的命令来玩Oracle 18c了。

[oracle@oracle-18c-vagrant ~]$ ps -ef | grep pmon
oracle    8004     1  0 23:20 ?        00:00:00 ora_pmon_ORCLCDB
oracle    9081  8972  0 23:32 pts/0    00:00:00 grep --color=auto pmon
[oracle@oracle-18c-vagrant ~]$ ps -ef | grep tns
root        21     2  0 22:53 ?        00:00:00 [netns]
oracle    4240     1  0 23:05 ?        00:00:00 /opt/oracle/product/18c/dbhome_1/bin/tnslsnr LISTENER -inherit
oracle    9086  8972  0 23:32 pts/0    00:00:00 grep --color=auto tns
[oracle@oracle-18c-vagrant ~]$ sqlplus / as sysdba
SQL*Plus: Release 18.0.0.0.0 - Production on Mon Oct 1 23:32:26 2018
Version 18.3.0.0.0

Copyright (c) 1982, 2018, Oracle.  All rights reserved.

Connected to:
Oracle Database 18c Enterprise Edition Release 18.0.0.0.0 - Production
Version 18.3.0.0.0
SQL> select name,open_mode from v$database;

NAME      OPEN_MODE
--------- --------------------
ORCLCDB   READ WRITE

SQL> show pdbs

    CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       READ ONLY  NO
         3 ORCLPDB1                       READ WRITE NO
SQL> exit
Disconnected from Oracle Database 18c Enterprise Edition Release 18.0.0.0.0 - Production
Version 18.3.0.0.0

我们安装VM,默认在oracle用户的home目录下。有个setPassword.sh的脚本。这个脚本是给sys,system,pdb_admin设置密码的。

[oracle@oracle-18c-vagrant ~]$ ls -lrt
total 4
-rwxr-xr-x. 1 root root 784 Oct  1 23:21 setPassword.sh
[oracle@oracle-18c-vagrant ~]$ more setPassword.sh
#!/bin/bash
# LICENSE UPL 1.0
#
# Copyright (c) 1982-2018 Oracle and/or its affiliates. All rights reserved.
#
# Since: November, 2016
# Author: gerald.venzl@oracle.com
# Description: Sets the password for sys, system and pdb_admin
#
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
#

ORACLE_PWD=$1
ORACLE_SID="`grep $ORACLE_HOME /etc/oratab | cut -d: -f1`"
ORACLE_PDB="`ls -dl $ORACLE_BASE/oradata/$ORACLE_SID/*/ | grep -v pdbseed | awk '{print $9}' | cut -d/ -f6`"
ORAENV_ASK=NO
source oraenv

sqlplus / as sysdba << EOF
      ALTER USER SYS IDENTIFIED BY "$ORACLE_PWD";
      ALTER USER SYSTEM IDENTIFIED BY "$ORACLE_PWD";
      ALTER SESSION SET CONTAINER=$ORACLE_PDB;
      ALTER USER PDBADMIN IDENTIFIED BY "$ORACLE_PWD";
      exit;
EOF

要想从虚拟机中退出来,输入exit就行了。

[oracle@oracle-18c-vagrant ~]$ exit
logout
[vagrant@oracle-18c-vagrant ~]$ exit
logout
Connection to 127.0.0.1 closed.

输入vagrant halt就关闭虚拟机了,如果你要删除整个虚拟机,运行vagrant destroy命令。

PS G:\VM DATA\vagrant-boxes\OracleDatabase\18.3.0> vagrant halt
==> oracle-18c-vagrant: Attempting graceful shutdown of VM...


当然我们还能运行一些命令查看配置情况,例如vagrant port可以查看端口情况,vagrant ssh-config可以查看ssh配置信息。

PS G:\VM DATA\vagrant-boxes\OracleDatabase\18.3.0> vagrant port
The forwarded ports for the machine are listed below. Please note that
these values may differ from values configured in the Vagrantfile if the
provider supports automatic port collision detection and resolution.

    22 (guest) => 2222 (host)
  1521 (guest) => 1521 (host)
  5500 (guest) => 5500 (host)
PS G:\VM DATA\vagrant-boxes\OracleDatabase\18.3.0> vagrant ssh-config
Host oracle-18c-vagrant
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile "G:/VM DATA/vagrant-boxes/OracleDatabase/18.3.0/.vagrant/machines/oracle-18c-vagrant/virtualbox/private_key"
  IdentitiesOnly yes
  LogLevel FATAL

关注dbDao.com的新浪微博

扫码加入微信Oracle小密圈,了解Oracle最新技术下载分享资源

TEL/電話+86 13764045638
Email service@parnassusdata.com
QQ 47079569