2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Docker で mongodb 環境を用意して接続するだけ

Posted at

そもそも mongodb の環境ってどうやって作るの?となったのでおぼえがき。

docker-compose の用意

docker-compose で書くのが好きなので

version: "3"
services:
  mongo:
    image: mongo
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: password
    ports:
      - 27017:27017

コンテナの起動

外部から接続したい時は

$ docker-compose up -d

もしくは

$ docker-compose up

内部でいろいろ試したい時は

$ docker-compose run --rm --service-ports mongo /bin/bash

--service-ports をつけておくと外部にポートが公開される。)

mongodb の起動

以降は 内部でいろいろ試したい時は の環境についてのみ記載。特に言及しない限り、コンテナ内での操作とする。

起動

/# mongod
なんやかんや

以降の操作は別プロセスで

デーモンで

/# mongod --fork --logpath /var/log/mongodb/mongod.log
about to fork child process, waiting until server is ready for connections.
forked process: 50
child process started successfully, parent exiting

接続

一応プロセスの確認

/# ps aux | grep mongo
root       50  1.6  0.9 1579820 97448 ?       Sl   16:36   0:01 mongod --fork --logpath /var/log/mongodb/mongodb.log

大丈夫そうなので接続

/# mongo

いろいろ操作

db 切り替え(作成)

> use sample
switched to db sample

insert (collection 作成)

> db.sample_collection.insert({id: 1, name: "taro"})
WriteResult({ "nInserted" : 1 })

find

> db.sample.find()
{ "_id" : ObjectId("5f107fa8182d6aa721d344fe"), "id" : 1, "name" : "taro" }

exit

> ^C
bye

ログをみてみる

/# cat /var/log/mongodb/mongodb.log
いろいろ書いてあった

mongo プロセスの終了

/# mongod --shutdown
2020-07-16T16:31:30.899+0000 I  CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2020-07-16T16:31:30.902+0000 W  ASIO     [main] No TransportLayer configured during NetworkInterface startup
killing process with pid: 50

参考

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?