원문 : http://mongocxx.org/mongocxx-v3/tutorial/

참고하도록 


Qt 프레임워크를 통해 MongoDB API 를 사용해보자``


먼저 QtCreator 를 사용하기전에 환경변수를 추가한다. 


$ vim ~/.bashrc

export PATH=$PATH:/opt/pc_qt5.11.1/bin

export PATH=~/bin:/opt/arm-2013.11/bin:/home/kj/DB/mongodb-linux-x86_64-ubuntu1804-4.0.4/bin:$PATH


1. qt 프로젝트 생성

2. qt Build Environment 에 몽고디비가 설치된 환경변수를 생성한다.

 LD_LIBRARY_PATH  = /home/kj/DB/client/lib

PKG_CONFIG_PATH t=/home/kj/DB/client/lib/pkgconfig


3. Application 작성


1). project.pro 파일 맨 아래에 다음 옵션 추가

CONFIG += link_pkgconfig
PKGCONFIG += libmongocxx libbsoncxx


2). 헤더파일 추가

#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/stdx.hpp>
#include <mongocxx/uri.hpp>
#include <mongocxx/instance.hpp> 
#include <QDebug>
#include <iostream>
#include <bsoncxx/builder/stream/document.hpp>


 

3) 소스 코드 작성


    mongocxx::instance inst{}; mongocxx::client conn{mongocxx::uri{}}; bsoncxx::builder::stream::document document{}; auto collection = conn["testdb1"]["testcollection"]; document << "hello" << "world3"; collection.insert_one(document.view()); auto cursor = collection.find({}); for (auto&& doc : cursor) { qDebug()<< QString::fromStdString(bsoncxx::to_json(doc)); } 

 


실행시 DB 에 데이터가 한개씩 추가되는 것을 알 수 있다.




Qt 에서 API 테스트 끝.

qt 버전 5.10.0


테스트 어플리케이션 첨부

MongoDB.tar.gz



'Database' 카테고리의 다른 글

[Docker] MySQL 설치하기  (0) 2021.03.22
[MongoDB] 관련 정리  (0) 2018.12.19
[MongoDB] mongocxx driver 설치  (0) 2018.12.19
[MongoDB] Ubuntu 18.04 설치하기  (0) 2018.12.19
[MongoDB] 설치하기 (webpage)  (0) 2018.12.19

아쉣더 날라가다니...






 cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/home/kj/DB/client ../


-- Auto-configuring bsoncxx to use MNMLSTC for polyfills since C++17 is inactive

CMake Error at src/bsoncxx/CMakeLists.txt:81 (find_package):

  Could not find a configuration file for package "libbson-1.0" that is

  compatible with requested version "1.13.0".


  The following configuration files were considered but not accepted:


    /usr/lib/x86_64-linux-gnu/cmake/libbson-1.0/libbson-1.0-config.cmake, version: 1.9.2



libbson 버전을 변경해야 될거 같다. 현재 사용버전은 1.9.2  -> 1.13





오 생성됨 

   "/home/kj/DB/Driver/libbson/mongo-c-driver-1.12.0/build/src/libbson/libbson-1.0.so.0.0.0"

  to "/usr/local/lib/libbson-1.0.so.0.0.0".


가보자 이제 다시 젤첨 코드를 빌드한다.


아앗 1.13 이아니라 1.12 를 받았다. ......................

다시 다운로드  

$ wget https://github.com/mongodb/mongo-c-driver/releases/download/1.13.0/mongo-c-driver-1.13.0.tar.gz


빌드진행 설치 ->>>

  cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/home/kj/DB/client ../


서설치가 되었다.!!!



정리해보면 다음과 같다.

mongo-c-driver-1.13.0 설치후 mongo-cxx-driver 를 설치한다.



설치된 경로 확인


/home/kj/DB/client 


패키지 pc 파일 정보 : /home/kj/DB/client/lib/pkgconfig

 - libbsoncxx.pc  libmongocxx.pc



코드 테스트해보도록 하자 : 예제참고링크


환경변수 등록



 

export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/home/kj/DB/client/lib/pkgconfig

c++ --std=c++11 test.cpp -o test $(pkg-config --cflags --libs libmongocxx)

 ./test 

./test: error while loading shared libraries: libmongocxx.so._noabi: cannot open shared object file: No such file or directory



export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/kj/DB/client/lib



./test 

{ "_id" : { "$oid" : "5c19ea2ebdf13c4bca573d92" }, "hello" : "world" }


동작 확인 완료

..



