1. 程式人生 > >Oracle管理

Oracle管理

一、客戶端連線伺服器
1.檢視伺服器監聽程式配置檔案(先不用改動)
[[email protected]/]#su - oracle
[[email protected]~]$ cd $ORACLE_HOME/network/admin
[[email protected]]$ vim listener.ora

2.檢視伺服器的例項名
SQL> select instance_name from v$instance;

INSTANCE_NAME

Orcl

3.設定客戶端配置檔案tnsnames.ora
ORCL = //ORCL即為連線識別符號
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = rhel1.benet.com)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SID = orcl)
)
4.連線例項
請輸入使用者名稱: sys/

[email protected] as sysdba
5.停止監聽器
[[email protected] admin]$lsnrctl stop listener
6.再連線

請輸入使用者名稱: sys/[email protected] as sysdba
ERROR:
ORA-12541: TNS: 無監聽程式

7、再建立一個監聽器,同時做靜態註冊,並進行連線
①.建立新的監聽器
LISTENER1 =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1522))
(ADDRESS = (PROTOCOL = TCP)(HOST = rhel1.benet.com)(PORT = 1522))
)
)
SID_LIST_LISTENER1 =
(SID_LIST =
(SID_DESC =
(SID_NAME = orcl)
(ORACLE_HOME = /opt/oracle/product/11.2/db_1)
(GLOBAL_DBNAME = orclabc)
)//所有()前面至少加一個空格
)
②.重新載入配置檔案(可以省略)
[

[email protected] admin]$lsnrctl reload
③.啟動listener1監聽器
The command completed successfully
[[email protected] admin]$lsnrctl start listener1
④.在客戶機上修改tnsnames.ora配置檔案
ORCL =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = rhel1.benet.com)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SID = orcl)
)
)
ORCL1 =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = rhel1.benet.com)(PORT = 1522))
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SID = orcl)
)
)
⑤.在客戶機上連線連線listener1監聽器
請輸入使用者名稱: sys/
[email protected]
as sysdba

連線到:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

二•管理控制檔案
1.獲得控制檔案資訊
SQL> select name from v$controlfile;

NAME

/u01/app/oracle/oradata/orcl/control01.ctl
/u01/app/oracle/oradata/orcl/control02.ctl

2.獲取控制檔案中包含的內容
SQL> select type,record_size,records_total,records_used from v$controlfile_record_section;
3.建立多路複用控制檔案
①先建立存放控制檔案的目錄,並更改屬主位oracle
[[email protected] ~]$su - root
[[email protected] ~]#mkdir -p /backup1/control
[[email protected] ~]#mkdir -p /backup2/control
[[email protected] ~]#chown -R oracle /backup1
[[email protected] ~]#chown -R oracle /backup2
②在資料庫仍然開啟時,修改spfile中的contro_files引數
[[email protected] ~]#su - oracle
[[email protected] ~]$sqlplus sys/123456 as sysdba
SQL> alter system set
2 control_files=
3 '/opt/oracle/oradata/orcl/control01.ctl',
4 '/backup1/control/control02.ctl',
5 '/backup2/control/control03.ctl' scope=spfile;
③關閉資料庫
SQL> shutdown immediate;
④使用作業系統命令將檔案複製到新的位置
SQL> quit;
[[email protected] ~]$ cp /opt/oracle/oradata/orcl/control01.ctl /backup1/control/control02.ctl
[[email protected] ~]$ cp /opt/oracle/oradata/orcl/control01.ctl /backup2/control/control03.ctl
⑤重新啟動資料庫
[[email protected] ~]$sqlplus sys/123456 as sysdba
SQL>startup
4.備份與恢復控制檔案
方法一:直接用現有的完好的控制檔案覆蓋損壞或丟失的控制檔案
①模擬故障,停止資料庫,刪除控制檔案
sql>shutdown immediat
sql>quit
$rm -f /badkup1/control/control02.ctl
②啟動資料庫
sql>startup 觀察現象
③恢復控制檔案
sql>shutdown abort;
sql>quit
$ cp /opt/oracle/oradata/orcl/control01.ctl /backup1/control/control02.ctl
④再次啟動資料庫
sql>startup
方法二:利用專用的備份資料庫語句
①建立備份
SQL> quit
[[email protected] ~]$su - root
[[email protected] ~]#mkdir /opt/oracle/oradata/orcl/backup
[[email protected] ~]#chown -R oracle /opt/oracle/oradata/orcl/backup/
SQL> alter database backup controlfile to '/opt/oracle/oradata/orcl/backup/control.bkp';
②模擬故障
SQL> shutdown immediate
SQL> quit
[[email protected] ~]$ rm backup1/control/control02.ctl
[[email protected] ~]$sqlplus sys/123456 as sysdba
SQL> startup
ORACLE instance started.

