Vagrant 快速生成Oracle 21c 数据库实验环境

笔记本空间有些紧张,原因是做实验让数据库虚机变大,以及有时不正常关机导致遗留虚机快照。今天重新生成了Oracle 21c数据库实验环境。

安装过程

  1. 摧毁现有的虚机。
vagrant destroy
  1. 清理VirtualBox中虚机的目录
    正常的话,虚机目录在上一步就会删除掉。但由于我之前遗留了虚机快照,因此需要手工删除目录。
Snapshot:2023-02-07T09-21-27-134096000Z.sav 429MB
  1. 下载相应版本的数据库软件到相应的vagrant项目目录。
    本例为vagrant-projects\OracleDatabase\21.3.0,数据库软件为LINUX.X64_213000_db_home.zip,大小3GB。
  2. 记录当前磁盘空间:64.8 GB可用,其中包括下载的数据库软件。
  3. 启动安装。整个过程耗时18分钟。
time vagrant up

以下是头和尾的安装日志:

Bringing machine 'oracle-21c-vagrant' up with 'virtualbox' provider...
==> oracle-21c-vagrant: Importing base box 'oraclelinux/8'...
==> oracle-21c-vagrant: Matching MAC address for NAT networking...
==> oracle-21c-vagrant: Checking if box 'oraclelinux/8' version '8.7.411' is up to date...
==> oracle-21c-vagrant: Setting the name of the VM: oracle-21c-vagrant
==> oracle-21c-vagrant: Clearing any previously set network interfaces...
==> oracle-21c-vagrant: Preparing network interfaces based on configuration...
    oracle-21c-vagrant: Adapter 1: nat
==> oracle-21c-vagrant: Forwarding ports...
    oracle-21c-vagrant: 1521 (guest) => 1521 (host) (adapter 1)
    oracle-21c-vagrant: 5500 (guest) => 5500 (host) (adapter 1)
    oracle-21c-vagrant: 22 (guest) => 2222 (host) (adapter 1)
==> oracle-21c-vagrant: Running 'pre-boot' VM customizations...
==> oracle-21c-vagrant: Booting VM...
==> oracle-21c-vagrant: Waiting for machine to boot. This may take a few minutes...
    oracle-21c-vagrant: SSH address: 127.0.0.1:2222
    oracle-21c-vagrant: SSH username: vagrant
    oracle-21c-vagrant: SSH auth method: private key
    oracle-21c-vagrant:
    oracle-21c-vagrant: Vagrant insecure key detected. Vagrant will automatically replace
    oracle-21c-vagrant: this with a newly generated keypair for better security.
    oracle-21c-vagrant:
    oracle-21c-vagrant: Inserting generated public key within guest...
    oracle-21c-vagrant: Removing insecure key from the guest if it's present...
    oracle-21c-vagrant: Key inserted! Disconnecting and reconnecting using new SSH key...
==> oracle-21c-vagrant: Machine booted and ready!
==> oracle-21c-vagrant: Checking for guest additions in VM...
==> oracle-21c-vagrant: Setting hostname...
==> oracle-21c-vagrant: Mounting shared folders...
    oracle-21c-vagrant: /vagrant => C:/Users/XiaoYu/Desktop/WORKSPACE/vagrant-projects/OracleDatabase/21.3.0
==> oracle-21c-vagrant: Running provisioner: shell...
    oracle-21c-vagrant: Running: C:/Users/XiaoYu/AppData/Local/Temp/vagrant-shell20230301-4796-6x5wqm.sh
    oracle-21c-vagrant: INSTALLER: Started up
	
