linux MongoDB4.0.13安装

linux mongodb4.0.13安装

■A安装环境

1.linux:CentOS7

2.MongoDB:mongodb-linux-x86_64-4.0.13.tgz

下载地址:https://www.mongodb.com/

[root@mongodb ~]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.13.tgz

■B安装过程

1.将下载好的文件解压缩。

2.在解压后的文件夹中创建data,log文件夹。在log文件夹中创建mongodb.log文件.

[root@mongodb ~]# tar xf mongodb-linux-x86_64-4.0.13.tgz
[root@mongodb ~]# mv mongodb-linux-x86_64-4.0.13 /data/www/mongodb
[root@mongodb ~]# mkdir /data/www/mongodb/{data,log} -p
[root@mongodb ~]# touch /data/www/mongodb/log/mongodb.log
[root@mongodb ~]# ll /data/www/mongodb/
total 196
drwxr-xr-x 2 root root 231 Oct 29 22:24 bin
drwxr-xr-x 2 root root 6 Oct 29 22:25 data
-rw-r--r-- 1 root root 30608 Oct 14 13:39 LICENSE-Community.txt
drwxr-xr-x 2 root root 25 Oct 29 22:25 log
-rw-r--r-- 1 root root 16726 Oct 14 13:39 MPL-2
-rw-r--r-- 1 root root 2601 Oct 14 13:39 README
-rw-r--r-- 1 root root 60005 Oct 14 13:39 THIRD-PARTY-NOTICES
-rw-r--r-- 1 root root 81355 Oct 14 13:40 THIRD-PARTY-NOTICES.gotools
[root@mongodb ~]# ll /data/www/mongodb/log/
total 0
-rw-r--r-- 1 root root 0 Oct 29 22:25 mongodb.log

3.创建MongoDB的配置文件:my.conf

[root@mongodb ~]# cd /data/www/mongodb/
[root@mongodb mongodb]# vim my.conf

port=27017 ##端口,默认27017,可以自定义
dbpath= /data/www/mongodb/data ##数据文件存放目录
logpath= /data/www/mongodb/log/mongodb.log ##日志文件存放目录
logappend=true ##开启日志追加添加日志
fork=true ##以守护程序的方式启用,即在后台运行
#auth=true ##开启认证

  

4.启动MongoDB

[root@mongodb mongodb]# ./bin/mongod --config /data/www/mongodb/my.conf
about to fork child process, waiting until server is ready for connections.
forked process: 3893
child process started successfully, parent exiting

 这样就启动成功了。我们可以使用MongoDB啦。

5.使用MongoDB

[root@mongodb mongodb]# vim /etc/profile

export PATH=$PATH:/data/www/mongodb/bin

[root@mongodb mongodb]# source /etc/profile

[root@mongodb mongodb]# mongo
MongoDB shell version v4.0.13
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("43bd322f-7ae8-4d2b-beaf-b39708c60e5a") }
MongoDB server version: 4.0.13
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:
2019-10-29T22:38:34.815-0400 I CONTROL [initandlisten]
2019-10-29T22:38:34.815-0400 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-10-29T22:38:34.815-0400 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2019-10-29T22:38:34.816-0400 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2019-10-29T22:38:34.816-0400 I CONTROL [initandlisten]
>

在>后就可以输入MongoDB命令了。

让我们来试试show dbs命令。

> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
>

■C使用Robomongo管理MongoDB

如果要远程访问MongoDB就必须要绑定IP地址。

[root@mongodb ~]# cd /data/www/mongodb/
[root@mongodb mongodb]# vim my.conf

port=27017 ##端口,默认27017,可以自定义
dbpath= /data/www/mongodb/data ##数据文件存放目录
logpath= /data/www/mongodb/log/mongodb.log ##日志文件存放目录
logappend=true ##开启日志追加添加日志
fork=true ##以守护程序的方式启用,即在后台运行
auth=true ##开启认证
bind_ip=0.0.0.0 #开启远程连接

这样远程访问就成功了。

下面说说建立用户。

> use admin
switched to db admin
> db.createUser({user:"sharpfire",pwd:"sharpfire",roles:[{role:"root",db:"admin"}]});
Successfully added user: {
"user" : "sharpfire",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
>


这里建立了一个用户sharpfire,密码是sharpfire,对应DB是admin,角色是root。

原文地址:https://www.cnblogs.com/xuanbao/p/11763432.html