1.下载安装包 https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.6.tgz
2.解压安装包 tar -zxvf mongodb-linux-x86_64-4.0.6.tgz.gz
3.进入目录, 创建数据存放目录及日志目录
[root@localhost opt]# cd mongodb-linux-x86_64-4.0.6
[root@localhost mongodb-linux-x86_64-4.0.6]# mkdir db
[root@localhost mongodb-linux-x86_64-4.0.6]# mkdir log
[root@localhost mongodb-linux-x86_64-4.0.6]# cd log
[root@localhost mongodb-linux-x86_64-4.0.6]# touch mongo.log
4.在mongodb-linux-x86_64-4.0.6目录下创建mongo.conf配置文件
port=27017
bind_ip=0.0.0.0
dbpath=/opt/mongodb-linux-x86_64-4.0.6/db
logpath=/opt/mongodb-linux-x86_64-4.0.6/log/mongo.log
logappend=true
5.启动mongodb nohup bin/mongod -f mongo.conf &
6.测试连接 bin/mongo 127.0.0.1:27017
[root@localhost mongodb-linux-x86_64-4.0.6]# bin/mongo 127.0.0.1:27017
MongoDB shell version v4.0.6
connecting to: mongodb://127.0.0.1:27017/test?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("339b511a-8b4c-45fe-853f-44a290b5ed2a") }
MongoDB server version: 4.0.6
Server has startup warnings:
2019-02-25T18:28:16.259+0800 I STORAGE [initandlisten]
2019-02-25T18:28:16.259+0800 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2019-02-25T18:28:16.259+0800 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2019-02-25T18:28:17.429+0800 I CONTROL [initandlisten]
2019-02-25T18:28:17.429+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-02-25T18:28:17.429+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2019-02-25T18:28:17.429+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2019-02-25T18:28:17.429+0800 I CONTROL [initandlisten]
2019-02-25T18:28:17.429+0800 I CONTROL [initandlisten]
2019-02-25T18:28:17.429+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-02-25T18:28:17.429+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-02-25T18:28:17.429+0800 I CONTROL [initandlisten]
2019-02-25T18:28:17.429+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-02-25T18:28:17.429+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-02-25T18:28:17.429+0800 I CONTROL [initandlisten]
>
7.开放27017端口 vi /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 27017-j ACCEPT
8.重启iptables服务 service iptables restart
1.切换到admin数据库 use admin
2.添加账户 db.createUser({ user: "root", pwd: "root", roles: [{ role: "userAdminAnyDatabase", db: "admin" }] })
3.修改mongo.conf文件, 增加配置
auth=true
4.重启MongoDB服务
5.测试连接 bin/mongo 127.0.0.1:27017 切换到admin库并登录验证
[root@localhost mongodb-linux-x86_64-4.0.6]# bin/mongo 127.0.0.1:27017
MongoDB shell version v4.0.6
connecting to: mongodb://127.0.0.1:27017/test?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("decbc00b-d514-4de3-b335-23752ab98417") }
MongoDB server version: 4.0.6
> use admin
switched to db admin
> db.auth("root", "root")
1
>