FireBirdSQL 小型(崁入型)資料庫 初探、下載、建立、設定


常常會有些時候,開發一些工具程式會需要自帶資料庫的時候。
在10年前,那時候最好的一些開發工具選擇可能還是 Foxpro 吧。
但是自從微軟不再對 Foxpro 做更新時,就已經是末路之流了。

近年來,桌機上的商用主流依然是 Windows 無誤,開發工具從我孰悉的 powerbuilder 漸漸轉向了 C# 。

雖然 powerbuilder 沒辦法使用 Embedded Database ,然而業務開發又需要提供具有資料庫能力的『單機版』程式,雖然說是單機版,但實際使用上通常都會在 3 ~ 5人左右的程式。

因為公司是希望開發好的程式/資料庫可以打包一起,然後是由不是很懂 IT 的業務人員去客戶那邊安裝(最好能一鍵安裝或傻瓜安裝就搞定)的。

MS SQL express 是不錯的選擇,但是太過肥大,安裝又是囉哩叭縮,要包成自動安裝難度太高,如果又要網路共享需要比較懂得人才有辦法去設定啊,也不是一般業務人員可以搞定啊。

雖然,MySQL、 、MariaDB 也是不錯,但還要安裝 ODBC 來分享這也不好搞的。

SQL lite 太過單機,小型群組分享資料庫是做不到了,還得透過 API 指令。

Access DB、Excel table 等太過精簡,操作一個不小心未正常關檔,檔案就掛了,欲哭無淚。Excel table 資料庫沒有加密,等於裸奔狀態。

游網之餘,看到了這個 FireBirdSQL ,其前身是 Interbase 也算元老級的,但支援小群組共同使用,支援加密、StoreProcedure、可以當 Embedded DB 也可以當 C/S 資料庫,雖然效能不怎樣,但是這樣在小程式上真的綽綽有餘,所以趕快來研究看看吧。

FireBirde官方網站

一、下載主程式
本篇討論使用的是 Firebird 3.0.4 版 ,由於需要能夠被打包所以就下載這個 ZIP 壓縮檔版本


為什麼下載 32-bit 版本? 因為沒辦法保證客戶使用的 Windows 平台是 64-bit 的,所以保險起見就用 32-bit 來執行。

下載 ZIP 檔案回來後解壓縮後(通常會放在 C:\FB3 ,實際依需求去放),就可以開始設定了。

二、建立一個放資料檔案的路徑 (我的範例是 D:\NEWDB)

三、建立資料庫檔案
到 FB3 路徑下執行 isql
C:\FB3\isql↵
Use CONNECT or CREATE DATABASE to specify a database


然後創建資料庫,創件資料庫時會使用系統管理帳號SYSDBA,而它的密碼預設則是 masterkey,當正式上線前記得要變更密碼避免被人猜到。
SQL>CREATE DATABASE 'D:\NEWDB\test.fdb' page_size 8192↵
CON>user 'SYSDBA' password 'masterkey';↵

完成後,驗證一下資料庫是否可以查詢
SQL>SELECT * FROM RDB$RELATIONS;↵

此時畫面會跑出許多內容,這些內容並不是資料表,而是一個資料庫最基本的內容- Metadata。
確認沒問題以後,就可以離開 isql 指令模式
SQL>QUIT;↵

※參考 Creating a database using isql

此時,資料庫就會有一個預設帳號 SYSDBA,這個 SYSDBA 擁有非常大的權限,屆時如果要操作資料庫內的資料表,就比較建議另外再建立一個新的 user 帳號來使用比較保險。

這時候資料庫雖然已經建立好,但裡面是『空』的,沒有任何資料表,因此我們接下來就是先把資料庫掛起來,再來建立資料表。

四、設定資料庫
在 FB3 路徑下會有一個 Firebird.conf 的設定檔,裡面有個設定 WireCrypt 要先改一下
# Should connection over the wire be encrypted?
# Has 3 different values: Required, Enabled or Disabled. Enabled behavior
# depends on the other side's requirements. If both sides are set to Enabled,
# the connection is encrypted when possible. Note that Wirecrypt should be set
# to Enabled when running a Firebird server with legacy authentication.
#
# Attention: default depends upon connection type: incoming (server)
#            or outgoing (client).
#
# Per-connection configurable.
#
# Type: string (predefined values)
#
#WireCrypt = Enabled (for client) / Required (for server)
WireCrypt = Enabled

