1. 程式人生 > >informix資料庫在linux中的安裝以及用java/c/c++訪問

informix資料庫在linux中的安裝以及用java/c/c++訪問

一、安裝前準備

安裝JDK(略)

到IBM官網上下載informix軟體:iif.12.10.FC9DE.linux-x86_64.tar放在某個大家都可以訪問的目錄比如:/mypkg,並解壓到該目錄下。

我也放到了百度雲天翼雲上供下載。

建立informix使用者、組和安裝目錄:

sudo adduser  --home /opt/informix  informix

二、準備informix環境變數

在.bashrc追加下面內容(在informix使用者和日常登入的使用者都加。OS使用者同事也是informix使用者)

export INFORMIXSERVER=ifxserver
export INFORMIXDIR=/opt/informix
export ONCONFIG=onconfig.ifxserver
export INFORMIXSQLHOSTS=$INFORMIXDIR/etc/sqlhosts.ifxserver
export PATH=$INFORMIXDIR/bin:/usr/bin:${PATH}:.
export DB_LOCALE=zh_CN.utf8
export CLIENT_LOCALE=zh_CN.utf8

三、建立資料庫檔案的安裝目錄

su - informix
mkdir $INFORMIXDIR/dbs
touch $INFORMIXDIR/dbs/rootdbs
chmod 660 $INFORMIXDIR/dbs/rootdbs

四、資料庫軟體安裝  使用root使用者執行/mypkg/ids_install檔案進行安裝, 注意這裡從使用者informix 切換到 root 使用者時,su root 而不是su - root,這樣可以保留informix環境,站長站,安裝時不需要即可安裝到指定目錄: 安裝過程中選擇不建立例項,其他都選擇預設選項。 =============================================================================== Server Instance ---------------

Type 'back' to go to the previous step or 'quit' to cancel the installation.

Create a database server instance?

  ->1- Yes - create a server instance     2- No - do not create a server instance

ENTER THE NUMBER FOR YOUR CHOICE, OR PRESS <ENTER> TO ACCEPT THE DEFAULT:: 2

當然你的硬碟很大也可以選建立預設安裝例項demo_no。

五、資料庫初始化 回到informix使用者: 將$INFORMIXDIR/etc/目錄中的onconfig.std文件拷貝一份,命名為$ONCONFIG變數指定(onconfig.ifxserver)

cd $INFORMIXDIR/etc  
cp onconfig.std onconfig.ifxserver

,並編輯以下引數:

ROOTPATH  /opt/informix/dbs/rootdbs 
DBSERVERNAME  ifxserver
MIRRORPATH $INFORMIXDIR/tmp/ifxserver.root_mirror

將$INFORMIXDIR/etc/目錄中的sqlhosts.std文件拷貝一份,名稱為$INFORMIXSQLHOSTS變數指定的名稱(sqlhosts.ifxserver)  

cp $INFORMIXDIR/etc/sqlhosts.std $INFORMIXDIR/etc/sqlhosts.ifxserver

,並編輯為一下內容:

#demo_on	onipcshm	on_hostname	on_servername
ifxserver  	onsoctcp  	127.0.0.1    	9088

初始化資料庫:oninit -ivy

六、啟動停止資料庫

啟動資料庫oninit -vy

停止onmode -ky

七、使用dbaccess建立資料庫testdb

1、使用dbaccess進入informix互動環境

2、選擇Database,回車

3、選擇 Create,回車

4、輸入資料庫名:testdb,回車

5、選擇 Dbspace(表空間)  ,回車

6、選擇跟局資料資料庫bureaudb一樣的表空間,回車

7、選擇 Log ,回車

8、選擇 Log,回車(選擇日誌模式,其他不支援事務)

9、選擇 Exit,回車

10、選擇 Create-new-database ,回車。

此時資料庫應該已經建立。

八、表指令碼sqlfile.sql

create table customer 
  (
    num serial not null ,
    name char(15),
    create_time datetime year to fraction(3)
  );

insert into customer values (1,'蘿莉','2010-11-17 15:41:00.000');

