m_shige1979のささやかな抵抗と欲望の日々

プログラムの勉強をしながら学習したことや経験したことをぼそぼそと書いていきます

なんとなく作ったサイト

http://www.it-check-matome.info/


Github(注意すること)

https://github.com/mshige1979

mongodbをインストールして、phpでアクセス

mongodbをインストールして、phpでアクセス

インストール参考

http://docs.mongodb.org/manual/tutorial/install-mongodb-on-red-hat-centos-or-fedora-linux/
※あまり参考にならないけど

環境

VMPlayer
 ・CentOS 6.4
  ・Apache
  ・mongodb
  ・PHP5.5.1

Linuxへmongodbをインストール

/etc/yum.repos.d/10gen.repoを作成
[10gen]
name=10gen Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64
gpgcheck=0
enabled=1
「yum update」でインストール
[root@localhost ~]# yum update
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.iij.ad.jp
 * extras: ftp.iij.ad.jp
 * updates: ftp.iij.ad.jp
10gen                                                                                     |  951 B     00:00
10gen/primary                                                                             |  11 kB     00:00
10gen                                                                                                      96/96
Setting up Update Process
No Packages marked for Update
[root@localhost ~]#
「yum install mongo-10gen mongo-10gen-server」でインストール
[root@localhost ~]# yum install mongo-10gen mongo-10gen-server
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.iij.ad.jp
 * extras: ftp.iij.ad.jp
 * updates: ftp.iij.ad.jp
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package mongo-10gen.x86_64 0:2.4.6-mongodb_1 will be installed
---> Package mongo-10gen-server.x86_64 0:2.4.6-mongodb_1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=================================================================================================================
 Package                          Arch                 Version                         Repository           Size
=================================================================================================================
Installing:
 mongo-10gen                      x86_64               2.4.6-mongodb_1                 10gen                72 M
 mongo-10gen-server               x86_64               2.4.6-mongodb_1                 10gen                12 M

Transaction Summary
=================================================================================================================
Install       2 Package(s)

Total download size: 84 M
Installed size: 214 M
Is this ok [y/N]: y
Downloading Packages:
(1/2): mongo-10gen-2.4.6-mongodb_1.x86_64.rpm                                             |  72 MB     06:12
(2/2): mongo-10gen-server-2.4.6-mongodb_1.x86_64.rpm                                      |  12 MB     00:05
-----------------------------------------------------------------------------------------------------------------
Total                                                                            226 kB/s |  84 MB     06:19
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : mongo-10gen-2.4.6-mongodb_1.x86_64                                                            1/2
  Installing : mongo-10gen-server-2.4.6-mongodb_1.x86_64                                                     2/2
  Verifying  : mongo-10gen-2.4.6-mongodb_1.x86_64                                                            1/2
  Verifying  : mongo-10gen-server-2.4.6-mongodb_1.x86_64                                                     2/2

Installed:
  mongo-10gen.x86_64 0:2.4.6-mongodb_1                mongo-10gen-server.x86_64 0:2.4.6-mongodb_1

Complete!
[root@localhost ~]#
サービスの起動を行う
[root@localhost ~]# service mongod start
Starting mongod: about to fork child process, waiting until server is ready for connections.
forked process: 2387
all output going to: /var/log/mongo/mongod.log
child process started successfully, parent exiting
                                                           [  OK  ]
[root@localhost ~]#
[root@localhost ~]# chkconfig mongod on
[root@localhost ~]# chkconfig --list mongod
mongod          0:off   1:off   2:on    3:on    4:on    5:on    6:off
[root@localhost ~]#
[root@localhost ~]# mongo --version
MongoDB shell version: 2.4.6
[root@localhost ~]# mongo
MongoDB shell version: 2.4.6
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user
> exit
bye
[root@localhost ~]#

PHPのmongoモジュールをインストール

pecl install mongo」でインストール
Build process completed successfully
Installing '/usr/local/php/lib/php/extensions/no-debug-zts-20121212/mongo.so'
install ok: channel://pecl.php.net/mongo-1.4.3
configuration option "php_ini" is not set to php.ini location
You should add "extension=mongo.so" to php.ini
php.iniへextentionを追加
extension=/usr/local/php/lib/php/extensions/no-debug-zts-20121212/mongo.so
再起動してphpinfoで確認

f:id:m_shige1979:20130916232644j:plain

サンプルデータを用意

[root@localhost mongo_sample]# mongo
MongoDB shell version: 2.4.6
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user
> use sample1;
switched to db sample1
> db.test1.insert({aaa: "111"});
> db.test1.insert({aaa: "222"});
>
> db.test1.find();
{ "_id" : ObjectId("5234a1c7d29a95c7cd5b11fd"), "aaa" : "111" }
{ "_id" : ObjectId("5234a1cbd29a95c7cd5b11fe"), "aaa" : "222" }
>
> exit
bye
[root@localhost mongo_sample]#

phpのファイル

example.php
<?php

// DBへ接続
$mongo = new Mongo();

// データベースを指定
$db = $mongo->selectDB("sample1");

// コレクションを指定1
$col = $db->selectCollection("test1");

// コレクションのドキュメントを全件取得
$cursor = $col->find();

// 表示
foreach ($cursor as $id =>$obj) {
	var_dump($obj);
}

実行結果

[root@localhost mongo_sample]# php -f example.php
array(2) {
  ["_id"]=>
  object(MongoId)#7 (1) {
    ["$id"]=>
    string(24) "5234a1c7d29a95c7cd5b11fd"
  }
  ["aaa"]=>
  string(3) "111"
}
array(2) {
  ["_id"]=>
  object(MongoId)#8 (1) {
    ["$id"]=>
    string(24) "5234a1cbd29a95c7cd5b11fe"
  }
  ["aaa"]=>
  string(3) "222"
}
[root@localhost mongo_sample]#

WEB+DB PRESS Vol.75

WEB+DB PRESS Vol.75


NOSQLの基礎知識 (ビッグデータを活かすデータベース技術)

NOSQLの基礎知識 (ビッグデータを活かすデータベース技術)

広告を非表示にする