一、选择阿里yum源,国外实在太慢
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/ (mongodb官网)
https://developer.aliyun.com/mirror/ (阿里镜像网站) 选择偶数版本,奇数版不适合生产使用
#vim /etc/yum.repos.d/mongodb-org-4.2.repo [mongodb-org-4.2] name=MongoDB Repository baseurl=https://mirrors.aliyun.com/mongodb/yum/redhat/7Server/mongodb-org/4.2/x86_64/ gpgcheck=0 enabled=1
# yum install -y mongodb-org
# systemctl restart mongod
# systemctl status mongod
#chkconfig mongod
on (开机自启)
二、使用
##进入数据库 # mongo (任意目录下) ## 查看数据库 > show dbs; ## 查看数据库版本 > db.version(); ## 常用命令帮助 > db.help();
三、错误记录
Job for mongod.service failed because the control process exited with error code. See "systemctl status mongod.service" and "journalctl -xe" for details.
解决:查看日志( /var/log/mongodb/)
#cat mongod.log
删除 # rm -rf mongodb-27017.sock
正确的关闭方式:
通过shell连上服务器:
>>use admin
>>db.shutdownServer()
切忌kill -9 pid
四、卸载
卸载移除mongo
#yum erase $(rpm -qa | grep mongodb-org)
移除数据库文件和日志文件
#rm -r /
var
/log/mongodb
#rm -r /
var
/lib/mongo
五、创建用户
创建超级管理员用户:
use admin
db.createUser(
... {
... user: "root",
... pwd: "root",
... roles: [ { role: "root", db: "admin" } ]
... }
... );
创建admin
db.createUser({
... user: "admin",
... pwd: "admin",
... roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
... });
参考 https://www.cnblogs.com/wusy/p/10460928.html