由於我門設這資料庫會使用 TCP/IP來連線或是提供其他電腦來連線,所以連線時會走加密資料因此必須設定 WireCrypt 開啟。

五、安裝資料庫服務
由於我們希望這個資料庫能夠開機後自動執行,因此必須把它掛載為服務器。

在 FB3 底下 有個 install_service.bat 的批次檔,請用 具有 windows 管理者身分的使用者來執行才能正常安裝為服務。
C:\FB3\install_service↵
instreg version WI-V3.0.4.33054 Firebird 3.0
Firebird has been successfully installed in the registry.
instsvc version WI-V3.0.4.33054 Firebird 3.0
Service "Firebird Server - DefaultInstance" successfully created.
Service "Firebird Server - DefaultInstance" successfully started.

上面指令執行後,會在 Windows 服務中多出一個服務 Firebird Server - DefaultInstance


它預設使用 DefaultInstance 當作實例名稱。

如果,你希望使用自己的實例名稱 ,例如 MyInstance,可以把名稱加在 install_service 後面
C:\FB3\install_service MyInstance↵

如果要移除服務,可以用 uninstall_service.bat 來做
C:\FB3\uninstall_service↵
Service "Firebird Server - DefaultInstance" successfully stopped.
instsvc version WI-V3.0.4.33054 Firebird 3.0
Service "Firebird Server - DefaultInstance" successfully deleted.
instreg version WI-V3.0.4.33054 Firebird 3.0
Firebird has been successfully deleted from the registry.

或帶實例名稱的移除
C:\FB3\install_service MyInstance↵

服務安裝完後會自動啟動該服務,你可以從 netstat -a 來查看通訊埠口是否開啟,Firebird  的監聽埠號預設為 3050。

如果希望變更埠號,可以先停用 Firebird 服務後,修改 Firebird.conf 的 RemoteServicePort
# ----------------------------
# TCP Protocol Settings
#
# The TCP Service name/Port number to be used for client database
# connections.
#
# It is only necessary to change one of the entries, not both.  The
# order of precendence is the 'RemoteServiceName' (if an entry is
# found in the 'services.' file) then the 'RemoteServicePort'.
#
# Per-connection configurable.
#
# Type: string, integer
#
#RemoteServiceName = gds_db
RemoteServicePort = 3051

重新啟用服務後,就會以新的設定執行。

六、 Windows 防火牆
如果你的 Windows 自帶的防火牆有開啟,可能會阻礙其他電腦跟 Firebird 的連線,所以要調整防火牆設定。

加入新的『輸入規則』

選擇 TCP/IP 協定

設定 TCP 連接埠 3050 ,這是Firebird SQL server 的預設埠口

然後允許這個埠口的連線

設定連線來源,依照使用範圍慎選!

設定名稱

設定好以後就可以在規則裡面看到了,此時就可以讓其他電腦連線進來


七、建立資料表
嘛.....我算是個懶人,不太想用 isql 指定建立,所以我用了 Dbeaver 這套第三方軟體來建立。
本篇不是講 Dbeaver 所以不會介紹太多它的用法。

Dbeaver 官方

開啟 Dbeaver,然後新增一個 Firebird 連線

輸入連線資訊,此時我用的帳號就是 test.fdb 資料庫的建立帳號 SYSDBA 來連線

如果沒有問題,就可以連線成功,此時就可以建立自己的資料表




八、關於封裝
當把 這個 Firebird 3 設定好之後就可以把整個 Firebird 資料夾壓縮起來,和自建的資料庫目錄一起打包到封裝軟體上。到客戶端解封裝時可以一並解開到指定路徑去,然後讓封裝程式到 Firebird 資料夾內去執行 install_service.bat 即可完成單機部屬。

若是要讓其他電腦連線,主要要檢查防火牆的設定並適當修改即可使用。


算是相當簡便的小型資料庫。



留言

這個網誌中的熱門文章

【研究】列印的條碼為什麼很難刷(掃描)

C# 使用 Process.Start 執行外部程式

統一發票列印小程式