Total System Global Area 780824576 bytes
Fixed Size 2217424 bytes
Variable Size 490736176 bytes
Database Buffers 281018368 bytes
Redo Buffers 6852608 bytes
ORA-00205: error in identifying control file, check alert log for more info
③恢復控制檔案
 使用os命令複製備份檔案到原來的路徑,為了保持一致,將沒有丟失的控制檔案也恢復一份
[[email protected] ~]$cp/opt/oracle/oradata/orcl/backup/control.bkp /opt/oracle/oradata/orcl/control01.ctl
[[email protected] orcl]$ cp /opt/oracle/oradata/orcl/backup/control.bkp /backup1/control/control02.ctl
[[email protected] orcl]$ cp /opt/oracle/oradata/orcl/backup/control.bkp /backup2/control/control03.ctl
[[email protected] orcl]$sqlplus sys/123456 as sysdba
SQL> alter database mount;
 檢視當前活動的日誌檔案
SQL> startup mount;
SQL> select group#,sequence#,archived,status from v$log;
GROUP# SEQUENCE# ARCHIV STATUS


 1      4 NO     INACTIVE
 3      6 NO     CURRENT
 2      5 NO     INACTIVE

 利用控制檔案來恢復資料庫
SQL> select group#,status,type,member from v$logfile;
SQL> recover database using backup controlfile;
ORA-00279: change 1039911 generated at 05/30/2017 12:55:30 needed for thread 1
ORA-00289: suggestion :
/opt/oracle/flash_recovery_area/ORCL/archivelog/2017_05_30/o1_mf_16%u_.arc
ORA-00280: change 1039911 for thread 1 is in sequence #6 //根據提示在下面輸入當前的日誌檔案
/opt/oracle/oradata/orcl/redo03.log
 開啟資料庫
SQL> alter database open resetlogs;
resetlogs選項的意思是要開啟資料時,重置重做日誌,即將重做日誌的sequence置零

三.管理重做日誌檔案

  1. 使用v$log檢視重做日誌資訊
    [[email protected]~]$sqlplus / as sysdba
    SQL> select group#,sequence#,bytes,members,archived,status from v$log;

    GROUP# SEQUENCE# BYTES MEMBERS ARCHIVED


    STATUS

    1 4 209715200 1 NO
    CURRENT

    2 2 209715200 1 NO
    INACTIVE

    3 3 209715200 1 NO
    INACTIVE

2.使用v$logfile檢視重做日誌組資訊
SQL> select group#,status,type,member from v$logfile;

GROUP# STATUS        TYPE

MEMBER

 3           ONLINE

/u01/app/oracle/oradata/orcl/redo03.log

 2           ONLINE

/u01/app/oracle/oradata/orcl/redo02.log

 1           ONLINE

/u01/app/oracle/oradata/orcl/redo01.log

3.建立重做日誌組
SQL> quitbr/>[[email protected]~]$su
密碼:
[[email protected]]#mkdir -p /backup/orcl/log
[[email protected]]#chown -R oracle /backup
[[email protected]~]$sqlplus / as sysdba
SQL> alter database add logfile group 4
('/u01/app/oracle/oradata/orcl/redo04a.log',
'/backup/orcl/log/redo04b.log') size 10m;
資料庫已更改。

4.刪除重做日誌組4(只是刪除了日誌組,日誌檔案並沒有刪除)
①刪除之前先檢視下,然後再刪除
SQL> select group#,sequence#,bytes,members,archived,status from v$log;

GROUP#  SEQUENCE#      BYTES    MEMBERS ARCHIVED

STATUS

 1  4  209715200      1 NO

CURRENT

 2  2  209715200      1 NO

INACTIVE

 3  3  209715200      1 NO

INACTIVE

GROUP#  SEQUENCE#      BYTES    MEMBERS ARCHIVED

STATUS

 4      0   10485760      2 YES

UNUSED

SQL> alter database drop logfile group 4;

資料庫已更改。
說明:
① 當前的日誌組不能刪除,要刪除當前日誌組需要先對當前日誌組進行切換,使用命令為alter system switch logfile
② 活動的日誌組不可以刪除
③ 沒有歸檔的日誌組不可以刪除(前提是已經執行在歸檔模式)

