5.创建实例
需要以root用户创建das和实例。分别用下面2个命令:
dascrt创建的是DB2 adminstration server,每台服务器只有一个这种server,为进行DB2管理(比如运行控制中心)所必须,同时指定其管理用户是db2das;
db2icrt 创建的是实例,其名字一般和管理用户名一样,这里均为db2inst1;
创建成功以后,系统在db2das和db2inst1相应的home目录下产生了一个子目录。/home/db2inst1/sqllib目录中包括了一个db2profile文件,包括了设定各个db2实例中用到的环境变量,如DB2INSTANCE和各种命令的搜索路径,库的路径等,必须执行它,才能进行各种操作,或者将. ~/sqllib/db2profile一行加入/home/db2inst1/.profile文件(因为创建db2inst1用户时指定了shell类型是/bin/sh,如果是其他shell,则采用不同的profile文件,比如bash则采用.bash_profile),这样当切换到db2inst1用户时就会自动执行这个脚本。db2das用户的设置脚本文件位于/home/db2das/das/dasprofile,同样需要执行这个脚本,才能执行db2admin等命令。用db2ilist命令可以查出当前已创建的实例名。
[root@redflag11012602 instance]# ./dascrt -u db2das
SQL4406W The DB2 Administration Server was started successfully.
DBI1070I Program dascrt completed successfully.
[root@redflag11012602 instance]# ./db2icrt -u db2inst1 db2inst1
DBI1070I Program db2icrt completed successfully.
-sh-3.2$ db2ilist
db2inst1
6.启动DB2实例
切换到db2das用户,执行db2admin start启动DB2管理服务器。
切换到db2inst1用户,执行db2start启动数据库实例。
root用户也可以执行db2admin start命令。如果首次执行,则会提示如下横线以下信息。一般出现在刚刚用dascrt命令创建das之后。
-sh-3.2$ . das/dasprofile
-sh-3.2$ db2admin start
SQL4406W The DB2 Administration Server was started successfully.
[root@redflag11012602 instance]# su - db2inst1
-sh-3.2$ ls
Desktop sqllib
-sh-3.2$ . sqllib/db2profile
-sh-3.2$ db2start
05/05/2011 20:13:08 0 0 SQL1063N DB2START processing was successful.
SQL1063N DB2START processing was successful.
----------
[root@redflag11012601 bin]# ./db2admin start
SQL4409W The DB2 Administration Server is already active.
7.创建和访问数据库
首先要启动数据库实例,切换到 db2inst1用户,执行db2start命令启动数据库实例。
若需要,首先用db2stop命令停止实例,如果db2stop命令不能成功执行,可运行下面的命令来关闭数据库实例。首先在db2inst1用户下强制关闭实例上的所有应用程序。
$ db2 force applications all
$ db2stop关闭数据库实例。
上述步骤也可以简化为改用db2stop force命令。
启动数据库实例后用db2 create database 命令创建数据库,可以设定字符集、页大小等数据库选项。
数据库创建成功后,可以用db2 connect to命令连接数据库。然后就可以进行创建表、查询等操作了。
如果要尽快测试db2数据库的功能,也可以用命令创建db2自带的sample数据库。
2011-05-09 11:01:56 0 0 SQL1025N The database manager was not stopped because databases are still active.
SQL1025N The database manager was not stopped because databases are still active.
-sh-3.2$ db2 force applications all
DB20000I The FORCE APPLICATION command completed successfully.
DB21024I This command is asynchronous and may not be effective immediately.
-sh-3.2$ db2stop
2011-05-09 11:06:03 0 0 SQL1064N DB2STOP processing was successful.
SQL1064N DB2STOP processing was successful.
-sh-3.2$ db2start
05/08/2011 11:14:24 0 0 SQL1063N DB2START processing was successful.
SQL1063N DB2START processing was successful.
-sh-3.2$ db2 "create database tpch USING CODESET UTF-8 TERRITORY CN pagesize 32 K"
DB20000I The CREATE DATABASE command completed successfully.
-sh-3.2$ db2 connect to tpch;
Database Connection Information
Database server = DB2/LINUXX8664 9.7.4
SQL authorization ID = DB2INST1
Local database alias = TPCH
db2 => select * from dual;
DUMMY
-----
X
1 record(s) selected.
----------------------------------------------------------------------
-sh-3.2$ cd /user1/ibm/db2/V9.7/bin
-sh-3.2$ ./db2sampl
Starting the DB2 instance...
Creating database "SAMPLE"...
Connecting to database "SAMPLE"...
Creating tables and data in schema "DB2INST1"...
Creating tables with XML columns and XML data in schema "DB2INST1"...
Stopping the DB2 instance...
'db2sampl' processing complete.
注意:如果创建数据库时没有指定创建位置,默认创建在/home/db2inst1/实例名下,在此目录下包括系统表空间、用户表空间的数据文件,通常这个目录下的空间不会太大,当需要导入大量数据时,会发生磁盘空间不足的错误,解决方法是在其他目录创建表空间,然后在创建表时指定表空间,或者在数据库关闭状态下,将此目录移动到其他空间足够的目录,再用Linux的ln -s命令将其他位置映射到此目录下。更好的办法是在一开始做好容量规划,将数据库创建目录指定到大容量的目录,具体命令行写法参考文档。软连接的例子如下:
[db2inst1@aix:/home/db2inst1]#>du -s /arch/IBM/ts/db2inst1
20111984 /arch/IBM/ts/db2inst1
[db2inst1@aix:/home/db2inst1]#>mv db2inst1 db2inst1ori
[db2inst1@aix:/home/db2inst1]#>ls -l
total 8
drwxrwxr-x 3 db2inst1 db2iadm1 256 May 10 16:03 db2inst1ori
drwxrwsr-t 23 db2inst1 db2iadm1 4096 May 10 15:49 sqllib
[db2inst1@aix:/home/db2inst1]#>ln -s /arch/IBM/ts/db2inst1 db2inst1