不多说,直接上干货!
本博文介绍了,并详细指引读者在Ubuntu下MongoDB的安装和使用。本教程在Ubuntu14.04下通过。
一、MongoDB介绍
MongoDB 是一个是一个基于分布式文件存储的,介于关系数据库和非关系数据库之间,是非关系数据库当中功能最丰富,最像关系数据库的。他支持的非常松散,是类似json的bson格式,因此可以存储比较复杂的数据类型。Mongo最大的特点是他支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引。
二、安装MongoDB
MongoDB安装很简单,无需下载源文件,可以直接用apt-get命令进行安装。
首先,查看下你的操作系统的版本。
root@zhouls-virtual-machine:~# cat /etc/issueUbuntu 14.04.4 LTS \n \lroot@zhouls-virtual-machine:~#
root@zhouls-virtual-machine:~# sudo apt-get install mongodbReading package lists... DoneBuilding dependency tree Reading state information... Donemongodb is already the newest version.0 upgraded, 0 newly installed, 0 to remove and 338 not upgraded.root@zhouls-virtual-machine:~# mongo -versionMongoDB shell version: 2.4.9root@zhouls-virtual-machine:~# sudo apt-get --purge remove mongodb mongodb-clients mongodb-serverReading package lists... DoneBuilding dependency tree Reading state information... DoneThe following packages were automatically installed and are no longer required: libboost-dev libboost-filesystem1.54.0 libboost-program-options1.54.0 libboost-thread1.54.0 libboost1.54-dev libgoogle-perftools4 libpcrecpp0 libsnappy1 libstdc++-4.8-dev libtcmalloc-minimal4 libunwind8 libv8-3.14.5 mongodb-devUse 'apt-get autoremove' to remove them.The following packages will be REMOVED: mongodb* mongodb-clients* mongodb-server*0 upgraded, 0 newly installed, 3 to remove and 338 not upgraded.After this operation, 107 MB disk space will be freed.Do you want to continue? [Y/n] y(Reading database ... 180474 files and directories currently installed.)Removing mongodb (1:2.4.9-1ubuntu2) ...Removing mongodb-server (1:2.4.9-1ubuntu2) ...arg: removemongodb stop/waitingPurging configuration files for mongodb-server (1:2.4.9-1ubuntu2) ...dpkg: warning: while removing mongodb-server, directory '/var/log/mongodb' not empty so not removeddpkg: warning: while removing mongodb-server, directory '/var/lib/mongodb' not empty so not removedRemoving mongodb-clients (1:2.4.9-1ubuntu2) ...Processing triggers for man-db (2.6.7.1-1ubuntu1) ...root@zhouls-virtual-machine:~# clearroot@zhouls-virtual-machine:~# sudo apt-get install mongodbReading package lists... DoneBuilding dependency tree Reading state information... DoneThe following extra packages will be installed: mongodb-clients mongodb-serverThe following NEW packages will be installed: mongodb mongodb-clients mongodb-server0 upgraded, 3 newly installed, 0 to remove and 338 not upgraded.Need to get 0 B/12.9 MB of archives.After this operation, 107 MB of additional disk space will be used.Do you want to continue? [Y/n] ySelecting previously unselected package mongodb-clients.(Reading database ... 180427 files and directories currently installed.)Preparing to unpack .../mongodb-clients_1%3a2.4.9-1ubuntu2_amd64.deb ...Unpacking mongodb-clients (1:2.4.9-1ubuntu2) ...Selecting previously unselected package mongodb-server.Preparing to unpack .../mongodb-server_1%3a2.4.9-1ubuntu2_amd64.deb ...Unpacking mongodb-server (1:2.4.9-1ubuntu2) ...Selecting previously unselected package mongodb.Preparing to unpack .../mongodb_1%3a2.4.9-1ubuntu2_amd64.deb ...Unpacking mongodb (1:2.4.9-1ubuntu2) ...Processing triggers for man-db (2.6.7.1-1ubuntu1) ...Processing triggers for ureadahead (0.100.0-16) ...ureadahead will be reprofiled on next rebootSetting up mongodb-clients (1:2.4.9-1ubuntu2) ...Setting up mongodb-server (1:2.4.9-1ubuntu2) ...mongodb start/running, process 3594Processing triggers for ureadahead (0.100.0-16) ...Setting up mongodb (1:2.4.9-1ubuntu2) ...root@zhouls-virtual-machine:~#
安装完成后,在终端输入以下命令查看MongoDB版本:
root@zhouls-virtual-machine:~# mongo -versionMongoDB shell version: 2.4.9
启动和关闭mongodb命令如下:
root@zhouls-virtual-machine:~# service mongodb startstart: Job is already running: mongodbroot@zhouls-virtual-machine:~# service mongodb stopmongodb stop/waitingroot@zhouls-virtual-machine:~#
默认设置MongoDB是随Ubuntu启动自动启动的。
输入以下命令查看是否启动成功:
pgrep mongo -l #注意:-l是英文字母l,不是阿拉伯数字1
卸载MongoDB
root@zhouls-virtual-machine:~# sudo apt-get --purge remove mongodb mongodb-clients mongodb-serverReading package lists... DoneBuilding dependency tree Reading state information... DoneThe following packages were automatically installed and are no longer required: libboost-dev libboost-filesystem1.54.0 libboost-program-options1.54.0 libboost-thread1.54.0 libboost1.54-dev libgoogle-perftools4 libpcrecpp0 libsnappy1 libstdc++-4.8-dev libtcmalloc-minimal4 libunwind8 libv8-3.14.5 mongodb-devUse 'apt-get autoremove' to remove them.The following packages will be REMOVED: mongodb* mongodb-clients* mongodb-server*0 upgraded, 0 newly installed, 3 to remove and 338 not upgraded.After this operation, 107 MB disk space will be freed.Do you want to continue? [Y/n] y(Reading database ... 180474 files and directories currently installed.)Removing mongodb (1:2.4.9-1ubuntu2) ...Removing mongodb-server (1:2.4.9-1ubuntu2) ...arg: removemongodb stop/waitingPurging configuration files for mongodb-server (1:2.4.9-1ubuntu2) ...dpkg: warning: while removing mongodb-server, directory '/var/log/mongodb' not empty so not removeddpkg: warning: while removing mongodb-server, directory '/var/lib/mongodb' not empty so not removedRemoving mongodb-clients (1:2.4.9-1ubuntu2) ...Processing triggers for man-db (2.6.7.1-1ubuntu1) ...root@zhouls-virtual-machine:~#