安装mongodb:
$ yum install mongodb-server -y
$ yum install mongodb -y
查看版本:
$ mongod -v
2017-07-11T20:12:29.537+0800 shardObjTest passed
2017-07-11T20:12:29.537+0800 isInRangeTest passed
2017-07-11T20:12:29.537+0800 shardKeyTest passed
2017-07-11T20:12:29.537+0800 [initandlisten] MongoDB starting : pid=14186 port=27017 dbpath=/data/db 64-bit host=iZ2ze46lyxfc5q7qi75e2wZ
2017-07-11T20:12:29.537+0800 [initandlisten] db version v2.6.12
2017-07-11T20:12:29.537+0800 [initandlisten] git version: nogitversion
2017-07-11T20:12:29.537+0800 [initandlisten] OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
2017-07-11T20:12:29.537+0800 [initandlisten] build info: Linux buildvm-16.phx2.fedoraproject.org 4.8.12-300.fc25.x86_64 #1 SMP Fri Dec 2 17:52:11 UTC 2016 x86_64 BOOST_LIB_VERSION=1_53
2017-07-11T20:12:29.537+0800 [initandlisten] allocator: tcmalloc
2017-07-11T20:12:29.537+0800 [initandlisten] options: { systemLog: { verbosity: 1 } }
2017-07-11T20:12:29.537+0800 [initandlisten] User Assertion: 10296:
*********************************************************************
ERROR: dbpath (/data/db) does not exist.
Create this directory or give existing directory in --dbpath.
See http://dochub.mongodb.org/core/startingandstoppingmongo
*********************************************************************
2017-07-11T20:12:29.538+0800 [initandlisten] exception in initAndListen: 10296
*********************************************************************
ERROR: dbpath (/data/db) does not exist.
Create this directory or give existing directory in --dbpath.
See http://dochub.mongodb.org/core/startingandstoppingmongo
*********************************************************************
, terminating
2017-07-11T20:12:29.538+0800 [initandlisten] dbexit:
2017-07-11T20:12:29.538+0800 [initandlisten] shutdown: going to close listening sockets...
2017-07-11T20:12:29.538+0800 [initandlisten] shutdown: going to flush diaglog...
2017-07-11T20:12:29.538+0800 [initandlisten] shutdown: going to close sockets...
2017-07-11T20:12:29.538+0800 [initandlisten] shutdown: waiting for fs preallocator...
2017-07-11T20:12:29.538+0800 [initandlisten] shutdown: lock for final commit...
2017-07-11T20:12:29.538+0800 [initandlisten] shutdown: final commit...
2017-07-11T20:12:29.538+0800 [initandlisten] shutdown: closing all files...
2017-07-11T20:12:29.538+0800 [initandlisten] closeAllFiles() finished
2017-07-11T20:12:29.538+0800 [initandlisten] dbexit: really exiting now
修改mongo的配置文件:
$ vim /etc/mongod.conf
bind_ip = 0.0.0.0
port = 27017
auth=true
重启mongodb:
$ service mongod restart
关闭mongodb:
$ service mongod stop
(1)使用kill PID -2来关闭mongodb服务器(不要使用-9参数,会导致数据库文件损坏)。
(2)登录mongo客户端之后,切换到admin数据库,再调用db.shutdownServer()函数来关闭mongodb服务。
mongo 操作实例:
$ mongo
MongoDB shell version: 2.6.12
connecting to: test
显示所有数据库
> show dbs;
admin (empty)
jiaxiaolei_test 0.078GB
local 0.078GB
切换到指定数据库
> use jiaxiaolei_test;
switched to db jiaxiaolei_test
查看当前所有表:
> show tables;
system.indexes
test
查看表test 中的全部数据;
> db.test.find();
{ "_id" : ObjectId("5965cd7867169d2ff6999555"), "name" : "jiaxiaolei", "age" : 20 }
导出数据:
$ mongodump –host [ip] -d cmdb
这里用 172.28.20.124 举例,导出cmdb database.
$ mongodump –host 172.28.20.124 -d cmdb
在当前目录下生成 dump 目录。
拷贝dump文件:
$ scp -r dump/ root@182.92.78.248:/root
导入dump 文件:
$ cd /root
$ mongorestore –drop
mongo 添加账户:(推荐使用createUser)
show dbs;
admin (empty)
cmdb 0.078GB
jiaxiaolei_test 0.078GB
local 0.078GB
uop 0.078GB
use uop;
switched to db uop
db.users
uop.users
db.addUser(‘uop’,’uop’)
WARNING: The ‘addUser’ shell helper is DEPRECATED. Please use ‘createUser’ instead
Successfully added user: { “user” : “uop”, “roles” : [ “dbOwner” ] }
show dbs;
admin 0.078GB
cmdb 0.078GB
jiaxiaolei_test 0.078GB
local 0.078GB
uop 0.078GB
use cmdb;
switched to db cmdb
db.addUser(‘cmdb’,’cmdb’)
WARNING: The ‘addUser’ shell helper is DEPRECATED. Please use ‘createUser’ instead
Successfully added user: { “user” : “cmdb”, “roles” : [ “dbOwner” ] }
mongo 的一个问题: TODO:
dump restore 之后, db 鉴权没生效,
配置了 auth, 有时候不需要 db.auth() 也可以登录
鉴权失败:
use uop;
switched to db uop
db.uop.addUser(‘uop’,’uop’)
2017-07-14T02:54:09.944+0000 TypeError: Property ‘addUser’ of object uop.uop is not a function
db.addUser(‘uop’,’uop’)
WARNING: The ‘addUser’ shell helper is DEPRECATED. Please use ‘createUser’ instead
Successfully added user: { “user” : “uop”, “roles” : [ “dbOwner” ] }
use cmdb;
switched to db cmdb
db.addUser(‘cmdb’,’cmdb’)
WARNING: The ‘addUser’ shell helper is DEPRECATED. Please use ‘createUser’ instead
2017-07-14T02:55:17.196+0000 Error: couldn’t add user: not authorized on cmdb to execute command { createUser: “cmdb”, pwd: “xxx”, roles: [ “dbOwner” ], digestPassword: false, writeConcern: { w: “majority”, wtimeout: 30000.0 } } at src/mongo/shell/db.js:1086
use cmdb;
switched to db cmdb
show tables;
2017-07-14T02:55:47.634+0000 error: {
“$err” : “not authorized for query on cmdb.system.namespaces”,
“code” : 13
} at src/mongo/shell/query.js:131
mongorestore –drop
connected to: 127.0.0.1
assertion: 13 not authorized on admin to execute command { getParameter: 1, authSchemaVersion: 1 }
]# mongo
MongoDB shell version: 2.6.12
connecting to: test
show dbs;
2017-07-14T03:26:13.984+0000 listDatabases failed:{
“ok” : 0,
“errmsg” : “not authorized on admin to execute command { listDatabases: 1.0 }”,
“code” : 13
} at src/mongo/shell/mongo.js:47
专题
配置文件
mongo v2.6.12开始, 配置文件有2个:
$ ls -l /etc/mongo
mongod.conf mongos.conf