mongodb 单节点安装
1.到官网下载mongodb:
https://www.mongodb.org
或直接下载:
https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.2.8.tgz
2.安装mongodb:
将上传服务器并文件解压:
可以将文件放在lamp环境下的server目录
mongodb不是源码包不需要编译,直接解压即可
[root@iZ28c6xv2w0Z server]# tar zxf mongodb-linux-x86_64-rhel62-3.2.8.tgz
文件夹名太长可以将文件夹重命名
[root@iZ28c6xv2w0Z server]# mv mongodb-linux-x86_64-rhel62-3.2.8 mongodb
然后在mongodb目录下创建data目录,作为数据库目录,启动mongodb时需要指定此文件夹
[root@iZ28c6xv2w0Z mongodb]# mkdir data
在mongodb下创dblogs文件作为数据库的日志文件,启动mongodb时需要指定此文件夹
[root@iZ28c6xv2w0Z mongodb]# touch dblogs
3.启动数据库:
--dbpath=数据库目录
--logpath=日志目录
--fork 后台启动
[root@iZ28c6xv2w0Z bin]# ./mongod --dbpath=/alidata/server/mongodb/data/ --logpath=/alidata/server/mongodb/dblogs --fork about to fork child process, waiting until server is ready for connections. forked process: 11322 child process started successfully, parent exiting
查看进程:
[root@iZ28c6xv2w0Z bin]# ps -ef | grep mongodb root 11322 1 0 17:23 ? 00:00:01 ./mongod --dbpath=/alidata/server/mongodb/data/ --logpath=/alidata/server/mongodb/dblogs --fork root 11344 11210 0 17:29 pts/0 00:00:00 grep mongodb
4.配置开机启动mongodb:
打开开机启动配置文件
[root@iZ28c6xv2w0Z bin]# vim /etc/rc.local
在最后添加要启动的mongodb数据库命令(/etc/rc.local 文件是启动比较靠后执行的文件)
#!/bin/sh # # This script will be executed *after* all the other init scripts. # You can put your own initialization stuff in here if you don't # want to do the full Sys V style init stuff. touch /var/lock/subsys/local /etc/init.d/mysqld start /etc/init.d/httpd start /etc/init.d/vsftpd start /alidata/server/mongodb/bin/mongod --dbpath=/alidata/server/mongodb/data/ --logpath=/alidata/server/mongodb/dblogs --fork
5.关闭mongodb:
[root@iZ28c6xv2w0Z bin]# pkill mongod [root@iZ28c6xv2w0Z bin]# ps -ef |grep mongodb root 11416 11210 0 17:43 pts/0 00:00:00 grep mongodb
如果用 kill -9 杀掉进程的话,启动时要去data(数据库目录)目录下删除数据库锁文件(mongod.lock) 才可重新启动。
启动命令常用参数:
--dbpath 制定数据库目录
--port 指定数据库的端口,默认是27017端口
--bind_ip 绑定IP
--directoryperdb 为每个db创建一个独立子目录
--logpath 指定日志存放目录
--logappend 指定日志生成方式
--pidfilepath 指定进程文件路径,不指定不产生进程文件
--KeyFile 集群模式的关键标识
--journal 启用日志
--nssize 指定.ns文件的大小,单位MB,默认16M,最大2GB
--maxConns 最大的并发连接数
--notablescan 不允许进行表扫描
--noprealloc 关闭数据文件的预分配功能
--fork 以后台Daemon形式运行服务
更多的参数选项 mongodb --help 进行查看
6.登录mongodb的数据库客户端:
默认进入test数据库
[root@iZ28c6xv2w0Z bin]# ./mongo MongoDB shell version: 3.2.8 connecting to: test Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-user Server has startup warnings: 2016-08-07T17:45:02.183+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended. 2016-08-07T17:45:02.185+0800 I CONTROL [initandlisten] 2016-08-07T17:45:02.227+0800 I CONTROL [initandlisten] 2016-08-07T17:45:02.227+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 3750 processes, 65535 files. Number of processes should be at least 32767.5 : 0.5 times number of files. >
退出数据库
> exit bye
二进制安装单节点的mongodb
创建运行mongo的用户:
useradd mongod
创建lvm:
pvcreate /dev/vdb vgcreate vg_mongo /dev/vdb lvcreate -l 100%VG -n lv_mongo vg_mongo
创建文件系统:
mkfs.xfs /dev/mapper/vg_mongo-lv_mongo
挂载到目录:
chown mongod.mongod /dv mount /dev/mapper/vg_mongo-lv_mongo /dv
查看磁盘信息:
~# blkid /dev/mapper/vg_mongo-lv_mongo /dev/mapper/vg_mongo-lv_mongo: UUID="b36b5af4-e7db-46fb-b8d3-3f9097490762" TYPE="xfs"
设置开机自动挂载:
~# vim /etc/fstab UUID="b36b5af4-e7db-46fb-b8d3-3f9097490762" /dv xfs defaults 0 0
测试自动挂载:
umount /dv mount | grep dv mount -a mount | grep dv
下载二进制包:
wget http://10.3.16.34/mongodb-linux-x86_64-ubuntu1404-3.2.0.tgz
解压到指定目录:
tar -xf mongodb-linux-x86_64-ubuntu1404-3.2.0.tgz -C /opt/ cd /opt chown -R mongod.mongod mongodb-linux-x86_64-ubuntu1404-3.2.0 ln -sv mongodb-linux-x86_64-ubuntu1404-3.2.0 mongodb
创建配置文件:
cat > /etc/mongod.conf <<EOF # mongod.conf # for documentation of all options, see: # http://docs.mongodb.org/manual/reference/configuration-options/ # where to write logging data. systemLog: destination: file logAppend: true path: /var/log/mongodb/mongod.log # Where and how to store data. storage: dbPath: /dv journal: enabled: true # engine: # mmapv1: # wiredTiger: # how the process runs processManagement: fork: true # fork and run in background pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile # network interfaces net: port: 27017 bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces. security: authorization: enabled # 开启安全验证 #operationProfiling: #replication: # replSetName: spock #sharding: ## Enterprise-Only Options #auditLog: #snmp: EOF
创建systemd文件:
cat > /usr/lib/systemd/system/mongod.service <<EOF [Unit] Description=MongoDB Database Server Documentation=https://docs.mongodb.org/manual After=network.target [Service] User=mongod Group=mongod Environment="OPTIONS=-f /etc/mongod.conf" ExecStart=/opt/mongodb/bin/mongod $OPTIONS ExecStartPre=/bin/mkdir -p /var/run/mongodb ExecStartPre=/bin/chown mongod:mongod /var/run/mongodb ExecStartPre=/bin/chmod 0755 /var/run/mongodb PermissionsStartOnly=true PIDFile=/var/run/mongodb/mongod.pid Type=forking # file size LimitFSIZE=infinity # cpu time LimitCPU=infinity # virtual memory size LimitAS=infinity # open files LimitNOFILE=64000 # processes/threads LimitNPROC=64000 # locked memory LimitMEMLOCK=infinity # total threads (user+kernel) TasksMax=infinity TasksAccounting=false # Recommended limits for for mongod as specified in # http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings [Install] WantedBy=multi-user.target EOF
启动:
systemctl enable mongod.service systemctl start mongod.service systemctl status mongod.service
查看日志:
tail -f /var/log/mongodb/mongod.log
生成随机密码:
~# < /dev/urandom tr -dc 0-9-A-Z-a-z-/|head -c ${1:-16};echo
环境变量:
cat > /etc/profile.d/mongodb.sh <<EOF export PATH=/opt/mongodb/bin/:$PATH EOF
连接mongo:
mongo
切换用户:
use admin
设置密码:
db.createUser( { user: "admin", pwd: "xH6IwXJBc2JVuw5d", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] } )
退出:
exit
使用认证的方式连接:
mongo --port 27017 -u "admin" -p "123456" --authenticationDatabase "admin