Oracle 10g RAC的负载均衡配置

转自:http://www.cnblogs.com/millen/archive/2009/05/06/1450844.html

Oracle 10g RAC的负载均衡配置
负载均衡是指连接的负载均衡。RAC的负载均衡主要是指新会话连接到RAC数据库时,如何判定这个新的连接要连到哪个节点进行工作。在RAC中,负载均衡分为两种,一种是基于客户端连接的,另外一种是基于服务器端的。
一、客户端负载均衡的配置

1、当前服务器中的数据库版本如下:

SQL> select * from v$version;



BANNER

----------------------------------------------------------------

Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod

PL/SQL Release 10.2.0.1.0 - Production

CORE    10.2.0.1.0      Production

TNS for Linux: Version 10.2.0.1.0 - Production

NLSRTL Version 10.2.0.1.0 - Production

2、在客户端的tnsnames.ora的配置中,只要连接的是整个数据库的服务名,不是实例名。

在服务器端查看RAC数据库的service_names:

SQL> show parameter service_names



NAME                                 TYPE        VALUE

------------------------------------ ----------- ------------------------------

service_names                        string      RACDB.chenxu.yo2.cn

SQL>

3、在客户端配置TNS:

客户端的负载均衡配置相对简单,只需要在tnsnames.ora中添加LOAD_BALANCE=ON这么一个选项即可。

RACDB =

  (DESCRIPTION =

    (ADDRESS_LIST =

      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.170)(PORT = 1521))

      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.171)(PORT = 1521))

      (LOAD_BALANCE = on)

    )

    (CONNECT_DATA =

      (SERVICE_NAME = racdb.chenxu.yo2.cn)

    )

  )

配置TNS中的HOST值是服务器端RAC配置中的虚拟IP即VIP,如下:

[root@NODE01 admin]# more /etc/hosts

# Do not remove the following line, or various programs

# that require network functionality will fail.

127.0.0.1       localhost.localdomain   localhost

::1     localhost6.localdomain6 localhost6



192.168.1.180         node01

192.168.1.181         node02

192.168.1.170         vip01

192.168.1.171         vip02

10.10.10.1            priv01

10.10.10.2            priv02

4、在客户端测试:

开启sqlplus_1:

SQL> conn sys/chenxu@racdb as sysdba

已连接。

SQL>

SQL> show parameter instance_name



NAME                                 TYPE        VALUE

------------------------------------ ----------- ------------------------------

instance_name                        string      RACDB2

SQL>

SQL> select instance_name from gv$instance;



INSTANCE_NAME

----------------

RACDB1

RACDB2

开启sqlplus_2:

SQL> conn sys/chenxu@racdb as sysdba

已连接。

SQL> show parameter instance_name



NAME                                 TYPE        VALUE

------------------------------------ ----------- ------------------------------

instance_name                        string      RACDB1

开启sqlplus_3:

SQL> conn sys/chenxu@racdb as sysdba

已连接。

SQL>

SQL> show parameter instance_name



NAME                                 TYPE        VALUE

------------------------------------ ----------- ------------------------------

instance_name                        string      RACDB2

开启sqlplus_4:

SQL> conn sys/chenxu@racdb as sysdba

已连接。

SQL> show parameter instance_name



NAME                                 TYPE        VALUE

------------------------------------ ----------- ------------------------------

instance_name                        string      RACDB1

5、总结

这样当客户端连接RAC数据库时,会随机在TNS里面挑个监听地址进行连接。在Oracle 10g以前,假如有节点宕机或者类似事故时,客户端可能还是选择连接到这个节点,这样会发生较长时间的TCP等待超时。而在10g以后,由于VIP和FAN的引入,这样的情况可以得到很大程度的改善。客户端的负载均衡在通常情况下能够较好地工作,但是由于连接是在客户端随机发起的,这样客户端并不知道RAC各节点的负荷及连接数情况,有可能负荷大的节点还会源源不断地增加新的连接,导致RAC节点无法均衡工作。