dbaccess testdb sqlfile.sql 或者 dbaccess [email protected] sqlfile.sql

九、java

$INFORMIXDIR/lib/ifxjdbc.jar匯入到CLASSPATH(eclipse構建路徑)

import java.sql.*;

public class TestInformixsql {
    static String url = "jdbc:informix-sqli://127.0.0.1:9088/testdb";
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO 自動生成的方法存根
        Connection conn = null;
        try {
            Class.forName("com.informix.jdbc.IfxDriver");
            conn = DriverManager.getConnection(url, "mymotif", "wxwpxh");
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM customer");
            int col_count=st.getResultSet().getMetaData().getColumnCount();
            while (rs.next()) {
                for(int col=1;col<=col_count;col++){
                    System.out.print(rs.getString(col));
                    System.out.print("  ");
                }
                System.out.println();
            }
            rs.close();
            st.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
}

十、esqlc

demo1.ec

#include <string.h>
#include <stdio.h>

EXEC SQL define NAME_LEN	15;

int main()
{
EXEC SQL BEGIN DECLARE SECTION;
    int  num;
    char name[ NAME_LEN + 1 ];
EXEC SQL END DECLARE SECTION;

    printf( "DEMO1 Sample ESQL Program running.\n\n");
    EXEC SQL WHENEVER ERROR STOP;
    EXEC SQL connect to 'testdb';

    EXEC SQL declare democursor cursor for
	select num, name
	   into :num, :name
	   from customer
	   where num < 100;

    EXEC SQL open democursor;
    for (;;)
	{
	EXEC SQL fetch democursor;
	if (strncmp(SQLSTATE, "00", 2) != 0)
	    break;

	printf("%d %s\n",num, name);
	}

    if (strncmp(SQLSTATE, "02", 2) != 0)
        printf("SQLSTATE after fetch is %s\n", SQLSTATE);

    EXEC SQL close democursor;
    EXEC SQL free democursor;

    EXEC SQL disconnect current;
    printf("\nDEMO1 Sample Program over.\n\n");

   exit(0);
}

Makefile

# targets
PRG 	= demo1
INCL	=-I$(INFORMIXDIR)/incl -I$(INFORMIXDIR)/incl/esql
ESQL 	= $(INFORMIXDIR)/bin/esql
RPATH	= $(INFORMIXDIR)/lib:$(INFORMIXDIR)/lib/esql

$(PRG): $(PRG).ec
	$(ESQL) -o $(PRG) $(INCL) $(PRG).ec  -Wl,-rpath=$(RPATH)
clean:
	rm $(PRG) $(PRG).c

十一、c++

queryex1.cpp

#include <iostream>
#include <it.h>
#include <string>
using namespace std;

int main(int argc, char *argv[])
{
    if (argc != 1)
        {
        cout << "Usage: " << argv[0]  << endl;
        return 1;
        }

    // Make a connection using defaults
    ITConnection conn;
    conn.SetDBInfo(ITDBInfo(ITString("testdb")));
    conn.Open();

    // Create a query object
    ITQuery q(conn);

    if (argc != 1)
        {
        // Drop the table first; ignore errors if it didn't exist
        cout << "Usage:./queryex1" << endl;
    	conn.Close();
    	return 0;
        }

    // Does the table exist? If not, then create it.                // 1 begin
    ITRow *r1 = q.ExecOneRow(
        "select owner from systables where tabname = 'customer';");
    if ( !r1
         && (!q.ExecForStatus(
             "create table customer (    num serial not null ,name char(15),create_time datetime year to fraction(3));"
			     )
            ))
        {
        cerr << "Could not create table `customer'!" << endl;
        return 1;
        }                                                            // 1 end

    // Show the contents of the table                              // 2 begin
    cout << "These are the members of the Informix fan club, version ";
    ITValue *rel = q.ExecOneRow                                     
        ( "select owner from systables where tabname = ' VERSION';" );
    cout << rel->Printable() << " ALWAYS_DIFFERENT" << endl;
    rel->Release();                                                 

    ITSet *set = q.ExecToSet
        ("select * from customer order by 1;"); 
    if( !set )
    {
	    cout << "Query failed!" << endl;
	    conn.SetTransaction(ITConnection::Abort);
	    conn.Close();
	    return -1;
    }
    ITValue *v;
    while ((v = set->Fetch()) != NULL)
    {
        cout << v->Printable() << endl;
        v->Release();
    }                                                           
    set->Release();                                                 // 2 end

    cout << endl;

    conn.Close();

    return 0;
}

Makefile

CPPIFDIR	= $(INFORMIXDIR)
CCHOME		= /usr

CCPP		= $(CCHOME)/bin/g++
CCPLUS		= $(CCPP) $(HEADEROPTS)
CCPPFLAGS 	= -I$(INFORMIXDIR)/incl/dmi -I$(INFORMIXDIR)/incl \
	-I$(INFORMIXDIR)/incl/esql
CCPLINK		= $(CCPLUS) $(CCFLAGS) $(CCPPFLAGS)
CC		= $(CCHOME)/bin/gcc
C-COMPILE-FLAGS	= $(CCFLAGS)
CCDEFS		= -DLINUX -DIT_HAS_DISTINCT_LONG_DOUBLE \
	-DIT_COMPILER_HAS_LONG_LONG -DIT_DLLIB -DMITRACE_OFF -fPIC
CCFLAGS		= -g $(CCDEFS) -fsigned-char

ESQL		= $(INFORMIXDIR)/bin/esql

RANLIB		= echo

LOCALINCL	= -I$(CPPIFDIR)/incl/c++

LIBS_SYSTEM	= -lm -ldl -lcrypt -lnsl
LIBS_ESQL	= -L$(INFORMIXDIR)/lib/esql -L$(INFORMIXDIR)/lib -lifsql \
 -lifasf -lifgen -lifos -lifgls -lifglx $(INFORMIXDIR)/lib/esql/checkapi.o
LIBS_LIBMI	= -L$(INFORMIXDIR)/lib/dmi -lifdmi 
LIBS_CPPIF	= -L$(CPPIFDIR)/lib/c++ -lifc++
LIBS		= $(LIBS_CPPIF) $(LIBS_LIBMI) $(LIBS_ESQL) $(LIBS_SYSTEM) 
RPATH		= $(INFORMIXDIR)/lib:$(INFORMIXDIR)/lib/esql:$(INFORMIXDIR)/lib/dmi:$(INFORMIXDIR)/lib/c++

PROGRAMS	= queryex1
.SUFFIXES: .cc .o .hdr .cpp

.cc.o:
	@rm -f [email protected]
	$(CCPLUS) $(CCFLAGS) $(LOCALINCL) $(CCPPFLAGS) -c $<

.cpp.o:
	@rm -f [email protected]
	$(CCPLUS) $(CCFLAGS) $(LOCALINCL) $(CCPPFLAGS) -c $<

all: 	$(PROGRAMS)

queryex1: queryex1.o
	$(CCPLINK) -o queryex1 queryex1.o $(SUBSYSTEMS.link) $(LIBS) -Wl,-rpath=$(RPATH)

clean:
	$(RM) *.o $(PROGRAMS) core

=============================================================

邏輯日誌滿處理:

$ oninit -vy

停留在

Initializing DBSPACETEMP list...succeeded ------------啟動資料庫到這裡不動了。

檢視/opt/informix/tmp/online.log :

21:17:40  Process exited with return code 127: /bin/sh /bin/sh -c /opt/informix/etc/alarmprogram.sh 3 20 "Logical Logs are Full -- Backup is Needed." "Logical Log Files are Full -- Backup is Needed

發現邏輯日誌滿了,需要備份。

touch /opt/informix/dbs/logbackup1
chmod 660 /opt/informix/dbs/logbackup1
vi etc/onconfig.ifxserver
把:
LTAPEDEV /dev/tapedev
改為:
LTAPEDEV /opt/informix/dbs/logbackup1

ontape -a