Linux下DB2命令学习及整理

DB2相关数据库命令

1.数据库实例的启动
首先要启动数据库的实例,即切换到db2inst1用户(注:db2inst1用户为当前数据库的实例),然后执行db2start启动数据库的实例

[root@localhost instance]# su - db2inst1
[db2inst1@localhost ~]$ db2start
SQL1063N  DB2START processing was successful.

2.数据库实例的关闭
首先在db2inst1用户下强制关闭实例上的所有应用程序,然后再关闭数据库实例

[db2inst1@localhost ~]$ db2 force application all
DB20000I  The FORCE APPLICATION command completed successfully.
DB21024I  This command is asynchronous and may not be effective immediately.

[db2inst1@localhost ~]$ db2stop
SQL1064N  DB2STOP processing was successful.
强制停止
[db2inst1@localhost DB2]$ db2stop force
SQL1064N  DB2STOP processing was successful.
[db2inst1@localhost DB2]$

3.显示所有的实例

[db2inst1@localhost ~]$ db2ilist
db2inst1
[db2inst1@localhost ~]$ 

4.显示当前的实例

[db2inst1@localhost ~]$ db2 get instance

 The current database manager instance is:  db2inst1

5.删除一个实例(注:需切换到root用户权限下)

[root@localhost ~]# cd /opt/ibm/db2/V9.7/instance
[root@localhost instance]# pwd
/opt/ibm/db2/V9.7/instance
[root@localhost instance]# ./db2idrop db2inst1
DBI1070I  Program db2idrop completed successfully.

6.列出当前实例中有哪些数据库

[root@localhost instance]# su - db2inst1
[db2inst1@localhost ~]$ db2 list db directory
SQL1031N  The database directory cannot be found on the indicated file system.

SQLSTATE=58031
[db2inst1@localhost ~]$

注:上面信息说明实例中没有数据库

7.创建数据库

[root@localhost instance]# su - db2inst1
[db2inst1@localhost ~]$ db2 create database test
SQL1032N  No start database manager command was issued.  SQLSTATE=57019
[db2inst1@localhost ~]$
[db2inst1@localhost ~]$ db2start
SQL5043N  Support for one or more communications protocols failed to start successfully. However, core database manager functionality started successfully.
[db2inst1@localhost ~]$ db2 create database test
DB20000I  The CREATE DATABASE command completed successfully.
[db2inst1@localhost ~]$ db2 list db directory

 System Database Directory

 Number of entries in the directory = 1

Database 1 entry:

 Database alias                       = TEST
 Database name                        = TEST
 Local database directory             = /home/db2inst1
 Database release level               = d.00
 Comment                              =
 Directory entry type                 = Indirect
 Catalog database partition number    = 0
 Alternate server hostname            =
 Alternate server port number         =

[db2inst1@localhost ~]$

注:上面通过创建一个test的数据库,并说明了创建数据库时,要先启动数据库,然后创建数据库test成功后并列出了当前实例中的所有数据库,只存在数据库test

使用UTF-8编码  
db2 create database test on '/home/db2inst1' using codeset UTF-8 territory CN
on '/home/db2inst1' 表示数据库路径
一般情况下'/home/db2inst1'为默认数据库路径

8.连接数据库

[db2inst1@localhost ~]$ db2 connect to test

   Database Connection Information

 Database server        = DB2/LINUX 9.7.1
 SQL authorization ID   = DB2INST1
 Local database alias   = TEST

[db2inst1@localhost ~]$

注:用密码情况下格式[db2inst1@localhost ~]$ db2 connect to test user username using password

db2 connect to <database> user <username> using  <password>

9.列出当前实例中所有激活的数据库

[root@localhost ~]# su - db2inst1
[db2inst1@localhost ~]$ db2 list active databases
SQL1032N  No start database manager command was issued.  SQLSTATE=57019
[db2inst1@localhost ~]$ db2 list db directory

 System Database Directory

 Number of entries in the directory = 1

Database 1 entry:

 Database alias                       = TEST
 Database name                        = TEST
 Local database directory             = /home/db2inst1
 Database release level               = d.00
 Comment                              =
 Directory entry type                 = Indirect
 Catalog database partition number    = 0
 Alternate server hostname            =
 Alternate server port number         =

[db2inst1@localhost ~]$ db2start
SQL1026N  The database manager is already active.
[db2inst1@localhost ~]$ db2 list active databases
SQL1611W  No data was returned by Database System Monitor.
[db2inst1@localhost ~]$ db2 connect to test

   Database Connection Information

 Database server        = DB2/LINUX 9.7.1
 SQL authorization ID   = DB2INST1
 Local database alias   = TEST

[db2inst1@localhost ~]$ db2 list active databases

                           Active Databases

Database name                              = TEST
Applications connected currently           = 1
Database path                              = /home/db2inst1/db2inst1/NODE0000/SQL00001/

[db2inst1@localhost ~]$ 
注:可以看出查看当前激活的数据库为已经启动后连接的数据库

  

  

  

 

  

  

 

  

 

有参考:http://blog.csdn.net/sunrier/article/details/7835578

http://blog.csdn.net/hanxin1987216/article/details/4506012

http://database.51cto.com/art/201008/219300.htm

posted @ 2016-04-27 16:16  小金乌会发光-Z&M  阅读(2604)  评论(0编辑  收藏  举报