Solve the error of couldn't add user: command createUser requires authentication in MongoDB

Question: MongoDB creates a new database, and an error is reported when adding a user

Description: In the Alibaba Cloud server, after entering MongoDB, a new database is created. When creating a user for this database, an error is reported: Error: command createUser requires authentication

Solution: The reason isInstalled MongoDB with username and password authentication enabled, if you directly create a database user without logging in, an error will be reported, because there is no permission to create, you need to log in to the super user first, for example:

// 切换到默认数据库
use admin
// 登录,用户名和密码请根据自己创建时的设定修改
db.auth("admin","111111")

Switch to the database where the user needs to be created

// 切换数据库
use demo

// 创建用户名为demo,密码为111111的用户,并且该用户的权限是readWrite, 该用户作用于demo数据库
db.createUser({
    
     user:'demo',pwd:'111111',roles:[ {
    
    role:"readWrite", db:'demo'}]});

Guess you like

Origin blog.csdn.net/zy21131437/article/details/122728062