1:创建仓库
vi /etc/yum.repos.d/mongodb-org-3.4.repo

 

 

 


2:把下面的内容复制到文件中 保存退出

[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc

 

注意,上面的数字比如5.0可以替换成其他已有的版本号,参见

下面这个链接可以选择linux版本

Index of redhat

假设我是centos 7的

Index of mongodb-org

我可以选择3.4版本


3:yum安装 如图安装完成

yum install -y mongodb-org

 

警告:外网下载很慢 

然后发现下载很慢,我就把图中列出的rpm包手动下载了,然后删除掉前面创建的repo文件。

 

 

 

 我的linux是centos 7 ,我准备下载5.0的MongoDB,我从下面这个链接下载rpm

Index of RPMS

 

 

 成功安装

 

 

 4、修改配置文件

 

把bindIP改成 0.0.0.0所有的机器都可以访问

 

 

 

 

 5、启动/关闭mogodb

systemctl start mongod   启动MongoDB

systemctl enable mongod  开启启动MongoDB

 

 

 

 

 

二、MongoDB创建用户

进入到mongo数据库里面执行命令:

1、use admin  使用admin数据库  如果使用其它的数据库 可以使用use XXXX  也是创建数据库的命令

2、db.createUser({user:"root",pwd:"123456",roles:"root"})   创建管理员用户  是根据roles来定义的

3、db.createUser({user:"root",pwd:"123456",roles:"read"})   针对某个数据库(database)建立用户(权限:read,readWrite,write)

 

三、mongodb入门命令-创建表数据库

1.mongodb入门命令

1.1 show databases; 或 show dbs; //查看当前的数据库

 

 

1.2 use databaseName  选择库

      show tables/collections 查看当前库下的collections

 

1.3 如何创建库

  mongodb 的库是隐式创建,你可以use一个不存在的库

  然后在该库下创建collection,即可创建库

1.4 db.createCollection('collectionName');  //创建collection

1.5 collection 允许隐式创建

   db.collectionName.insert(document);

1.6 db.collectionName.drop();       /删除collection

1.7插入user表语句

(1)自动生成id值

 db.user.insert({name:'lisi',age:22})
WriteResult({ "nInserted" : 1 })
> db.user.find();
{ "_id" : ObjectId("5d73077c71b815674de4d152"), "name" : "lisi", "age" : 22 }
(2) 指定生成id

> db.user.insert({_id:2,name:'wangwu',age:25})
WriteResult({ "nInserted" : 1 })
> db.user.find();
{ "_id" : ObjectId("5d73077c71b815674de4d152"), "name" : "lisi", "age" : 22 }
{ "_id" : 2, "name" : "wangwu", "age" : 25 }

(3) 插入多层

> db.user.insert({_id:3,name:'xiaobing',hobby:['basketball','football'],intro:{'title':'My intro','content':'from china'}});
WriteResult({ "nInserted" : 1 })
> db.user.find();
{ "_id" : ObjectId("5d73077c71b815674de4d152"), "name" : "lisi", "age" : 22 }
{ "_id" : 2, "name" : "wangwu", "age" : 25 }
{ "_id" : 3, "name" : "xiaobing", "hobby" : [ "basketball", "football" ], "intro" : { "title" : "My intro", "content" : "from china" } }
>




1.8 其实mongodb不需要声明表,可直接写入表数据,即可创建成功!

> show tables;
user
> db.goods.insert({_id:1,name:'oppoR11',price:'3000'});
WriteResult({ "nInserted" : 1 })
> show tables;
goods
user
> db.goods.find()
{ "_id" : 1, "name" : "oppoR11", "price" : "3000" }

1.9 删除表 db.collectionName.drop();


> show collections
goods
user
> db.goods.drop();
true
> show collections;
user

2.0 删除数据库 db.dropDatabase()

> show dbs;
admin 0.000GB
config 0.000GB
local 0.000GB
shop 0.000GB
> use shop;
switched to db shop

> db.dropDatabase();
{ "dropped" : "shop", "ok" : 1 }
> show dbs;
admin 0.000GB
config 0.000GB
local 0.000GB

2.1 查询帮助 db.help();


> 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) - deprecated
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb, todb, fromhost) - deprecated
db.createCollection(name, {size: ..., capped: ..., max: ...})
db.createView(name, viewOn, [{$operator: {...}}, ...], {viewOptions})
db.createUser(userDocument)
db.currentOp() displays currently executing operations in the db
db.dropDatabase()
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.getPrevError()
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.dropUser(username)
db.repairDatabase()
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.setWriteConcern(<write concern doc>) - sets the write concern for writes to the db
db.unsetWriteConcern(<write concern doc>) - unsets the write concern for writes to the db
db.setVerboseShell(flag) display extra information in shell output
db.shutdownServer()
db.stats()
db.version() current version of the server




 

posted on 2021-10-26 11:09  松晨  阅读(706)  评论(0编辑  收藏  举报