在DB2数据库中,编目(catalog)这个单词很难理解,我自己当初在学习DB2的时候也常常被这个编目搞的很不明白,直到现在我个人也感觉到DB2中编目(catalog)这个术语用的不是很好,具体来说编目有编目节点,编目数据库等。如果要理解编目,我先简单讲一下DB2数据库的体系结构,在DB2数据库中最大的概念是系统(节点)也就是主机,下面是实例,实例下面是数据库,然后是表空间,然后是数据库对象。现在假设你有一个数据库服务器在p570的机器上,你有一个客户端在windows,linux或任何平台上,你现在想建立一个客户端到服务器端的连接,具体步骤是什么呢?
第一步:
你必须要在客户端的机器上能够把远程的服务器能够识别出来,这个具体如何来做呢?在DB2使用的编目(catalog)方式,具体来说就是通过编目把远程的服务器写在本地客户端的一个文件中:
db2 catalog tcpip node p570 remote 172.10.10.10 server 50000
在上面的这条命令中p570是一个节点名(在一个机器上必须是唯一的),remote后面是服务器的IP地址,server是远程服务器上实例对应的端口号。DB2通过这种方式在本地的SQLNODIR文件中把远程服务器的信息记录下来,所以编目节点其实就是把远程服务器映射到本地,通过SQLNODIR中的记录能够找到远程服务器和实例,类似指向远程服务器和实例的地址指针。
第二步:
当把远程的服务器IP地址,实例编目下来后,第二步应该把该实例下的数据库编目到本地
db2 catalog db REMOTEDB at node p570
在这条命令中,REMOTEDB是远程实例下的数据库,p570是我们在第一步中编目的节点名
这条命令执行后会在本地SQLDBDIR文件中记录远程数据库的信息,这这里编目数据库可以理解为把远程服务器实例下的数据库映射到本地为一个别名
=========================================
上面是客户端和服务器不在同一台机器上,是通过编目节点,编目数据库来实现客户端连接到服务器上数据库的目的,如果是连接在同一台机器上,那么这时候不要显示的编目节点,但是在服务器上当我们创建一个实例的时候,有一个隐含的把实例在本地编目的过程,假设在p570上创建一个实例名为db2inst1,其实有一个隐含的
db2 catalog local node db2inst1 instance db2inst1 system p570 ostype aix的步骤,
同样当你在db2inst1下创建一个数据库MYDB的时候,有一个隐含的编目(catalog)数据库的步骤:
db2 catalog db mydb at node db2inst1的步骤
至此你可以这样理解编目(catalog),编目就是在本地或远程建立客户端到服务器的数据库连接的目的。
===============================================
db2 catalog 命令
catalog一个普通的数据库
db2 catalog tcpip node<Node Name> remote <IP> server <Port>
db2 catalog db <Database Name> at node <Node Name>
db2 connect to <Database Name> user<DB2 UserName> using<DB2 Password>
db2 connect reset
uncatalog
db2 uncatalog node <Node Name>
catalog db2 on z/OS
db2 catalog tcpip node <Node Name> remote <IP> server<Port>
db2 catalog db <SubSystem Name> at node <Node Name>
db2 connect to <SubSystem Name>user <DB2 UserName> using <DB2 Password>
db2 connect reset
uncatalog
db2 uncatalog node <Node Name>
================================================
DB2:Database Connection Services Directory
The Database Connection Services (DCS) directory contains connectivity information for host databases residing on System z (z/OS or OS/390) or System i (OS/400) server. You need to have DB2 Connect software installed.
To list the contents of the DCS directory, issue the following command:
db2 list dcs directory
To enter information into the DCS directory, use
db2 catalog dcs dbdbname as location_name
For example
db2 catalog dcs db mydb as db1g
Three table spaces—SYSCATSPACE (holding the Catalog tables), TEMPSPACE1 (system temporary
space), and USERSPACE1 (the default user table space)—are automatically created
when you create a database. SYSCATSPACE and TEMPSPACE1 can be considered system
structures, as they are needed for the normal operation of your database. SYSCATSPACE contains
the catalog tables containingmetadata(data about your database objects) and must exist at
all times. Some other RDBMSs call this structure a“data dictionary.”
If the database you want to access is MYDB2, which was in themyinstinstance, you would first
need to create this instance, switch to the instance, and then issue thecatalogcommand as
shown below.
db2icrt myinst
set DB2INSTANCE=myinst
catalog db mydb2 on h:
It is a good practice to back up the contents of all your configuration files as shown below.
db2 get dbm cfg > dbmcfg.bk
db2 get db cfg for database_name> dbcfg.bk
db2set -all > db2set.bk
db2 list db directory > systemdbdir.bk
db2 list node directory > nodedir.bk
db2 list dcs directory > dcsdir.bk
Database federated support in DB2 allows tables from multiple databases to be presented as
local tables to a DB2 server. The databases may be local or remote; they can also belong to different
RDBMSs.
First of all, make sure that your server allows federated support: The database manager parameter
FEDERATED must be set to YES.