
mongodb 文档型数据库以json格式存储数据
mongodb与RDBMS最的区别在于:没有固定的行列组织数据结构
mongodb数据存储格式:BSON
二进制的JSON,JSON文档的二进制编码存储格式
BSON有JSON没有的Date和BinData
mongodb中的document以及BSON形式存放
| Mongodb逻辑结构 | MySQL逻辑结构 |
|---|---|
| 库 | 库 |
| 集合 | 表 |
| 文档 | 数据行 |

安装部署
系统准备
redhat或centos6.2以上系统
系统开发包完整
ip地址和hosts文件解析正常
iptables防火墙和SeLinux关闭
关闭大页内存机制
在/etc/rc.local最后添加如下代码
echo "永久关闭大页内存机制"
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi
echo "临时关闭大页内存机制"
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag
其他版本参照官方文档修改方法
修改/etc/security/limits.conf
* - nofile 65535mongodb安装
创建所需用户和组
useradd mongod
echo "mongod:mongod" | chpasswd
创建mongodb所需目录结构
mkdir -p /mongodb/conf
mkdir -p /mongodb/log
mkdir -p /mongodb/bin
mkdir -p /mongodb/data
下载软件并解压
cd /opt
wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.6.12.tgz
tar xf mongodb-linux-x86_64-rhel70-3.6.12.tgz
cp -a /opt/mongodb-linux-x86_64-rhel70-3.6.12/bin/* /mongodb/bin
设置目录结构权限
chown -R mongod:mongod /mongodb设置环境变量
su - mongod
cat >/home/mongod/.bash_profile <<EOF
export PATH=/mongodb/bin/:\$PATH
EOF
source .bash_profile
启动mongodb
su - mongod
mongod --dbpath=/mongodb/data --logpath=/mongodb/log/mongodb.log --port=27017 --logappend --fork
登录mongodb
mongo配置文件结构
mongodb推荐使用YAML格式
NOTE:
YAML does not support tab characters for identation: use spaces instead
--系统日志有关
systemLog:
destination: file
path: "/mongodb/log/mongodb.log" --日志位置
logAppend: true --日志位置追加模式记录
--数据存储有关
storage:
journal:
enabled: true --是否开启日志
dbPath: "/mongodb/data" --数据路径的位置
--进程控制
processManagement:
fork: true --后台守护进程
pidFilePath: <string> --pid文件的位置,一般不用配置,可以去掉这行,自动生成到data中
--网络配置有关
net:
bindIP: <ip> --监听地址,默认监听0.0.0.0
port: <port> --端口号,默认监听27017
--安全验证有关配置
security:
authorization: enabled --是否打开用户密码验证
配置文件示例
在mongod用户下执行如下脚本
cat > /mongodb/conf/mongo.conf <<EOF
systemLog:
destination: file
path: "/mongodb/log/mongodb.log"
logAppend: true
storage:
journal:
enabled: true
dbPath: "/mongodb/data"
processManagement:
fork: true
net:
bindIp: 0.0.0.0
port: 27017
security:
authorization: enabled
EOF
关闭mongodb
mongod -f /mongodb/conf/mongo.conf --shutdown启动mongodb
mongod -f /mongodb/conf/mongo.conf配置systemd管理mongodb
注意,需要以root权限操作管理mongodb
cat > /etc/systemd/system/mongod.service <<EOF
[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target
[Service]
User=mongod
Type=forking
ExecStart=/mongodb/bin/mongod --config /mongodb/conf/mongo.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/mongodb/bin/mongod --config /mongodb/conf/mongo.conf --shutdown
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
管理命令
注意:启动时需要将原来普通用户启动的mongodb服务关闭,否则启动失败
systemctl restart mongod
systemctl stop mongod
systemctl start mongod
登录mongodb
mongo
show databases;
use admin;
show tables;

文章转载自anyux,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




