MongoDB的安装启动和服务化以及连接是怎样的
MongoDB的安装启动和服务化以及连接是怎样的,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
MongoDB安装,启动,服务化,连接
install
下载:https://www.mongodb.com/download-center/community/releases ,注意下载对应的版本
解压:将下载的文件解压到一个文件夹
-
创建数据文件夹 mongo-data, 创建log文件夹:mongo-log
本文中的目录是这样的
/opt/mongo/ bin/ # mongo 程序 data/ # 数据 log/ # 日志 mongod.conf # 日志
所有命令相对路径都是相对 /opt/mongo
-
创建用户组和用户
groupadd mongod useradd -g mongod mongod
第一个条命令是添加用户组,第二条是向 mongod 用户组添加用户 mongod
组mongo 用户 对 mongo相关文件的访问权限
chown -R mongo:mongo <mongo相关文件夹>
启动
./bin/mongod --logpath=log/mongo.log --dbpath=data --port=9999 --fork
上面的命令是用登出用户启动的,mongo.log 这个文件要先创建好,否则会报错
--fork 代表后台运行
-
测试是否启动成功
./bin/mongo --port=9999
使用mongo客户端连接一下,上面这个命令是连接localhosts的9999端口上的MONGOD
连接成功后你会看到很多warn
-
Access control is not enabled for the database. Read and write access to data and configuration is unrestricted,
没有使用访问权限限制,后面解决
-
You are running this process as the root user, which is not recommended
不推荐使用root 启动服务,后面解决
-
This server is bound to localhost. Remote systems will be unable to connect to this server. Start the server with --bind_ip <address> to specify which IP addresses it should serve responses from, or with --bind_ip_all to bind to all interfaces. If this behavior is desired, start the server with --bind_ip 127.0.0.1 to disable this warning
说现在服务使用 localhost启动,本机以外的客户端不能访问,并告诉你应该怎么处理,后面在配置文件中解决
-
Soft rlimits too low :
这个问题的官方文档:https://docs.mongodb.com/manual/reference/ulimit/
这个问题的大概意思是linux系统对用户使用各种资源有数量限制,当前系统的一个限制会影响mongo的运行,使用
ulimit -a
查看各种限制官方指出下面几种资源会对mongo的运行有影响,并给出了推荐值
-f (file size): unlimited -t (cpu time): unlimited -v (virtual memory): unlimited [1] -l (locked-in-memory size): unlimited -n (open files): 64000 -m (memory size): unlimited [1] [2] -u (processes/threads): 64000
参照上面的推荐值,和
-ulimit -a
的结果对比一下(按照前面的 -f,-t来对比),哪个不对,就改哪个,比如 现在系统的-n
值是 1024 ,比推荐的64000小,那么就执行命令-ulimit -n 64000
, 之后kill服务再启动如果你的系统是使用systemd的,那么也可以在 .service 的文件中加入下面的配置
[Service] # Other directives omitted # (file size) LimitFSIZE=infinity # (cpu time) LimitCPU=infinity # (virtual memory size) LimitAS=infinity # (locked-in-memory size) LimitMEMLOCK=infinity # (open files) LimitNOFILE=64000 # (processes/threads) LimitNPROC=64000
配置文件:一个简单的例子
systemLog: destination: file path: "/opt/mongo/mongodb/log/mongo.log" net: port: 9999 bindIp: 192.168.145.220,127.0.0.1 storage: dbPath: "/opt/mongo/mongodb/data" processManagement: fork: true
启动时:bin/mongod -f mongod.conf
-
systemctl 启动mongodb
[Unit] Description=mongodb After=network.target remote-fs.target nss-lookup.target [Service] Type=forking ExecStart=/opt/mongo/mongodb/bin/mongod --config /opt/mongo/mongodb/mongod.conf ExecStop=/opt/mongo/mongodb/bin/mongod --shutdown --config /opt/mongo/mongodb/mongod.conf PrivateTmp=true LimitFSIZE=infinity LimitCPU=infinity LimitAS=infinity LimitMEMLOCK=infinity LimitNOFILE=64000 LimitNPROC=64000 Group=mongod User=mongod ExecReload=/bin/kill -s HUP $MAINPID [Install] WantedBy=multi-user.target
对应配置文件应该有如下的配置
processManagement: fork: true pidFilePath: "/opt/mongo/mongodb/log/9999.pid"
如果不设置 pid文件,只设置 fork为true,不能启动,如果两个都没有,那么启动时(service mongo start)时会卡住,但这时会 ctrl + c 你会发现 服务已经启动了。。只有在这两个选项都有的时候,才能特别正常
另外注意 .service 文件中的Group 与User两个选项,设置为最开始的 mongo 就会消除前面的警告
-
创建用户
不创建用户你的数据库就只能在没有安全的情况下运行创建用户以后就可以在登陆 mongo只要都带着用户登陆信息
db.createUser( { user: "superuser", pwd: "123456", roles: [ "root" ] } )
到此为止,你的mongo就可以开启 认证登陆了,mongo的权限是基于角色的,我们现在创建了一个root角色的 superuser 用户
-
在配置文件中加入下面的配置,开启认证登陆
security: authorization: enabled
重启服务
-
登陆时有两种方式
bin/mongo --port 9999 --username superuser --password 123456 --authenticationDatabase admin ## --authenticationDatabase 代表你创建用户的那个数据库 bin/mongo mongodb://localhost:9999/admin?authSource=admin --username superuser ## 然后会让你输入 superman 的密码 ## /admin 代表你要登陆的数据库 ## authSource 与 --authenticationDatabases 是一样的作用
现在我们拥有一个root权限的 superman 用户
先在不需要 auth 的情况下登陆
创建后
use admin
,切换到admin 数据库创建用户
-
完整的创建用户的语句
use reporting db.createUser( { user: "reportsUser", pwd: "2222", roles: [ { role: "read", db: "reporting" }, { role: "read", db: "products" }, { role: "read", db: "sales" }, { role: "readWrite", db: "accounts" } ] } )
对上面的语句解释:
所有这个用户在需时就需要这样
bin/mongo --port 9999 --username reportsUser --password 2222 --authenticationDatabase reporting
或
bin/mongo mongodb://localhost:9999/admin?reporting=admin --username superuser
使用 reporting 数据库
创建一个用户,用户名 reportsUser, 密码:2222 , 对 reporting,products,sales 三个数据库有读的权限,对 accounts 数据库有读写权限
-
删除/添加用户角色
use reporting db.revokeRolesFromUser( "reportsUser", [ { role: "readWrite", db: "accounts" } ] )
use reporting db.grantRolesToUser( "reportsUser", [ { role: "read", db: "accounts" } ] )
更改用户密码
先用有更新用户密码权限的用户登录
-
下面
db.changeUserPassword("reporting", "SOh4TbYhxuLiW8ypJPxmt1oOfL")
看完上述内容,你们掌握MongoDB的安装启动和服务化以及连接是怎样的的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!