进入容器
[penglai@bogon ~]$ sudo docker ps
[sudo] password for penglai:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3a958e106be4 river/centos-mysql-redis-rabbitmq "init" 43 minutes ago Up 43 minutes 0.0.0.0:3306->3306/tcp, 0.0.0.0:5672->5672/tcp, 0.0.0.0:6379->6379/tcp, 0.0.0.0:15672->15672/tcp friendly_leakey
[penglai@bogon ~]$ sudo docker exec -it 3a958e106be4 bash
[root@3a958e106be4 /]#
安装mongo
创建文件
vi /etc/yum.repos.d/mongodb-org-4.0.repo
[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
安装命令
yum install -y mongodb-org
修改外网可以访问
配置文件位置 /etc/mongod.conf
将net:bindIp: 127.0.0.1 改为 0.0.0.0
启动服务
systemctl start mongod #启动服务
systemctl ennable mongod #设置开机自动启动
生成docker image
docker commit -m="has mysql" -a="river" 3a958e106be4 river/centos5
docker stop 3a958e106be4
启动新的docker
docker run -itd --privileged=true -p 3306:3306 -p 6379:6379 -p 5672:5672 -p 15672:15672 -p 27017:27017 river/centos5 init
sudo docker exec -it b6250d32c91a bash #进入docker
通过用工具链接测试
image.png显示已经ok 了。
设置用账号登录
配置文件修改如下
image.png
重启服务
[root@b6250d32c91a /]# systemctl restart mongod
登录mongo
[root@b6250d32c91a /]# mongo
创建root用户
use admin //切换到 admin仓库
db.createUser({user:'root',pwd:'root',roles:['userAdminAnyDatabase']}) //创建root用户
show dbs // 提示错误 command listDatabases requires authentication 需要认证
db.auth('root','root') //认证
创建普通用户
>use mydb
> db.createUser({user:'river',pwd:'123456',roles:['readWrite']})
Successfully added user: { "user" : "river", "roles" : [ "readWrite" ] }
> db.auth("river","123456")
如果提示认证用户太多的话,退出重新登录
[root@b6250d32c91a /]# mongo --port 27017 -u "river" -p "123456" --authenticationDatabase "mydb"
再通过客户端链接
image.png查看下 user信息,有时候这里配置不对用户访问会出问题
[root@b6250d32c91a /]# mongo --port 27017 -u "admin" -p "123456" --authenticationDatabase "admin"
MongoDB shell version v4.0.9
connecting to: mongodb://127.0.0.1:27017/?authSource=admin&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("7a2b6f48-f1f9-4c05-a7d3-81929090040d") }
MongoDB server version: 4.0.9
> use admin
switched to db admin
> show users
{
"_id" : "admin.admin",
"userId" : UUID("e6d53075-699c-46e8-9f89-9258608c3be5"),
"user" : "admin",
"db" : "admin",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
},
{
"role" : "readWriteAnyDatabase",
"db" : "admin"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
> user weimall
2019-04-28T13:38:34.152+0000 E QUERY [js] SyntaxError: missing ; before statement @(shell):1:5
> use weimall
switched to db weimall
> show users
{
"_id" : "weimall.penglai",
"userId" : UUID("ce8a8ee8-fbed-4e25-a231-69f513ac00c0"),
"user" : "penglai",
"db" : "weimall",
"roles" : [
{
"role" : "readWrite",
"db" : "weimall"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
>
ok 这样就设置成了。
可以生成自己的docker image 了
设置mongo 开机自启动
systemctl enable mongod.service
参考地址
https://segmentfault.com/a/1190000016877915
https://my.oschina.net/u/3939059/blog/1933194
https://www.jianshu.com/p/79caa1cc49a5
网友评论