oracle11g数据库

oracle11g数据库

软件版本
oracle11g
节点IP系统功能CPU内存硬盘
node110.80.10.1centos7.9oracle4核心8GB20GB

centos7安装oracle11g环境准备

node1

安装基础软件依赖环境:

1
# yum install -y binutils compat-libstdc++-33 elfutils-libelf elfutils-libelf-devel expat gcc gcc-c++ glibc glibc-common glibc-devel glibc-headers libaio libaio-devel libgcc libstdc++ libstdc++-devel make pdksh sysstat unixODBC unixODBC-devel ksh

添加oracle用户和用户组:

1
2
3
4
5
6
# groupadd oinstall
# groupadd dba
# groupadd asmdba
# useradd -g oinstall -G dba,asmdba oracle
# id oracle
uid=1000(oracle) gid=1000(oinstall) groups=1000(oinstall),1001(dba),1002(asmdba)

添加优化内核参数:

1
2
3
4
5
6
7
8
9
10
11
12
13
# vim /etc/sysctl.conf
# 末尾,添加配置
fs.aio-max-nr = 1048576
fs.file-max = 6815744
kernel.shmall = 2097152
kernel.shmmni = 4096
kernel.shmmax = 1073741824
kernel.sem = 250 32000 100 128
net.ipv4.ip_local_port_range = 9000 65500
net.core.rmem_default = 262144
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 1048586
1
# sysctl -p

添加最大文件打开数和进程数:

1
2
3
4
# vim /etc/security/limits.conf
# 尾行,添加配置
* - nproc 16384
* - nofile 65536
1
2
3
# vim /etc/security/limits.d/20-nproc.conf
# 5行,删除配置
* soft nproc 4096

重新打开一个终端,配置生效:

1
2
3
4
# ulimit -a | grep 'open files'
open files (-n) 65536
# ulimit -a | grep 'max user processes'
max user processes (-u) 16384

配置账户限制:

1
2
3
4
# vim /etc/pam.d/login
# 尾行,添加配置
session required /lib64/security/pam_limits.so
session required pam_limits.so

验证操作:

1
2
3
4
5
6
#  sysctl -a | grep fs.file-max
fs.file-max = 6815744
sysctl: reading key "net.ipv6.conf.all.stable_secret"
sysctl: reading key "net.ipv6.conf.default.stable_secret"
sysctl: reading key "net.ipv6.conf.ens33.stable_secret"
sysctl: reading key "net.ipv6.conf.lo.stable_secret"

创建安装目录:

1
2
3
4
5
6
7
8
9
10
11
12
# mkdir -p /usr/local/oracle/product/11.2.0
# mkdir -p /usr/local/oracle/oradata
# mkdir -p /usr/local/oracle/inventory
# mkdir -p /usr/local/oracle/fast_recovery_area
# chown -R oracle:oinstall /usr/local/oracle
# chmod -R 775 /usr/local/oracle
# ll /usr/local/oracle/
total 0
drwxrwxr-x 2 oracle oinstall 6 12月 11 11:51 fast_recovery_area
drwxrwxr-x 2 oracle oinstall 6 12月 11 11:51 inventory
drwxrwxr-x 2 oracle oinstall 6 12月 11 11:51 oradata
drwxrwxr-x 3 oracle oinstall 20 12月 11 11:51 product

新建窗口,切换到oracle用户,配置环境变量:

1
2
3
4
5
6
7
8
9
10
11
12
13
# su - oracle
$ vim .bashrc
# 尾行,添加配置
umask 022
export ORACLE_HOSTNAME=10.80.10.1
export ORACLE_BASE=/usr/local/oracle
export ORACLE_HOME=$ORACLE_BASE/product/11.2.0/
export ORACLE_SID=ORCL
export PATH=.:$ORACLE_HOME/bin:$ORACLE_HOME/OPatch:$ORACLE_HOME/jdk/bin:$PATH
export LC_ALL="en_US"
export LANG="en_US"
export NLS_LANG="AMERICAN_AMERICA.ZHS16GBK"
export NLS_DATE_FORMAT="YYYY-MM-DD HH24:MI:SS"

验证配置:

1
2
3
4
$ source .bashrc
$ echo "$ORACLE_HOME"
/usr/local/oracle/product/11.2.0/
$ exit

centos7安装oracle11g数据库

node1

上传oracle11g安装包,修改权限:

下载地址:https://www.oracle.com/database/technologies/instant-client/linux-x86-64-downloads.html

下载地址:http://download.oracle.com/otn/linux/oracle11g/R2/linux.x64_11gR2_database_1of2.zip