二、服务器端负载均衡的配置

从Oracle 10g开始,服务器端的负载均衡可以根据RAC中各节点的负荷及连接数情况,而判定将新的客户端连接分配到负荷最小的节点上去。RAC中各节点的PMON进程每3秒会将各自节点的负荷(包括LOAD、最大LOAD、CPU使用率)及连接数更新到service_register里面,然后假如节点的负荷有发生变化,将会通知到监听程序,由监听程序再决定新的客户端连接分配至哪个节点。假如RAC中一个节点的监听失败了,PMON每一分钟会去检查一次是否已经恢复正常。

服务器端的监听配置是在各节点的tnsnames.ora里面添加一个连接到各个节点监听的条目,然后再在初始化参数里面设置remote_listeners这个参数。

1、测试客户端的TNS

修改客户端tnsnames.ora的文件,内容如下:

RACDB =

  (DESCRIPTION =

    (ADDRESS_LIST =

      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.170)(PORT = 1521))

    )

    (CONNECT_DATA =

      (SERVICE_NAME = racdb.chenxu.yo2.cn)

    )

  )

测试连接:

SQL>  conn sys/chenxu@racdb as sysdba

已连接。

SQL>

SQL> show parameter instance_name



NAME                                 TYPE        VALUE

------------------------------------ ----------- ------------------------------

instance_name                        string      RACDB1



2、配置服务器端TNS

服务器端的监听配置是在各节点的tnsnames.ora里面添加一个连接到各个节点监听的条目(红色代码),在服务器端每个节点的tnsnames.ora里面的内容如下:

[root@NODE01 admin]# pwd

/orac/orahome/oracle/product/10.2.0/db_1/network/admin

[root@NODE01 admin]#

[root@NODE01 admin]# more tnsnames.ora

# tnsnames.ora Network Configuration File: /orac/orahome/oracle/product/10.2.0/db_1/network/admin/tnsnames.ora

# Generated by Oracle configuration tools.



RACDB1 =

  (DESCRIPTION =

    (ADDRESS = (PROTOCOL = TCP)(HOST = vip01)(PORT = 1521))

    (CONNECT_DATA =

      (SERVER = DEDICATED)

      (SERVICE_NAME = RACDB.chenxu.yo2.cn)

      (INSTANCE_NAME = RACDB1)

    )

  )



RACDB =

  (DESCRIPTION =

    (ADDRESS = (PROTOCOL = TCP)(HOST = vip01)(PORT = 1521))

    (ADDRESS = (PROTOCOL = TCP)(HOST = vip02)(PORT = 1521))

    (LOAD_BALANCE = yes)

    (CONNECT_DATA =

      (SERVER = DEDICATED)

      (SERVICE_NAME = RACDB.chenxu.yo2.cn)

    )

  )



LISTENERS_RACDB =

  (ADDRESS_LIST =

    (ADDRESS = (PROTOCOL = TCP)(HOST = vip01)(PORT = 1521))

    (ADDRESS = (PROTOCOL = TCP)(HOST = vip02)(PORT = 1521))

  )



RACDB2 =

  (DESCRIPTION =

    (ADDRESS = (PROTOCOL = TCP)(HOST = vip02)(PORT = 1521))

    (CONNECT_DATA =

      (SERVER = DEDICATED)

      (SERVICE_NAME = RACDB.chenxu.yo2.cn)

      (INSTANCE_NAME = RACDB2)

    )

  )

3、在初始化参数中设置参数remote_listeners

SQL> conn sys/chenxu@racdb as sysdba

已连接。

SQL> show parameter remote_listener



NAME                                 TYPE        VALUE

------------------------------------ ----------- ------------------------------

remote_listener                      string

SQL>

SQL> alter system set remote_listener='LISTENERS_RACDB' sid='*';