기본환경변수 패스에 추가해 준다. vim ~/.bashrc


export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/home/kj/DB/client/lib/pkgconfig

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/kj/DB/client/lib



테스트 코드는 아래 링크를 따라가세요.

[MongoDB] API 튜토리얼 



'Database' 카테고리의 다른 글

[Docker] MySQL 설치하기  (0) 2021.03.22
[MongoDB] 관련 정리  (0) 2018.12.19
[MongoDB] API 튜토리얼  (0) 2018.12.19
[MongoDB] Ubuntu 18.04 설치하기  (0) 2018.12.19
[MongoDB] 설치하기 (webpage)  (0) 2018.12.19


몽고DB 설치하기


참고 페이지 : https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu-tarball/



나의 우분투버전은 Ubuntu 18.04 (Bionic 버전이다.)



MongoDB 를 설치하기위해서는 아래 패키지를 설치한다.


 sudo apt-get install libcurl4 openssl



1. 설치 파일 다운로드 :https://www.mongodb.com/download-center/community?jmp=docs



 2. 설치방법


1) 압축 파일 해제

  /home/db  $ tar xvf mongodb-linux-x86_64-ubuntu1804-4.0.4.tgz

2) 실행 폴더 환경변수 등록

export PATH=$PWD: (Mongodb/bin 패스)


나의 경우는 export PATH=$PWD:/home/kj/DB/mongodb-linux-x86_64-ubuntu1804-4.0.4/bin

환경변수 ~/.bashrc 파일안에 설정하였다.

3) data 와 log 디렉토리 생성

 sudo mkdir -p /MongoDB/db

 sudo mkdir -p /var/log/mongodb


4). 실행

sudo ./mongod --dbpath /MongoDB/db --logpath /var/log/mongodb/mongod.log --fork

 2018-12-19T15:06:28.614+0900 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'

about to fork child process, waiting until server is ready for connections.

forked process: 5739

child process started successfully, parent exiting




5.) ./mongo 를 통해 접속가능한지 확인

MongoDB shell version v4.0.4

connecting to: mongodb://127.0.0.1:27017

Implicit session: session { "id" : UUID("d1753a27-7745-47ef-9693-8696ca35a337") }

MongoDB server version: 4.0.4

Server has startup warnings: 

2018-12-19T15:06:28.622+0900 I STORAGE  [initandlisten] 

2018-12-19T15:06:28.622+0900 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine

2018-12-19T15:06:28.622+0900 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem

2018-12-19T15:06:29.847+0900 I CONTROL  [initandlisten] 

2018-12-19T15:06:29.847+0900 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.

2018-12-19T15:06:29.847+0900 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.

2018-12-19T15:06:29.847+0900 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.

2018-12-19T15:06:29.847+0900 I CONTROL  [initandlisten] 

2018-12-19T15:06:29.847+0900 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.

2018-12-19T15:06:29.847+0900 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server. 

2018-12-19T15:06:29.847+0900 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP 

2018-12-19T15:06:29.847+0900 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to

2018-12-19T15:06:29.847+0900 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the

2018-12-19T15:06:29.847+0900 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.

2018-12-19T15:06:29.847+0900 I CONTROL  [initandlisten] 

---

Enable MongoDB's free cloud-based monitoring service, which will then receive and display

metrics about your deployment (disk utilization, CPU, operation statistics, etc).


The monitoring data will be available on a MongoDB website with a unique URL accessible to you

and anyone you share the URL with. MongoDB may use this information to make product

improvements and to suggest MongoDB products and deployment options to you.


To enable free monitoring, run the following command: db.enableFreeMonitoring()

To permanently disable this reminder, run the following command: db.disableFreeMonitoring()




사용자 를 등록해준다.

> usb admin

2018-12-19T15:11:15.962+0900 E QUERY    [js] SyntaxError: missing ; before statement @(shell):1:4

> use admin

switched to db admin

> db.createUser({user: "root", pwd: "XXX!",roles:[{role: "root",db:"admin"}]});

Successfully added user: {

"user" : "root",

"roles" : [

{

"role" : "root",

"db" : "admin"

}

]

}



활용 테스트는 나중에. 

'Database' 카테고리의 다른 글

[Docker] MySQL 설치하기  (0) 2021.03.22
[MongoDB] 관련 정리  (0) 2018.12.19
[MongoDB] API 튜토리얼  (0) 2018.12.19
[MongoDB] mongocxx driver 설치  (0) 2018.12.19
[MongoDB] 설치하기 (webpage)  (0) 2018.12.19

+ Recent posts