越来越多的公司要求程序员需要会创建部署Oracle环境,环境有了,那怎么创建实例呢?下面文章将讲述如何创建Oracle实例,简单好操作
提示:以下是本篇文章正文内容,亲测有效,可参考
服务器上已正确安装了Oracle环境,并且有权限操作Oracle用户!!!这是前提,若还没有安装Oracle环境,请移步Oracle环境部署文档(该文档还在编辑中)
安装实例的服务器需有5G以上空间,空间大小也是最容易导致错误的原因
切换到oracle用户,su - 用户名
[root@hadoop3 ~]$ su - oracle
创建实例的文件夹(mkdir),后续需要的文件都放到该文件夹下,便于后期的整合
[oracle@hadoop3 disk1]$ mkdir ora_csdn
创建参数文件(touch),文件名自定义,建议和实例名一致,便于记忆查找
[oracle@hadoop3 ora_csdn]$ touch ora_csdn.ora
修改参数文件内容,文件包括了实例的参数化配置,请仔细编辑ora参数文件的内容
[oracle@hadoop3 ora_csdn]$ vim ora_csdn.ora
db_name=ora_csdn
db_block_size=8192
control_files=(/mnt/disk1/ora_csdn/ora_csdn.ctl)
undo_tablespace=undotbs1
undo_management=auto
sga_max_size=1024M
sga_target=1024M
pga_aggregate_target=1024M
log_buffer=10485760
processes=300
open_cursors=2000
sessions=330
audit_trail ='db'
db_domain=''
remote_login_passwordfile='EXCLUSIVE'
compatible ='11.2.0.1.0'
参数解释:
db_name:数据库名称,需要改,自定义为所需实例名
db_block_size:数据库块大小,可不改
control_files:十分重要!!!控制文件,“/mnt/disk1/ora_csdn/ora_csdn.ctl”地址自定义,需规范命名xxx.ctl,建议和实例名一致。
sga_max_size:数据库SGA大小,可不改
compatible:Oracle版本号
其余的均可不改
★★★提示:Oracle版本号如何查看:用sqlplus命令就可以查看到当前Oracle的版本号
[oracle@hadoop3 ora_csdn]$ sqlplus / as sysdba
SQL*Plus: Release **11.2.0.1.0** Production on 星期二 4月 6 17:33:14 2021
Copyright (c) 1982, 2009, Oracle. All rights reserved.
Connected to an idle instance.
SQL>
建议和参数文件在同一目录下(便于整理)
[oracle@hadoop3 ora_csdn]$ mkdir redo
[oracle@hadoop3 ora_csdn]$ mkdir data
[oracle@hadoop3 ora_csdn]$ ll
drwxr-xr-x 2 oracle oinstall 4096 6 17:38 data
-rw-r--r-- 1 oracle oinstall 353 6 17:34 ora_csdn.ora
drwxr-xr-x 2 oracle oinstall 4096 6 17:38 redo
★★★必须执行export ORACLE_SID=实例名,告知服务器需要连接哪个数据库
[oracle@hadoop3 ora_csdn]$ export ORACLE_SID=ora_csdn
[oracle@hadoop3 ora_csdn]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.1.0 Production on 星期二 4月 6 17:42:13 2021
Copyright (c) 1982, 2009, Oracle. All rights reserved.
Connected to an idle instance.
SQL>
将pfile地址指向第一步创建的参数文件地址(.ora文件),不要打错哦
SQL> startup nomount pfile='/mnt/disk1/ora_csdn/ora_csdn.ora'
ORACLE instance started.
Total System Global Area 1068937216 bytes
Fixed Size 2220200 bytes
Variable Size 281022296 bytes
Database Buffers 771751936 bytes
Redo Buffers 13942784 bytes
SQL>
出现以上内容,表示Oracle实例已启动,但是目前只有一个壳,没有灵魂
如果有报错,可按照提示的错误信息去调整,基本上错误为参数文件内容书写的问题,需仔细检查
由于sqlplus的换行机制,命令行的sql会比较难看,请复制修改下方sql至命令行执行
CREATE DATABASE ora_csdn
USER SYS IDENTIFIED BY system
USER SYSTEM IDENTIFIED BY system
DATAFILE '/mnt/disk1/ora_csdn/data/system_01.dbf' SIZE 1024m autoextend on
SYSAUX DATAFILE '/mnt/disk1/ora_csdn/data/sysaux_01.dbf' SIZE 512m autoextend on
UNDO TABLESPACE undotbs1 DATAFILE '/mnt/disk1/ora_csdn/data/undotbs1_01.dbf' SIZE 1024m autoextend on
DEFAULT TEMPORARY TABLESPACE temp TEMPFILE
'/mnt/disk1/ora_csdn/data/temp_01.dbf' SIZE 1024m REUSE autoextend on
LOGFILE
GROUP 1 ('/mnt/disk1/ora_csdn/redo/redo11.log','/mnt/disk1/ora_csdn/redo/redo12.log') SIZE 50m,
GROUP 2 ('/mnt/disk1/ora_csdn/redo/redo21.log','/mnt/disk1/ora_csdn/redo/redo22.log') SIZE 50m,
GROUP 3 ('/mnt/disk1/ora_csdn/redo/redo31.log','/mnt/disk1/ora_csdn/redo/redo32.log') SIZE 50m
CHARACTER SET ZHS16GBK
NATIONAL CHARACTER SET UTF8;
参数解释:
create database xxx:xxx为数据库实例名称,需改
user sys identified by system:设置sys用户密码,可改
user system identified by system:设置system用户密码,可改
datafile:数据文件,需改为第2步创建的data目录下,dbf名称不改,只改路径
logfile:日志文件,需改为第2步创建的redo目录下,log名称不改,只改路径
★★★切记:整个复制粘贴至命令行 执行,不要分段★★★
SQL> CREATE DATABASE ora_csdn
USER SYS IDENTIFIED BY system
USER SYSTEM IDENTIFIED BY system
DATAFILE '/mnt/disk1/ora_csdn/data/system_01.dbf' SIZE 1024m autoextend on
SYSAUX DATAFILE '/mnt/disk1/ora_csdn/data/sysaux_01.dbf' SIZE 512m autoextend on
UNDO TABLESPACE undotbs1 DATAFILE '/mnt/disk1/ora_csdn/data/undotbs1_01.dbf' SIZE 1024m autoextend on
DEFAULT TEMPORARY TABLESPACE temp TEMPFILE
'/mnt/disk1/ora_csdn/data/temp_01.dbf' SIZE 1024m REUSE autoextend on
LOGFILE
GROUP 1 ('/mnt/disk1/ora_csdn/redo/redo11.log','/mnt/disk1/ora_csdn/redo/redo12.log') SIZE 50m,
2 GROUP 2 ('/mnt/disk1/ora_csdn/redo/redo21.log','/mnt/disk1/ora_csdn/redo/redo22.log') SIZE 50m,
3 4 5 6 7 8 9 10 11 12 GROUP 3 ('/mnt/disk1/ora_csdn/redo/redo31.log','/mnt/disk1/ora_csdn/redo/redo32.log') SIZE 50m
CHARACTER SET ZHS16GBK
NATIONAL CHARACTER SET UTF8; 13 14
回车,执行一段时间后,出现Database created.字样,代表数据库创建成功
SQL> CREATE DATABASE ora_csdn
USER SYS IDENTIFIED BY system
USER SYSTEM IDENTIFIED BY system
DATAFILE '/mnt/disk1/ora_csdn/data/system_01.dbf' SIZE 1024m autoextend on
SYSAUX DATAFILE '/mnt/disk1/ora_csdn/data/sysaux_01.dbf' SIZE 512m autoextend on
UNDO TABLESPACE undotbs1 DATAFILE '/mnt/disk1/ora_csdn/data/undotbs1_01.dbf' SIZE 1024m autoextend on
DEFAULT TEMPORARY TABLESPACE temp TEMPFILE
2 '/mnt/disk1/ora_csdn/data/temp_01.dbf' SIZE 1024m REUSE autoextend on
LOGFILE
3 4 5 6 7 8 9 10 GROUP 1 ('/mnt/disk1/ora_csdn/redo/redo11.log','/mnt/disk1/ora_csdn/redo/redo12.log') SIZE 50m,
GROUP 2 ('/mnt/disk1/ora_csdn/redo/redo21.log','/mnt/disk1/ora_csdn/redo/redo22.log') SIZE 50m,
GROUP 3 ('/mnt/disk1/ora_csdn/redo/redo31.log','/mnt/disk1/ora_csdn/redo/redo32.log') SIZE 50m
CHARACTER SET ZHS16GBK
NATIONAL CHARACTER SET UTF8; 11 12 13 14
Database created.
创建spfile文件,地址指向参数文件
SQL> create spfile from pfile='/mnt/disk1/ora_csdn/ora_csdn.ora';
File created.
SQL>
执行创建系统表和存储过程脚本文件(Oracle自带的),有两个命令
@?/rdbms/admin/catalog
@?/rdbms/admin/catproc
@:表示运行脚本文件
?:表示Oracle环境地址,环境变量中指定的ORACLE_HOME
SQL> @?/rdbms/admin/catalog
第一个命令执行需要大约10分钟,请耐心等待
执行第二个脚本命令
SQL> @?/rdbms/admin/catproc
★配置了动态监听,后续在启动实例时,就不需要在tns.ora和listener.ora中配置监听了。
★当数据库启动的时候,会自动将instance_name,service_name两个参数将实例和服务动态注册到listener中。
★因为这个监听是从数据库发起的,这个时候监听肯定是有对应的数据库的,所以显示的状态是READ,当数据库关闭后这个监听也就消失了。
没有动态监听:
Service "ora_csdn" has 1 instance(s).
Instance "ora_csdn", status UNKNOWN, has 1 handler(s) for this service...
配置了动态监听
Service "ora_csdn" has 1 instance(s).
Instance "ora_csdn", status READY, has 1 handler(s) for this service...
SQL> alter system set LOCAL_LISTENER='(ADDRESS=(PROTOCOL=TCP)(HOST=172.1.3.229)(PORT=1599))';
System altered.
SQL> alter system register;
System altered.
SQL>
参数解释:
HOST:服务器IP,需要改成当前登录的服务器IP
PORT:listener端口,需要改成当前Oracle监听的端口
如不知道IP和端口,请执行lsnrctl status命令查看,如下:
[oracle@hadoop3 ora_csdn]$ lsnrctl status
lsnrctl stop
lsnrctl start
[oracle@hadoop3 ora_csdn]$ lsnrctl stop
LSNRCTL for Linux: Version 11.2.0.1.0 - Production on 07-4月 -2021 09:45:44
Copyright (c) 1991, 2009, Oracle. All rights reserved.
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=hadoop3.sunyard.com)(PORT=1599)))
The command completed successfully
[oracle@hadoop3 ora_csdn]$ lsnrctl start
~~中间内容不可外泄,出现successfully字样即表示成功重启~~
The command completed successfully
[oracle@hadoop3 ora_csdn]$
export ORACLE_SID=ora_csdn
sqlplus / as sysdba
show parameter local_listener 先查看监听是否配置,应该显示的是3.5步配置的监听
shutdown immediate
startup
[oracle@hadoop3 ora_csdn]$ export ORACLE_SID=ora_csdn
[oracle@hadoop3 ora_csdn]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.1.0 Production on 星期三 4月 7 09:53:05 2021
Copyright (c) 1982, 2009, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> show parameter local_listener
NAME TYPE
------------------------------------ ---------------------------------
VALUE
------------------------------
local_listener string
(ADDRESS=(PROTOCOL=TCP)(HOST=IP)(PORT=PORT))
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup
ORACLE instance started.
Total System Global Area 1068937216 bytes
Fixed Size 2220200 bytes
Variable Size 281022296 bytes
Database Buffers 771751936 bytes
Redo Buffers 13942784 bytes
Database mounted.
Database opened.
SQL>
我们可以看到现在sqlplus进去数据库,已经不在是一开始的“Connected to an idle instance.”(3.1步骤的进入提示)
配置完之后,我们查看监听状态的时候,就能看到我们的监听已经动态配置了
Service "ora_csdn" has 1 instance(s).
Instance "ora_csdn", status READY, has 1 handler(s) for this service...
以上就是今天要讲的内容,本文介绍了如何用命令行在Linux服务器创建数据库实例,步骤不难,需要细心仔细,有疑问可以留言哦~
又是美好的一天,今天你摸鱼了吗~