下载地址:http://download.oracle.com/otn/linux/oracle11g/R2/linux.x64_11gR2_database_2of2.zip

1
2
3
4
5
6
# cd /usr/local/oracle/
# chown oracle:oinstall *.zip
# ll *.zip
-rw-r--r-- 1 oracle oinstall 60704657 Dec 11 12:04 instantclient-basic-linux.x64-11.2.0.4.0.zip
-rw-r--r-- 1 oracle oinstall 1239269270 Dec 11 12:04 linux.x64_11gR2_database_1of2.zip
-rw-r--r-- 1 oracle oinstall 1111416131 Dec 11 12:04 linux.x64_11gR2_database_2of2.zip

解压安装:

1
2
3
4
# su - oracle
$ cd /usr/local/oracle/
$ unzip linux.x64_11gR2_database_1of2.zip -d /usr/local/oracle/
$ unzip linux.x64_11gR2_database_2of2.zip -d /usr/local/oracle/

修改配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$ vim /usr/local/oracle/database/response/db_install.rsp
# 29行,修改配置
oracle.install.option=INSTALL_DB_SWONLY
# 37行,修改配置
ORACLE_HOSTNAME=10.80.10.1
# 42行,修改配置
UNIX_GROUP_NAME=oinstall
# 47行,修改配置
INVENTORY_LOCATION=/usr/local/oracle/inventory
# 78行,修改配置
SELECTED_LANGUAGES=en,zh_CN
# 83行,修改配置
ORACLE_HOME=/usr/local/oracle/product/11.2.0
# 88行,修改配置
ORACLE_BASE=/usr/local/oracle
# 99行,修改配置
oracle.install.db.InstallEdition=EE
# 108行,修改配置
oracle.install.db.isCustomInstall=true
# 142行,修改配置
oracle.install.db.DBA_GROUP=dba
# 147行,修改配置
oracle.install.db.OPER_GROUP=dba
# 385行,修改配置
DECLINE_SECURITY_UPDATES=true

安装oracle数据库:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ cd /usr/local/oracle/database/
$ ./runInstaller -silent -ignorePrereq -responseFile /usr/local/oracle/database/response/db_install.rsp
Starting Oracle Universal Installer...

Checking Temp space: must be greater than 120 MB. Actual 10589 MB Passed
Checking swap space: must be greater than 150 MB. Actual 2047 MB Passed
Preparing to launch Oracle Universal Installer from /tmp/OraInstall2023-12-11_12-12-56PM. Please wait ...[oracle@node1 database]$ [WARNING] [INS-32055] The Central Inventory is located in the Oracle base.
CAUSE: The Central Inventory is located in the Oracle base.
ACTION: Oracle recommends placing this Central Inventory in a location outside the Oracle base directory.
[WARNING] [INS-32055] The Central Inventory is located in the Oracle base.
CAUSE: The Central Inventory is located in the Oracle base.
ACTION: Oracle recommends placing this Central Inventory in a location outside the Oracle base directory.
You can find the log of this install session at:
/usr/local/oracle/inventory/logs/installActions2023-12-11_12-12-56PM.log

新建窗口,查看日志,等待安装完成,比较慢,安装完成会提示:

1
2
3
# tail -f /usr/local/oracle/inventory/logs/installActions2023-12-11_12-12-56PM.log
# 日志提示安装成功
INFO: Shutdown Oracle Database 11g Release 2 Installer

切换root账户,执行两个脚本:

1
2
3
4
5
6
7
8
9
# bash /usr/local/oracle/inventory/orainstRoot.sh
Changing permissions of /usr/local/oracle/inventory.
Adding read,write permissions for group.
Removing read,write,execute permissions for world.

Changing groupname of /usr/local/oracle/inventory to oinstall.
The execution of the script is complete.
# bash /usr/local/oracle/product/11.2.0/root.sh
Check /usr/local/oracle/product/11.2.0/install/root_node1_2023-03-01_12-10-31.log for the output of root script

oracle数据库初始化和启动

node1

切换oracle用户,更改配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ vim /usr/local/oracle/database/response/dbca.rsp
# 78行
GDBNAME = "orcl"
# 149行
SID = "orcl"
# 190行
SYSPASSWORD = "oracle"
# 200行
SYSTEMPASSWORD = "oracle"
# 231行
SYSMANPASSWORD = "oracle"
# 241行
DBSNMPPASSWORD = "oracle"
# 357行
DATAFILEDESTINATION = /usr/local/oracle/oradata
# 367行
RECOVERYAREADESTINATION=/usr/local/oracle/fast_recovery_area
# 415行
CHARACTERSET = "AL32UTF8"
# 540行
TOTALMEMORY = "800"

