不一样平台安装不一样的版本,官网下载地址,我是安装在linux服务器上的,因此选择对应的linux版本linux
下载以后解压,把目录放到经常使用的文件夹里面mongodb
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1404-3.4.6.tgz tar -zxvf mongodb-linux-x86_64-ubuntu1404-3.4.6.tgz mv mongodb-linux-x86_64-ubuntu1404-3.4.6/ /usr/local/mongodb
配置环境变量数据库
export PATH=/usr/local/mongodb/bin:$PATH source /etc/profile
建立数据库和日志目录ubuntu
mkdir -p /data/mongodb/ mkdir -p /data/mongodb/db
在mongodb的安装目录下新建mongodb的配置文件mongod.confbash
dbpath = /data/mongodb/db logpath = /data/mongodb/mongodb.log logappend = true port = 27017 fork = true #auth = true
auth这个参数先诸注释掉,由于尚未配置用户权限服务器
启动mongodbapp
mongod -f /usr/local/mongodb/mongod.conf
先用use命令建立一个库spa
use test
mongodb的用户权限是依附于数据库的,因此建立的用户的时候要指明是那个库的用户日志
db.createUser( { user: "test", pwd: "123456", roles: [ { role: "readWrite", db: "test" } ] } ) user,用户名; pwd,密码; roles,用户角色;db,数据库名称。
修改配置文件,重启mongodb服务code
dbpath = /data/mongodb/db logpath = /data/mongodb/mongodb.log logappend = true port = 27017 fork = true auth = true
使用use命令切换到test库
use test
查看collections,而后会报没有权限的错误
show collections "not authorized on blog to execute command"
使用db.auth命令受权,受权以后就能够进行查询和插入操做
db.auth("test", "123456")
前面的配置文件里面没有--bind_ip这个参数,若是你的配置文件里面配置了这个参数,只要注释掉就能够了,而后再配置下防火墙,ubuntu下配置iptables,保证27017端口能够访问
在本地使用mongo命令登录到远程数据库
mongo ip地址:端口/数据库 -u 用户名 -p 密码
db:当前使用的数据库
show dbs:所有数据库
use:切换数据库,若是库不存在,则建立
db.xxx.find:查询数据
db.xxx.insert: 插入数据
db.xxx.remove:删除数据