以下内容是关于运行、配置、启动、登录和验证MongoDB数据库的详细步骤。
1 2 3 |
cd ~ find / -name mongod /usr/local/mongo/bin/mongod --version |
1 2 |
rm -f /etc/mongodb.cnf vim /etc/mongodb.cnf |
1 2 3 4 5 6 7 |
dbpath=/data/mongo logpath=/data/mongo/mongo.log logappend=true port=27017 fork=true auth=false bind_ip=0.0.0.0 |
配置文件参数说明:
1
|
nohup /usr/local/mongo/bin/mongod -f /etc/mongodb.cnf & |
1
|
ps -ef | grep mongod |
可以看到数据库进程ID为36249且已正常启动。
1
|
netstat -anpt
|
在本例中MongoDB数据库的监测端口为27017。
1 2 |
cd /usr/local/mongo/bin ./mongo |
查看登录参数。
1 2 |
/usr/local/mongo/bin/mongo --h /usr/local/mongo/bin/mongo --help |
1
|
show dbs
|
1 2 |
use mongotest-database show dbs |
1 2 3 |
use mongotest-database show collections db.createCollection("mongo_test") |
如果要查看已有集合,可以使用show collections或show tables命令。
1 2 |
show collections show tables |
1 2 3 4 5 6 7 |
db.mongo_test.insert({title: 'MongoDB 教程', ... description: 'MongoDB 是一个 Nosql 数据库', ... by: '菜鸟教程', ... url: 'http://www.runoob.com', ... tags: ['mongodb', 'database', 'NoSQL'], ... likes: 100 ... }) |
以上实例中mongo_test是集合名,如果该集合不在数据库中,MongoDB会自动创建集合并插入文档。
查看已插入文档:
1
|
db.mongo_test.find()
|
1 2 |
db.mongo_test.update({'title':'MongoDB 教程'},{$set:{'title':'MongoDB'}}) db.mongo_test.find() |
1 2 |
db.mongo_test.remove({'title':'MongoDB'}) db.mongo_test.find() |
1 2 3 |
db.mongo_test.getIndexes() db.mongo_test.createIndex({"title_test":1}) db.mongo_test.getIndexes() |
1
|
db.mongo_test.dropIndex("title_test_1") |
1 2 3 |
use mongotest-database show collections db.mongo_test.drop() |
1 2 3 |
use mongotest-database show dbs db.dropDatabase() |
1
|
exit
|