安装mongodb的方法参考此文档:https://blog.csdn.net/ywd1992/article/details/81947357
/usr/local/mongodb/bin/mongo --port 21000
切换到admin用户下,添加一个新的用户
use admin
db.createUser(
{
user: "csdn",
pwd: "123456",
roles: ["userAdminAnyDatabase", "dbAdminAnyDatabase", "readWriteAnyDatabase", "clusterAdmin"]
}
)
可以看到如下成功提示
Successfully added user: {
"user" : "csdn",
"roles" : [
"userAdminAnyDatabase",
"dbAdminAnyDatabase",
"readWriteAnyDatabase",
"clusterAdmin"
]
}
/usr/local/mongodb/bin/mongo --port 27001
切换到admin用户下,添加一个新的用户
use admin
db.createUser(
{
user: "csdn",
pwd: "123456",
roles: ["userAdminAnyDatabase", "dbAdminAnyDatabase", "readWriteAnyDatabase", "clusterAdmin"]
}
)
可以看到如下成功提示
Successfully added user: {
"user" : "csdn",
"roles" : [
"userAdminAnyDatabase",
"dbAdminAnyDatabase",
"readWriteAnyDatabase",
"clusterAdmin"
]
}
/usr/local/mongodb/bin/mongo --port 27002
切换到admin用户下,添加一个新的用户
use admin
db.createUser(
{
user: "csdn",
pwd: "123456",
roles: ["userAdminAnyDatabase", "dbAdminAnyDatabase", "readWriteAnyDatabase", "clusterAdmin"]
}
)
可以看到如下成功提示
Successfully added user: {
"user" : "csdn",
"roles" : [
"userAdminAnyDatabase",
"dbAdminAnyDatabase",
"readWriteAnyDatabase",
"clusterAdmin"
]
}
/usr/local/mongodb/bin/mongo --port 27003
切换到admin用户下,添加一个新的用户
use admin
db.createUser(
{
user: "csdn",
pwd: "123456",
roles: ["userAdminAnyDatabase", "dbAdminAnyDatabase", "readWriteAnyDatabase", "clusterAdmin"]
}
)
可以看到如下成功提示
Successfully added user: {
"user" : "csdn",
"roles" : [
"userAdminAnyDatabase",
"dbAdminAnyDatabase",
"readWriteAnyDatabase",
"clusterAdmin"
]
}
任意节点生成,发送到集群中其他节点
所有节点创建密钥目录
mkdir /usr/local/mongodb/key
openssl rand -base64 756 >/usr/local/mongodb/key/mongo_auth.key
scp /usr/local/mongodb/key/mongo_auth.key [email protected]:/usr/local/mongodb/key/
scp /usr/local/mongodb/key/mongo_auth.key [email protected]:/usr/local/mongodb/key/
chmod 0600 /usr/local/mongodb/key/mongo_auth.key
mongos配置文件添加配置(所有节点)
vim /usr/local/mongodb/conf/mongos.conf
注意yml文件格式,前面写了两个空格
keyFile=/usr/local/mongodb/key/mongo_auth.key
config和shard配置文件分别添加(所有节点)
vim /usr/local/mongodb/conf/config.conf
vim /usr/local/mongodb/conf/shard1.conf
vim /usr/local/mongodb/conf/shard2.conf
vim /usr/local/mongodb/conf/shard3.conf
auth=true
keyFile=/usr/local/mongodb/key/mongo_auth.key
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/config.conf
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/shard1.conf
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/shard2.conf
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/shard3.conf
/usr/local/mongodb/bin/mongos -f /usr/local/mongodb/conf/mongos.conf
mongo --host 192.168.0.101 --port 20000
use admin
show dbs
db.auth("csdn","123456")
mongo 192.168.0.101:20000/admin -u csdn -p123456
use my_database;
db.createUser({
user:"zhangsan",
pwd:"123456",
roles: [ { role: "readWrite",db:"my_database"}]
})