項目 | 參數 |
---|---|
操作系統 | CentOS 6.7 |
系統內存 | 2GB |
MongoDB版本 | 4.0.16 |
下載軟件
登錄MongoDB官網,選擇社區版軟件下載學習使用。
選好後直接Download即可
安裝軟件
1. ftp先上傳下載好的軟件壓縮包到Linux系統中
sftp> put mongodb-linux-x86_64-rhel62-4.0.16.tar
Uploading mongodb-linux-x86_64-rhel62-4.0.16.tar to /root/mongodb-linux-x86_64-rhel62-4.0.16.tar
100% 284260KB 31584KB/s 00:00:09
/Users/ivy_dong/Downloads/mongodb-linux-x86_64-rhel62-4.0.16.tar: 291082240 bytes transferred in 9 seconds (31584 KB/s)
sftp>
查看壓縮包,已順利上傳
[root@pg01 ~]# ll mongodb-linux-x86_64-rhel62-4.0.16.tar
-rw-r--r--. 1 root root 291082240 Mar 19 2020 mongodb-linux-x86_64-rhel62-4.0.16.tar
2. 解壓安裝包
[root@pg01 ~]# tar -xvf mongodb-linux-x86_64-rhel62-4.0.16.tar
3. 進入解壓有目錄中
[root@pg01 ~]# mv mongodb-linux-x86_64-rhel62-4.0.16 mongodb
[root@pg01 ~]# cd mongodb
[root@pg01 mongodb]# ls
bin LICENSE-Community.txt MPL-2 README THIRD-PARTY-NOTICES THIRD-PARTY-NOTICES.gotools
4. 安排數據存放位置
[root@pg01 ~]# mkdir -p /mongodb/data
啓動MongoDB
[root@pg01 bin]# ./mongod --port 27017 --dbpath=/mongodb/data
啓動過程中,會有較多的輸出。其中看到以下字樣說明服務已啓動。之後停住不動了。暫時不管。
2020-03-15T03:52:17.672+0800 I NETWORK [initandlisten] waiting for connections on port 27017
2020-03-15T03:52:18.031+0800 I INDEX [LogicalSessionCacheRefresh] build index done.
驗證安裝
登錄MongoDB
再打開一個session,我們就可以登錄進MongoDB了
[root@pg01 ~]# cd mongodb/bin/
[root@pg01 bin]# ./mongo
.... (省略)
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
>
- 查看所有數據庫
## 方法一
> show databases;
admin 0.000GB
config 0.000GB
local 0.000GB
## 方法二
> show dbs;
admin 0.000GB
config 0.000GB
local 0.000GB
- 使用、選擇數據庫
> use local;
switched to db local
- 查看當前所在庫
> db
local
創建新數據庫
使用一個不存在的數據庫
> use dong
switched to db dong
這裏卻沒有dong,因爲mongo不顯示沒有集合的庫
> show databases;
admin 0.000GB
config 0.000GB
local 0.000GB
創建集合(創建表)
- 查看當前庫中的集合
> show collections;
startup_log
> show tables;
startup_log
- 創建集合 (建表)區分大小寫
> db.createCollection("t_user");
{ "ok" : 1 }
> show tables;
t_user
>
> show databases;
admin 0.000GB
config 0.000GB
dong 0.000GB
local 0.000GB