Mac下MongoDB安装及使用

mac下 MongoDB学习

sudo apt-get install -y mongodb-org=3.2.9 mongodb-org-server=3.2.9 mongodb-org-shell=3.2.9 mongodb-org-mongos=3.2.9 mongodb-org-tools=3.2.9

1. 安装

MongoDB Download Center | MongoDB

解压

下载文件放入/usr/local

open -e .bash_profile 在打开的文件中加入

export PATH=${PATH}:/usr/local/mongodb/bin

source .bash_profile

查看MongoDB 版本

mongod -version

创建数据存放文件

sudo mkdir -p /data/db

添加修改权限

chmod 777 /data/db

2. 设置

mongod –config /usr/local/mongodb/data/etc/mongodb.conf

在mongodb.conf中设置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Where to store the data.
dbpath=/usr/local/mongodb/data/db

#where to log
logpath=/usr/local/mongodb/data/logs/mongodb.log

logappend=true

#bind_ip = 127.0.0.1
#port = 27017

# Enable journaling, http://www.mongodb.org/display/DOCS/Journaling
journal=true

# Enables periodic logging of CPU utilization and I/O wait
#cpu = true

# Turn on/off security. Off is currently the default
#noauth = true
#auth = true

新窗口

mongo

use admin

设置超级用户

db.createUser({user:”admin”,pwd:”admin”,roles:[{role:”root”,db:”admin”}]})

重启

use admin

db.auth(“admin”,”admin1”)

返回1即为成功

普通用户

use bike 创建数据库

db.createUser({user:”bike”,pwd:”123456”,roles:[“readWrite”]})

db.auth(“bike”,”123456”)

3. 启动

mongod –config /data/etc/mongodb.conf

mongo

命令:

use bike

db.auth(“bike”,”123456”)

操作

db.createCollection(“bikes”) 建表

show tables 查看表

db.bikes.insert({“bikeno”:10000,”status”:0}) 插入数据

db.bikes.find() 查看数据

db.dropDatabase(); 删除表

db.users.remove({“_id”: 0}); 对应数据

本文结束  感谢您的阅读