4
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 5 years have passed since last update.

IPv6ネットワーク経由で外部のMongoDBに接続する

Posted at

バックエンドのDBにもグローバルIPv4を振っていたので、せっかくIPv6は潤沢にあるし使ってみようということでIPv6に変更しました。
MongoDBはデフォルトでIPv6が無効になっているらしいので有効にします。

  • OS
  • Ubuntu 14.04
  • MongoDB
  • 2.6.5

設定

mongod.confでIPv6を有効にするオプションを書く必要があります。
ipv6 = trueが有効化のオプションです(多分)。
さらに、デフォルトだとbind_ip = 127.0.0.1となっていて外部から利用できないのでlistenしたいIPアドレスを書きます。
もしくはどのインタフェースでもlistenするのであればbind_ipの行をコメントアウトしてもよいです。
設定を変えたらあとは再起動するだけです。

# vim /etc/mongod.conf
logpath=/var/log/mongodb/mongod.log
logappend=true
port = 27017
+ ipv6 = true
# service mongod restart
Restarting database: mongod.

これで使えるはず、と思って外部からアクセスしてみたのですが、一向に利用できませんでした。

# lsof -i:27017
COMMAND  PID    USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
mongod  8434 mongodb    8u  IPv4  12214      0t0  TCP *:27017 (LISTEN)
mongod  8434 mongodb   11u  IPv6  12215      0t0  TCP *:27017 (LISTEN)

うーんやはり全インタフェースできちんとLISTENになっている。
と少し悩んで、iptablesが原因であることに気づきました。
IPv6なのでip6tablesですね。
この27017番ポートを開放します。

$ ip6tables -A INPUT -p tcp --dport 27017 -j ACCEPT

これで外部から利用できるはずです。

クライアントのコマンド

余談ですが、クライアント側のコマンドも--ipv6を付けないとIPv6でアクセスしてくれません。
IPv6のアドレスと書いているところは適当に読み替えて下さい。

$ mongo IPv6のアドレス:27017/test
MongoDB shell version: 2.4.9
connecting to: IPv6のアドレス:27017/test
Thu Oct 23 02:48:09.538 getaddrinfo("IPv6のアドレス") failed: Address family for hostname not supported
Thu Oct 23 02:48:09.539 Error: couldn't connect to server IPv6のアドレス:27017 at src/mongo/shell/mongo.js:147
$ mongo IPv6のアドレス:27017/test --ipv6
MongoDB shell version: 2.4.9
connecting to: IPv6のアドレス:27017/test
>
4
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
4
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?