MongoDB与Redis在doker上的安装配置
MongoDB
参考:
- mvertes/alpine-mongo:https://hub.docker.com/r/mvertes/alpine-mongo
- MongoDB 教程:http://www.runoob.com/mongodb/mongodb-tutorial.html
- Redis: [https://hub.docker.com//redis?tab=description](https://hub.docker.com//redis?tab=description)
- redis默认配置:http://download.redis.io/redis-stable/redis.conf
创建网络 net-mysql
docker network create -d bridge network_backend
创建mongod容器
docker run --rm -it \
--name my-mongo \
--network network_backend \
-p 27017:27017 \
-v $PWD/data/db:/data/db \
-v $PWD/conf/mongod.conf:/etc/mongod.conf \
-v $PWD/log:/var/log/mongodb \
mvertes/alpine-mongo sh
docker run --rm -it \
--name my-mongo \
--network network_backend \
-p 27017:27017 \
-v $PWD/data/db:/data/db \
-v $PWD/conf/mongod.conf:/etc/mongod.conf \
-v $PWD/log:/var/log/mongodb \
mvertes/alpine-mongo \
mongod --bind_ip 0.0.0.0
连接mongodb,添加用户
docker run --rm -it \
--network network_backend \
mvertes/alpine-mongo \
mongo --host my-mongo
show dbs
use admin
//不受访问限制的超级用户root
db.createUser(
{
user:"root",
pwd:"xxxxxxxxxxxxx",
roles:["root"]
}
)
//admin超级管理员用户
db.createUser(
{
user: "luowei",
pwd: "xxxxxxxxxxxxxxxx",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
//业务数据库管理员用户
db.createUser({
user:"mongo",
pwd:"xxxxxxxxxxxx",
customData:{
name:"mongo",
email:"luowei@wodedata.com",
age:28,
},
roles:[
{role:"read",db:"admin"}
]
})
开启登录认证
docker run --rm -it \
--name my-mongo \
--network network_backend \
-p 27017:27017 \
-v $PWD/data/db:/data/db \
-v $PWD/conf/mongod.conf:/etc/mongod.conf \
-v $PWD/log:/var/log/mongodb \
mvertes/alpine-mongo \
mongod --bind_ip 0.0.0.0 --auth
docker run -d \
--name my-mongo \
--restart always \
--network network_backend \
-p 27017:27017 \
-v $PWD/data/db:/data/db \
-v $PWD/conf/mongod.conf:/etc/mongod.conf \
-v $PWD/log:/var/log/mongodb \
mvertes/alpine-mongo \
mongod --bind_ip 0.0.0.0 --auth
# docker exec -it my-mongo sh
授权登录
docker run --rm -it \
--network network_backend \
mvertes/alpine-mongo \
mongo --host my-mongo \
-u root \
-p xxxxxxxxxxx \
--authenticationDatabase admin
> show dbs;
> exit;
docker run --rm -it \
--network network_backend \
mvertes/alpine-mongo \
mongo --host my-mongo \
-u mongo \
-p xxxxxxxxxxxxxxx \
--authenticationDatabase admin
> show dbs;
使用userAdminAnyDatabase以上权限用户登录后,给mongo数据库授权
use db_biz
db.createUser({
user:"mongo",
pwd:"xxxxxxxxxxxxx",
customData:{
name:"mongo",
email:"luowei@wodedata.com",
age:28,
},
roles:[
{role:"readWrite",db:"test"},
{role:"dbAdmin",db:"db_user"}
]
})
//或者
db.grantRolesToUser(
"mongo",
[
{role:"readWrite",db:"db_user"}
]
)
db.createUser({
user:"luowei",
pwd:"xxxxxxxxxxxxx",
customData:{
name:"mongo",
email:"luowei@wodedata.com",
age:28,
},
roles:[
{role:"readWrite",db:"test"},
{role:"readWrite",db:"db_user"}
]
})
db.collection_test.insert({"name":"你好collection_test!"})
MongoDB常用命令
创建数据库使用`use xxx`命令即可
use db_test
列出所有数据库
show dbs
使用`db`显示当前正在use的数据库
db
但空数据库使用`show dbs`是不是显示出来,只要向`db_test`数据库添加一个集合即可显示了
db.createCollection("collection_a")
插入文档,若集合`collection_b`不存在,则会自动创建
db.collection_b.insert({"name":"你好collection_b!"})
查看集合中已插入的文档
db.collection_b.find()
清除集合中的数据
db.collection_b.remove({})
查看已有集合
show collections
删除集合
db.collection_a.drop()
db.collection_b.drop()
删除数据库
use db_test
db.dropDatabase()
Redis
运行Redis
docker run --rm -it\
--name my-redis \
--network network_backend \
-p 6379:6379 \
-v $PWD/data:/data \
redis:alpine \
redis-server
docker run --rm -it\
--name my-redis \
--network network_backend \
-p 6379:6379 \
-v $PWD/data:/data \
-v $PWD/conf/redis.conf:/etc/redis/redis.conf \
redis:alpine \
redis-server /etc/redis/redis.conf
docker run -d\
--name my-redis \
--restart always \
--network network_backend \
-p 6379:6379 \
-v $PWD/data:/data \
-v $PWD/conf/redis.conf:/etc/redis/redis.conf \
redis:alpine \
redis-server /etc/redis/redis.conf
//使用redis-cli连接
docker exec -it my-redis sh
/data # redis-cli
> auth xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
OK