【MongoDB】安装和简单上手D1

是作业,不知道算不算多,主要参考是蓝皮那本

====================================

一、下载安装MongoDB

1,下载MongoDB安装包,本机为ubuntu16.04,使用版本3.44

wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1604-3.4.4.tgz?_ga=2.264308111.1229498005.1521979616-137841276.1519801125

2,设置存放目录

tar zxvf mongodb-linux-x86_64-ubuntu1604-3.4.4.gz
mkdir /Apps #根目录
mv mongodb-linux-x86_64-ubuntu1604-3.4.4 /Apps
mv /Apps/mongodb-linux-x86_64-ubuntu1604-3.4.4 /Apps/mongo

3,设置数据文件存放目录和log文件(并不看

mkdir -p /data/db
mkdir -p /Apps/mongo/logs/
touch /Apps/mongo/logs/mongodb.log
/Apps/mongo/bin/mongod --dbpath=/data/db -logpath=/Apps/mongo/logs/mongodb.log

//Adobe可太傻了

//回得到反馈如下

root@ubuntu:/Apps# /Apps/mongo/bin/mongod --dbpath=/data/db -logpath=/Apps/mongo/logs/mongodb.log
2018-04-03T00:53:36.515+0800 I CONTROL  [main] log file "/Apps/mongo/logs/mongodb.log" exists; moved to "/Apps/mongo/logs/mongodb.log.2018-04-02T16-53-36".
/Apps/mongo/bin/mongod --dbpath=/data/db --logpath=/Apps/mongo/logs/mongodb.log

感觉这里已经开始有问题了啊

4,启动MongoDB

root@ubuntu:/home/dimitrisun# /Apps/mongo/bin/mongod --dbpath=/data/db --logpath=/Apps/mongo/logs/mongodb.log
2018-04-03T01:03:55.612+0800 I CONTROL  [main] log file "/Apps/mongo/logs/mongodb.log" exists; moved to "/Apps/mongo/logs/mongodb.log.2018-04-02T17-03-55".

测试几次,emmmmm貌似世界线最终收束在文件已存在了,没问题

5,设置开机启动服务

vi /etc/rc.local
 

/Apps/mongo/bin/mongod --dbpath=/data/db-logpath=/Apps/mongo/logs/mongodb.log

测试:

/Apps/mongo/bin/mongo

然后就会给反应,welcome to MongoDB shell,emmmmmmmm

日志就不看了,我说了不看的,不康的

二,在简单命令的边缘试探

1,关闭

> db.shutdownserver();
2018-04-03T01:18:53.344+0800 E QUERY    [thread1] TypeError: db.shutdownserver is not a function :
@(shell):1:1
> db.shutdownServer();
server should be down...
2018-04-03T01:19:06.400+0800 I NETWORK  [thread1] trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
2018-04-03T01:19:06.400+0800 W NETWORK  [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused
2018-04-03T01:19:06.401+0800 I NETWORK  [thread1] reconnect 127.0.0.1:27017 (127.0.0.1) failed failed 
> 

要求还蛮严格的嘛

对了,不能用kill -9PID,被这样警告了

2,创建文件

**MongoDB Shell 是MongoDB 自带的交互式JavaScript Shell ,用来对MongoDB 进行操

作和管理的交互式环境。

//面瘫

每次打开log越来越长了??幻觉??

万能的还是--help,大家都开心啦

> j = { name : "mongo" };
{ "name" : "mongo" }
> t = { x : 3 };
{ "x" : 3 }
> db.things.save(j);
WriteResult({ "nInserted" : 1 })
> db.things.save(t);
WriteResult({ "nInserted" : 1 })
> db.things.find()
{ "_id" : ObjectId("5ac26849bc00b738b1551605"), "name" : "mongo" }
{ "_id" : ObjectId("5ac2685abc00b738b1551606"), "x" : 3 }
> 

--不需要预先创建一个集合,在第一次插入数据的时候会自动创建。
--在文档中其实可以存储任何结构的数据,当然在实际应用我们存储的还是相同类型文档的集合。这个特性其实可以在应用里很灵活,你不需要类似alter table 语句
来修改你的数据结构。

--每次插入数据的时候集合中都会有一个ID,名字叫id 。

> for( var i = 1; i < 10;i++ ) db.things.save( { x:4, j:i } );
WriteResult({ "nInserted" : 1 })
> db.things.find()
{ "_id" : ObjectId("5ac26849bc00b738b1551605"), "name" : "mongo" }
{ "_id" : ObjectId("5ac2685abc00b738b1551606"), "x" : 3 }
{ "_id" : ObjectId("5ac26989bc00b738b1551607"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("5ac26989bc00b738b1551608"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("5ac26989bc00b738b1551609"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("5ac26989bc00b738b155160a"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("5ac26989bc00b738b155160b"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("5ac26989bc00b738b155160c"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("5ac26989bc00b738b155160d"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("5ac26989bc00b738b155160e"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("5ac26989bc00b738b155160f"), "x" : 4, "j" : 9 }
> 

3,_id key

存储在MongoDB 集合中的每个文档(document) 都有一个默认的主键一id ,这个主键名称是固定的,它可以是MongoDB 支持的任何数据类型,默认是ObjectId.

在MongoDB 中,每一个集合都必须有一个叫做一id 的字段,字段类型默认是ObjectId。换句话说,字段类型可以不是Objectld.

遇到问题:

root@ubuntu:/home/dimitrisun# /Apps/mongo/bin/mongod
2018-04-03T02:09:04.360+0800 I CONTROL  [initandlisten] MongoDB starting : pid=9704 port=27017 dbpath=/data/db 64-bit host=ubuntu
2018-04-03T02:09:04.360+0800 I CONTROL  [initandlisten] db version v3.4.4
2018-04-03T02:09:04.360+0800 I CONTROL  [initandlisten] git version: 888390515874a9debd1b6c5d36559ca86b44babd
2018-04-03T02:09:04.360+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016
2018-04-03T02:09:04.360+0800 I CONTROL  [initandlisten] allocator: tcmalloc
2018-04-03T02:09:04.360+0800 I CONTROL  [initandlisten] modules: none
2018-04-03T02:09:04.360+0800 I CONTROL  [initandlisten] build environment:
2018-04-03T02:09:04.360+0800 I CONTROL  [initandlisten]     distmod: ubuntu1604
2018-04-03T02:09:04.360+0800 I CONTROL  [initandlisten]     distarch: x86_64
2018-04-03T02:09:04.360+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2018-04-03T02:09:04.360+0800 I CONTROL  [initandlisten] options: {}
2018-04-03T02:09:04.361+0800 I STORAGE  [initandlisten] exception in initAndListen: 98 Unable to lock file: /data/db/mongod.lock Resource temporarily unavailable. Is a mongod instance already running?, terminating
2018-04-03T02:09:04.361+0800 I NETWORK  [initandlisten] shutdown: going to close listening sockets...
2018-04-03T02:09:04.361+0800 I NETWORK  [initandlisten] shutdown: going to flush diaglog...
2018-04-03T02:09:04.361+0800 I CONTROL  [initandlisten] now exiting
2018-04-03T02:09:04.361+0800 I CONTROL  [initandlisten] shutting down with code:100

先看端口

root@ubuntu:/home/dimitrisun# netstat -lanp | grep "27017" 
tcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN      9432/mongod     
unix  2      [ ACC ]     流        LISTENING     70735    9432/mongod         /tmp/mongodb-27017.sock

瞎搞一波,还是不行

查了一波,看来是未正常退出吃锁了

root@ubuntu:/data# cd /data/db
root@ubuntu:/data/db# ls
collection-0--3394627052753736532.wt  index-1--3394627052753736532.wt  journal          storage.bson      WiredTiger.turtle
collection-0-4509620538467275102.wt   index-1-4509620538467275102.wt   _mdb_catalog.wt  WiredTiger        WiredTiger.wt
collection-2-4509620538467275102.wt   index-3-4509620538467275102.wt   mongod.lock      WiredTigerLAS.wt
diagnostic.data                       index-4-4509620538467275102.wt   sizeStorer.wt    WiredTiger.lock
root@ubuntu:/data/db# 

QTE了,删除

绝了,现在是:

root@ubuntu:~# /Apps/mongo/bin/mongod
2018-04-03T02:35:36.125+0800 I CONTROL  [initandlisten] MongoDB starting : pid=9827 port=27017 dbpath=/data/db 64-bit host=ubuntu
2018-04-03T02:35:36.125+0800 I CONTROL  [initandlisten] db version v3.4.4
2018-04-03T02:35:36.125+0800 I CONTROL  [initandlisten] git version: 888390515874a9debd1b6c5d36559ca86b44babd
2018-04-03T02:35:36.125+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016
2018-04-03T02:35:36.125+0800 I CONTROL  [initandlisten] allocator: tcmalloc
2018-04-03T02:35:36.126+0800 I CONTROL  [initandlisten] modules: none
2018-04-03T02:35:36.126+0800 I CONTROL  [initandlisten] build environment:
2018-04-03T02:35:36.126+0800 I CONTROL  [initandlisten]     distmod: ubuntu1604
2018-04-03T02:35:36.126+0800 I CONTROL  [initandlisten]     distarch: x86_64
2018-04-03T02:35:36.126+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2018-04-03T02:35:36.126+0800 I CONTROL  [initandlisten] options: {}
2018-04-03T02:35:36.156+0800 E NETWORK  [initandlisten] listen(): bind() failed Address already in use for socket: 0.0.0.0:27017
2018-04-03T02:35:36.156+0800 E NETWORK  [initandlisten]   addr already in use
2018-04-03T02:35:36.156+0800 E NETWORK  [initandlisten] Failed to set up sockets during startup.
2018-04-03T02:35:36.156+0800 E STORAGE  [initandlisten] Failed to set up listener: InternalError: Failed to set up sockets
2018-04-03T02:35:36.156+0800 I NETWORK  [initandlisten] shutdown: going to close listening sockets...
2018-04-03T02:35:36.156+0800 I NETWORK  [initandlisten] shutdown: going to flush diaglog...
2018-04-03T02:35:36.156+0800 I CONTROL  [initandlisten] now exiting
2018-04-03T02:35:36.156+0800 I CONTROL  [initandlisten] shutting down with code:48

先重启冷静下

先记录一个老外给出的意见:

It seems like you have already a process running on the port where you want to start mongodb:

listen(): bind() failed Address already in use for socket: 0.0.0.0:27017
2017-02-06T16:26:27.038+0000 E NETWORK  [initandlisten]   addr already in use

you could try to kill the process that runs on that port with this command: sudo kill sudo lsof -t -i:27017

or define another port for mongodb if you have another program using that port.
to run mongodb on a port other than its default port (27017) use the --port 27018 argument when starting mongodb from the terminal

我以后有钱了绝对上10TSSD

先写到这里吧

猜你喜欢

转载自blog.csdn.net/baidu_34331290/article/details/79797298
d1
今日推荐