...
...

    oracle-21c-vagrant: SQL> Disconnected from Oracle Database 21c Enterprise Edition Release 21.0.0.0.0 - Production
    oracle-21c-vagrant: Version 21.3.0.0.0
    oracle-21c-vagrant: INSTALLER: Database created
    oracle-21c-vagrant: INSTALLER: Oratab configured
    oracle-21c-vagrant: Created symlink /etc/systemd/system/multi-user.target.wants/oracle-rdbms.service → /etc/systemd/system/oracle-rdbms.service.
    oracle-21c-vagrant: INSTALLER: Created and enabled oracle-rdbms systemd's service
    oracle-21c-vagrant: INSTALLER: setPassword.sh file setup
    oracle-21c-vagrant: INSTALLER: Running user-defined post-setup scripts
    oracle-21c-vagrant: INSTALLER: Done running user-defined post-setup scripts
    oracle-21c-vagrant: ORACLE PASSWORD FOR SYS, SYSTEM AND PDBADMIN: 6j44mKUS4LA=1
    oracle-21c-vagrant: INSTALLER: Installation complete, database ready to use!

real    18m10.535s
user    0m0.015s
sys     0m0.031s

这是整个安装过程最耗时的部分,时间主要取决于网速(因为要yum update)和磁盘IO(因为要拷贝和解压数据库介质)。

  1. 记录剩余空间
    此时磁盘剩余49.5GB,删除数据库软件后,剩余52.4GB。
  2. 修改sys和system口令。
    安装日志的最后部分显示了随机设置的管理员口令,如果你当时没记录,还可以改:
cd ~oracle
./setPassword.sh Welcome1
  1. 安装git, oracle sample schema等软件。
  2. 禁止归档(默认),禁止recycle bin
ALTER SYSTEM SET recyclebin = OFF SCOPE = SPFILE; 
STARTUP FORCE;
  1. 计算安装所需空间。
    目前磁盘49.3 GB可用,与最初的64.8 GB相比较,整个安装消耗空间15.5GB:
$ bc <<< "64.8 - 49.3"
15.5
  1. 配置数据库环境
    以下为~/.bash_profile:
export SYSPWD='Welcome1'
export USERPWD='Welcome1'
export PDB=PDB1
alias sqlplus='rlwrap sqlplus'
alias sys='sqlplus sys/${SYSPWD}@$PDB as sysdba'
alias eoda='sqlplus eoda/foo@$PDB'
alias scott='sqlplus scott/tiger@$PDB'

export ORACLE_PATH=/home/oracle

以下为~/login.sql:

define _editor=vi
set serveroutput on size 1000000
set trimspool on
set long 5000
set linesize 100
set pagesize 9999
column plan_plus_exp format a80
set sqlprompt '&_user.@&_connect_identifier.> '

看下效果:

$ eoda

SQL*Plus: Release 21.0.0.0.0 - Production on Wed Mar 1 13:50:45 2023
Version 21.3.0.0.0

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

Last Successful login time: Wed Mar 01 2023 13:12:41 +08:00

Connected to:
Oracle Database 21c Enterprise Edition Release 21.0.0.0.0 - Production
Version 21.3.0.0.0

EODA@ORCLPDB1> connect / as sysdba
Connected.
SYS@ORCLCDB> exit
Disconnected from Oracle Database 21c Enterprise Edition Release 21.0.0.0.0 - Production
Version 21.3.0.0.0

可以看到新安装数据库的版本为21.3.0。

  1. 关闭数据库虚机
vagrant halt
  1. 开启数据库虚机
vagrant up

错误处理

开启数据库虚机时,我遇到了以下的错误:

Bringing machine 'oracle-21c-vagrant' up with 'virtualbox' provider...
==> oracle-21c-vagrant: Checking if box 'oraclelinux/8' version '8.7.411' is up to date...
==> oracle-21c-vagrant: Clearing any previously set forwarded ports...
==> oracle-21c-vagrant: Clearing any previously set network interfaces...
==> oracle-21c-vagrant: Preparing network interfaces based on configuration...
    oracle-21c-vagrant: Adapter 1: nat
==> oracle-21c-vagrant: Forwarding ports...
    oracle-21c-vagrant: 1521 (guest) => 1521 (host) (adapter 1)
    oracle-21c-vagrant: 5500 (guest) => 5500 (host) (adapter 1)
    oracle-21c-vagrant: 22 (guest) => 2222 (host) (adapter 1)
