Linux_CentOS in Mongodb4.x installation, remote management, configuration mongodb administrator password

Mongodb4.x installation

The official document: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/

1 , arranged yum source
1 ), the path /etc/yum.repos.d/ created file mongodb-org-4.2.repo
cd /etc/yum.repos.d/
touch mongodb-org-4.2.repo
2 ) in the file mongodb-org-4.2.repo write the following in the ( following content can be copied directly, or you can copy official documents )
[mongodb-org-4.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc
2 , yum installation MongoDB (installed mongodb-org package and its dependencies mongodb-org-Server , mongodb-org-mongos , mongodb-org-the shell , mongodb-org-Tools )
Examples of the default database in / var / lib / mongo of the path, the default log in / var / log / mongodb of the path, you can also modify the configuration file /etc/mongod.conf file storage.dbPath and systemLog.path configuration
Installation mongodb
yum install -y mongodb-org

mongodb Service

systemctl start mongod
Boot mongoDb
systemctl enable mongod

Stop mongodb Service

systemctl stop mongod

 

Restart mongoDB Service

systemctl restart mongod

Mongodb4.x Uninstall

Out of service

service mongod stop

Remove the installation package

rpm -qa | grep mongodb-org list all packages
  yum remove -y $(rpm -qa | grep mongodb-org)

Delete the data and log

rm -r / was / log / mongodb
rm -r / was / lib / mongo

Remote connection mongodb

1 , modify mongo.conf file
command:

south you /etc/mongod.conf 

 

将原来 bindIp:127.0.0.1 修改为 0.0.0.0(mongodb 的配置文件中的 bind_ip 默认为 127.0.0.1,默认只有本机可以连接。 此时,需要将 bind_ip 配置为 0.0.0.0,表示接受任何 IP 的连接。)

2、重启动 mongo 服务:service mongod restart

3、永久开放 27017 端口:

firewall-cmd --zone=public --add-port=27017/tcp --permanent ; (–permanent 永久生效,没有此参数重启后 失效)
firewall-cmd --reload

配置 Mongodb4.x 管理员密码

 输入 mongo 命令连接服务端

客户端:mongo 使用数据库
客户端:mongo 使用数据库 ip 地址:端口号

 创建超级管理用户

use admin 
db.createUser({
    user:'admin',
    pwd:'admin', 
    roles:[{role:'root',db:'admin'}]
})

修改 mongo.conf 文件

 

sudo vi /etc/mongod.conf 

 

添加:

security:
    authorization: enabled

重启 mongodb 服务

 

systemctl restart mongod

 

超级管理员账户连接数据库

mongo admin -u 用户名 -p 密码
mongo 192.168.1.200:27017/test -u user -p password

testDB 数据库创建一个用户 只能访问 testDB 不能访问其他数据库

 

use testDB
db.createUser( {
    user: "testadmin",
    pwd: "123456",
    roles: [ { role: "dbOwner", db: "testDB" } ]
} )

连接testDB数据库

mongo 192.168.1.200:27017/testDB -u testadmin -p 123456

Mongodb 账户权限配置中常用的命令

show users; #查看当前库下的用户
db.dropUser("eggadmin") #删除用户
db.updateUser( "admin",{pwd:"password"}); #修改用户密码
db.auth("admin","password"); #密码认证

Mongodb 数据库角色   

1.数据库用户角色:readreadWrite;

2. database administration roles: dbadmin , dbowner , useradmin ;

3. Cluster Management role: clusterAdmin , ClusterManager , clusterMonitor , hostManager ;

4. backup and recovery roles: Backup , Restore ;

5. All database roles: readAnyDatabase , readWriteAnyDatabase , userAdminAnyDatabase , dbAdminAnyDatabase

6. superuser roles: root,

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/loaderman/p/11652233.html