【安装文档】mongodb安装步骤说明(Linux版)
前言
本文主要讲解在Linux服务器上安装mongodb服务。
所需安装文件:
mongodb-linux-x86_64-rhel55-3.2.8.tgz
安装步骤
[@bx_44_177 /opt/mongodb]#mkdir -p /opt/mongodb/data
[@bx_44_177 /opt/mongodb]#mkdir -p /opt/mongodb/logs
[@bx_44_177 /opt/mongodb]#tar zxvf mongodb-linux-x86_64-rhel55-3.2.8.tgz
[@bx_44_177 /opt/mongodb]#cd mongodb-linux-x86_64-rhel55-3.2.8/bin/
[@bx_44_177 /opt/mongodb]#mongod --dbpath /opt/mongodb/data/
启动mongodb命令:
[@bx_44_177 /opt/mongodb]#./mongod -port 10001 --dbpath /opt/mongodb/data/ --logpath /opt/mongodb/logs/mongodb.log --httpinterface
参数:
-port 指定启动端口号
--dbpath 指定数据文件路径
--logpath 指定日志文件路径
--fork 后台运行
--httpinterface enable http interface 开启http端口访问日志
详细参数说明详见:./mongod -h
为方便使用,可以将mongodb参数放到mongodb.conf 配置文件:
[@bx_44_177 /opt/mongodb]#mkdir conf/
[@bx_44_177 /opt/mongodb]# vi conf/mongodb.conf
#mongodb conf file
port=10001
dbpath=/opt/mongodb/data/
logpath=/opt/mongodb/logs/mongodb.log
logappend=true
解释说明:
port=10001【代表端口号,如果不指定则默认为 27017 】
dbpath=data/ 【数据库路径】
logpath=log/mongodb.log 【日志路径】
logappend=true 【日志文件自动累加,而不是覆盖】
启动mongodb:
[@bx_44_177/opt/mongodb]#
./mongodb-linux-x86_64-rhel55-3.2.8/bin/mongod -f /opt/mongodb/conf/mongodb.conf
使用mongo连接测试
使用mongo终端连接mongodb数据库验证:
[@bx_44_177 /opt/mongodb]#cd mongodb-linux-x86_64-rhel55-3.2.8/bin/
[@bx_44_177/opt/mongodb/mongodb-linux-x86_64-rhel5-3.2.8/bin]#./mongo localhost:10001
MongoDB shell version: 3.2.8
connecting to: localhost:10001/test
Server has startup warnings:
2016-08-14T16:20:36.167+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2016-08-14T16:20:36.167+0800 I CONTROL [initandlisten]
2016-08-14T16:20:36.167+0800 I CONTROL [initandlisten]
2016-08-14T16:20:36.167+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. The locked memory size is 32768 bytes, it should be at least 65536 bytes
2016-08-14T16:20:36.167+0800 I CONTROL [initandlisten]
2016-08-14T16:20:36.167+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 16384 processes, 65535 files. Number of processes should be at least 32767.5 : 0.5 times number of files.
>db.foo.save({a:1})
WriteResult({ "nInserted" : 1 })
>db.foo.save({a:2})
WriteResult({ "nInserted" : 1 })
> db.foo.find() --查询所有
{ "_id" : ObjectId("57b0203970e2164b52f83a06"), "a" : 1 }
{ "_id" : ObjectId("57b0204170e2164b52f83a07"), "a" : 2 }
> db.foo.find({a:1}) --按指定条件查询
{ "_id" : ObjectId("57b0203970e2164b52f83a06"), "a" : 1 }
> show dbs --相当于mysql的show databases;
local 0.000GB
test 0.000GB
> use test --切换到test数据库
switched to db test
> show tables --查看当前数据库表
foo
>
查看mongod服务端口
[@bx_44_177 /opt/mongodb]# netstat -tlnup|grep mongod
tcp 0 0 0.0.0.0:10001 0.0.0.0:* LISTEN 11196/./mongodb-lin
[@bx_44_177 /opt/mongodb]#
以上mongodb数据库就安装测试完毕!
创建mongodb用户并授权
下面开始mongodb数据库用户的创建:
[@bx_44_177/opt/mongodb/mongodb-linux-x86_64-rhel55-3.2.8/bin]#./mongo localhost:10001
> use admin
switched to db admin
然后创建用户whw:
db.createUser(
{
user: "whw",
pwd: "123456",
roles: [ { role: "__system", db: "admin" } ]
}
);
创建用户结果:
> db.createUser(
... {
... user: "whw",
... pwd: "123456",
... roles: [ { role: "__system", db: "admin" } ]
... }
... );
Successfully added user: {
"user" : "whw",
"roles" : [
{
"role" : "__system",
"db" : "admin"
}
]
}
创建完成后,给whw用户授权:
db.grantRolesToUser(
"whw",
[
{ role: "read", db:"test" }
]
);
使用mongo连接测试
使用创建的用户连接mongodb数据库命令:
./mongo localhost:10001/admin -u whw -p 123456
使用mongochef连接测试
安装mongochef-x64.msi客户端,通过可视化工具连接mongodb。
安装完之后,创建连接,指定IP和端口,Authorization中输入用户名和密码,点击完成即可成功连接mongodb服务,如下图示:
(完)