Oracle数据库学习笔记 - 创建、备份和恢复

Oracle数据库学习笔记 创建,备份和恢复

Oracle 版本基于11g

尽量不使用图形界面方式,操作适用于linux和windows

创建数据库

创建实例

sh 复制代码
# 步骤1:设置环境变量
export ORACLE_SID=myorcl
export ORACLE_HOME=/u01/app/oracle/product/19.0.0/dbhome_1
export PATH=$ORACLE_HOME/bin:$PATH
# 步骤2:创建初始化参数文件
# 创建pfile
cat > $ORACLE_HOME/dbs/initmyorcl.ora << EOF
db_name=myorcl
memory_target=2G
processes=300
sessions=472
db_block_size=8192
compatible=19.0.0
db_create_file_dest=/u01/app/oracle/oradata
db_recovery_file_dest=/u01/app/oracle/fast_recovery_area
db_recovery_file_dest_size=4G
undo_management=AUTO
control_files=(/u01/app/oracle/oradata/myorcl/control01.ctl,/u01/app/oracle/oradata/myorcl/control02.ctl)
EOF
# 步骤3:创建必要目录
mkdir -p /u01/app/oracle/oradata/myorcl
mkdir -p /u01/app/oracle/fast_recovery_area
mkdir -p /u01/app/oracle/admin/myorcl/adump
# 步骤4:启动实例并创建数据库
# 启动sqlplus
sqlplus / as sysdba

# 在sqlplus中执行
STARTUP NOMOUNT;

CREATE DATABASE myorcl
USER SYS IDENTIFIED BY oracle123
USER SYSTEM IDENTIFIED BY oracle123
LOGFILE GROUP 1 ('/u01/app/oracle/oradata/myorcl/redo01.log') SIZE 100M,
        GROUP 2 ('/u01/app/oracle/oradata/myorcl/redo02.log') SIZE 100M,
        GROUP 3 ('/u01/app/oracle/oradata/myorcl/redo03.log') SIZE 100M
MAXLOGFILES 3
MAXLOGMEMBERS 3
MAXDATAFILES 100
CHARACTER SET AL32UTF8
NATIONAL CHARACTER SET AL16UTF16
DATAFILE '/u01/app/oracle/oradata/myorcl/system01.dbf' SIZE 700M REUSE
SYSAUX DATAFILE '/u01/app/oracle/oradata/myorcl/sysaux01.dbf' SIZE 550M REUSE
DEFAULT TABLESPACE users
   DATAFILE '/u01/app/oracle/oradata/myorcl/users01.dbf' SIZE 500M REUSE AUTOEXTEND ON MAXSIZE UNLIMITED
DEFAULT TEMPORARY TABLESPACE tempts1
   TEMPFILE '/u01/app/oracle/oradata/myorcl/temp01.dbf' SIZE 20M REUSE
UNDO TABLESPACE undotbs1
   DATAFILE '/u01/app/oracle/oradata/myorcl/undotbs01.dbf' SIZE 200M REUSE AUTOEXTEND ON MAXSIZE UNLIMITED;
# 步骤5:创建数据字典
-- 在sqlplus中继续执行
@?/rdbms/admin/catalog.sql
@?/rdbms/admin/catproc.sql
@?/sqlplus/admin/pupbld.sql

windows下可以直接使用Database Configuration Assistant

创建Listener

直接编辑listener.ora

sql 复制代码
# listener.ora Network Configuration File: C:\app\Administrator\product\11.2.0\dbhome_1\network\admin\listener.ora
# Generated by Oracle configuration tools.

# 单个listener监听多个端口,所有实例都可通过任一端口访问
LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = WIN-S6D4H9HDL8B)(PORT = 1521))
      (ADDRESS = (PROTOCOL = TCP)(HOST = WIN-S6D4H9HDL8B)(PORT = 1522))
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
    )
  )

# 所有实例的静态注册(可通过1521或1522端口访问)
SID_LIST_LISTENER =
  (SID_LIST =
    (SID_DESC =
      (GLOBAL_DBNAME = hicon)
      (ORACLE_HOME = C:\app\Administrator\product\11.2.0\dbhome_1)
      (SID_NAME = hicon)
    )
    (SID_DESC =
      (GLOBAL_DBNAME = orcl)
      (ORACLE_HOME = C:\app\Administrator\product\11.2.0\dbhome_1)
      (SID_NAME = orcl)
    )
    (SID_DESC =
      (GLOBAL_DBNAME = orclh1)
      (ORACLE_HOME = C:\app\Administrator\product\11.2.0\dbhome_1)
      (SID_NAME = orclh1)
    )
  )

ADR_BASE_LISTENER = C:\app\Administrator

这是静态注册方式,直接告诉listener有多少个实例,SID_LIST项可以不存在,这时oracle会动态注册实例到listener,可能会由一定的延时才能连上

实例可以自主决定使用哪个listener

一般一个实例下设置3~5个Schemas为宜

创建Schema

  • 首先连接到具体实例
sql 复制代码
create user xxx  identified by "password";
grant dba to xxx;

备份恢复数据库

bash 复制代码
# 创建备份目录
mkdir D:\oracle_dump
# 登入现有实例
## 方式1
sqlplus system/123456@"(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(
HOST=WIN-S6D4H9HDL8B)(PORT=1521))(CONNECT_DATA=(SID=orclh1)))"
## 方式2
sqlplus system/password@WIN-7JA8JJ4UHP2:1522:orcl2 as sysdba 

## 配置oracle目录对象
CREATE DIRECTORY dump_dir AS 'D:\oracle_dump';
GRANT READ, WRITE ON DIRECTORY dump_dir TO system;
exit -- 退出sql模式
## 备份数据库 - 从orcl实例中导出
expdp system/123456@WIN-S6D4H9HDL8B:1521/orcl DIRECTORY=dump_dir DUMPFILE=smpA_export.dmp LOGFILE=smpA_export.log SCHEMAS=smpA

## 恢复数据库 - 将导出的数据库恢复到orclh1实例
impdp system/123456@WIN-S6D4H9HDL8B:1522/orclh1 DIRECTORY=dump_dir DUMPFILE=smpA_export.dmp LOGFILE=smpA_import.log SCHEMAS=smpA
相关推荐
天水幼麟21 分钟前
动手学深度学习-学习笔记【二】(基础知识)
笔记·深度学习·学习
焱焱枫25 分钟前
Oracle获取执行计划之10046 技术详解
数据库·oracle
沧海一笑-dj1 小时前
【51单片机】51单片机学习笔记-课程简介
笔记·学习·51单片机·江科大·江科大学习笔记·江科大单片机·江科大51单片机
老虎06271 小时前
JavaWeb(苍穹外卖)--学习笔记04(前端:HTML,CSS,JavaScript)
前端·javascript·css·笔记·学习·html
qq_392397122 小时前
Redis常用操作
数据库·redis·wpf
大苏打seven2 小时前
Docker学习笔记:Docker网络
笔记·学习·docker
一只fish4 小时前
MySQL 8.0 OCP 1Z0-908 题目解析(17)
数据库·mysql
Green1Leaves4 小时前
pytorch学习-9.多分类问题
人工智能·pytorch·学习
花好月圆春祺夏安4 小时前
基于odoo17的设计模式详解---装饰模式
数据库·python·设计模式
慕y2744 小时前
Java学习第十五部分——MyBatis
java·学习·mybatis