首页 公告 项目 RSS

mongodb安装

May 30, 2018 本文有 1121 个字 需要花费 3 分钟阅读

概述

mongodb是一个nosql数据库,所有的数据都是以bson格式去存储在数据库里面的,什么是bson呢,bson是一种比json更强的数据存储格式,如果你是小白,可以直接看做json

centos下安装

首先添加MongoDB的软件源
编辑下面这个文件

vim /etc/yum.repos.d/mongodb.repo

添加

[mongodb-org-3.6]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/amazon/2013.03/mongodb-org/3.6/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc

刷新软件源缓存

yum makecache

安装

sudo yum install mongodb-org

启动

systemctl start mongod

查看运行状态

systemctl status mongod

连接之后会有几个警告

[root@bboysoul ~]# mongo --host localhost
MongoDB shell version v3.6.5
connecting to: mongodb://localhost:27017/
MongoDB server version: 3.6.5
Server has startup warnings:
2018-05-30T17:22:25.377+0800 I CONTROL  [initandlisten]
2018-05-30T17:22:25.377+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-05-30T17:22:25.377+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-05-30T17:22:25.377+0800 I CONTROL  [initandlisten]
2018-05-30T17:22:25.378+0800 I CONTROL  [initandlisten]
2018-05-30T17:22:25.378+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-05-30T17:22:25.378+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-05-30T17:22:25.378+0800 I CONTROL  [initandlisten]
2018-05-30T17:22:25.378+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-05-30T17:22:25.378+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-05-30T17:22:25.378+0800 I CONTROL  [initandlisten]
2018-05-30T17:22:25.378+0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 4096 processes, 64000 files. Number of processes should be at least 32000 : 0.5 times number of files.
2018-05-30T17:22:25.378+0800 I CONTROL  [initandlisten]

我们一步一步解决它
第二三个警告是

2018-05-30T17:22:25.378+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-05-30T17:22:25.378+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-05-30T17:22:25.378+0800 I CONTROL  [initandlisten]
2018-05-30T17:22:25.378+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-05-30T17:22:25.378+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'

意思是要禁用linux的大内存页,输入下面命令禁用

echo never > /sys/kernel/mm/transparent_hugepage/enabled

echo never > /sys/kernel/mm/transparent_hugepage/defrag

这样做其实重启机器之后大内存页是会自动开启的,如果你想一直禁止使用大内存页那么你要这么做修改rc.local文件

vi /etc/rc.d/rc.local

之后加入

if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
   echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
   echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi

注意rc.local要有可执行权限

chmod +x rc.local

之后解决第四项警告

WARNING: soft rlimits too low. rlimits set to 4096 processes, 64000 files. Number of processes should be at least 32000 : 0.5 times number of files.

这个意思是你的系统限制的最大进程数是4096,最大能打开的文件是64000个,所以我们要修改配置文件来消除这个警告,修改下面这个文件

vi /etc/security/limits.conf

加入

mongod soft nofile 64000  
mongod hard nofile 64000  
mongod soft nproc 32000  
mongod hard nproc 32000

之后重启服务,查看还有没有这个警告了

最后解决第一个警告

2018-05-30T23:01:38.417+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-05-30T23:01:38.417+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricte

这个问题的原因就是mongo推荐你使用验证登录数据库,也就是说要设置密码,那么我们就来设置密码使用admin数据库

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
> use admin
switched to db admin

加入密码

db.createUser(
  {
    user: "admin", //用户名
    pwd: "passwd", //密码
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] //设置权限
  }
)

之后修改systemed中的文件

vi /etc/mongod.conf

加入

security:
   authorization: enabled

之后重启数据库登录就好了

macos 安装

首先下载

wget https://fastdl.mongodb.org/osx/mongodb-osx-ssl-x86_64-3.6.5.tgz

之后解压

tar -xvf mongodb-osx-ssl-x86_64-3.6.5.tgz

接着移动到一个自己喜欢的目录并且重命名

mv mongodb-osx-x86_64-3.6.5 ~/Applications/mongo

在zshrc或者bashrc中加入环境变量

vim ~/.zshrc

加入

export PATH=$PATH:/Users/bboysoul/Applications/mongo/bin

使环境变量生效

source ~/.zshrc

执行下mongo看看是不是配置成功,接着新建一个目录来存放mongo的数据

mkdir mongodb

启动服务

mongod --dbpath /Users/bboysoul/Applications/mongo/mongodb --bind_ip 0.0.0.0 &

之后连接数据库

mongo --host localhost

创建用户

> use admin
switched to db admin
> db.createUser(
...   {
...     user: "admin", //用户名
...     pwd: "passwd", //密码
...     roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] //设置权限
...   }
... )
2018-05-30T23:31:55.172+0800 I STORAGE  [conn1] createCollection: admin.system.users with generated UUID: ee4acb87-b294-4359-9397-fe8b5b2581b7
Successfully added user: {
	"user" : "admin",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	]
}

找到mongo的pid kill之后再次打开数据库

mongod --dbpath /Users/bboysoul/Applications/mongo/mongodb --bind_ip 0.0.0.0 --auth &

注意这次是家里–auth的

欢迎关注我的博客www.bboy.app
Have Fun