1.下载
http://downloads.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.0.tgz
2.解压
tar -zxvf mongodb-linux-x86_64-rhel70-4.2.0.tgz -C /usr/local
cd /usr/local/mongodb-linux-x86_64-rhel70-4.2.0
3.创建文件夹
mkdir -p logs data/db conf
4.配置文件
vim conf/mongod.conf
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /usr/local/mongodb-linux-x86_64-rhel70-4.2.0/logs/mongod.log
# Where and how to store data.
storage:
dbPath: /usr/local/mongodb-linux-x86_64-rhel70-4.2.0/data/db
journal:
enabled: true
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /usr/local/mongodb-linux-x86_64-rhel70-4.2.0/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting开启远程访问.
# 开启远程认证
security:
authorization: enabled
5.开机自启动
[root@vm-test1 mongodb-linux-x86_64-rhel70-4.2.0]# cat /lib/systemd/system/mongodb.service
[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/mongodb-linux-x86_64-rhel70-4.2.0/bin/mongod -f /usr/local/mongodb-linux-x86_64-rhel70-4.2.0/conf/mongod.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/usr/local/mongodb-linux-x86_64-rhel70-4.2.0/bin/mongod --shutdown -f /usr/local/mongodb-linux-x86_64-rhel70-4.2.0/conf/mongod.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
启动
systemctl start mongodb.service
关闭
systemctl stop mongodb.service
注册到开机启动
systemctl enable mongodb.service
[root@vm-test1 mongodb-linux-x86_64-rhel70-4.2.0]# mongo --host 127.0.0.1
> use school
switched to db school
>
>
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
> db.help()
DB methods:
db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [just calls db.runCommand(...)]
db.aggregate([pipeline], {options}) - performs a collectionless aggregation on this database; returns a cursor
db.auth(username, password)
db.cloneDatabase(fromhost) - will only function with MongoDB 4.0 and below
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb, todb, fromhost) - will only function with MongoDB 4.0 and below
db.createCollection(name, {size: ..., capped: ..., max: ...})
db.createUser(userDocument)
db.createView(name, viewOn, [{$operator: {...}}, ...], {viewOptions})
db.currentOp() displays currently executing operations in the db
db.dropDatabase(writeConcern)
db.dropUser(username)
db.eval() - deprecated
db.fsyncLock() flush data to disk and lock server for backups
db.fsyncUnlock() unlocks server following a db.fsyncLock()
db.getCollection(cname) same as db['cname'] or db.cname
db.getCollectionInfos([filter]) - returns a list that contains the names and options of the db's collections
db.getCollectionNames()
db.getLastError() - just returns the err msg string
db.getLastErrorObj() - return full status object
db.getLogComponents()
db.getMongo() get the server connection object
db.getMongo().setSlaveOk() allow queries on a replication slave server
db.getName()
db.getProfilingLevel() - deprecated
db.getProfilingStatus() - returns if profiling is on and slow threshold
db.getReplicationInfo()
db.getSiblingDB(name) get the db at the same server as this one
db.getWriteConcern() - returns the write concern used for any operations on this db, inherited from server object if set
db.hostInfo() get details about the server's host
db.isMaster() check replica primary status
db.killOp(opid) kills the current operation in the db
db.listCommands() lists all the db commands
db.loadServerScripts() loads all the scripts in db.system.js
db.logout()
db.printCollectionStats()
db.printReplicationInfo()
db.printShardingStatus()
db.printSlaveReplicationInfo()
db.resetError()
db.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into {cmdObj: 1}
db.serverStatus()
db.setLogLevel(level,<component>)
db.setProfilingLevel(level,slowms) 0=off 1=slow 2=all
db.setVerboseShell(flag) display extra information in shell output
db.setWriteConcern(<write concern doc>) - sets the write concern for writes to the db
db.shutdownServer()
db.stats()
db.unsetWriteConcern(<write concern doc>) - unsets the write concern for writes to the db
db.version() current version of the server
db.watch() - opens a change stream cursor for a database to report on all changes to its non-system collections.
> > db.dropDatabase()
{ "ok" : 1 }
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
>
>
> db test1
2019-09-25T15:21:34.511+0800 E QUERY [js] uncaught exception: SyntaxError: unexpected token: identifier :
@(shell):1:3
>
> use test1
switched to db test1
>
>
> db.createCollection('info')
{ "ok" : 1 }
>
> show tables
info> show tables
info
>
> db.info.in
db.info.initializeOrderedBulkOp( db.info.insert( db.info.insertOne(
db.info.initializeUnorderedBulkOp( db.info.insertMany(
> db.info.insert({'id':1,'name':'jack','hobby:['game:,'talk','sport']})
2019-09-25T15:24:00.532+0800 E QUERY [js] uncaught exception: SyntaxError: missing : after property id :
@(shell):1:46
>
> db.info.insert({'_id':1,'name':'jack','hobby:['game:,'talk','sport']})
2019-09-25T15:24:10.622+0800 E QUERY [js] uncaught exception: SyntaxError: missing : after property id :
@(shell):1:47
> db.info.insert({'_id':1,'name':'jack','hobby':['game:,'talk','sport']})
2019-09-25T15:24:45.502+0800 E QUERY [js] uncaught exception: SyntaxError: missing ] after element list :
@(shell):1:55
>
>
> db.info.insert({'_id':1,'name':'jack','hobby':['game','talk','sport']})
WriteResult({ "nInserted" : 1 })
>
> show tables
info
> db.info.find()
{ "_id" : 1, "name" : "jack", "hobby" : [ "game", "talk", "sport" ] }
> db.info.count()
1
> db.info.insert({'_id':2,'name':'tom','hobby':['game','talk','sport']})
WriteResult({ "nInserted" : 1 })
> db.info.count()
2
> db.info.find()
{ "_id" : 1, "name" : "jack", "hobby" : [ "game", "talk", "sport" ] }
{ "_id" : 2, "name" : "tom", "hobby" : [ "game", "talk", "sport" ] }
>
> db.info.find()
{ "_id" : 1, "name" : "jack", "hobby" : [ "game", "talk", "sport" ] }
{ "_id" : 2, "name" : "tom", "hobby" : [ "game", "talk", "sport" ] }
> db.info.remove({"_id":1})
WriteResult({ "nRemoved" : 1 })
> db.info.find()
{ "_id" : 2, "name" : "tom", "hobby" : [ "game", "talk", "sport" ] }
>
>
> > db.info.insert({'_id':1,'name':'jack','hobby':['game','talk','sport']})
WriteResult({ "nInserted" : 1 })
> db.info.find()
{ "_id" : 2, "name" : "tom", "hobby" : [ "game", "talk", "sport" ] }
{ "_id" : 1, "name" : "jack", "hobby" : [ "game", "talk", "sport" ] }
> db.info.remove({"name":"tom"})
WriteResult({ "nRemoved" : 1 })
> db.info.find()
{ "_id" : 1, "name" : "jack", "hobby" : [ "game", "talk", "sport" ] }
> > show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
test1 0.000GB
> use test1
switched to db test1
>
>
> db.info.find()
{ "_id" : 1, "name" : "jack", "hobby" : [ "game", "talk", "sport" ] }
>
>
>
> db.info.update({"_id":1},{$set:{"name","jack tom"}})
2019-09-25T15:34:21.224+0800 E QUERY [js] uncaught exception: SyntaxError: missing : after property id :
@(shell):1:38
> db.info.update({"_id":"1"},{$set:{"name","jack tom"}})
2019-09-25T15:34:34.925+0800 E QUERY [js] uncaught exception: SyntaxError: missing : after property id :
@(shell):1:40
>
>
> db.info.update({"_id":"1"},{$set:{"name":"jack tom"}})
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
>
> db.info.find()
{ "_id" : 1, "name" : "jack", "hobby" : [ "game", "talk", "sport" ] }
> db.info.update({"_id":"1"},{$set:{"name":"jack-tom"}})
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
> db.info.find()
{ "_id" : 1, "name" : "jack", "hobby" : [ "game", "talk", "sport" ] }
> db.info.update({"_id":"1"},{$set:{"name":"jack-tom"}})
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
>
>
>
>
>
>
>
>
> db.info.update({"_id":"1"},{$set:{"name":"jack-tom"}})
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
> db.info.find()
{ "_id" : 1, "name" : "jack", "hobby" : [ "game", "talk", "sport" ] }
> db.info.update({"_id":1},{$set:{"name":"jack-tom"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.info.find()
{ "_id" : 1, "name" : "jack-tom", "hobby" : [ "game", "talk", "sport" ] }
> db.info.update({"_id":1},{$set:{"name":"jack tom"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.info.find()
{ "_id" : 1, "name" : "jack tom", "hobby" : [ "game", "talk", "sport" ] }
>
[root@localhost soft]# mongo 172.16.174.145:27017/admin -uadmin -p
MongoDB shell version v4.2.0
Enter password:
connecting to: mongodb://172.16.174.145:27017/admin?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("0f7fa7a9-21a6-452b-826e-5c9f3db80f92") }
MongoDB server version: 4.2.0
Server has startup warnings:
2019-09-25T16:23:40.313+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2019-09-25T16:23:40.313+0800 I CONTROL [initandlisten]
2019-09-25T16:23:40.313+0800 I CONTROL [initandlisten]
2019-09-25T16:23:40.313+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-09-25T16:23:40.313+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-09-25T16:23:40.313+0800 I CONTROL [initandlisten]
2019-09-25T16:23:40.313+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-09-25T16:23:40.313+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-09-25T16:23:40.313+0800 I CONTROL [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
test1 0.000GB
lmy233
努力工作学习生活的人呐~~