mongodb——安装、配置
一、安装MongoDB
[root@localhost opt]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.0.tgz
[root@localhost opt]# ls
mongodb-linux-x86_64-4.0.0.tgz rh
二、解压安装包
[root@localhost opt]# mv mongodb-linux-x86_64-4.0.0.tgz /usr/local
[root@localhost opt]# cd /usr/local/
[root@localhost local]# tar -zxvf mongodb-linux-x86_64-4.0.0.tgz
······
[root@localhost local]# mv mongodb-linux-x86_64-4.0.0 mongodb
三、相关配置文件
创建db和logs文件夹,分别用来保存数据以及日志。
[root@localhost local]# cd mongodb/
[root@localhost mongodb]# mkdir db
[root@localhost mongodb]# mkdir logs
然后进入bin文件夹,创建并配置mongo.conf,代码如下:
[root@localhost mongodb]# cd bin/
[root@localhost bin]# touch mongo.conf
[root@localhost bin]# vi mongo.conf
#存放数据配置路径
dbpath=/usr/local/mongodb/db
#存放日志配置路径
logpath=/usr/local/mongodb/logs/mongo.log
#端口
port=27017
#守护程序启动,即允许后台启动。
fork=true
---------------------------------------------
dbpath=/usr/local/mongodb/db
logpath=/usr/local/mongodb/logs/mongo.log
port=27017
fork=true
四、启动MongoDB
[root@localhost bin]# ./mongod -f mongo.conf --bind_ip_all
about to fork child process, waiting until server is ready for connections.
forked process: 2986
child process started successfully, parent exiting
------------------------------------------------------
-f 指定配置文件
–bind_ip_all 允许所有远程地址连连接该MongoDB
将启动命令全局配置
[root@localhost bin]# cp /usr/local/mongodb/bin/mongo /usr/bin/
登录MongoDB
[root@localhost bin]# mongo
MongoDB shell version v4.0.0
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.0.0
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
2021-12-10T14:06:22.463+0800 I CONTROL [initandlisten]
2021-12-10T14:06:22.463+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2021-12-10T14:06:22.463+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2021-12-10T14:06:22.463+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2021-12-10T14:06:22.463+0800 I CONTROL [initandlisten]
2021-12-10T14:06:22.464+0800 I CONTROL [initandlisten]
2021-12-10T14:06:22.464+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2021-12-10T14:06:22.464+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2021-12-10T14:06:22.464+0800 I CONTROL [initandlisten]
2021-12-10T14:06:22.464+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2021-12-10T14:06:22.464+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2021-12-10T14:06:22.464+0800 I CONTROL [initandlisten]
> db.version()
4.0.0
>
五、MongoDB关闭
默认情况登录的是test库,而关闭需要admin库中执行。
> use admin
switched to db admin
> db.shutdownServer();
server should be down...
2021-12-10T14:33:27.856+0800 I NETWORK [js] trying reconnect to 127.0.0.1:27017 failed
2021-12-10T14:33:27.856+0800 I NETWORK [js] reconnect 127.0.0.1:27017 failed failed
>
六、安全管理
默认情况下,启动MongDB是没有登录密码,在生产环境中不安全。
MongoDB中每个库都有自己独立的密码。
要配置密码,首先需要创建一个用户,例如在admin下创建一个用户。
如下:
> use admin;
> db.createUser({user:"zhangsan",pwd:"abc123",roles:[{role:"readWrite",db:"test"}]})