mongodb部署
时间:2017-08-06 11:26 来源:潇湘夜雨 作者:华嵩阳 点击:次
mongdb 3.2.6
1、安装
[Asion@mobile ~]$ mkdir mongodb #准备安装目录
[Asion@mobile ~]$ cd mongodb
[Asion@mobile mongodb]$ tar -zxf mongodb-linux-x86_64-rhel62-3.2.6.gz
[Asion@mobile mongodb]$ ls
mongodb-linux-x86_64-rhel62-3.2.6 mongodb-linux-x86_64-rhel62-3.2.6.gz
[Asion@mobile mongodb]$ cp -r mongodb-linux-x86_64-rhel62-3.2.6/* ./
[Asion@mobile mongodb]$ ls
GNU-AGPL-3.0 MPL-2 README THIRD-PARTY-NOTICES bin mongodb-linux-x86_64-rhel62-3.2.6 mongodb-linux-x86_64-rhel62-3.2.6.gz
[Asion@mobile mongodb]$ rm -rf mongodb-linux-x86_64-rhel62-3.2.6
[Asion@mobile mongodb]$ ls
GNU-AGPL-3.0 MPL-2 README THIRD-PARTY-NOTICES bin mongodb-linux-x86_64-rhel62-3.2.6.gz
2、设置环境变量
[root@mobile ~]# vim /etc/profile.d/mongodb.sh
export PATH=$PATH:/home/Asion/mongodb/bin
[root@mobile ~]# source /etc/profile
[root@mobile openssh]# mongod --version #如果系统版本比较低,可能会出现依赖错误
mongod: /usr/lib64/libcrypto.so.10: no version information available (required by mongod)
mongod: /usr/lib64/libcrypto.so.10: no version information available (required by mongod)
mongod: /usr/lib64/libssl.so.10: no version information available (required by mongod)
mongod: relocation error: mongod: symbol TLSv1_1_client_method, version libssl.so.10 not defined in file libssl.so.10 with link time reference
解决方法:到官方yum源下载最新的openssl的包
wget http://mirrors.163.com/centos/6.9/os/x86_64/Packages/openssl-1.0.1e-57.el6.x86_64.rpm
wget http://mirrors.163.com/centos/6.9/os/x86_64/Packages/openssl-devel-1.0.1e-57.el6.x86_64.rpm
rpm -ivh openssl-1.0.1e-57.el6.x86_64.rpm #要先卸载才能安装,如果有依赖性报错,可以添加上忽略参数
rpm -ivh --nodeps openssl-devel-1.0.1e-57.el6.x86_64.rpm
[Asion@mobile mongodb]$ mongod --version
db version v3.2.6
git version: 05552b562c7a0b3143a729aaa0838e558dc49b25
OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
allocator: tcmalloc
modules: none
build environment:
distmod: rhel62
distarch: x86_64
target_arch: x86_64
首先查看mongod的帮助信息
1 [root@test ~]# mongod --help
2 Options:
3
4 General options:
5 -h [ --help ] show this usage information
6 --version show version information
7 -f [ --config ] arg configuration file specifying
8 additional options
9 -v [ --verbose ] [=arg(=v)] be more verbose (include multiple times
10 for more verbosity e.g. -vvvvv)
11 --quiet quieter output
12 --port arg #指定mongodb服务的端口号,默认为:27017
13 --bind_ip arg #在多网卡的机器上指定mongodb服务绑定到哪一个ip上
15 --ipv6 enable IPv6 support (disabled by
16 default)
17 --maxConns arg #指定最大客户端连接数
19 --logpath arg #指定日志文件路径,必须是一个文件,而不是目录
20 --syslog log to system's syslog facility instead
23 of file or stdout
24 --syslogFacility arg syslog facility used for mongodb syslog
25 message
26 --logappend #以追加的方式打印日志到--logpath参数指定的日志文件中
28 --logRotate arg set the log rotation behavior
29 (rename|reopen)
30 --timeStampFormat arg Desired format for timestamps in log
31 messages. One of ctime, iso8601-utc or
32 iso8601-local
33 --pidfilepath arg full path to pidfile (if not set, no
34 pidfile is created)
35 --keyFile arg private key for cluster authentication
36 --setParameter arg Set a configurable parameter
37 --httpinterface enable http interface
38 --clusterAuthMode arg Authentication mode used for cluster
39 authentication. Alternatives are
40 (keyFile|sendKeyFile|sendX509|x509)
41 --nounixsocket disable listening on unix sockets
42 --unixSocketPrefix arg alternative directory for UNIX domain
43 sockets (defaults to /tmp)
44 --filePermissions arg permissions to set on UNIX domain
45 socket file - 0700 by default
46 --fork #以daemon的形式运行服务进程
105 --dbpath arg #指定数据目录路径
3、启动服务
[Asion@mobile mongodb]$ vim conf/mongodb.conf #创建配置文件
port=27017
dbpath=/home/Asion/mongodb/data
logpath=/home/Asion/mongodb/log/mongodb.log
pidfilepath=/home/Asion/mongodb/log/mongodb.pid
directoryperdb=true
logappend=true
oplogSize=1000
fork=true
#noprealloc=true
master=true
[Asion@mobile mongodb]$ mongod -f /home/Asion/mongodb/conf/mongodb.conf #启动服务
about to fork child process, waiting until server is ready for connections.
forked process: 45002
child process started successfully, parent exiting
[Asion@mobile mongodb]$ netstat -tnlp|grep mongod
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 45002/mongod
以上可看出端口27017已经运行
4、客户端测试
启动mongodb的shell:
[Asion@mobile mongodb]$ mongo
MongoDB shell version: 3.2.6
connecting to: test
Server has startup warnings:
2017-08-01T13:36:39.570+0800 I CONTROL [initandlisten]
2017-08-01T13:36:39.570+0800 I CONTROL [initandlisten] ** WARNING: You are running on a NUMA machine.
2017-08-01T13:36:39.570+0800 I CONTROL [initandlisten] ** We suggest launching mongod like this to avoid performance problems:
2017-08-01T13:36:39.570+0800 I CONTROL [initandlisten] ** numactl --interleave=all mongod [other options]
2017-08-01T13:36:39.570+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 users
> show dbs
local 0.000GB
由于它是一个JavaScript shell,您可以运行一些简单的算术运算:
> 2+2
4
现在让我们插入一些简单的数据,并对插入的数据进行检索:
> db.runoob.insert({x:10})
WriteResult({ "nInserted" : 1 })
> db.runoob.find()
{ "_id" : ObjectId("5980154a0d170e29a3824d68"), "x" : 10 }
5、创建数据库
MongoDB 创建数据库的语法格式如下:
use DATABASE_NAME
> use testdb
switched to db testdb
> db
testdb
> show dbs
local 0.000GB
test 0.000GB
> db
testdb
> show dbs #可以看到,我们刚创建的数据库 runoob 并不在数据库的列表中, 要显示它,我们需要向 testdb 数据库插入一些数据。
local 0.000GB
test 0.000GB
> db.runoob.insert({"name":"菜鸟教程"})
WriteResult({ "nInserted" : 1 })
> show dbs
local 0.000GB
test 0.000GB
testdb 0.000GB
6、远程连接
你可以使用 MongoDB shell 来连接 MongoDB 服务器。你也可以使用 PHP 来连接 MongoDB。
本教程我们会使用 MongoDB shell 来连接 Mongodb 服务,之后的章节我们将会介绍如何通过php 来连接MongoDB服务。
标准 URI 连接语法:
mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
[Asion@mobile mongodb]$ mongo --host=192.168.2.26
MongoDB shell version: 3.2.6
connecting to: 192.168.2.26:27017/test
7、添加用户的权限认证
> show dbs
local 0.000GB
test 0.000GB
testdb 0.000GB
看到admin是不存在的(这是3.0以上版本改变了的),我们需要自己给他创建个admin数据库。
> use admin
switched to db admin
> db.createUser(
... {
... user: "admin",
... pwd: "admin",
... roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
... }
... )
Successfully added user: {
"user" : "admin",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
]
}
> show users #查看刚才创建的用户
{
"_id" : "admin.admin",
"user" : "admin",
"db" : "admin",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
]
}
配置认证需要在配置文件中添加auth参数,然后重启
生产环境中关闭服务最好不要用kill命令,可以通过如下命令关闭
> db.shutdownServer()
shutdown command only works with the admin database; try 'use admin'
> use admin
switched to db admin
> db.shutdownServer()
server should be down...
2017-08-01T15:03:35.064+0800 I NETWORK [thread1] trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
2017-08-01T15:03:35.064+0800 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2017-08-01T15:03:35.064+0800 I NETWORK [thread1] reconnect 127.0.0.1:27017 (127.0.0.1) failed failed
[Asion@mobile mongodb]$ mongo
MongoDB shell version: 3.2.6
connecting to: test
> show dbs #没有输入口令会报错
2017-08-01T14:13:10.605+0800 E QUERY [thread1] Error: listDatabases failed:{
"ok" : 0,
"errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",
"code" : 13
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:62:1
shellHelper.show@src/mongo/shell/utils.js:760:19
shellHelper@src/mongo/shell/utils.js:650:15
@(shellhelp2):1:1
> use admin
switched to db admin
> db.auth("admin","admin")
1 #认证,返回1表示成功
> show dbs
admin 0.000GB
local 0.000GB
test 0.000GB
testdb 0.000GB
>
admin用户只是管理员权限,下面创建用户,用户都跟着库走,创建的用户都是
> use test
switched to db test
> db.createUser(
... {
... user: "test",
... pwd: "test123",
... roles: [
... { role: "readWrite", db: "test" }
... ]
... }
... )
Successfully added user: {
"user" : "test",
"roles" : [
{
"role" : "readWrite",
"db" : "test"
}
]
}
查看系统所有用户
> use admin
switched to db admin
> db.system.users.find()
{ "_id" : "admin.admin", "user" : "admin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "VqGWPYN4oycGfzpdMzS5OA==", "storedKey" : "wSz5+Y3Po1thbxDMXSh/lqgpP9M=", "serverKey" : "lORSfJ/Agi96baxUFmas3RUDJi8=" } }, "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] }
{ "_id" : "test.test", "user" : "test", "db" : "test", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "7JrMglVDYqnIs8BCdT911g==", "storedKey" : "2Xri7BfLUrkM3vTx69CBqrertgg=", "serverKey" : "27vdvcdniiJARrewPajrDnieF+I=" } }, "roles" : [ { "role" : "readWrite", "db" : "test" } ] }
>
创建完毕,验证一下
> use test
> db.auth("test","test123")
1
> show collections
runoob
db.addUser("asion", "asion801")
具体角色:
Read:允许用户读取指定数据库
readWrite:允许用户读写指定数据库
dbAdmin:允许用户在指定数据库中执行管理函数,如索引创建、删除,查看统计或访问system.profile
userAdmin:允许用户向system.users集合写入,可以找指定数据库里创建、删除和管理用户
clusterAdmin:只在admin数据库中可用,赋予用户所有分片和复制集相关函数的管理权限。
readAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的读权限
readWriteAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的读写权限
userAdminAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的userAdmin权限
dbAdminAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的dbAdmin权限。
root:只在admin数据库中可用。超级账号,超级权限
> db.createUser(
{
user: "asion",
pwd: "asion801",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
db.createUser(
{
user: "root",
pwd: "asion801",
roles: [ { role: "root", db: "admin" } ]
}
)
|