Oracle啟動(dòng)、建立表空間、用戶、授權(quán)、數(shù)據(jù)庫(kù)導(dǎo)入導(dǎo)出使用教程

字號(hào):


    數(shù)據(jù)庫(kù)最基本的操作就是數(shù)據(jù)的導(dǎo)入導(dǎo)出及建立表空間、用戶、授權(quán)了,今天小編也為各位介紹一下關(guān)于Oracle啟動(dòng)、建立表空間、用戶、授權(quán)、數(shù)據(jù)庫(kù)導(dǎo)入導(dǎo)出的命令吧。
    **啟動(dòng)**
    1、啟動(dòng)數(shù)據(jù)庫(kù)實(shí)例,分為兩步:第一步,啟動(dòng)監(jiān)聽;第二步,啟動(dòng)數(shù)據(jù)庫(kù)實(shí)例。
    1.1進(jìn)入到sqlplus啟動(dòng)實(shí)例
    {{{
    [oracle@redhat ~]$ su - oracle --“切換到oracle用戶”
    [oracle@redhat ~]$ lsnrctl start --“打開監(jiān)聽”
    [oracle@redhat ~]$ sqlplus /nolog --“進(jìn)入到sqlplus”
    SQL> conn /as sysdba --“連接到sysdba”
    SQL> startup --“啟動(dòng)數(shù)據(jù)庫(kù)實(shí)例”
    SQL> shutdown immediate --“關(guān)閉數(shù)據(jù)庫(kù)實(shí)例”
    [oracle@redhat ~]$ lsnrctl stop --“關(guān)閉監(jiān)聽”
    }}}
    **常用的操作**
    {{{
    連接到其他用戶:SQL> conn ts/ts
    查看用戶下所有的表 SQL>select * from user_tables;
    查看有哪些數(shù)據(jù)庫(kù)實(shí)例: SQL>select instance_name from v$instance; 【查看有哪些實(shí)例】
    查看有哪些用戶: SQL> select username from dba_users; 【查看對(duì)應(yīng)的用戶】
    查看Oracle的版本: SQL> select * from v$version ;
    創(chuàng)建用戶 SQL> create user ts identified by ts; SQL> grant dba to ts;
    }}}
    **建立表空間、用戶、授權(quán)**
    {{{
    -- 查看dbf存放位置
    SQL> select * from dba_data_files; -- 查看文件位置 SELECT * FROM dba_directories;
    -- 新建表空間
    SQL> create tablespace bp_oracle logging datafile '/u02/oradata/devdb/bp_oracle.dbf' size 100m autoextend on next 50m maxsize 200m extent management local;
    -- 新建用戶
    SQL> create user bp_oracle identified by bp_oracle default tablespace bp_oracle;
    -- 用戶授權(quán)
    SQL> grant connect,resource to bp_oracle; -- cms系統(tǒng)中需要增加grant create view to bp_oracle視圖權(quán)限
    -- 用戶可以 訪問(wèn)dump_dir 方便進(jìn)行導(dǎo)入和導(dǎo)出操作
    SQL> grant read,write on directory dump_dir to bp_oracle; -- 如果沒(méi)有dump_dir可以建立
    SQL> create directory dump_dir as 'G:/oracle_dump_dir'; --查看目錄 select * from dba_directories;
    -- 數(shù)據(jù)庫(kù)導(dǎo)入1:正常情況
    SQL> impdp bp_oracle/bp_oracle directory=dump_dir dumpfile=bp_oracle20120209.dmp
    -- 數(shù)據(jù)庫(kù)導(dǎo)入2:映射情況
    SQL> impdp bp_oracle/bp_oracle directory=dump_dir dumpfile=ncp20120209.dmp remap_schema=ncp:bp_oracle remap_tablespace=ncp:bp_oracle
    -- 數(shù)據(jù)導(dǎo)出,可以帶版本
    SQL> expdp bp_oracle/bp_oracle DIRECTORY=dump_dir dumpfile=bp_oracle.dmp version=10.2.0.1.0
    -- 刪除表空間
    SQL> drop tablespace bp_oracle including CONTENTS and datafiles;
    -- 刪除用戶 ,執(zhí)行該語(yǔ)句請(qǐng)小心,會(huì)級(jí)聯(lián)刪除該用戶下所有對(duì)象。
    SQL> drop user bp_oracle cascade;
    --修改用戶密碼
    SQL> alter user bp_oracle identified by bp_oracle;
    }}}
    **expdp 導(dǎo)入導(dǎo)出**
    {{{
    -- 導(dǎo)出數(shù)據(jù)庫(kù)不帶版本
    SQL>expdp bp_oracle/bp_oracle schemas=bp_oracle DUMPFILE=bp_oracle20120221.dmp DIRECTORY=DUMP_DIR JOB_NAME=full
    -- 導(dǎo)出數(shù)據(jù)庫(kù) 帶版本
    SQL> expdp bp_oracle/bp_oracle schemas=bp_oracle DIRECTORY=dump_dir dumpfile=bp_oracle20120221.dmp version=10.2.0.1.0
    }}}
    **EXP、IMP導(dǎo)入導(dǎo)出 (常用的方式)**
    {{{
    -- 導(dǎo)出數(shù)據(jù) 指定表名數(shù)據(jù)
    SQL>exp nmswxt_mhwz/nmswxt_mhwz file=/home/oracle/dmp/nmswxt_mhwz_news_content.dmp tables=表1,表2,表3 //tables后面不要帶括號(hào),并且tables不要和owner一起用,會(huì)嘗試沖突,owner與tables不能同時(shí)指定。owner是指定要導(dǎo)出指定用戶的數(shù)據(jù),tables參數(shù)指定要導(dǎo)出的表
    -- 導(dǎo)入數(shù)據(jù),帶映射關(guān)系
    SQL>imp nmswxt_mhzz/nmswxt_mhzz file=/home/oracle/dmp/nmswxt_mhwz_news_content.dmp fromuser=nmswxt_mhwz touser=nmswxt_mhzz
    }}}