Centos7 the basic operation of MongoDB 3.6.6 MongoDB Introduction
MongoDB is a database based on distributed file storage. Written by the C + + language. Designed to provide scalable, high-performance data storage solutions for Web applications.
MongoDB is a product between a relational database and a non-relational database, and is the most versatile and most like relational database in a non-relational database. The data structure it supports is very loose and is a JSON-like Bson format, so you can store more complex data types. The most important feature of MONGO is that the query language it supports is very powerful, its syntax is somewhat similar to object-oriented query language, it can implement most functions like relational database single table query, and it also supports indexing data.
Turn on multi-instance
cp -p /etc/mongod.conf /etc/mongod2.conf## 复制一份配置文件给第二个实例vim /etc/mongod2.conf ##配置实例 path: /data/mongodb/mongod2.log ##日志文件位置 dbPath: /data/mongodb/mongo ##数据位置 port: 27018 ##不同实例的端口不同mkdir -p /data/mongodb/ ##创建数据文件夹cd /data/mongodb/mkdir mongo touch mongod2.log ##创建日志文件chmod 777 mongod2.log ##给予日志文件权限 mongod -f /etc/mongod2.conf ##开启第二份实例mongo --port 27018 ##进入数据库
Basic operations
> use mydb; ## 创建数据库 ,不存在会创建,不建立集合又会删除> db.createCollection(‘a‘) ##创建集合> db.a.insert({"id":1,"name":"zhangsan"}) ## 在集合中插入数据> db.a.find() ## 查看集合中的数据> a=db.users.findOne({"id":2}) ##查找指定记录并赋予别名a> typeof(a.id) ##查看属性类型> db.users.update({"id":10},{$set:{"name":"tom"}}) ##更改数据> show collections ##查看集合> db.a.drop() ##删除集合> db.dropDatabase() ##删除数据库> db.copyDatabase("mydb","mydb1") ##复制数据库
Import and Export data
mongoexport -d kgc -c users -o /opt/users.json ##导出mongoimport -d kgc -c user1 --file users.json ##导入mongoexport -d kgc -c user1 -q ‘{"id":{"$eq":10}}‘ -o /opt/top10.json ##根据条件进行操作
Backup and Recovery
mkdir /backupmongodump -d kgc -o /backup/ ##备份mongorestore -d kgc2 --dir=/backup/kgc ##恢复
Clone collection
mongo --port 27018 ##进入另一个实例db.runCommand####({"cloneCollection":"kgc.users","from":"192.168.100.100:27017"}) ## 完成克隆
Create an administrative user
> use admin> db.createUser({"user":"root","pwd":"123","roles":["root"]}) ##用户名,密码,权限> db.auth("root","123") ##进行验证
Centos7 under MongoDB 3.6.6 basic operation