初始化数据库,等待完成:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$ dbca -silent -responseFile /usr/local/oracle/database/response/dbca.rsp
Copying database files
1% complete
3% complete
11% complete
18% complete
26% complete
37% complete
Creating and starting Oracle instance
40% complete
45% complete
50% complete
55% complete
56% complete
60% complete
62% complete
Completing Database Creation
66% complete
70% complete
73% complete
85% complete
96% complete
100% complete
Look at the log file "/usr/local/oracle/cfgtoollogs/dbca/orcl/orcl.log" for further details.

启动端口监听:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
$ lsnrctl start

LSNRCTL for Linux: Version 11.2.0.1.0 - Production on 01-MAR-2023 12:48:53

Copyright (c) 1991, 2009, Oracle. All rights reserved.

Starting /usr/local/oracle/product/11.2.0//bin/tnslsnr: please wait...

TNSLSNR for Linux: Version 11.2.0.1.0 - Production
Log messages written to /usr/local/oracle/diag/tnslsnr/node1/listener/alert/log.xml
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=node1)(PORT=1521)))

Connecting to (ADDRESS=(PROTOCOL=tcp)(HOST=)(PORT=1521))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for Linux: Version 11.2.0.1.0 - Production
Start Date 01-MAR-2023 12:48:53
Uptime 0 days 0 hr. 0 min. 20 sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Log File /usr/local/oracle/diag/tnslsnr/node1/listener/alert/log.xml
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=node1)(PORT=1521)))
The listener supports no services
The command completed successfully

关闭端口监听(暂不操作):

1
2
3
4
5
6
7
8
$ lsnrctl stop

LSNRCTL for Linux: Version 11.2.0.1.0 - Production on 16-OCT-2022 02:02:57

Copyright (c) 1991, 2009, Oracle. All rights reserved.

Connecting to (ADDRESS=(PROTOCOL=tcp)(HOST=)(PORT=1521))
The command completed successfully

检测端口和进程:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
$ netstat -tlunp | grep 1521
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp6 0 0 :::1521 :::* LISTEN 44266/tnslsnr
$ ps aux | grep ora
root 28657 0.0 0.0 191880 2356 pts/2 S 12:05 0:00 su - oracle
oracle 28658 0.0 0.0 117604 2116 pts/2 S 12:05 0:00 -bash
oracle 59810 0.0 0.2 1062564 16272 ? Ss 12:21 0:00 ora_pmon_orcl
oracle 59812 0.0 0.1 1036600 12520 ? Ss 12:21 0:00 ora_vktm_orcl
oracle 59817 0.0 0.1 1036600 12512 ? Ss 12:21 0:00 ora_gen0_orcl
oracle 59819 0.0 0.1 1036600 12532 ? Ss 12:21 0:00 ora_diag_orcl
oracle 59821 0.0 0.2 1036600 20552 ? Ss 12:21 0:00 ora_dbrm_orcl
oracle 59823 0.0 0.1 1036600 12780 ? Ss 12:21 0:00 ora_psp0_orcl
oracle 59825 0.0 0.2 1037112 16032 ? Ss 12:21 0:00 ora_dia0_orcl
oracle 59827 0.2 0.3 1036600 30316 ? Ss 12:21 0:00 ora_mman_orcl
oracle 59829 0.0 0.2 1042356 19132 ? Ss 12:21 0:00 ora_dbw0_orcl
oracle 59831 0.0 0.2 1052152 20476 ? Ss 12:21 0:00 ora_lgwr_orcl
oracle 59833 0.0 0.1 1037112 15548 ? Ss 12:21 0:00 ora_ckpt_orcl
oracle 59835 0.0 0.3 1037116 31124 ? Ss 12:21 0:00 ora_smon_orcl
oracle 59837 0.0 0.1 1036600 12548 ? Ss 12:21 0:00 ora_reco_orcl
oracle 59839 0.2 0.7 1063004 58644 ? Ss 12:21 0:00 ora_mmon_orcl
oracle 59841 0.0 0.2 1036600 17312 ? Ss 12:21 0:00 ora_mmnl_orcl
oracle 59843 0.0 0.1 1062232 13144 ? Ss 12:21 0:00 ora_d000_orcl
oracle 59845 0.0 0.1 1037796 11720 ? Ss 12:21 0:00 ora_s000_orcl
oracle 60119 0.0 0.1 1036600 14448 ? Ss 12:21 0:00 ora_qmnc_orcl
oracle 60152 0.1 0.5 1041208 43316 ? Ss 12:21 0:00 ora_cjq0_orcl
oracle 60274 0.2 0.6 1038288 48336 ? Ss 12:21 0:00 ora_j000_orcl
oracle 60276 0.2 0.5 1038300 42004 ? Ss 12:21 0:00 ora_j001_orcl
oracle 60278 0.0 0.4 1038136 35484 ? Ss 12:21 0:00 ora_j002_orcl
oracle 60280 0.2 0.5 1038204 46312 ? Ss 12:21 0:00 ora_j003_orcl
oracle 60282 0.0 0.1 1036596 12248 ? Ss 12:21 0:00 ora_j004_orcl
oracle 60284 0.0 0.1 1036596 12304 ? Ss 12:21 0:00 ora_j005_orcl
oracle 60390 0.1 0.1 235108 12960 ? Ssl 12:21 0:00 /usr/local/oracle/product/11.2.0//bin/tnslsnr LISTENER -inherit
oracle 60530 0.0 0.3 1038140 28788 ? Ss 12:21 0:00 ora_q000_orcl
oracle 60532 0.0 0.1 1036596 15624 ? Ss 12:21 0:00 ora_q001_orcl
oracle 61122 0.0 0.0 155420 1796 pts/2 R+ 12:21 0:00 ps aux
oracle 61123 0.0 0.0 114872 984 pts/2 S+ 12:21 0:00 grep --color=auto ora

