MongoDB 2.x を LinuxMint12 にインストールする

Ruby スクリプトから mongoid を利用して MongoDB 1.8 を操作しようとした時にエラーが出たので対応メモ。

環境

  • OS: LinuxMint 12 Lisa 64bit
  • MongoDB:
    • 1.8.2
    • 2.0.6
  • Ruby: 1.9.3
  • RubyGems: 1.8.24
  • mongoid: 2.4.11

テストプログラム

単純に MongoDB に接続してデータを入れた後、一件取るだけのRubyスクリプトを書きました。

mongodb_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/usr/bin/env ruby
## -*- coding: utf-8 -*-

require 'rubygems'
require "mongoid"

# MongoDB の接続設定
Mongoid.configure do |conf|
conf.master = Mongo::Connection.new('localhost').db('mongoid-test')
end

# ユーザモデル
class User
include Mongoid::Document
field :name, :type => String
field :age, :type => Integer
end

# ユーザデータを作成
User.new(
:name => "Kiichi Nakai",
:age => "35"
).save

# ユーザデータを一件取得して表示
p User.first

プログラムを実行して接続テストしてみる

mongodb_test.rb を実行してみます。

Terminal
1
2
3
4
5
6
$ ruby mongodb_test.rb
/home/nanigashi/.rvm/gems/ruby-1.9.3-p194@mongodb_test/gems/mongoid-2.4.11/lib/mongoid/config.rb:266:in `check_database!': MongoDB 1.8.2 not supported, please upgrade to 2.0.0. (Mongoid::Errors::UnsupportedVersion)
from /home/nanigashi/.rvm/gems/ruby-1.9.3-p194@mongodb_test/gems/mongoid-2.4.11/lib/mongoid/config.rb:207:in `master='
from mongodb_test.rb:8:in `block in <main>'
from /home/nanigashi/.rvm/gems/ruby-1.9.3-p194@mongodb_test/gems/mongoid-2.4.11/lib/mongoid.rb:116:in `configure'
from mongodb_test.rb:7:in `<main>'

1.8.2 はサポートしてないので 2.0.0 にアップグレードしろというエラーが起こったようです。
念の為バージョン確認。

Terminal
1
2
$ mongo --version
MongoDB shell version: 1.8.2

確かに 1.8.2 なので 2.x 系にバージョンをあげたいと思います。

MongoDB を 1.8 から 2.xにあげる

DebianまたはUbuntu LinuxにMongoDBをインストール を参考にやっていきます。

まず、現在インストールされている MongoDB を削除します。

Terminal
1
$ sudo aptitude remove mongodb

アンインストール後、公開鍵をいれます。
まずは公式に載ってる方法を試します。

Terminal
1
2
3
4
5
6
7
$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
Executing: gpg --ignore-time-conflict --no-options --no-default-keyring --secret-keyring /tmp/tmp.C3N2XUyIEN --trustdb-name /etc/apt/trustdb.gpg --keyring /etc/apt/trusted.gpg --primary-keyring /etc/apt/trusted.gpg --keyserver keyserver.ubuntu.com --recv 7F0CEB10
gpg: 鍵7F0CEB10をhkpからサーバーkeyserver.ubuntu.comに要求
?: keyserver.ubuntu.com: Connection refused
gpgkeys: HTTP fetch error 7: couldn't connect: Connection refused
gpg: 有効なOpenPGPデータが見つかりません。
gpg: 処理数の合計: 0

有効なOpenPGPデータが見つかりません。 となってエラーになります。
今日の時点では keyserver.ubuntu.com には存在しないみたいなので、公式で公開してるものを直接取り込むことにします。

Terminal
1
$ wget -O - http://docs.mongodb.org/10gen-gpg-key.asc | sudo apt-key add -

無事とりこめたので /etc/apt/sources.list の最後にリポジトリを追記します。

/etc/apt/sources.list
1
2
# 10gen repository
deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen

update すると mongodb の他に mongodb-10gen というのが追加されます。

Terminal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$ sudo aptitude update
$ aptitude show mongodb-10gen
パッケージ: mongodb-10gen
新規: yes
状態: インストールされていません
バージョン: 2.0.6
優先度: 任意
セクション: devel
メンテナ: Richard Kreuter <richard@10gen.com>
展開サイズ: 86.3 M
依存: libc6 (>= 2.3.2), libgcc1 (>= 1:4.1.1), libstdc++6 (>= 4.1.1)
競合: mongodb, mongodb, mongodb-10gen-unstable, mongodb-10gen-unstable, mongodb-nightly, mongodb-nightly, mongodb-stable, mongodb-stable, mongodb-unstable, mongodb-unstable,
mongodb-10ge
説明: An object/document-oriented database
MongoDB is a high-performance, open source, schema-free document-oriented data store that's easy to deploy, manage and use. It's network accessible, written in C++ and offers the
following features :

* Collection oriented storage - easy storage of object- style data
* Full index support, including on inner objects
* Query profiling
* Replication and fail-over support
* Efficient storage of binary data including large objects (e.g. videos)
* Auto-sharding for cloud-level scalability (Q209)

High performance, scalability, and reasonable depth of functionality are the goals for the project.
ホームページ: http://www.mongodb.org

バージョンが 2.x 以上になってるのを確認したのでインストールします。

Terminal
1
$ sudo aptitude install -V mongodb-10gen

念の為バージョンを確認。

Terminal
1
2
$ mongo --version
MongoDB shell version: 2.0.6

再度 mongodb_test.rb を実行します。

Terminal
1
2
$ ruby mongodb_test.rb
#<User _id: 4fd93b250ee5cd1214000001, _type: nil, name: "Kiichi Nakai", age: 35>

無事 insertfind が実行できたようです。

以上です。

関連資料

© 2024 磁力式駆動 All Rights Reserved.
Theme by hiero