Centos7 manual installation and configuration of MongoDB

1. Download the installation package from MongoDB official website and install it

#上传安装包解压到安装目录
tar -zxvf mongodb-linux-x86_64-rhel70-4.4.1.tgz  -C /usr/local
# 进入到安装目录
cd /usr/local
#修改安装文件夹名称为mongodb
mv mongodb-linux-x86_64-rhel70-4.4.1 mongodb 

2. Create the necessary folders

# 创建存放数据的目录
mkdir -p /usr/local/mongodb/data/db
# 创建存放日志的目录
mkdir -p /usr/local/mongodb/logs
# 创建日志记录文件
touch /usr/local/mongodb/logs/mongodb.log

# 赋予文件夹独写权限
chomd -R 777 /usr/local/mongodb/data/db
#赋予文件读写权限
chmod a+w -R /usr/local/mongodb/logs/mongodb.log

3. Start

# 切换至指定目录
cd /usr/local/mongodb/

3.1. Foreground start

# 启动后控制台是阻塞的状态
bin/mongod --dbpath /usr/local/mongodb/data/db --logpath /usr/local/mongodb/logs/mongodb.log --logappend --port 27017 --bind_ip 0.0.0.0
# 结束前台启动
ctrl+c
3.2, background start
# 控制台不会出现阻塞
bin/mongod --dbpath /usr/local/mongodb/data/db --logpath /usr/local/mongodb/logs/mongodb.log --logappend --port 27017 --bind_ip 0.0.0.0 --fork
# 结束后台启动
bin/mongod --dbpath /usr/local/mongodb/data/db --logpath /usr/local/mongodb/logs/mongodb.log --logappend --port 27017 --bind_ip 0.0.0.0 --fork --shutdown

--dbpath:指定数据文件存放目录
--logpath:指定日志文件,注意是指定文件不是目录
--logappend:使用追加的方式记录日志
--port:指定端口,默认为27017
--bind_ip:绑定服务IP,若绑定127.0.0.1,则只能本机访问,默认本机访问 设置为0.0.0.0为所有机器可以访问

测试是否启动,进入到/usr/local/mongodb使用客户端访问
连接命令:bin/mongo

3.3. Configuration file method (recommended method)

cd /usr/local/mongodb
# 创建配置文件
vim bin/mongodb.conf
# 赋予文件可执行权限
chmod a+w -R /usr/local/mongodb/bin/mongodb.conf

###################### mongodb.conf内容填充 开始 #######################
# 数据文件存放目录
dbpath  = /usr/local/mongodb/data/db
# 日志文件存放目录
logpath = /usr/local/mongodb/logs/mongodb.log
# 以追加的方式记录日志
logappend = true
# 端口默认为 27017
port = 27017
# 对访问IP地址不做限制,默认本机地址127.0.0.1
bind_ip = 0.0.0.0
# 以守护进程的方式启用,即在后台运行
fork = true
###################### mongodb.conf内容填充 结束 #######################

# 配置完配置文件,启动指定配置文件
bin/mongod -f bin/mongodb.conf
# 停止方式
bin/mongod -f bin/mongodb.conf --shutdown

4. MongoDB related configuration

# 打开环境变量文件
vim /etc/profile
# 找到环境变量的配置
/export
# 环境变量设置
JAVA_HOME=/usr/local/java/jdk1.8.0_261
CLASSPATH=$JAVA_HOME/lib/
MONGODB_HOME=/usr/local/mongodb
PATH=$PATH:$JAVA_HOME/bin:$MONGODB_HOME/bin
export PATH JAVA_HOME CLASSPATH

# 使环境变量立即生效
source /etc/profile

# 配置完环境变量的启动方式
mongod -f /usr/local/mongodb/bin/mongodb.conf
# 关闭服务
mongod -f /usr/local/mongodb/bin/mongodb.conf --shutdown

5. MongoDB project command operation

use admin  #切换数据库
db.shutdownServer()  #关闭服务
# 创建用户(用户适用于任意数据库)
db.createUser({
    
    user: "uaad",pwd: "uaad",roles:[{
    
     role: "userAdminAnyDatabase", db: "admin" }]});
# 更改用户角色
db.updateUser("uaad",{
    
    roles:[{
    
     role: "userAdminAnyDatabase", db: "admin" },{
    
     role: "readWriteAnyDatabase", db: "admin" },{
    
     role: "dbAdminAnyDatabase", db: "admin" }]});
# 更改用户密码
db.updateUser("用户名"{
    
    "pwd":"新密码"});
db.changeUserPassword("用户名","新密码");
# 删除用户 需要具有userAdmin或userAdminAnyDatabase或root角色的用户才可以删除其他用户
db.dropUser("用户名称")
#### 在配置文件/usr/local/mongodb/bin/mongodb.conf新增身份认证命令
# 开启身份认证
auth = true
# 认证身份 设置的用户名和密码
db.auth("uaad","uaad");
#可视化工具下载安装(下载右边开源免费版本)
https://robomongo.org/download

6. Open port number

firewall-cmd --zone=public --add-port=27017/tcp --permanent
firewall-cmd --reload   # 配置立即生效

7. Project connection

If it is a newly created database, you need to verify that you need to set the corresponding user and password for the database before you can connect

 # 新创建数据库lm_db
 use lm_db
 # 创建对应的用户
 db.createUser({
    
    user: "uaad",pwd: "uaad",roles:[{
    
     role: "dbAdmin", db: "lm_db" }]});

MongoDB currently has 7 roles built in.

Database user roles: read, readWrite;
database management roles: dbAdmin, dbOwner, userAdmin;
cluster management roles: clusterAdmin, clusterManager, clusterMonitor, hostManager;
backup and recovery roles: backup, restore;
all database roles: readAnyDatabase, readWriteAnyDatabase, userAdminAnyDatabase, dbAdminAnyDatabase
super User role: root; here are several roles that indirectly or directly provide access to system superusers (dbOwner, userAdmin, userAdminAnyDatabase)
Internal roles: __system
The corresponding functions of these roles are as follows:

Read: Allows users to read the specified database
readWrite: Allows users to read and write specified databases
dbAdmin: Allows users to perform management functions in the specified database, such as index creation, deletion, viewing statistics or accessing system.profile
userAdmin: Allows users to collect system.users Write, you can create, delete, and manage user
clusterAdmin in the specified database: it is only available in the admin database, and grants the user management permissions for all shards and replica set-related functions.
readAnyDatabase: Available only in the admin database, granting the user read permissions for all databases
readWriteAnyDatabase: Available only in the admin database, granting the user read and write permissions for
all databases userAdminAnyDatabase: Available only in the admin database, granting the user userAdmin permissions for all databases
dbAdminAnyDatabase : Available only in the admin database, granting the user dbAdmin privileges for all databases.
root: only available in the admin database. super account, super authority

Guess you like

Origin blog.csdn.net/qq_21875331/article/details/117189553