【MongoDB安装】MongoDB在centos linux平台安装
参考:http://www.runoob.com/mongodb/mongodb-linux-install.html
一、.下载安装包
下载方式:
1.登录官网download,然后通过xftp传到服务器
下载地址:https://www.mongodb.com/download-center#community
2.wget直接下载到服务器(前提是知道地址)
wget地址:https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-amazon-3.4.7.tgz
[root@zhangmeng ~]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-amazon-3.4.7.tgz --2017-09-07 15:11:23-- https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-amazon-3.4.7.tgz Resolving fastdl.mongodb.org... 54.230.86.225, 54.230.86.86, 54.230.86.44, ... Connecting to fastdl.mongodb.org|54.230.86.225|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 100691605 (96M) [application/x-gzip] Saving to: ?.ongodb-linux-x86_64-amazon-3.4.7.tgz? 100%[=================================>] 100,691,605 5.23M/s in 14s 2017-09-07 15:11:39 (6.63 MB/s) - ?.ongodb-linux-x86_64-amazon-3.4.7.tgz.saved [100691605/100691605]
3.curl 下载
可以找个时间研究wget和curl的区别?
[root@zhangmeng ~]# curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-amazon-3.4.7.tgz % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:--
0 96.0M 0 64736 0 0 34549 0 0:48:34 0:00:01 0:48:33 76
1 96.0M 1 1039k 0 0 360k 0 0:04:32 0:00:02 0:04:30 5
4 96.0M 4 4191k 0 0 1073k 0 0:01:31 0:00:03 0:01:28 14
11 96.0M 11 11.2M 0 0 2372k 0 0:00:41 0:00:04 0:00:37 30
19 96.0M 19 18.7M 0 0 3277k 0 0:00:30 0:00:05 0:00:25 39
27 96.0M 27 26.3M 0 0 3925k 0 0:00:25 0:00:06 0:00:19 53
35 96.0M 35 33.7M 0 0 4388k 0 0:00:22 0:00:07 0:00:15 67
43 96.0M 43 41.5M 0 0 4794k 0 0:00:20 0:00:08 0:00:12 77
51 96.0M 51 49.4M 0 0 5137k 0 0:00:19 0:00:09 0:00:10 78
59 96.0M 59 57.4M 0 0 5413k 0 0:00:18 0:00:10 0:00:08 79
68 96.0M 68 65.5M 0 0 5657k 0 0:00:17 0:00:11 0:00:06 80
76 96.0M 76 73.8M 0 0 5873k 0 0:00:16 0:00:12 0:00:04 82
85 96.0M 85 81.7M 0 0 6037k 0 0:00:16 0:00:13 0:00:03 82
93 96.0M 93 89.9M 0 0 6194k 0 0:00:15 0:00:14 0:00:01 82
98 96.0M 98 95.0M 0 0 5575k 0 0:00:17 0:00:17 --:--:--
58100 96.0M 100 96.0M 0 0 5631k 0 0:00:17 0:00:17 --:--:-- 5576k
二、解压
1.解压安装包
[root@zhangmeng ~]# tar -zxvf mongodb-linux-x86_64-amazon-3.4.7.tgz
2.将解压包拷贝到指定目录
[root@zhangmeng ~]# mv mongodb-linux-x86_64-amazon-3.4.7/ /usr/local/mongodb
三、创建数据库目录
mkdir -p 创建目录,如果没有连带父级目录也创建,如果存在也不会报错
[root@zhangmeng ~]# mkdir -p /data/db
四、命令行中运行MongoDB服务
PS:如果你的数据库目录不是/data/db,可以通过 --dbpath 来指定。
[root@zhangmeng ~]# cd /usr/local/mongodb/bin/ [root@zhangmeng bin]# ls bsondump mongodump mongoimport mongoreplay mongostat mongo mongoexport mongooplog mongorestore mongotop mongod mongofiles mongoperf mongos [root@zhangmeng bin]# [root@zhangmeng bin]# mongod -bash: mongod: command not found [root@zhangmeng bin]# ./mongod 2017-09-07T17:06:18.874+0800 I CONTROL [initandlisten] MongoDB starting : pid=1577 port=27017 dbpath=/data/db 64-bit host=zhangmeng.example.com 2017-09-07T17:06:18.874+0800 I CONTROL [initandlisten] db version v3.4.7 2017-09-07T17:06:18.874+0800 I CONTROL [initandlisten] git version: cf38c1b8a0a8dca4a11737581beafef4fe120bcd 2017-09-07T17:06:18.874+0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.0-fips 29 Mar 2010
五、MongoDB后台管理SHELL
下面报错是因为我把上面的窗口关闭了
[root@zhangmeng ~]# cd /usr/local/mongodb/bin [root@zhangmeng bin]# [root@zhangmeng bin]# ls bsondump mongodump mongoimport mongoreplay mongostat mongo mongoexport mongooplog mongorestore mongotop mongod mongofiles mongoperf mongos [root@zhangmeng bin]# ./mongo MongoDB shell version v3.4.7 connecting to: mongodb://127.0.0.1:27017 2017-09-07T17:11:46.865+0800 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused 2017-09-07T17:11:46.865+0800 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed : connect@src/mongo/shell/mongo.js:237:13 @(connect):1:6 exception: connect failed [root@zhangmeng bin]#
正常连接后
由于它是一个JavaScript shell,您可以运行一些简单的算术运算:
mm/transparent_hugepage/defrag is 'always'. 2017-09-07T17:12:56.346+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2017-09-07T17:12:56.346+0800 I CONTROL [initandlisten] > > 3*5 15
将数字 10 插入到 runoob 集合的 x 字段中。
2017-09-07T17:12:56.346+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2017-09-07T17:12:56.346+0800 I CONTROL [initandlisten] > > > db.runoob.insert({x:10}) WriteResult({ "nInserted" : 1 }) > db.runoob.find() { "_id" : ObjectId("59b10eeb5639ea8e2b75c4bb"), "x" : 10 } >
六、基础命令
1.显示所有数据的列表 show dbs
> show dbs admin 0.000GB local 0.000GB test 0.000GB
2.显示当前数据库对象或集合 db
> db
test
>