日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

嵌入式linux mongodb,小白在Ubuntu安装mongoDB与mongo-c-driver

發(fā)布時間:2023/12/10 linux 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 嵌入式linux mongodb,小白在Ubuntu安装mongoDB与mongo-c-driver 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

目的:

本人從事嵌入式軟件,項目中需要使用MongoDB,最終需要熟悉c-driver的API,小白在搭建環(huán)境有些浪費時間,故寫這文章讓與我同樣狀態(tài)的開發(fā)人員學習學習。

在Ubuntu上的安裝mongodb

方法一:sudo apt-get ?install mongodb-server

測試是否安裝成功:

1.運行mongoDB服務命令(需要使用root權限):mongod

如有下輸出說明已經(jīng)運行:

2.Mongodb后臺管理Shell(root下):mongo

有如下現(xiàn)象是正常工作:

方法二:官網(wǎng)下載對應位數(shù)的安裝包。

下載地址: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 的可執(zhí)行文件位于 bin 目錄下,所以可以將其添加到PATH 路徑中:

export PATH=/bin:$PATH

為你 MongoDB 的安裝路徑。如本文的 /usr/local/mongodb

建議將其寫入開機啟動腳本:~/.bashrc中。

創(chuàng)建數(shù)據(jù)庫目錄

MongoDB的數(shù)據(jù)存儲在data目錄的db目錄下,但是這個目錄在安裝過程不會自動創(chuàng)建,所以你需要手動創(chuàng)建data目錄,并在data目錄中創(chuàng)建db目錄。

以下實例中我們將data目錄創(chuàng)建于根目錄下(/)。

注意:/data/db 是 MongoDB 默認的啟動的數(shù)據(jù)庫路徑(--dbpath)。

mkdir -p /data/db

現(xiàn)在我們只需要找到/usr/local/mongodb/bin目錄下的:mongod與mongo,和上面的操作運行一樣。

注意:

1.大概解釋一下bin中文件:

mongod:暫且理解為服務器

mongo:暫且理解為可以輸入命令的客戶端。

mongodimport:導入數(shù)據(jù)

mongoexport:導出數(shù)據(jù)

網(wǎng)上教程還有使用一些參數(shù)設置

mongod --port 27017(默認)--dbpath /data/db ?--logpath /……….(不了解)

2.在后臺shell測試命令(mydb和mycoll是我自己創(chuàng)建的,剛剛安裝是沒有的,剛安裝可能只有l(wèi)ocal):

show ?dbs ?----》 查看已經(jīng)存在的數(shù)據(jù):

use mydb ?---》使用mydb數(shù)據(jù)庫,查看里面有幾個集合:

db.mycoll.find() ?----->查看mycoll這個集合中的文檔:

安裝mongo-c-driver

預安裝

需要先安裝依賴庫OpenSSL,來建立ssl連接到MongoDB

RedHat / Fedora系統(tǒng):

$ sudo yum install pkg-config openssl-devel cyrus-sasl-devel

Debian / Ubuntu系統(tǒng):

$ sudo apt-get install pkg-config libssl-dev libsasl2-dev

FreeBSD系統(tǒng):

$ su -c 'pkg install pkgconf openssl cyrus-sasl2'

這里下載的MongoDB的C語言驅(qū)動是 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

/*

* 創(chuàng)建連接

創(chuàng)建一個數(shù)據(jù)庫 ?mydb

創(chuàng)建一個集合 ?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");

/*

創(chuàng)建一個集合db_name ?集合中的表叫coll_name(暫且這樣叫吧)

* Get a handle on the database "mydb" and collection "mycoll"

*/

database = mongoc_client_get_database (client, "mydb");//獲取或者創(chuàng)建數(shù)據(jù)庫的名稱

collection = mongoc_client_get_collection (client, "mydb", "mycoll"); //在這個db_name中獲取或者創(chuàng)建一個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通了,就執(zhí)行插入操作。

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

如果在運行的時候報錯找不到動態(tài)鏈接庫,需要檢查一下是否安裝正確。

正常安裝的話會有以下庫:libbson.so------>mongodb解析的數(shù)據(jù)為bson數(shù)據(jù),類似json。

其中l(wèi)ibbson是附帶的捆綁的庫,系統(tǒng)若無,腳本會自動安裝。

如果運行成功輸出:{ "ok" : 1 }

在shell后臺使用以上命令:(我們這創(chuàng)建了一個mydb數(shù)據(jù)庫,在這個數(shù)據(jù)庫中添加了一個集合,在這個集合添加一條文檔 hello:“world”)

查看mongo-c-driver官方API使用

官方參考鏈接:http://mongoc.org/libmongoc/1.6.2/api.html

在運行代碼之后,需要在后臺shell查看是否有增添改查。之后還會更詳細的講解增刪改查的api使用。個人感覺官方給的例子沒有解釋代碼意思,需要查看文檔。

總結

以上是生活随笔為你收集整理的嵌入式linux mongodb,小白在Ubuntu安装mongoDB与mongo-c-driver的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。