==> oracle-21c-vagrant: Running 'pre-boot' VM customizations...
==> oracle-21c-vagrant: Booting VM...
==> oracle-21c-vagrant: Waiting for machine to boot. This may take a few minutes...
    oracle-21c-vagrant: SSH address: 127.0.0.1:2222
    oracle-21c-vagrant: SSH username: vagrant
    oracle-21c-vagrant: SSH auth method: private key
==> oracle-21c-vagrant: Machine booted and ready!
==> oracle-21c-vagrant: Checking for guest additions in VM...
==> oracle-21c-vagrant: Setting hostname...
==> oracle-21c-vagrant: Mounting shared folders...
    oracle-21c-vagrant: /vagrant => C:/Users/XiaoYu/Desktop/WORKSPACE/vagrant-projects/OracleDatabase/21.3.0
Vagrant was unable to mount VirtualBox shared folders. This is usually
because the filesystem "vboxsf" is not available. This filesystem is
made available via the VirtualBox Guest Additions and kernel module.
Please verify that these guest additions are properly installed in the
guest. This is not a bug in Vagrant and is usually caused by a faulty
Vagrant box. For context, the command attempted was:

mount -t vboxsf -o uid=1000,gid=1000,_netdev vagrant /vagrant

The error output from the command was:

: No such device

其实也没有什么大碍,但是在虚机和主机之间传文件就不能通过共享目录了。

问题的原因是VirtualBox GuestAddition的版本不是最新,按照这篇文章修改就可以了。

Vagrant 常用命令

帮助如下:

$ vagrant --help
Usage: vagrant [options] <command> [<args>]

    -h, --help                       Print this help.

Common commands:
     autocomplete    manages autocomplete installation on host
     box             manages boxes: installation, removal, etc.
     cloud           manages everything related to Vagrant Cloud
     destroy         stops and deletes all traces of the vagrant machine
     global-status   outputs status Vagrant environments for this user
     halt            stops the vagrant machine
     help            shows the help for a subcommand
     init            initializes a new Vagrant environment by creating a Vagrantfile
     login
     package         packages a running vagrant environment into a box
     plugin          manages plugins: install, uninstall, update, etc.
     port            displays information about guest port mappings
     powershell      connects to machine via powershell remoting
     provision       provisions the vagrant machine
     push            deploys code in this environment to a configured destination
     putty
     rdp             connects to machine via RDP
     reload          restarts vagrant machine, loads new Vagrantfile configuration
     resume          resume a suspended vagrant machine
     serve           start Vagrant server
     snapshot        manages snapshots: saving, restoring, etc.
     ssh             connects to machine via SSH
     ssh-config      outputs OpenSSH valid configuration to connect to the machine
     status          outputs status of the vagrant machine
     suspend         suspends the machine
     up              starts and provisions the vagrant environment
     upload          upload to machine via communicator
     validate        validates the Vagrantfile
     version         prints current and latest Vagrant version
     winrm           executes commands on a machine via WinRM
     winrm-config    outputs WinRM configuration to connect to the machine

For help on any individual command run `vagrant COMMAND -h`

Additional subcommands are available, but are either more advanced
or not commonly used. To see all subcommands, run the command
`vagrant list-commands`.
        --[no-]color                 Enable or disable color output
        --machine-readable           Enable machine readable output
    -v, --version                    Display Vagrant version
        --debug                      Enable debug output
        --timestamp                  Enable timestamps on log output
        --debug-timestamp            Enable debug output with timestamps
        --no-tty                     Enable non-interactive output

常用的就几个:

## 启动虚机
vagrant up
## 登录虚机
vagrant putty 或 vagrant ssh
## 关闭虚机
vagrant halt
## 重启虚机
vagrant reload
## 查看虚机状态 
vagrant status
## 摧毁虚机,慎用
vagrant destroy

猜你喜欢

转载自blog.csdn.net/stevensxiao/article/details/129279421