DB2入门(6)——从远程连接说开来

配置客户机至服务器的通信

基本组件:

  • 客户机 :192.168.225.161
  • 服务器:192.168.225.160
  • 通信 协议

    • TCP/IP
    • IPC(进程间通信):此协议用于本地连接。

    1.首先来实现一个远程连接

    在DB2客户机上连接远程DB2服务之前,必须正确设置服务端通信协议。DB2支持的协议有TCP/IP、NetBIOS、NPIPE等。

连接远程DB2数据库服务的基本步骤如下:
1. 在客户机上对远程DB2节点进行编目。
2. 在客户机上对远程数据库进行编目。

1、第一步

首先在客户机上对远程节点进行编目,这里需要确认远程主机在客户机上的名称、IP地址、端口号等基本命令如下:

db2 catalog  TCPIP  NODE  <node_name> REMOTE  <hostname or IP> SERVER  <service_name or posr_number>

示例:

[db2inst1@db22 db2inst1]$ db2 catalog TCPIP node ns1 remote 192.168.225.160 server 50000
DB20000I  The CATALOG TCPIP NODE command completed successfully.
DB21056W  Directory changes may not be effective until the directory cache is 
refreshed.

其他命令:

db2 terminate    --刷新缓存
db2 uncatalog node <node_name>   --删除节点目录
db2 list node directory   --查看本地节点目录的内容
[db2inst1@db22 db2inst1]$ db2 list node directory

 Node Directory

 Number of entries in the directory = 1

Node 1 entry:

 Node name                      = NS1
 Comment                        =
 Directory entry type           = LOCAL
 Protocol                       = TCPIP
 Hostname                       = 192.168.225.160
 Service name                   = 50000

2、第二步

再对已编目的节点进行数据库编目,这里用户需要确认远程连接的数据库的名称、该数据库在本机上的别名、第一步设置的远程主机在客户机上的别名(即节点名)和设置认证方式。命令如下:

db2 CATALOG db <remote_db_name>  AS <local_db_name or alias>  AT NODE <local_node_nam>     

--示例
[db2inst1@db22 db2inst1]$ db2 catalog db test as rem at node ns1
DB20000I  The CATALOG DATABASE command completed successfully.
DB21056W  Directory changes may not be effective until the directory cache is 
refreshed.

[db2inst1@db22 db2inst1]$ db2 list db directory

 System Database Directory

 Number of entries in the directory = 2

Database 1 entry:

 Database alias                       = REM
 Database name                        = TEST
 Node name                            = NS1
 Database release level               = 14.00
 Comment                              =
 Directory entry type                 = Remote
 Catalog database partition number    = -1
 Alternate server hostname            =
 Alternate server port number         =

Database 2 entry:

 Database alias                       = SAMPLE
 Database name                        = SAMPLE
 Local database directory             = /home/db2inst1
 Database release level               = 14.00
 Comment                              =
 Directory entry type                 = Indirect
 Catalog database partition number    = 0
 Alternate server hostname            =
 Alternate server port number         =
3、第三步

连接远程数据库,使用第二步中设置的本地数据库别名来连接。命令如下:

CONNECT TO local_db_name     //利用本地数据库别名连接远程数据库
USER userid                  //用户ID
USING password               //登陆密码
[db2inst1@db22 db2inst1]$ db2 connect to rem user db2inst1 using "123456"
SQL30081N  A communication error has been detected. Communication protocol 
being used: "TCP/IP".  Communication API being used: "SOCKETS".  Location 
where the error was detected: "192.168.225.160".  Communication function 
detecting the error: "connect".  Protocol specific error code(s): "113", "*", 
"*".  SQLSTATE=08001
防火墙设置

在160上防火墙中添加允许,并开放50000端口

[root@localhost cfg]# iptables --list -n  -----查看160上开放的端口
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22 
REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
[root@localhost cfg]# iptables -I INPUT -s 192.168.225.0/24  -p tcp --dport 50000 -j ACCEPT
[root@localhost cfg]#  service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]
[root@localhost cfg]#  service iptables restart
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Unloading modules:                               [  OK  ]
iptables: Applying firewall rules:                         [  OK  ]

连接成功!

db2 => connect to rem user db2inst1 using "123456"

   Database Connection Information

 Database server        = DB2/LINUXX8664 11.1.1.1
 SQL authorization ID   = DB2INST1
 Local database alias   = REM

深入了解DB2节点目录、数据库目录

  • 节点目录
  • 系统数据库
  • 本地数据库

在DB2中,目录是存储有关系统、数据库及其连接信息的二进制文件。DB2中有以下几种目录:

1.节点目录

节点目录用于存储远程数据库的所有连通性信息。本节我们只讲TCP/IP协议。在节点目录中大多项将和TCP/IP信息有关,比如机器的主机名和IP地址,还有相关的DB2的端口号。

  • 列示本地节点目录的内容
db2 list node directory
  • 将信息输入节点目录进行编目
db2 catalog  TCPIP  node <node_name> remote <hostname or IP-address> server <port_name or port_number>
  • 要除去节点目录
db2 uncatalog node n1

要想得到想要连接的远程实例的端口号,可以查看该实例的dbm cfg中的svcname参数来实现

[db2inst1@localhost NODE0000]$ db2 get dbm cfg|grep -i svcename
 TCP/IP Service name                          (SVCENAME) = 50000
 SSL service name                         (SSL_SVCENAME) = 

-i:忽略大小写
SVCENAME:通信端口

2.系统数据库目录(或系统db目录)

db2 list db directory

这里写图片描述

  • 将信息录入系统db目录, 使用catalog命令:
db2 catalog db <remote_db_name>  AS <alias>  at node <node_name>     
  • 删除数据库目录
db2 uncatalog db <db_name>
  • 要列出本地数据库目录的内容:
db2 list db directory on <path>

--示例:
[db2inst1@db22 db2inst1]$ db2 get dbm cfg | grep -i database
          Database Manager Configuration
     Node type = Database Server with local and remote clients
 Database manager configuration release level            = 0x1400
 Max number of concurrently active databases     (NUMDB) = 32
 Federated Database System Support           (FEDERATED) = NO
 Default database monitor switches
 Monitor health of instance and databases   (HEALTH_MON) = OFF
 Database manager authentication        (AUTHENTICATION) = SERVER
 Default database path                       (DFTDBPATH) = /home/db2inst1
 Database monitor heap size (4KB)          (MON_HEAP_SZ) = AUTOMATIC(90)
 Transaction manager database name         (TM_DATABASE) = 1ST_CONN
[db2inst1@db22 db2inst1]$ db2 list  db directory on /home/db2inst1

 Local Database Directory on /home/db2inst1

 Number of entries in the directory = 1

Database 1 entry:

 Database alias                       = SAMPLE
 Database name                        = SAMPLE
 Database directory                   = SQL00001
 Database release level               = 14.00
 Comment                              =
 Directory entry type                 = Home
 Catalog database partition number    = 0
 Database member number               = 0

参考书目:《循序渐进DB2》–牛新庄

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值