5.新增/刪除重做日誌檔案,分別向日志組1和2新增一個日誌檔案
①新增重做日誌檔案
SQL> alter database add logfile member
2 '/backup/orcl/log/redo01a.log' to group 1,
3 '/backup/orcl/log/redo02b.log' to group 2;

資料庫已更改。

②刪除日誌檔案
SQL> alter database drop logfile member
'/backup/orcl/log/redo02b.log';

資料庫已更改。
③檢視日誌檔案
SQL> select group#,status,type,member from v$logfile;

GROUP# STATUS        TYPE

MEMBER

 3           ONLINE

/u01/app/oracle/oradata/orcl/redo03.log

 2           ONLINE

/u01/app/oracle/oradata/orcl/redo02.log

 1           ONLINE

/u01/app/oracle/oradata/orcl/redo01.log

GROUP# STATUS        TYPE

MEMBER

 1 INVALID       ONLINE

/backup/orcl/log/redo01a.log

說明:
 不能刪除當前組的成員,若要刪除則先執行強制性切換重做日誌的命令
 活動的日誌成員不可以刪除
 沒有歸檔的日誌檔案不能刪除(前提是已執行在歸檔模式下)
 當日志組只有一個成員

6.日誌切換和檢查點時間
①強制切換日誌檔案
SQL> alter system swith logfile;
②強制產生檢查點事件
SQL> alter system checkpoint;

四•管理歸檔日誌檔案

  1. 配置資料庫歸檔日誌
    ① 檢視資料庫歸檔模式,確定當前不處於歸檔模式
    SQL> archive log list;
    資料庫日誌模式 非存檔模式
    自動存檔 禁用
    存檔終點 /u01/app/oracle/product/12.2.0/dbhome_1/dbs/arch
    最早的聯機日誌序列 2
    當前日誌序列 4

② 關閉資料庫並啟動資料庫到mount狀態
SQL> shutdown immediate
資料庫已經關閉。
已經解除安裝資料庫。
ORACLE 例程已經關閉。
SQL> startup mount;
ORACLE 例程已經啟動。

Total System Global Area 1593835520 bytes
Fixed Size 8793256 bytes
Variable Size 1023411032 bytes
Database Buffers 553648128 bytes
Redo Buffers 7983104 bytes
資料庫裝載完畢。

③ 將資料庫設定為歸檔模式並檢視歸檔模式是否改變
SQL> alter database archivelog;

資料庫已更改。

SQL> archive log list;
資料庫日誌模式 存檔模式
自動存檔 啟用
存檔終點 /u01/app/oracle/product/12.2.0/dbhome_1/dbs/arch
最早的聯機日誌序列 2
下一個存檔日誌序列 4
當前日誌序列 4
SQL> alter database open ;

資料庫已更改。

④ 檢視歸檔日誌檔案的路徑
檢視當前有效的歸檔日誌檔案儲存目錄
SQL> selectdest_id,dest_name,status,destination from v$archive_dest ;
DEST_ID

DEST_NAME

STATUS

DESTINATION

 1

LOG_ARCHIVE_DEST_1
VALID
/u01/app/oracle/product/12.2.0/dbhome_1/dbs/arch

DEST_ID

DEST_NAME

STATUS

DESTINATION

 2

LOG_ARCHIVE_DEST_2
INACTIVE//省略部分資訊
SQL> select dest_id,name,archived from v$archived_log;
DEST_ID NAME ARCHIV
1/opt/oracle/flash_recovery_area/ORCL/archivelog/2016_11_19/o1_mf_1_8d2yh7kfx.arc YES
⑤ 改歸檔日誌檔案存放路徑
[[email protected]~]#mkdir /aa
[[email protected]~]#chown -R oracle /aa
[[email protected]~]#su – oracle
[[email protected]~]$sqlplus / as sysdba
SQL> alter system set log_archive_dest='/aa' scope=spfile;

實驗五:資料字典管理
1.建立一個班級表空間,空間大小為100m,資料檔案放在/data目錄下
[[email protected] ~]#mkdir /data
[[email protected] ~]#chown -R oracle /data
[[email protected] ~]#su - oracle
[[email protected] ~]$sqlplus / as sysdba
SQL> create tablespace t374
datafile '/data/t374.dbf' size 100m;
2.建立本人使用者,預設表空間為班級表空間
SQL> create user name
identified by 123456
default tablespace t374;

  1. 授予其連線資料庫和建立以及建立視圖表的許可權
    SQL> grant connect,resource,create view to name;
  2. 切換稱本人賬戶並建立表student,表裡包含name和passwd欄位
    SQL>SQL> conn name;
    SQL> create table student
    (name varchar(10),
    passwd varchar(20));
    5.建立檢視student_view
    SQL> create view student_view
    2 as
    3 select * from student;

