欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mongoDB数据库安装

程序员文章站 2022-03-03 14:47:24
...

1、 新建文件

vim /etc/yum.repos.d/mongodb-org-4.0.repo
在文件中写入如下内容(注:官网中的gpgkey中有空格和冒号(中文的冒号),是错的)

[mongodb-org-4.0] 
name = MongoDB Repository 
baseurl = https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/ 
gpgcheck = 1 
enabled = 1 
gpgkey = https://www.mongodb.org/static/pgp/server-4.0.asc

2、 安装

[[email protected] yum.repos.d]# yum -y install mongodb-org

3、 配置文件vim /etc/ mongod.conf

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log【日志存放的路径】

# Where and how to store data.
storage:
  dbPath: /var/lib/mongo【数据存放的路径】
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
#限制IP访问,没有逗号 重启
net:
  #bindIpAll:false
  port: 端口
  bindIp: 127.0.0.1 *.*.0.57 #Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
#security:

4、问题

运行命令:./mongod -f …/etc/mongodb.conf时出现如下错误
about to fork child process, waiting until server is ready for connections.
forked process: 6405
ERROR: child process failed, exited with error number 100
To see additional information in this output, start without the “–fork” option.
解决办法:进入数据目录db下,讲mongodb.lock删除,然后在运行该命令就可以啦

5、创建用户角色

> show dbs
admin    0.000GB
config   0.000GB
local    0.000GB
web_db   0.000GB
web_log  0.000GB
> use web_db;
switched to db web_db
> show collections 
device_logs
soft_running_log
user_logs
> db.createUser( {user: "admin",pwd: "123456",roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]})
Successfully added user: {
	"user" : "admin",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	]
}

user:用户名
pwd:密码
roles:指定用户的角色,可以用一个空数组给新用户设定空角色;在roles字段,可以指定内置角色和用户定义的角色。role里的角色可以选:
Built-In Roles(内置角色):
1. 数据库用户角色:read、readWrite;
2. 数据库管理角色:dbAdmin、dbOwner、userAdmin;
3. 集群管理角色:clusterAdmin、clusterManager、clusterMonitor、hostManager;
4. 备份恢复角色:backup、restore;
5. 所有数据库角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase
6. 超级用户角色:root
// 这里还有几个角色间接或直接提供了系统超级用户的访问(dbOwner 、userAdmin、userAdminAnyDatabase)
7. 内部角色:__system

相关标签: 安装