嵌入式linux mongodb,小白在Ubuntu安装mongoDB与mongo-c-driver
目的:
本人從事嵌入式軟件,項目中需要使用MongoDB,最終需要熟悉c-driver的API,小白在搭建環境有些浪費時間,故寫這文章讓與我同樣狀態的開發人員學習學習。
在Ubuntu上的安裝mongodb
方法一:sudo apt-get ?install mongodb-server
測試是否安裝成功:
1.運行mongoDB服務命令(需要使用root權限):mongod
如有下輸出說明已經運行:
2.Mongodb后臺管理Shell(root下):mongo
有如下現象是正常工作:
方法二:官網下載對應位數的安裝包。
下載地址:https://www.mongodb.com/download-center#community
我使用的32為Ubuntu,所以下載的包為3.3.3版本。關于版本編號自行百度。
下載安裝包:mongodb-Linux-i686-3.0.1.tgz
解壓:tar zxvf mongodb-linux-i686-3.0.1.tgz
mv ?mongodb-linux-i686-3.0.1/ /usr/local/mongodb ? # 將解壓包拷貝到指定目錄
MongoDB 的可執行文件位于 bin 目錄下,所以可以將其添加到PATH 路徑中:
export PATH=/bin:$PATH
為你 MongoDB 的安裝路徑。如本文的 /usr/local/mongodb
建議將其寫入開機啟動腳本:~/.bashrc中。
創建數據庫目錄
MongoDB的數據存儲在data目錄的db目錄下,但是這個目錄在安裝過程不會自動創建,所以你需要手動創建data目錄,并在data目錄中創建db目錄。
以下實例中我們將data目錄創建于根目錄下(/)。
注意:/data/db 是 MongoDB 默認的啟動的數據庫路徑(--dbpath)。
mkdir -p /data/db
現在我們只需要找到/usr/local/mongodb/bin目錄下的:mongod與mongo,和上面的操作運行一樣。
注意:
1.大概解釋一下bin中文件:
mongod:暫且理解為服務器
mongo:暫且理解為可以輸入命令的客戶端。
mongodimport:導入數據
mongoexport:導出數據
網上教程還有使用一些參數設置
mongod --port 27017(默認)--dbpath /data/db ?--logpath /……….(不了解)
2.在后臺shell測試命令(mydb和mycoll是我自己創建的,剛剛安裝是沒有的,剛安裝可能只有local):
show ?dbs ?----》 查看已經存在的數據:
use mydb ?---》使用mydb數據庫,查看里面有幾個集合:
db.mycoll.find() ?----->查看mycoll這個集合中的文檔:
安裝mongo-c-driver
預安裝
需要先安裝依賴庫OpenSSL,來建立ssl連接到MongoDB
RedHat / Fedora系統:
$ sudo yum install pkg-config openssl-devel cyrus-sasl-devel
Debian / Ubuntu系統:
$ sudo apt-get install pkg-config libssl-dev libsasl2-dev
FreeBSD系統:
$ su -c 'pkg install pkgconf openssl cyrus-sasl2'
這里下載的MongoDB的C語言驅動是 mongo-c-driver-1.3.5.tar.gz。
解壓后打開mongo-c-driver-1.3.5目錄下的 README 文件,按其中講的方法安裝,如下:
# tar xzf ?mongo-c-driver-1.3.5.tar.gz
# cd mongo-c-driver-1.3.5
# ./configure
# make
# sudo make install
注意:有可能在安裝的時候有error,目前我測試在seeing虛擬機安裝失敗,在我自己的虛擬機安裝成功。安裝失敗可能原因:版本不懂。
官方參考鏈接:
http://mongoc.org/libmongoc/1.6.2/index.html
編寫連接MongoDB的程序 test.c
/*
* 創建連接
創建一個數據庫 ?mydb
創建一個集合 ?mycoll
**
**/
#include
#include
#include
int main (int ? argc, charchar *argv[])
{
mongoc_client_t ? ? ?*client;
mongoc_database_t ? ?*database;
mongoc_collection_t ?*collection;
bson_t ? ? ? ? ? ? ? *command,
reply,
*insert;
bson_error_t ? ? ? ? ?error;
char ? ? ? ? ? ? ? ? *str;
bool ? ? ? ? ? ? ? ? ?retval;
/*
* Required to initialize libmongoc's internals
*/
mongoc_init();
/*
* Create a new client instance
*/
client = mongoc_client_new ("mongodb://localhost:27017");
/*
* Register the application name so we can track it in the profile logs
* on the server. This can also be done from the URI (see other examples).
*/
//mongoc_client_set_appname (client, "connect-example");
/*
創建一個集合db_name ?集合中的表叫coll_name(暫且這樣叫吧)
* Get a handle on the database "mydb" and collection "mycoll"
*/
database = mongoc_client_get_database (client, "mydb");//獲取或者創建數據庫的名稱
collection = mongoc_client_get_collection (client, "mydb", "mycoll"); //在這個db_name中獲取或者創建一個cioll_name的集合
/*
* Do work. This example pings the database, prints the result as JSON and
* performs an insert
*/
command = BCON_NEW ("ping", BCON_INT32(1));//如何連接ping通了,就執行插入操作。
retval = mongoc_client_command_simple (client, "admin", command, NULL, &reply, &error);
if (!retval) {
fprintf (stderr, "%s\n", error.message);
return EXIT_FAILURE;
}
str = bson_as_json (&reply, NULL);
//printf ("line %d\n", __LINE__);
printf ("%s\n", str);
//printf ("line %d\n", __LINE__);
insert = BCON_NEW ("hello", BCON_UTF8 ("world"));
if (!mongoc_collection_insert (collection, MONGOC_INSERT_NONE, insert, NULL, &error)) {
fprintf (stderr, "%s\n", error.message);
}
bson_destroy (insert);
bson_destroy (&reply);
bson_destroy (command);
bson_free (str);
/*
* Release our handles and clean up libmongoc
*/
mongoc_collection_destroy (collection);
mongoc_database_destroy (database);
mongoc_client_destroy (client);
mongoc_cleanup ();
return 0;
}
編譯test.c
# gcc -o test test.c -I/usr/local/include/libmongoc-1.0-I/usr/local/include/libbson-1.0/ -lmongoc-1.0 -lbson-1.0
如果在運行的時候報錯找不到動態鏈接庫,需要檢查一下是否安裝正確。
正常安裝的話會有以下庫:libbson.so------>mongodb解析的數據為bson數據,類似json。
其中libbson是附帶的捆綁的庫,系統若無,腳本會自動安裝。
如果運行成功輸出:{ "ok" : 1 }
在shell后臺使用以上命令:(我們這創建了一個mydb數據庫,在這個數據庫中添加了一個集合,在這個集合添加一條文檔 hello:“world”)
查看mongo-c-driver官方API使用
官方參考鏈接:http://mongoc.org/libmongoc/1.6.2/api.html
在運行代碼之后,需要在后臺shell查看是否有增添改查。之后還會更詳細的講解增刪改查的api使用。個人感覺官方給的例子沒有解釋代碼意思,需要查看文檔。
總結
以上是生活随笔為你收集整理的嵌入式linux mongodb,小白在Ubuntu安装mongoDB与mongo-c-driver的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 100W超级快充加持!华为nova 10
- 下一篇: 编程题【System类】计算一千万个数添