docker 安装mongodb和初步使用

安装步骤和一些命令行操作 默认安装  版本装的4.0.3 


[root@VM_0_8_centos ~]# docker pull  mongo  
Using default tag: latest
latest: Pulling from library/mongo
3b37166ec614: Pull complete 
504facff238f: Pull complete 
ebbcacd28e10: Pull complete 
c7fb3351ecad: Pull complete 
2e3debadcbf7: Pull complete 
004c7a04feb1: Pull complete 
897284d7f640: Pull complete 
af4d2dae1422: Pull complete 
5e988d91970a: Pull complete 
aebe46e3fb07: Pull complete 
6e52ad506433: Pull complete 
47d2bdbad490: Pull complete 
0b15ac2388a7: Pull complete 
7b8821d8bba9: Pull complete 
Digest: sha256:4ad50a4f3834a4abc47180eb0c5393f09971a935ac3949920545668dd4253396
Status: Downloaded newer image for mongo:latest
[root@VM_0_8_centos ~]# mongo -version
-bash: mongo: command not found

[root@VM_0_8_centos ~]# docker run  --name some-mongo   -p 27017:27017   -d mongo   --auth  //运行
c4b8386fe05a87feada72bde4ee6441f50ed2810594c677d1808fcb2add7c47b
[root@VM_0_8_centos ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                      NAMES
c4b8386fe05a        mongo               "docker-entrypoint.s…"   6 seconds ago       Up 5 seconds        0.0.0.0:27017->27017/tcp   some-mongo
[root@VM_0_8_centos ~]# docker    exec  -it c4b8386fe05a   /bin/bash         //c4b8386fe05a 是前面镜像的容器id 
root@c4b8386fe05a:/# mongo    //进入命令行
MongoDB shell version v4.0.3
connecting to: mongodb://127.0.0.1:27017
Implicit session: session { "id" : UUID("b09a229e-5b88-4004-980b-a9b352135870") }
MongoDB server version: 4.0.3
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
    http://docs.mongodb.org/
Questions? Try the support group
    http://groups.google.com/group/mongodb-user
> use admin   //admin是个默认的数据库
switched to db admin
> db.createUser({user:"root",pwd:"root",roles:[{role:'root',db:'admin'}]})  
Successfully added user: {
    "user" : "root",
    "roles" : [
        {
            "role" : "root",
            "db" : "admin"
        }
    ]
}
> exit
bye
root@c4b8386fe05a:/# mongo  127.0.0.1/admin  -utest -p   //-u前面对应账号root 这里故意写错看看能否登陆
MongoDB shell version v4.0.3
Enter password: 
connecting to: mongodb://127.0.0.1:27017/admin
Implicit session: session { "id" : UUID("fe22489a-00f0-4f9b-b22f-4c0429650bfd") }
MongoDB server version: 4.0.3
2018-10-10T09:27:58.092+0000 E QUERY    [js] Error: Authentication failed. :
DB.prototype._authOrThrow@src/mongo/shell/db.js:1685:20
@(auth):6:1
@(auth):1:2
exception: login failed
root@c4b8386fe05a:/# mongo  127.0.0.1/admin  -uroot -p   //还是写对才能登陆
MongoDB shell version v4.0.3
Enter password: 
connecting to: mongodb://127.0.0.1:27017/admin
Implicit session: session { "id" : UUID("2c285f96-5025-4057-9b2f-94d5510a572e") }
MongoDB server version: 4.0.3
Server has startup warnings: 
2018-10-10T09:18:40.312+0000 I STORAGE  [initandlisten] 
2018-10-10T09:18:40.312+0000 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2018-10-10T09:18:40.312+0000 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2018-10-10T09:18:41.107+0000 I CONTROL  [initandlisten] 
2018-10-10T09:18:41.107+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-10-10T09:18:41.107+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-10-10T09:18:41.108+0000 I CONTROL  [initandlisten] 
2018-10-10T09:18:41.108+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-10-10T09:18:41.108+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-10-10T09:18:41.108+0000 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()
---

> set name hello   //和redis不同 不能这么写
set name to hello
> get name     //和redis不同 不能这么写
2018-10-10T09:28:50.080+0000 E QUERY    [js] SyntaxError: missing ; before statement @(shell):1:4
> get
2018-10-10T09:29:01.011+0000 E QUERY    [js] ReferenceError: get is not defined :
@(shell):1:1
> mongo
2018-10-10T09:29:47.116+0000 E QUERY    [js] ReferenceError: mongo is not defined :
@(shell):1:1
> show dbs   //查看当前数据库
admin   0.000GB
config  0.000GB
local   0.000GB
> show collections
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
> use test   //创建一个test库 但是要插入数据才能生效
switched to db test
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
> db.hello.insert({"name":"testdb"})   //插入数据才能生效 hello 是个collection 属于新建
WriteResult({ "nInserted" : 1 })

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
test    0.000GB
> db
test
> show collections
hello
> db.h
db.hasOwnProperty  db.hello           db.help(           db.hostInfo(
> db.hello.find()                   //查询
{ "_id" : ObjectId("5bbdc71dbf9746c968669b43"), "name" : "testdb" }
 

参考: 

docker中安装mongo

https://blog.csdn.net/meibo69631310/article/details/79709500

docker pull  mongo 

 

docker run  --name some-mongo   -p 27017:27017   -d mongo   --auth     //这里的--name 放在前面并映射端口

 

docker    exec  -it  容器ID   /bin/bash     //进入容器

 

mongo  

 

use admin

 

db.createUser({user:"root",pwd:"root",roles:[{role:'root',db:'admin'}]})   //创建用户,此用户创建成功,则后续操作都需要用户认证

 

exit  

 

 

测试:

 

mongo  宿主机ip/admin  -utest -p

 

查看连接是否成功  

 

 

默认mongodb是不使用用户认证。

 

show dbs

如果出现not authorized on admin to execute command { listDatabases: 1.0, $db: \"admin\" }错误,那就进行用户认证

use admin

db.auth("root","root")

返回 1 认证成功。

MongoDB 基础命令行

https://www.cnblogs.com/wywnet/p/5102946.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值