安装 MongoDB
Linux 下安装 MongoDB
# 下载源码
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel80-4.2.5.tgz
# 解压
tar -zxvf mongodb-linux-x86_64-rhel80-4.2.5.tgz
mv mongodb-linux-x86_64-rhel80-4.2.5 /usr/local/mongodb
# 启动
./mongod --config /data/mongodb/mongod.conf
# 查看
netstat -lanp | grep 27017
# 进入数据库后台
./mongo
# 修改配置文件
vim /data/mongodb/mongod.conf
配置文件
systemLog:
destination: file
path: "/data/mongodb/mongod.log"
logAppend: true
storage:
dbPath: "/data/mongodb"
journal:
enabled: true
processManagement:
fork: true
net:
bindIp: 192.168.123.122
port: 27017
setParameter:
enableLocalhostAuthBypass: false
security:
authorization: enabled
Windows 下安装 MongoDB
可以选 社区版 和 企业版 下载。这里选用企业版。
# 安装为服务指令,会随开机启动(注意要用管理员)
.\mongod.exe --config "F:\WorkSpace\ServerSoft\mongodb-win32-x86_64-enterprise-windows-6.0.1\bin\mongo.conf" --install --serviceName "mongodb"
# 如果安装为服务了,可以使用以下指令启动/停止(注意要管理员)
net start mongodb
net stop mongodb
# 卸载服务
.\mongod.exe --remove
配置文件
# 数据库文件
dbpath=F:\WorkSpace\ServerSoft\mongodb-win32-x86_64-windows-6.0.1\data\db
# 端口
port=27017
# 地址
bind_ip=0.0.0.0
# 日志文件
logpath=F:\WorkSpace\ServerSoft\mongodb-win32-x86_64-windows-6.0.1\log\mogodb.log
# 日志采用追加模式
logappend = true
# 是否开启验证
auth=false
遇到的问题
{"t":{"$date":"2022-09-01T13:54:27.690Z"},"s":"F", "c":"CONTROL", "id":20574, "ctx":"-","msg":"Error during global initialization","attr":{"error":{"code":2,"codeName":"BadValue","errmsg":"config requires an absolute file path with Windows services"}}}
指定配置文件需要是绝对路径的,不能是相对路径。
{"t":{"$date":"2022-09-01T13:57:34.285Z"},"s":"F", "c":"CONTROL", "id":20574, "ctx":"-","msg":"Error during global initialization","attr":{"error":{"code":38,"codeName":"FileNotOpen","errmsg":"logpath \"F:\\WorkSpace\\ServerSoft\\mongodb-win32-x86_64-windows-6.0.1\\log\" should name a file, not a directory."}}}
配置的日志路径是文件,不是文件夹路径。
注意,在 MongoDB 6.0 之后,shell是要单独下载的,MongoDB Shell下载地址
下载好后,把它放入环境变量。
这样在命令行输入 mongosh
就可以使用了。
MongoDB用户管理
# 创建用户
mongo>db.createUser(
{ user: "<name>",
pwd: "<cleartext password>",
customData: { <any information> },
roles: [
{ role: "<role>", db: "<database>" } | "<role>",
...
]}
)
# 例子
use admin
db.createUser(
{
user:"root",
pwd:"lgq51233",
roles:[{role:"root",db:"admin"}]
}
)
# 查询用户
show users
# 删除用户
db.dropUser("用户名")
# 修改用户
use admin
db.updateUser("root",{roles:[{role:"readWriteAnyDatabase",db:"admin"}]})
# 修改密码
db.changeUserPassword("root","123")
MongoDB内置角色
- 数据库用户角色:read, readWrite
- 数据库管理角色:dbAdmin, dbOwner, userAdmin
- 集群管理角色:clusterAdmin, clusterManager, clusterMonitor, hostManager
- 备份恢复角色:backup, restore;
- 所有数据库角色:readAnyDatabase, readWriteAnyDatabase, userAdminAnyDatabase, dbAdminAnyDatabase
- 超级用户角色:root
MongoDB 复制集搭建
# 创建3个mongo容器
docker create --name mongo01 -p 27017:27017 -v mongo-data-01:/data/db mongo --replSet "rs0" --bind_ip_all
docker create --name mongo02 -p 27018:27017 -v mongo-data-02:/data/db mongo --replSet "rs0" --bind_ip_all
docker create --name mongo03 -p 27019:27017 -v mongo-data-03:/data/db mongo --replSet "rs0" --bind_ip_all
# 启动容器
docker start mongo01 mongo02 mongo03
# 进入容器操作
docker exec -it mongo01 /bin/bash
# 登录到mongodb
mongo 192.168.123.121:27017
# 初始化复制集集群
rs.initiate( {
_id : "rs0",
members: [
{ _id: 0, host: "192.168.123.121:27017" },
{ _id: 1, host: "192.168.123.121:27018" },
{ _id: 2, host: "192.168.123.121:27019" }
]
})
# 响应结果
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1583976891, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1583976891, 1)
}
# 查看集群信息
rs.status()
测试复制集
# 在主库插入数据
mongo 192.168.123.121:27018
rs0:PRIMARY> use test
rs0:PRIMARY> db.user.insert({"id":1001,"name":"zhangsan"})
WriteResult({ "nInserted" : 1 })
rs0:PRIMARY> db.user.find()
{ "_id" : ObjectId("5e6993e66f3ea7f962ac620f"), "id" : 1001, "name" : "zhangsan" }
# 在复制库查询数据
mongo 192.168.123.121:27017
rs0:SECONDARY> use test
rs0:SECONDARY> db.user.find()
# 报错 默认情况下,复制库没有读写权限
Error: error: {
"operationTime" : Timestamp(1583977533, 1),
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk",
"$clusterTime" : {
"clusterTime" : Timestamp(1583977533, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
# 设置允许从库读取
rs.slaveOk()
添加arbiter节点
# 移除节点
rs.remove("192.168.123.121:27017")
# 进入主节点
mongo 192.168.123.121:27018
# 添加arbiter节点
rs.addArb("192.168.123.121:27017")
# 查看集群状态
rs.status()
搭建分片集群
# 创建3个config节点
docker create --name configsvr01 -p 17000:27019 -v mongoconfigsvr-data01:/data/configdb mongo --configsvr --replSet "rs_configsvr" --bind_ip_all
docker create --name configsvr02 -p 17001:27019 -v mongoconfigsvr-data02:/data/configdb mongo --configsvr --replSet "rs_configsvr" --bind_ip_all
docker create --name configsvr03 -p 17002:27019 -v mongoconfigsvr-data03:/data/configdb mongo --configsvr --replSet "rs_configsvr" --bind_ip_all
# 启动服务
docker start configsvr01 configsvr02 configsvr03
# 进去容器进行操作
docker exec -it configsvr01 /bin/bash
mongo 192.168.123.121:17000
# 集群初始化
rs.initiate(
{
_id: "rs_configsvr",
configsvr: true,
members: [
{ _id : 0, host : "192.168.123.121:17000" },
{ _id : 1, host : "192.168.123.121:17001" },
{ _id : 2, host : "192.168.123.121:17002" }
]
}
)
# 创建2个shard集群,每个集群都有3个数据节点
# 集群1
docker create --name shardsvr01 -p 37000:27018 -v mongoshardsvr-data-01:/data/db mongo --replSet "rs_shardsvr1" --bind_ip_all --shardsvr
docker create --name shardsvr02 -p 37001:27018 -v mongoshardsvr-data-02:/data/db mongo --replSet "rs_shardsvr1" --bind_ip_all --shardsvr
docker create --name shardsvr03 -p 37002:27018 -v mongoshardsvr-data-03:/data/db mongo --replSet "rs_shardsvr1" --bind_ip_all --shardsvr
# 集群2
docker create --name shardsvr04 -p 37003:27018 -v mongoshardsvr-data-04:/data/db mongo --replSet "rs_shardsvr2" --bind_ip_all --shardsvr
docker create --name shardsvr05 -p 37004:27018 -v mongoshardsvr-data-05:/data/db mongo --replSet "rs_shardsvr2" --bind_ip_all --shardsvr
docker create --name shardsvr06 -p 37005:27018 -v mongoshardsvr-data-06:/data/db mongo --replSet "rs_shardsvr2" --bind_ip_all --shardsvr
# 启动容器
docker start shardsvr01 shardsvr02 shardsvr03
docker start shardsvr04 shardsvr05 shardsvr06
# 进去容器执行
docker exec -it shardsvr01 /bin/bash
mongo 192.168.123.121:37000
# 初始化集群1
rs.initiate(
{
_id: "rs_shardsvr1",
members: [
{ _id : 0, host : "192.168.123.121:37000" },
{ _id : 1, host : "192.168.123.121:37001" },
{ _id : 2, host : "192.168.123.121:37002" }
]
}
)
# 初始化集群2
mongo 192.168.123.121:37003
rs.initiate(
{
_id: "rs_shardsvr2",
members: [
{ _id : 0, host : "192.168.123.121:37003" },
{ _id : 1, host : "192.168.123.121:37004" },
{ _id : 2, host : "192.168.123.121:37005" }
]
}
)
# 创建mongos节点容器(路由),需要指定config服务
docker create --name mongos -p 6666:27017 --entrypoint "mongos" mongo:4.0.3 --configdb rs_configsvr/192.168.123.121:17000,192.168.123.121:17001,192.168.123.121:17002 --bind_ip_all
# 进入容器执行
docker exec -it mongos bash
mongo 192.168.123.121:6666
# 添加shard节点
sh.addShard("rs_shardsvr1/192.168.123.121:37000,192.168.123.121:37001,192.168.123.121:37002")
sh.addShard("rs_shardsvr2/192.168.123.121:37003,192.168.123.121:37004,192.168.123.121:37005")
# 启用分片
sh.enableSharding("test")
# 设置分片规则,按照_id的hash进行区分
sh.shardCollection("test.order", {"_id": "hashed" })
# 插入测试数据
use test
for (i = 1; i <= 1000; i=i+1){
db.order.insert({'id':i , 'price': 100+i})
}
# 分别在2个shard集群中查询数据进行测试
db.order.count()
# 集群操作(在mongos中执行)
use config
# 列出所有数据库分片情况
db.databases.find()
# 查看分片的片键
db.collections.find()
# 查询分片集群的状态信息
sh.status()