一直好奇 MongoDB 的这个名字的由来,之前土鳖,总认为是 MangoDB,后来才意识到是 MongoDB
引用官网的一段话:(https://www.mongodb.com/cn)
MongoDB(来自于英文单词“Humongous”,中文含义为“庞大”)是可以应用于各种规模的企业、各个行业以及各类应用程序的开源数据库。作为一个适用于敏捷开发的数据库,MongoDB的数据模式可以随着应用程序的发展而灵活地更新。与此同时,它也为开发人员 提供了传统数据库的功能:二级索引,完整的查询系统以及严格一致性等等。 MongoDB能够使企业更加具有敏捷性和可扩展性,各种规模的企业都可以通过使用MongoDB来创建新的应用,提高与客户之间的工作效率,加快产品上市时间,以及降低企业成本。
设置 Yum 源 参考(https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/)
$ touch /etc/yum.repos.d/mongodb-org-3.2.repo
[mongodb-org-3.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.2.asc
$ yum install -y mongodb-org
[root@vm-10-185-30-240:/tmp/pear/download/]
$ service mongod start
Starting mongod: [ OK ]
[root@vm-10-185-30-240:/tmp/pear/download/]
$ cat /var/log/mongodb/mongod.log
2016-11-22T14:15:21.874+0800 I CONTROL [main] ***** SERVER RESTARTED *****
2016-11-22T14:15:21.885+0800 I CONTROL [initandlisten] MongoDB starting : pid=27026 port=27017 dbpath=/var/lib/mongo 64-bit host=vm-10-185-30-240
2016-11-22T14:15:21.885+0800 I CONTROL [initandlisten] db version v3.2.11
2016-11-22T14:15:21.886+0800 I CONTROL [initandlisten] git version: 009580ad490190ba33d1c6253ebd8d91808923e4
2016-11-22T14:15:21.886+0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
2016-11-22T14:15:21.886+0800 I CONTROL [initandlisten] allocator: tcmalloc
2016-11-22T14:15:21.886+0800 I CONTROL [initandlisten] modules: none
2016-11-22T14:15:21.886+0800 I CONTROL [initandlisten] build environment:
2016-11-22T14:15:21.886+0800 I CONTROL [initandlisten] distmod: rhel62
2016-11-22T14:15:21.886+0800 I CONTROL [initandlisten] distarch: x86_64
2016-11-22T14:15:21.886+0800 I CONTROL [initandlisten] target_arch: x86_64
2016-11-22T14:15:21.886+0800 I CONTROL [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1", port: 27017 }, processManagement: { fork: true, pidFilePath: "/var/run/mongodb/mongod.pid" }, storage: { dbPath: "/var/lib/mongo", journal: { enabled: true } }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log" } }
2016-11-22T14:15:21.900+0800 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=8G,session_max=20000,eviction=(threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2016-11-22T14:15:22.029+0800 I CONTROL [initandlisten]
2016-11-22T14:15:22.029+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-11-22T14:15:22.029+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2016-11-22T14:15:22.029+0800 I CONTROL [initandlisten]
2016-11-22T14:15:22.029+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-11-22T14:15:22.029+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2016-11-22T14:15:22.029+0800 I CONTROL [initandlisten]
2016-11-22T14:15:22.029+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 65536 processes, 600000 files. Number of processes should be at least 300000 : 0.5 times number of files.
2016-11-22T14:15:22.029+0800 I CONTROL [initandlisten]
2016-11-22T14:15:22.030+0800 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/var/lib/mongo/diagnostic.data'
2016-11-22T14:15:22.030+0800 I NETWORK [HostnameCanonicalizationWorker] Starting hostname canonicalization worker
2016-11-22T14:15:22.039+0800 I NETWORK [initandlisten] waiting for connections on port 27017
$ chkconfig mongod on
默认情况下,MongoDB是不启动 Auth 的。因此,需要手动开启,参考官网的说明:(https://docs.mongodb.com/manual/tutorial/enable-authentication/)
- 连接 MongoDB, 新增
myUserAdmin
用户。
use admin
db.createUser(
{
user:"myUserAdmin",
pwd:"123456",
roles:[{role:"userAdminAnyDatabase", db:"admin"}]}
)
use test
db.createUser(
{
user: "myTester",
pwd: "123456",
roles: [ { role: "readWrite", db: "test" },
{ role: "read", db: "reporting" } ]
}
)
exit
说明: MongoDB 本身用户认证并非是与 MySQL 相同。MongoDB 每个用户授权管理是要「显示」的跟上授权认证的数据库的。
最开始,看官网的文档,我错误的以为 Demo 中的例子(创建 myUserAdmin
这个用户)中,指定 db 到 admin
,是所有用户授权都需要到 admin
库。后续才明白这个admin
库的指定,并非 MySQL 中的 mysql 库和 user 表的操作。而就是具体的授权操作的数据库。
因此,针对myUserAdmin
这个用户,能够访问的就是 admin
这个库;而针对myTester
这个用户,则只能读写test
这个库。
- 退出后,重新连接 MongoDB (使用
myUserAdmin
账户)
$ mongo -u"myUserAdmin" -p"123456" --authenticationDatabase "admin"
# 或者先直接登录用,使用db.auth 方法进行进行认证
$ mongo
> db.auth("myUserAdmin","123456")
- 使用
myTester
账户进行登录
$ mongo -u"myTester" -p"123456" --authenticationDatabase "test"
MongoDB shell version: 3.2.11
connecting to: test
Server has startup warnings:
2016-11-23T11:20:53.044+0800 I CONTROL [initandlisten]
2016-11-23T11:20:53.044+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-11-23T11:20:53.044+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2016-11-23T11:20:53.044+0800 I CONTROL [initandlisten]
2016-11-23T11:20:53.044+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-11-23T11:20:53.044+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2016-11-23T11:20:53.044+0800 I CONTROL [initandlisten]
> help
db.help() help on db methods
db.mycoll.help() help on collection methods
sh.help() sharding helpers
rs.help() replica set helpers
help admin administrative help
help connect connecting to a db help
help keys key shortcuts
help misc misc things to know
help mr mapreduce
show dbs show database names
show collections show collections in current database
show users show users in current database
show profile show most recent system.profile entries with time >= 1ms
show logs show the accessible logger names
show log [name] prints out the last segment of log in memory, 'global' is default
use <db_name> set current database
db.foo.find() list objects in collection foo
db.foo.find( { a : 1 } ) list objects in foo where a == 1
it result of the last line evaluated; use to further iterate
DBQuery.shellBatchSize = x set default number of items to display on shell
exit quit the mongo shell
> show collections
runoob
> db.runoob.find();
{ "_id" : ObjectId("58350c167bbec78698226f29"), "x" : 10 }
>
- 使用 RPM 安装的 MongoDB (Version 3.2.11),配置文件位置:
/etc/mongod.conf
。可以查看对应日志存储位置(这个很重要) 和数据存储目录。如下:
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# Where and how to store data.
storage:
dbPath: /var/lib/mongo
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1 # Listen to local interface only, comment to listen on all interfaces.
#security:
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options
#auditLog:
#snmp:
- 可以通过查看
/var/log/mongodb/mongod.log
来查看一些 MongoDB的问题。例如:启动失败;或者为什么authenticate failed.
...
291 2016-11-30T10:16:53.828+0800 I - [initandlisten] Detected data files in /var/lib/mongo created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
292 2016-11-30T10:16:53.828+0800 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=8G,session_max=20000,eviction=(threads_max=4),conf ig_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wai t=60,log_size=2GB),statistics_log=(wait=0),
293 2016-11-30T10:16:53.836+0800 E STORAGE [initandlisten] WiredTiger (13) [1480472213:836045][12830:0x7fbcfa4c2d40], file:WiredTiger.wt, connection: / var/lib/mongo/WiredTiger.turtle: handle-open: open: Permission denied
294 2016-11-30T10:16:53.836+0800 I - [initandlisten] Assertion: 28595:13: Permission denied
295 2016-11-30T10:16:53.837+0800 I STORAGE [initandlisten] exception in initAndListen: 28595 13: Permission denied, terminating
296 2016-11-30T10:16:53.837+0800 I CONTROL [initandlisten] dbexit: rc: 100
337 2016-11-30T10:20:43.811+0800 I NETWORK [HostnameCanonicalizationWorker] Starting hostname canonicalization worker
338 2016-11-30T10:20:43.812+0800 I NETWORK [initandlisten] waiting for connections on port 27017
339 2016-11-30T10:20:53.206+0800 I NETWORK [initandlisten] connection accepted from 127.0.0.1:29400 #1 (1 connection now open)
340 2016-11-30T10:20:53.209+0800 I ACCESS [conn1] SCRAM-SHA-1 authentication failed for myTester on admin from client 127.0.0.1 ; UserNotFound: Could not find user myTester@admin
前者是因为数据存储的文件权限变成了 root
而非 mongod:mongod
;
后者很明显是myTester
指定库到了admin
,而非正常授权的test
库。
需要比较好的网络能够连接 pecl 的服务器,进行自动化下载安装。
参考1
参考2
然后,将扩展添加到 php.ini
中。
$ pecl install mongodb
$ echo "extension=mongodb.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`
$ git clone https://github.com/mongodb/mongo-php-driver.git
$ cd mongo-php-driver
$ git submodule sync && git submodule update --init
$ phpize
$ ./configure --with-php-config=/usr/local/php/bin/php-config
$ make all -j 5
$ sudo make install
- 参考相关组件GitHub网址
$ composer update --no-scripts
$ composer require jenssegers/mongodb
- 在
config/app.php
中:
Jenssegers\Mongodb\MongodbServiceProvider::class,
- 在
config/database.php
- 参考相关组件GitHub网址
$ composer update --no-scripts
$ composer require jenssegers/mongodb