#(reset命令可以撤销设置,恢复默认值)

系统已更改。



SQL>

SQL> show parameter remote_listener



NAME                                 TYPE        VALUE

------------------------------------ ----------- ------------------------------

remote_listener                      string      LISTENERS_RACDB

正确配置参数后,通过lsnrctl status命令看到在监听启动以后,可以看到监听器上有2个instance。

[root@NODE01 bin]# lsnrctl status



LSNRCTL for Linux: Version 10.2.0.1.0 - Production on 18-DEC-2008 05:40:08



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



Connecting to (ADDRESS=(PROTOCOL=tcp)(HOST=)(PORT=1521))

STATUS of the LISTENER

------------------------

Alias                     LISTENER_NODE01

Version                   TNSLSNR for Linux: Version 10.2.0.1.0 - Production

Start Date                18-DEC-2008 00:18:07

Uptime                    0 days 5 hr. 22 min. 1 sec

Trace Level               off

Security                  ON: Local OS Authentication

SNMP                      OFF

Listener Parameter File   /orac/orahome/oracle/product/10.2.0/db_1/network/admin/listener.ora

Listener Log File         /orac/orahome/oracle/product/10.2.0/db_1/network/log/listener_node01.log

Listening Endpoints Summary...

  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=192.168.1.170)(PORT=1521)))

  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=192.168.1.180)(PORT=1521)))

  (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC)))

Services Summary...

Service "+ASM" has 1 instance(s).

  Instance "+ASM1", status BLOCKED, has 1 handler(s) for this service...

Service "+ASM_XPT" has 1 instance(s).

  Instance "+ASM1", status BLOCKED, has 1 handler(s) for this service...

Service "RACDB.chenxu.yo2.cn" has 2 instance(s).

  Instance "RACDB1", status READY, has 2 handler(s) for this service...

  Instance "RACDB2", status READY, has 1 handler(s) for this service...

Service "RACDBXDB.chenxu.yo2.cn" has 2 instance(s).

  Instance "RACDB1", status READY, has 1 handler(s) for this service...

  Instance "RACDB2", status READY, has 1 handler(s) for this service...

Service "RACDB_XPT.chenxu.yo2.cn" has 2 instance(s).

  Instance "RACDB1", status READY, has 2 handler(s) for this service...

  Instance "RACDB2", status READY, has 1 handler(s) for this service...

The command completed successfully

这时在客户端用sqlplus连接服务器数据库可能会出现如下错误:

ERROR:

ORA-12545: 因目标主机或对象不存在,连接失败

4、解决ORA-12545连接失败问题

方法一、配置客户端的Hosts文件

通过在客户端的Hosts文件中加入对两个服务名的名字解析可以解决ORA-12545问题。在windows下Hosts文件在C:"WINDOWS"system32"drivers"etc目录下,在linux修改/etc/hosts文件的内容。添加如下内容:

192.168.1.170   node01

192.168.1.171   node02



其中node01、node02为服务器节点的主机名。



[root@NODE01 bin]# hostname

NODE01

方法二、配置参数local_listener

这边就不详细描述,具体参考论坛文章,网址如下:(http://www.itpub.net/viewthread.php?tid=1060068)

5、通过客户端测试负载均衡

开启sqlplus_1:

SQL> conn sys/chenxu@racdb as sysdba

已连接。

SQL> show parameter instance_name



NAME                                 TYPE        VALUE

------------------------------------ ----------- ------------------------------

instance_name                        string      RACDB1

开启sqlplus_2:

SQL>  conn sys/chenxu@racdb as sysdba

已连接。

SQL>  show parameter instance_name



NAME                                 TYPE        VALUE

------------------------------------ ----------- ------------------------------

instance_name                        string      RACDB2

可以看到客户端能够连接到实例RACDB2,说明基于服务器端的负载均衡配置成功。


你可能感兴趣的:(oracle,sql,linux,windows,SQL Server)