1,工具准备,官网下载mongodb linux 版本
2,安装
[root@localhost ~]# cd /usr/local
创建数据库主文件夹
[root@localhost local]# mkdir mongodb
解压下载得到的 mongodb-linux-x86_64-3.6.3.tgz
[root@localhost files]# tar -zxvf mongodb-linux-x86_64-3.6.3.tgz
[root@localhost files]# cd mongodb-linux-x86_64-3.6.3/
将解压后的文件移动到/usr/local/mongodb目录下
[root@localhost mongodb-linux-x86_64-3.6.3]# mv * /usr/local/mongodb/
[root@localhost mongodb-linux-x86_64-3.6.3]# cd /usr/local/mongodb/
创建 存放数据的data文件夹
[root@localhost mongodb]# mkdir data
创建日志文件夹
[root@localhost mongodb]# mkdir log
3,将mongodb服务加入系统环境变量
[root@localhost mongodb]# vim /etc/profile
export MONGODB_HOME=/usr/local/mongodb
export PATH=$MONGODB_HOME/bin:$PATH
保存并退出,测试
[root@localhost ~]# mongod -v
2018-02-26T14:58:00.490+0800 D NETWORK [main] fd limit hard:4096 soft:1024 max conn: 819
2018-02-26T14:58:00.506+0800 I CONTROL [initandlisten] MongoDB starting : pid=1721 port=27017 dbpath=/data/db 64-bit host=localhost.localdomain
2018-02-26T14:58:00.506+0800 I CONTROL [initandlisten] db version v3.6.3
2018-02-26T14:58:00.506+0800 I CONTROL [initandlisten] git version: 9586e557d54ef70f9ca4b43c26892cd55257e1a5
2018-02-26T14:58:00.506+0800 I CONTROL [initandlisten] allocator: tcmalloc
2018-02-26T14:58:00.506+0800 I CONTROL [initandlisten] modules: none
2018-02-26T14:58:00.506+0800 I CONTROL [initandlisten] build environment:
2018-02-26T14:58:00.506+0800 I CONTROL [initandlisten] distarch: x86_64
2018-02-26T14:58:00.506+0800 I CONTROL [initandlisten] target_arch: x86_64
2018-02-26T14:58:00.506+0800 I CONTROL [initandlisten] options: { systemLog: { verbosity: 1 } }
2018-02-26T14:58:00.506+0800 D - [initandlisten] User Assertion: 29:Data directory /data/db not found. src/mongo/db/service_context_d.cpp 98
2018-02-26T14:58:00.506+0800 I STORAGE [initandlisten] exception in initAndListen: NonExistentPath: Data directory /data/db not found., terminating
2018-02-26T14:58:00.506+0800 I CONTROL [initandlisten] now exiting
2018-02-26T14:58:00.506+0800 I CONTROL [initandlisten] shutting down with code:100
4,添加启动配置文件
[root@localhost ~]# cd /usr/local/mongodb
创建配置文件
[root@localhost mongodb]# vim mongodb.conf
dbpath=/usr/local/mongodb/data
logpath=/usr/local/mongodb/log/mongodb.log
logappend=true
port=27017
fork=true #后台运行
##auth = true #初始创建时先注释,创建账户密码后重新启用
5 , 启动测试
[root@localhost mongodb]# mongod -f mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 1578
child process started successfully, parent exiting
6 进入 MongoDB
后台管理 Shell
cd /usr/local/mongodb/bin
./mongo
创建数据
命令 use test
switched to db test
创建用户, 设置权限
db.createUser(
{
user: "test",
pwd: "test",
roles: [ { role: "readWrite", db: "test" } ]
}
)