拷贝配置文件,init.ora.后的参数是随机的:

1
$ cp /usr/local/oracle/admin/orcl/pfile/init.ora.* /usr/local/oracle/product/11.2.0/dbs/initORCL.ora

oracle用户登录数据库:

1
2
3
4
5
6
7
8
9
$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.1.0 Production on Wed Mar 1 12:51:53 2023

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

Connected to an idle instance.

SQL>

启动实例:

1
2
3
4
5
6
7
8
9
SQL> startup
ORACLE instance started.

Total System Global Area 835104768 bytes
Fixed Size 2217952 bytes
Variable Size 490735648 bytes
Database Buffers 335544320 bytes
Redo Buffers 6606848 bytes
ORA-01102: cannot mount database in EXCLUSIVE mode

遇到报错,关闭数据库,新建窗口进行修改:

1
2
3
4
5
SQL> shutdown immediate
ORA-01507: database not mounted


ORACLE instance shut down.

参考启动问题处理,处理后使用oracle用户启动实例:

1
2
3
4
5
6
7
8
9
10
SQL> startup
ORACLE instance started.

Total System Global Area 835104768 bytes
Fixed Size 2217952 bytes
Variable Size 490735648 bytes
Database Buffers 335544320 bytes
Redo Buffers 6606848 bytes
Database mounted.
Database opened.

查看版本:

1
2
3
4
5
6
7
8
9
SQL> select * from v$version;

BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
PL/SQL Release 11.2.0.1.0 - Production
CORE 11.2.0.1.0 Production
TNS for Linux: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production

开启scott用户,设置密码:userpwd:

1
2
3
4
5
6
7
SQL> alter user scott account unlock;

User altered.

SQL> alter user scott identified by userpwd;

User altered.

退出sql:

1
2
3
4
SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining,
Oracle Database Vault and Real Application Testing options

附:启动问题处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# MEMORY_TARGET not supported on this system解决方法
mount -o remount,size=3G /dev/shm/

# cannot mount database in EXCLUSIVE mode解决方案
\rm /usr/local/oracle/product/11.2.0/dbs/lkORCL

# error in identifying control file解决方案,ipcrm的参数是第一个key的semid
ipcs -s
ipcrm -s 17
ipcs -m

# database not open解决方法
alter database open;
connection lost contact
sqlplus / as sysdba

# 重新登录
程序连接不到Oracle,需要配置/etc/hosts,重启监听

python操作oracle数据库

node1

切换oracle用户,使用scott用户登录oracle数据库:

1
2
3
4
5
6
7
8
9
10
11
12
13
$ sqlplus scott/userpwd

SQL*Plus: Release 11.2.0.1.0 Production on Wed Mar 1 12:55:33 2023

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, Oracle Label Security, OLAP, Data Mining,
Oracle Database Vault and Real Application Testing options

SQL>

创建t1表:

1
2
3
SQL> create table t1(id int not null, name varchar(8) not null);

Table created.

t1表插入数据:

