Mongodb介绍
Mongodb是一个基于分布式文件存储的数据库,由C++语言编写,为WEB应用提供可扩展的高性能数据存储解决方案
Mongodb是一款介于关系型数据库与非关系型数据库之间的产品, Mongodb是不同于以往的如redis、memcached,它是一种叫文档数据库,存储的是文档(bson-->json的二进制化)
特点:
最大的特点是支持查询语言非常强大,内部执行的引擎是JS解释器,把文档存储成bson结构,查询时将文档转换成JS对象文件,并通过熟悉JS语法来操作
同传统数据库比较:
1、传统数据库是结构化数据,有表结构,每一行内容是符合表结构,且列的类型也一样
2、mongodb数据库是以文档形式存储数据,每一个文档都是有自己独特的结构(js对象)与属性、值,因此它没有特定的规范与格式
Mongodb安装
官方网站:http://mongodb.org
下载最新的stable版本
[root@mingongge ~]# cd /usr/local/src/
[root@mingongge src]# wget https://www.mongodb.com/dr/fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.4.6.tgz
[root@Centos-2 src]# tar zxf mongodb-linux-x86_64-rhel62-3.4.6.tgz
[root@Centos-2 src]# cd mongodb-linux-x86_64-rhel62-3.4.6
[root@Centos-2 mongodb-linux-x86_64-rhel62-3.4.6]# cd bin/
[root@Centos-2 bin]# ll
total 277780
-rwxr-xr-x 1 root root 10431547 Jul 6 02:23 bsondump
#二进制导出(bson结构)
-rwxr-xr-x 1 root root 29870496 Jul 6 02:48 mongo
#客户端
-rwxr-xr-x 1 root root 54389424 Jul 6 02:48 mongod
#服务端
-rwxr-xr-x 1 root root 12771652 Jul 6 02:24 mongodump
#导出数据库
-rwxr-xr-x 1 root root 10783691 Jul 6 02:23 mongoexport
#导出易识别的json文档或CSV
-rwxr-xr-x 1 root root 10668482 Jul 6 02:23 mongofiles
-rwxr-xr-x 1 root root 10942731 Jul 6 02:23 mongoimport
-rwxr-xr-x 1 root root 10433507 Jul 6 02:24 mongooplog
-rwxr-xr-x 1 root root 53753432 Jul 6 02:48 mongoperf
-rwxr-xr-x 1 root root 14070941 Jul 6 02:24 mongoreplay
-rwxr-xr-x 1 root root 14127528 Jul 6 02:24 mongorestore
#导入数据库
-rwxr-xr-x 1 root root 30539024 Jul 6 02:48 mongos
#路由器(分片)
-rwxr-xr-x 1 root root 11003296 Jul 6 02:23 mongostat
#状态
-rwxr-xr-x 1 root root 10631445 Jul 6 02:24 mongotop
[root@Centos-2 src]# mv mongodb-linux-x86_64-rhel62-3.4.6 /usr/local/mongodb
[root@Centos-2 src]# cd /usr/local/mongodb/
启动服务
创建数据目录与日志目录
[root@Centos-2 ~]# mkdir /data/mongodb -p
[root@Centos-2 ~]# mkdir /data/mongodblog -p
[root@Centos-2 mongodb]# ./bin/mongod --dbpath /data/mongodb/ --logpath /data/mongodblog/mongo.log --fork --port 27017
about to fork child process, waiting until server is ready for connections.
forked process: 19027
child process started successfully, parent exiting
[root@Centos-2 mongodb]# lsof -i :27017
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mongod 19027 root 7u IPv4 48419 0t0 TCP *:27017 (LISTEN)
参数说明:
--dbpath 指定数据存储目录
--logpath 指定日志存储目录
--fork 后台运行
--port 指定端口(默认27017)
连接数据库
[root@Centos-2 mongodb]# ./bin/mongo
MongoDB shell version v3.4.6
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.6
Server has startup warnings:
2017-07-29T10:36:50.683+0800 I STORAGE [initandlisten]
2017-07-29T10:36:50.683+0800 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2017-07-29T10:36:50.683+0800 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2017-07-29T10:36:51.494+0800 I CONTROL [initandlisten]
2017-07-29T10:36:51.494+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-07-29T10:36:51.494+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2017-07-29T10:36:51.494+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2017-07-29T10:36:51.494+0800 I CONTROL [initandlisten]
2017-07-29T10:36:51.495+0800 I CONTROL [initandlisten]
2017-07-29T10:36:51.495+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2017-07-29T10:36:51.495+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2017-07-29T10:36:51.495+0800 I CONTROL [initandlisten]
2017-07-29T10:36:51.495+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2017-07-29T10:36:51.496+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2017-07-29T10:36:51.496+0800 I CONTROL [initandlisten]
2017-07-29T10:36:51.496+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 7671 processes, 65535 files. Number of processes should be at least 32767.5 : 0.5 times number of files.
2017-07-29T10:36:51.496+0800 I CONTROL [initandlisten]
报错解决方法如下:
WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
强烈建议使用带WiredTiger存储引擎的XFS文件系统
WARNING:Access control is not enabled for the database.
意思是:未对数据库启用访问控制,对数据和配置的读写访问不受限制
解决方法:开启数据库的认证就可以解决
在配置文件mongod.conf中开启,如下:
security:
authorization: enabled
WARNING:/sys/kernel/mm/transparent_hugepage/enabled is 'always'.
#cat /sys/kernel/mm/transparent_hugepage/enabled
[always] madvise never
关闭命令:
#echo never > /sys/kernel/mm/transparent_hugepage/enabled
WARNING:/sys/kernel/mm/transparent_hugepage/defrag is 'always'.
将/sys/kernel/mm/transparent_hugepage/defrag设置为never
#cat /sys/kernel/mm/transparent_hugepage/defrag
[always] madvise never
关闭命令:
echo never >/sys/kernel/mm/transparent_hugepage/defrag
WARNING:soft rlimits too low. rlimits set to 1024 processes, 64000 files. Number of processes should be at least 32000
设置ulimit
vi /etc/security/limits.conf
mongod soft nofile 64000
mongod hard nofile 64000
mongod soft nproc 32000
mongod hard nproc 32000
重启mongodb服务后重新登陆
[root@Centos-2 mongodb]# ./bin/mongo
MongoDB shell version v3.4.6
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.6
> show databases;
admin 0.000GB
local 0.000GB
> show dbs; #查看当前的数据库
admin 0.000GB #管理数据库
local 0.000GB