ORACLE 11g手动内存管理

ORACLE 11g手动内存管理

  • 前言
    • 检查系统内核参数
    • 备份spfile
    • 修改内存参数后重新启动数据库。
    • 修改内核参数后重启数据库
    • 结束语

前言

oracle 11g的手动内存管理是一个老生常谈的话题,今天实际操作了一下,发现真的是网上文章一大抄,很多人只是拷贝了命令,但是却忽视了一些其他的限制条件,所以在这里把这个知识点重新梳理了一下,有言在先,如果您是着急解决问题,应该搜索其他直接贴出命令的文章。可以参考其他人的一篇文章:
Oracle 11g 内存手动管理

检查系统内核参数

在centos系统下,当启动手动内存管理的时候,首先要检查一下系统内核的参数配置
在centos下登录ROOT用户,然后编辑内核参数

vim /etc/sysctl.conf
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
fs.aio-max-nr = 1048576
fs.file-max = 6815744
#kernel.shmall = 2097152
kernel.shmall=4194304
kernel.shmmax = 1073741824
kernel.shmmni = 4096
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 = 1048576
net.ipv4.ip_forward = 1

这里的kernel.shmall是我已经修改过的,默认值很小。
关于kernel.shmall,百度的概念是共享内存总量,LINUX下的内存是以页为单位来管理的,共享内存页大小是4K,具体通过一下命令查看:

getconf PAGE_SIZE

默认显示4096,也就是4KB,那么如果你想给ORACLE设置手动内存为8G,计算方法是8G * 1024(转换为M) * 1024(转换为KB) / 4 =2097152,如果你要手动设置ORACLE内存是16G,那么这个值就是4194304,ORACLE所能设置的内存的最大值,不能超过kernel.shmall,否则会报错

SQL> startup
ORA-27102: out of memory
Linux-x86_64 Error: 28: No space left on device
Additional information: 1023410176
Additional information: 1

但是这一点在网上很多设置手动内存的文章里都没有提,难道只有我的CENTOS是需要修改的么?

备份spfile

在ORACLE中任何涉及ALTER SYSTEM SET xxxx的操作之前,首先要备份spfile,备份spfile,备份spfile,
通过sqlplus / as sysdba登录后,首先备份

create pfile='/home/oracle/pfile_201911161352' from spfile;

备份的作用是当修改的参数不合适导致数据库启动失败时,可以随时保证数据库可以正常启动。网上很多修改oracle内存的文章都不提备份的事儿,真的这么有自信一点儿都不怕出事儿是么
按照网上的步骤修改(此时我还未修改kernel.shmall)

##注意我再操作时按照如下设置是报错的。
SQL> alter system set sga_max_size=16G scope=spfile;
SQL>  alter system set  sga_target=16G scope=spfile;
SQL> alter system set pga_aggregate_target=8G scope=spfile;
SQL> alter system set MEMORY_TARGET=0 scope=spfile;
SQL> alter system set memory_max_target=0 scope=spfile;
SQL> shutdown immediate;
Database closed.
Database dismounted.
startupORACLE instance shut down.
#启动数据库
SQL> startup;
ORA-00843: Parameter not taking MEMORY_MAX_TARGET into account
ORA-00849: SGA_TARGET 17179869184 cannot be set to more than MEMORY_MAX_TARGET 0.
SQL> show parameter mem;
ORA-01034: ORACLE not available
Process ID: 0
Session ID: 96 Serial number: 3

GOOD,妥妥的启动失败了。因为之前已经备份了spfile,所以此处使用备份的spfile文件启动数据库

SQL> startup pfile='/home/oracle/pfile_201911161352'
ORACLE instance started.

Total System Global Area 1068937216 bytes
Fixed Size                  2251096 bytes
Variable Size             402654888 bytes
Database Buffers          637534208 bytes
Redo Buffers               26497024 bytes
Database mounted.
Database opened.
SQL> create spfile from pfile;
SQL>shutdown immediate;
SQL>startup;

运维无大事儿,只要多备份。
create spfile from pfile;是因为当前是用pfile来启动的,所以要切换到spfile启动,不然在修改ORACLE参数时无法指定scope = spfile或者scope=both,会提示报错:

SQL> alter system set sga_max_size=16g scope=both;
alter system set sga_max_size=16g scope=both
                 *
ERROR at line 1:
ORA-02095: specified initialization parameter cannot be modified

修改内存参数后重新启动数据库。

##以下按照8G内存修改
SQL> alter system set pga_aggregate_target=8g scope=spfile;
System altered.
SQL> alter system set memory_target=0 scope=spfile;
System altered.
SQL> alter system set memory_max_target=0 scope=spfile;
System altered.
SQL> shutdown immediate;
Database closed.
Database dismounted.
startupORACLE instance shut down.
SQL> startup
ORA-00843: Parameter not taking MEMORY_MAX_TARGET into account
ORA-00849: SGA_TARGET 17179869184 cannot be set to more than MEMORY_MAX_TARGET 0.

编辑/home/oracle/pfile_201911161352,找到该文件中的memory_max_target和memory_target=0并删除这两行,保存后直接创建spfile,然后启动数据库报错

SQL> create spfile from pfile='/home/oracle/pfile_201911161352';
File created.
SQL> startup
ORA-27102: out of memory
Linux-x86_64 Error: 28: No space left on device
Additional information: 1023410176
Additional information: 1

修改内核参数后重启数据库

ORA-27102就是我在开头说的报错,我所使用的centos没有修改过内核参数,所以设置的内存不能超过2G,按照开头的内容修改内核参数为16G后,首先刷新使修改生效

[root@server db-back]# /sbin/sysctl -p
fs.aio-max-nr = 1048576
fs.file-max = 6815744
kernel.shmall = 4194304
kernel.shmmax = 1073741824
kernel.shmmni = 4096
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 = 1048576
net.ipv4.ip_forward = 1

然后再次启动数据库

SQL> startup
ORACLE instance started.

Total System Global Area 8551575552 bytes
Fixed Size                  2270360 bytes
Variable Size            2415922024 bytes
Database Buffers         6123683840 bytes
Redo Buffers                9699328 bytes
Database mounted.
Database opened.

查看内存设置,发现修改已生效

SQL> show parameter sga

NAME                                 TYPE                              VALUE
------------------------------------ --------------------------------- ------------------------------
lock_sga                             boolean                           FALSE
pre_page_sga                         boolean                           FALSE
sga_max_size                         big integer                       8G
sga_target                           big integer                       8G
SQL> show parameter mem

NAME                                 TYPE                              VALUE
------------------------------------ --------------------------------- ------------------------------
hi_shared_memory_address             integer                           0
memory_max_target                    big integer                       0
memory_target                        big integer                       0
shared_memory_address                integer                           0
SQL> show parameter pga

NAME                                 TYPE                              VALUE
------------------------------------ --------------------------------- ------------------------------
pga_aggregate_target                 big integer                       4G
SQL> show parameter memory_max_target

NAME                                 TYPE                              VALUE
------------------------------------ --------------------------------- ------------------------------
memory_max_target                    big integer                       0

结束语

写了这么多,对于着急解决问题的人来说,这篇文章不免啰嗦。晚上有很多其他的文章直接罗列好了命令,使用起来确实方便,但是没有自己试验过只是拷贝命令,很容易踩坑

你可能感兴趣的:(数据库,oracle,手动内存管理)