1
2
3
4
5
6
7
8
9
SQL> insert into t1(id, name) values (1, 'student');

1 row created.

SQL> select * from t1;

ID NAME
---------- ----------------
1 student

更新数据:

1
2
3
4
5
6
7
8
9
SQL> update t1 set id=3 where id=1;

1 row updated.

SQL> select * from t1;

ID NAME
---------- ----------------
3 student

设置事务操作:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
SQL> savepoint node1;

Savepoint created.

SQL> update t1 set id=5;

1 row updated.

SQL> select * from t1;

ID NAME
---------- ----------------
5 student

SQL> savepoint node2;

Savepoint created.

SQL> update t1 set id=7;

1 row updated.

SQL> select * from t1;

ID NAME
---------- ----------------
7 student

SQL> rollback to node2;

Rollback complete.

SQL> select * from t1;

ID NAME
---------- ----------------
5 student

SQL> rollback to node1;

Rollback complete.

SQL> select * from t1;

ID NAME
---------- ----------------
3 student

SQL> quit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining,
Oracle Database Vault and Real Application Testing options

安装oracle基础包:

1
2
$ cd /usr/local/oracle/
$ unzip instantclient-basic-linux.x64-11.2.0.4.0.zip

切换root用户,配置环境变量:

1
2
3
4
# vim /root/.bashrc
# 尾行,添加配置:
export ORACLE_HOME=/usr/local/oracle/instantclient_11_2
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME

加载环境变量:

1
# source /root/.bashrc

python安装oracle扩展:

1
2
# yum install -y python2-pip
# pip install cx_Oracle==7.3 -i https://mirrors.aliyun.com/pypi/simple/

使用python程序操作oracle:

1
2
3
4
5
6
7
8
9
10
# vim /tmp/oracle_test.py
import cx_Oracle
dsn = cx_Oracle.makedsn('10.80.10.1', '1521', 'orcl')
db = cx_Oracle.connect('scott', 'userpwd', dsn)
cr=db.cursor()
sql = 'select * from t1'
cr.execute(sql)
rs=cr.fetchall()
for x in rs:
print(x)

测试python脚本:

1
2
# python /tmp/oracle_test.py
(3, 'student')
1
2
3
4
5
没有加载环境变量,执行python脚本会报错:
`Traceback (most recent call last):
File "/tmp/oracle_test.py", line 3, in <module>
db = cx_Oracle.connect('scott', 'userpwd', dsn)
cx_Oracle.DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library: "libclntsh.so: cannot open shared object file: No such file or directory". See https://oracle.github.io/odpi/doc/installation.html#linux for help`

切换oracle用户,数据库添加数据:

1
2
3
4
5
6
7
8
9
10
11
$ sqlplus scott/userpwd

SQL> insert into t1(id, name) values (2, 'teacher');

1 row created.

SQL> commit;

Commit complete.

SQL> quit

再次执行脚本查看数据:

1
2
3
$ python /tmp/oracle_test.py
(3, 'student')
(2, 'teacher')

开启事务自动提交,字节有长度限制:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ sqlplus scott/userpwd

SQL> SET AUTOCOMMIT ON;

SQL> insert into t1(id, name) values (8, 'classm');

1 row created.

Commit complete.

SQL> insert into t1(id, name) values (9, 'people');

1 row created.

Commit complete.

SQL> quit

再次执行脚本查看数据:

1
2
3
4
5
$ python /tmp/oracle_test.py
(3, 'student')
(2, 'teacher')
(8, 'classm')
(9, 'people')

oracle数据库重新初始化:

1
2
3
4
5
6
7
关闭oracle和监听
删除admin的orcl
删除oradata的orcl
编辑etc/oratab,删除最后一行

切换到oracle用户,初始化数据库
dbca -silent -responseFile /usr/local/oracle/database/response/dbca.rsp

oracles数据库开机自动启动,加执行权限:

1
2
3
4
5
6
$ vim /usr/local/oracle/start.sh
/usr/local/oracle/product/11.2.0/bin/lsnrctl start
sleep 2
/usr/local/oracle/product/11.2.0/bin/sqlplus / as sysdba << EOF >/tmp/start.log 2>&1
startup
EOF

关闭脚本,添加到/etc/rc.local,加执行权限:

1
2
3
4
5
6
$ vim /usr/local/oracle/stop.sh
/usr/local/oracle/product/11.2.0/bin/lsnrctl stop
sleep 2
/usr/local/oracle/product/11.2.0/bin/sqlplus / as sysdba << EOF >/tmp/stop.log 2>&1
shutdown
EOF