Ubuntu安装与配置Mongodb:
- 安装:
sudo apt-get install mongodb
- 在自己想要的位置新建
data
,logs
文件夹分别用于存放数据,日志文件:mkdir Mongodb cd Mongodb mkdir data mkdir logs
- 修改配置文件:
vim /etc/mongodb.conf
显示如下:
# mongodb.conf # Where to store the data. #dbpath=/var/lib/mongodb dbpath=/home/ziven/Workspace/Work/ziven/Mongodb/data #where to log #logpath=/var/log/mongodb/mongodb.log logpath=/home/ziven/Workspace/Work/ziven/Mongodb/logs/mongodb.log logappend=true #bind_ip = 127.0.0.1 bind_ip = 0.0.0.0 port = 27017 # Enable journaling, http://www.mongodb.org/display/DOCS/Journaling journal=true # Enables periodic logging of CPU utilization and I/O wait #cpu = true # Turn on/off security. Off is currently the default #noauth = true #auth = true ... ... ...
注:将其中的
dbpath
与logpath
分别改成你所设定绝对路径,其中logpath
指向的是/logs/mongodb.log
文件,该文件不需要自己创建,写上去就好 - 重启mongodb服务:
sudo /etc/init.d/mongod restart
- 设置开机自动启动与关机关闭:
sudo vim /lib/systemd/system/mongodb.service
[Unit] Description=An object/document-oriented database Documentation=man:mongod(1) [Service] User=mongodb ExecStart=/usr/bin/mongod --config /etc/mongodb.conf # my add ExecStop=/usr/bin/mongod --shutdown --config /etc/mongodb.conf PrivateTmp=false [Install] WantedBy=multi-user.target
使用Mongodb:
- 后台运行mongodb:
nohup mongod --dbpath /home/ziven/Workspace/Work/ziven/Mongodb/data/ & [1] 2245 ziven@Lucy:~/Workspace/Work/ziven/Mongodb/etc$ nohup: ignoring input and appending output to 'nohup.out' # 可以使用jobs查看后台任务 jobs -l [1]+ 2245 Running nohup mongod --dbpath /home/ziven/Workspace/Work/ziven/Mongodb/data/ & # 输入mongo进入mongodb mongo MongoDB shell version: 2.6.10 connecting to: test >
- 简单使用mongodb:
mongo MongoDB shell version: 2.6.10 connecting to: test > show dbs admin 0.078GB local 0.078GB > use test switched to db test > db.col.insert({"name":"jack","age":"8"}) WriteResult({ "nInserted" : 1 }) > show tables col system.indexes > db.col.findOne() { "_id" : ObjectId("5bc03f306328d30a46c88a55"), "name" : "jack", "age" : "8" } > quit()
使用Robo 3T:
Robo 3T是mongodb的可视化客户端,需要使用图像界面,支持多种平台
-
下载并安装Robo 3T: https://robomongo.org/download
- 点击左上方电脑图标
- 点击
Create
,在Address
一栏中输入IPv4地址,若未进行设置且在本地运行的Robo3T则默认为localhost - 点击
Save
- 点击
Connect
- 此时多了一个连接,打开下拉菜单可以找到刚刚我们创建的test和col,以及其中的jack
关闭Mongodb服务:
进入mongo
mongo
MongoDB shell version: 2.6.10
connecting to: test
> use admin
switched to db admin
> db.shutdownServer()
2018-10-12T14:43:11.660+0800 DBClientCursor::init call() failed
server should be down...
2018-10-12T14:43:11.664+0800 trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
2018-10-12T14:43:11.664+0800 warning: Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2018-10-12T14:43:11.664+0800 reconnect 127.0.0.1:27017 (127.0.0.1) failed failed couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed
> quit()
[1]+ Done nohup mongod --dbpath /home/ziven/Workspace/Work/ziven/Mongodb/data/
此时输入jobs -l
可以发现,后台的任务已经关闭