6.查詢當前使用者有哪些表
SQL> desc user_tables;
SQL>select * from user_tables;
SQL> select table_name fromuser_tables;
TABLE_NAME

STUDENT
7.檢視當前使用者有哪些檢視
SQL> desc user_views;
SQL> select view_name fromuser_views;
VIEW_NAME

STUDENT_VIEW
8.查詢當前使用者有哪些資料庫物件
SQL> select object_name from user_objects;
OBJECT_NAME

STUDENT_VIEW
STUDENT
9.查詢當前使用者的資訊,包括使用者id,使用者狀態,預設表空間
①查看錶結構
SQL> desc user_users;
Name Null? Type


USERNAME NOT NULL VARCHAR2(30)
USER_ID NOT NULL NUMBER
ACCOUNT_STATUS NOT NULL VARCHAR2(32)
LOCK_DATE DATE
EXPIRY_DATE DATE
DEFAULT_TABLESPACE NOT NULL VARCHAR2(30)
TEMPORARY_TABLESPACE NOT NULL VARCHAR2(30)
CREATED NOT NULL DATE
INITIAL_RSRC_CONSUMER_GROUP VARCHAR2(30)
EXTERNAL_NAME
②格式化輸出
SQL> col user_id for 999
SQL> col account_status for a10
SQL> col default_tablespace for a30
③查詢
SQL> select user_id,account_status,default_tablespace from user_users;

USER_ID ACCOUNT_ST DEFAULT_TABLESPACE


 91 OPEN       T374

10.查詢當前使用者能訪問的所有物件
SQL> select owner,object_name,object_type from all_objects;
11.查詢所有的資料字典,並查詢所有的以user開頭的所有表
SQL> desc dictionary;
Name Null? Type


TABLE_NAME VARCHAR2(30)
COMMENTS VARCHAR2(4000)
SQL> select table_name from dictionary where table_name like 'USER%';
12.檢視scott使用者的表和表空間
SQL> col owner for a10
SQL> col table_namefor a20
SQL> col tablespace_name for a30
SQL> select owner,table_name,tablespace_name from dba_tables where owner='SCOTT';
OWNER TABLE_NAME TABLESPACE_NAME


SCOTT DEPT USERS
SCOTT EMP USERS
SCOTT BONUS USERS
SCOTT SALGRADE USERS

實驗六:動態資料字典
1.檢視和日誌檔案相關的資訊(注意大寫)
SQL> select * from v$fixed_table where name like 'v$LOG%';
2.檢視日誌組狀態資訊
SQL> select group#,members,archived,status from v$log;
GROUP# MEMBERS ARCHIV STATUS


 1      1 NO     INACTIVE
 2      1 NO     INACTIVE
 3      1 NO     CURRENT

3.檢視日誌檔案資訊
SQL> col type for a10
SQL> col group# for 99
SQL> select * fromv$logfile;
GROUP# STATUS TYPE MEMBER IS_REC


 3            ONLINE     /opt/oracle/oradata/orcl/redo03.log      NO
 2            ONLINE     /opt/oracle/oradata/orcl/redo02.log      NO
 1            ONLINE     /opt/oracle/oradata/orcl/redo01.log      NO

4.檢視當前正在使用的重做日誌檔案的資訊
SQL> select l.group#,l.archived,l.status,lf.type,lf.member from v$log l, v$logfilelf where l.group#=lf.group#;

GROUP# ARCHIV STATUS TYPE


MEMBER

 3 NO     INACTIVE                 ONLINE

/opt/oracle/oradata/orcl/redo03.log

 2 NO     INACTIVE                 ONLINE

/opt/oracle/oradata/orcl/redo02.log

 1 NO     CURRENT                  ONLINE

/opt/oracle/oradata/orcl/redo01.log
5.檢視例項資訊
SQL> col instance_name for a20;
SQL> col host_name for a10
SQL> select instance_name,host_name,version,startup_time,logins from v$instance;

INSTANCE_NAME HOST_NAME VERSION STARTUP_TIME


LOGINS

orcl rhel1.bene 11.2.0.1.0 30-MAY-17
t.com
ALLOWED
6.檢視資料庫資訊
SQL> col name for a10;
SQL> select name,created,log_mode from v$database;

NAME CREATED LOG_MODE


ORCL 30-MAY-17 NOARCHIVELOG