1.rpm包安装mongodb3.4.6 下载地址:https://repo.mongodb.org/yum/redhat/7/mongodb-org/3.4/x86_64/RPMS/ rpm -ivh mongodb-org-shell-3.4.6-1.el7.x86_64.rpm rpm -ivh mongodb-org-tools-3.4.6-1.el7.x86_64.rpm rpm -ivh mongodb-org-server-3.4.6-1.el7.x86_64.rpm rpm -ivh mongodb-org-3.4.6-1.el7.x86_64.rpm 2.启动mongodb,设置开机自启动 [root@node1 mongodb]# systemctl start mongod [root@node1 mongodb]# systemctl enable mongod 通过启动MongoDB的服务mongod运行起来之后可以通过netstat查看服务端口(mongodb运行在TCP的27017端口上) [root@node1 mongodb]# netstat -tanlp |grep mongod tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 63532/mongod 3.MongoDB的文件和目录说明 # 配置文件; /etc/mongod.conf # 日志文件; /var/log/mongodb/mongod.log # PID文件; /var/run/mongodb/mongod.pid # 数据目录; /var/lib/mongo # 配置文件; /etc/mongod.conf MongoDB工具集 MongoDB在bin目录下提供了一系列有用的工具,这些工具提供了MongoDB在运维管理上的方便,mongodb 3.0官方使用GO语言全部重写了这些工具。 Bsondump – 将bson格式的文件转储为json格式的数据。 Mongo – 客户端命令行工具,其实也是一个javascript解释器,支持js语法。 Mongod – 数据库服务器端,每个实例启动一个进程,可以fork为后台运行。 Mongodump/mongorestore – 数据库备份和恢复工具。 Mongoexport/mongoimport – 数据导出和导入工具。 Mongofiles – GridFS管理工具,可实现二进制文件的存储。 Mongos – 分片路由,如果是用来sharding功能,则应用程序连接的是mongos而不是mongod。 Mongostat – 实例统计信息。 Mongotop – 数据库磁盘I/O实时分析工具。 Mongooplog – 日志分析工具。 Mongoopperf – 性能测试工具。 MongoDB后台管理 Shell 当MongoDB安装好并启动之后,如果你需要进入MongoDB后台管理,你需要先打开mongodb装目录的下的bin目录,然后执行mongo命令文件。MongoDB Shell是MongoDB自带的交互式Javascript shell,用来对MongoDB进行操作和管理的交互式环境。 当你进入mongoDB后台后,它默认会链接到 test 文档(数据库): [root@node1 mongodb]# mongo MongoDB shell version v3.4.6 connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 3.4.6 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: 2018-06-08T22:05:22.147+0800 I CONTROL [initandlisten] 2018-06-08T22:05:22.147+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database. 2018-06-08T22:05:22.147+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. 2018-06-08T22:05:22.147+0800 I CONTROL [initandlisten] 2018-06-08T22:05:22.169+0800 I CONTROL [initandlisten] 2018-06-08T22:05:22.169+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. 2018-06-08T22:05:22.169+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2018-06-08T22:05:22.169+0800 I CONTROL [initandlisten] > > 上面通过mongo客户端进入到了mongodb shell交互式接口,但是会有两个警告信息,警告信息是关于Linux中hugepage的。 MongoDB警告信息就是要求关闭hugepage,Oracle也要求关闭,HDFS会因为这个性能严重受影响。就是允许hugepage可以动态分配,而不是系统启动时预先分配,看上去对内存消耗很大的服务都不喜欢它 以下操作是临时关闭hugepage开机自动分配: [root@node1 mongodb]# echo "never" > /sys/kernel/mm/transparent_hugepage/enabled [root@node1 mongodb]# echo "never" > /sys/kernel/mm/transparent_hugepage/defrag 4.开始配置集群环境 master node1: 10.11.0.210 slave01 node2: 10.11.0.212 slave02 node5: 10.11.0.215 # 配置文件(三台主机保持一致) [root@node1 data]# cat /etc/mongod.conf systemLog: destination: file logAppend: true path: /var/log/mongodb/mongod.log storage: dbPath: /var/lib/mongo journal: enabled: true processManagement: fork: true # fork and run in background pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile # 监听ip和端口 net: port: 27017 bindIp: 0.0.0.0 # http的访问接口,可以通过http://ip:28017访问 http: RESTInterfaceEnabled: true replication: oplogSizeMB: 1024 # 集群名称 replSetName: wanxin 配置文件修改的时候一定要注意空格,两个空格,类似jinja的语法 一定要注意空格,NND害S人,wx是我随便取的名字。 然后关机克隆三份虚拟文件,也就是在做三台机器出来。 # 集群配置 [root@node1 mongodb]# mongo MongoDB shell version v3.4.6 connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 3.4.6 > config = {_id:"wanxin", members:[{_id:0, host:"10.11.0.210:27017"},{_id:1, host:"10.11.0.212:27017"},{_id:2, host:"10.11.0.215:27017"}]} { "_id" : "wanxin", "members" : [ { "_id" : 0, "host" : "10.11.0.210:27017" }, { "_id" : 1, "host" : "10.11.0.212:27017" }, { "_id" : 2, "host" : "10.11.0.215:27017" } ] } # 初始化集群配置 > rs.initiate(config) { "ok" : 1 } wanxin:SECONDARY> wanxin:PRIMARY> wanxin:PRIMARY> # 查看集群状态 wanxin:PRIMARY> rs.status() { "set" : "wanxin", "date" : ISODate("2018-06-11T07:34:53.872Z"), "myState" : 1, "term" : NumberLong(1), "heartbeatIntervalMillis" : NumberLong(2000), "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1528702485, 1), "t" : NumberLong(1) }, "appliedOpTime" : { "ts" : Timestamp(1528702485, 1), "t" : NumberLong(1) }, "durableOpTime" : { "ts" : Timestamp(1528702485, 1), "t" : NumberLong(1) } }, "members" : [ { "_id" : 0, "name" : "10.11.0.210:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 813, "optime" : { "ts" : Timestamp(1528702485, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2018-06-11T07:34:45Z"), "electionTime" : Timestamp(1528702353, 1), "electionDate" : ISODate("2018-06-11T07:32:33Z"), "configVersion" : 1, "self" : true }, { "_id" : 1, "name" : "10.11.0.212:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 150, "optime" : { "ts" : Timestamp(1528702485, 1), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1528702485, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2018-06-11T07:34:45Z"), "optimeDurableDate" : ISODate("2018-06-11T07:34:45Z"), "lastHeartbeat" : ISODate("2018-06-11T07:34:52.013Z"), "lastHeartbeatRecv" : ISODate("2018-06-11T07:34:52.376Z"), "pingMs" : NumberLong(0), "syncingTo" : "10.11.0.215:27017", "configVersion" : 1 }, { "_id" : 2, "name" : "10.11.0.215:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 150, "optime" : { "ts" : Timestamp(1528702485, 1), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1528702485, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2018-06-11T07:34:45Z"), "optimeDurableDate" : ISODate("2018-06-11T07:34:45Z"), "lastHeartbeat" : ISODate("2018-06-11T07:34:52.013Z"), "lastHeartbeatRecv" : ISODate("2018-06-11T07:34:52.356Z"), "pingMs" : NumberLong(0), "syncingTo" : "10.11.0.210:27017", "configVersion" : 1 } ], "ok" : 1 } 主节点中有一些数据,不会自动同步到slave节点,在slave节点查询报错: wanxin:SECONDARY> db.students.find() Error: error: { "ok" : 0, "errmsg" : "not master and slaveOk=false", "code" : 13435, "codeName" : "NotMasterNoSlaveOk" } wanxin:SECONDARY> # 解决 wanxin:SECONDARY> rs.slaveOk();
mongodb的简单安装
需求: 给MongoDB添加两个用户分别用于开发环境和测试环境,用户访问指定的db并拥有db的最高权限。 解决步骤: 1.关闭权限验证、启动MongoDB 2.创建MongoDB超级管理员,给予最高权限 3.分别创建两个用户,给予各自db的最高权限 4.开启权限验证、启动MongoDB 5.测试 一、安装并配置mongodb 1.将mongodb二进制mongodb-linux-x86_64-3.4.6.tgz解压到/usr/local目录下 2.创建软连接 cd /usr/local ln -s mongodb-linux-x86_64-3.4.6 mongodb # 修改mongodb的配置 /usr/loca/mongodb/conf/mongodb.conf dbpath=/data/mongodb logpath=/data/mongodb/log/mongodb.log logappend=true port=27017 fork=true auth=true #开启认证 verbose=true vvvv=true #启动verbose冗长信息,它的级别有 vv~vvvvv,v越多级别越高,在日志文件中记录的信息越详细. maxConns=20000 #默认值:取决于系统(即的ulimit和文件描述符)限制。MongoDB中不会限制其自身的连接。 #logappend=true #写日志的模式:设置为true为追加。 pidfilepath=/var/run/mongo.pid #cpu=true #设置为true会强制mongodb每4s报告cpu利用率和io等待,把日志信息写到标准输出或日志文件。 #directoryperdb=ture #数据目录存储模式,如果直接修改原来的数据会不见了 #nohttpinterface=false #28017 端口开启的服务。默认false,支持 #notablescan=false#不禁止表扫描操作 profile=0 #数据库分析等级设置,0 关 2 开。包括所有操作。 1 开。仅包括慢操作. slowms=200 #记录profile分析的慢查询的时间,默认是100毫秒. quiet=true syncdelay=60 #刷写数据到日志的频率,通过fsync操作数据。默认60秒 3.执行启动 # 创建mongodb数据目录 mkdir -p /data/mongodb/log # 启动mongodb ./mongodb.sh start # 启动脚本 [root@eus_pe_web02:/usr/local/mongodb]# cat mongodb.sh #!/bin/sh # chkconfig: 2345 93 18 # author:QingFeng # description:MongoDB(MongoDB-2.4.9) #默认参数设置 #mongodb 家目录 MONGODB_HOME=/usr/local/mongodb #mongodb 启动命令 MONGODB_BIN=$MONGODB_HOME/bin/mongod #mongodb 配置文件 MONGODB_CONF=$MONGODB_HOME/conf/mongodb.conf #mongodb PID MONGODB_PID=/var/run/mongo.pid #最大文件打开数量限制 SYSTEM_MAXFD=65535 #mongodb 名字 MONGODB_NAME="mongodb" . /etc/rc.d/init.d/functions if [ ! -f $MONGODB_BIN ] then echo "$MONGODB_NAME startup: $MONGODB_BIN not exists! " exit fi start(){ ulimit -HSn $SYSTEM_MAXFD $MONGODB_BIN --config="$MONGODB_CONF" ret=$? if [ $ret -eq 0 ]; then action $"Starting $MONGODB_NAME: " /bin/true else action $"Starting $MONGODB_NAME: " /bin/false fi } stop(){ PID=$(ps aux |grep "$MONGODB_NAME" |grep "$MONGODB_CONF" |grep -v grep |wc -l) if [[ $PID -eq 0 ]];then action $"Stopping $MONGODB_NAME: " /bin/false exit fi kill -HUP `cat $MONGODB_PID` ret=$? if [ $ret -eq 0 ]; then action $"Stopping $MONGODB_NAME: " /bin/true rm -f $MONGODB_PID else action $"Stopping $MONGODB_NAME: " /bin/false fi } restart() { stop sleep 2 start } case "$1" in start) start ;; stop) stop ;; status) status $prog ;; restart) restart ;; *) echo $"Usage: $0 {start|stop|status|restart}" esac 二、登录mongodb进行授权 # 添加环境变量 export MONGODB_HOME=/usr/local/mongodb export PATH=$MONGODB_HOME/bin:$PATH PATH=/usr/local/mongodb/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/ruby/bin;export PATH 具体步骤: 1.关闭权限验证、重新启动MongoDB MongoDB默认是不开启权限验证的,所以直接使用命令启动(需要在bin目录下): # 修改mongodb的配置注释掉下面的认证 /usr/loca/mongodb/conf/mongodb.conf # auth=true #开启认证 2.添加并认证管理员 先在另一个命令窗口进入客户端(Linux可以让服务端在后台运行):mongo 然后在admin添加管理员(role:root表示最高权限): use admin db.createUser({ user:"admin", pwd:"admin", roles:[{ role:"root", db:"admin" }] }) db.auth("admin", "admin") 3.添加用户并认证用户(要注意的是“dbAdmin”其实是没有读写权限的) > use chinasoft switched to db chinasoft > db.createUser({ user:"china", pwd:"china2018", roles:[{ role:"dbAdmin", db:"chinasoft" },{ role:"readWrite", db:"chinasoft" }] }) > db.auth("china", "china2018") 1 > quit() 4.开启权限验证、重新启动MongoDB(先关闭第1步启动的服务端) # 通过文件导入collections操作 [root@eus_pe_web02:/data]# mongoimport -h 172.17.0.37:27017 --db chinasoft -u china -p china2018 --collection attach --file /data/attach.json 2018-10-24T02:57:39.940-0700 connected to: 172.17.0.37:27017 2018-10-24T02:57:39.957-0700 imported 84 documents # 验证登录 mongo -h 172.17.0.37:27017/chinasoft_prod -u china -p china2018 [root@eus_pe_web02:/data]# mongo MongoDB shell version v3.4.6 connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 3.4.6 > use chinasoft switched to db chinasoft_prod > db.auth("hipdfUser", "hipdf2018") 1 > show collections attach > db.attach.find() { "_id" : ObjectId("5bab1fc5b83567b81e4d4c72"), "attach_id" : "0000-4794-1661-b22c-eb7", "remote_path" : "chinasoft/files/0000-4794-1661-b22c-eb7.zip", "rmfile" : false, "code" : "88102", "name" : "Resume - Timeless Dark", "file_name" : "timeless_dark.zip", "status" : 1, "create_at" : 1539676594, "permission" : 4, "download_inc" : 2 } { "_id" : ObjectId("5bab1fc5b83567b81e4d4c6e"), "attach_id" : "0000-4794-1661-11ae-eb7", "remote_path" : "files/0000-3083-1665-be96-249.jpg", "rmfile" : false, "code" : "sdasd", "name" : "asdasdas", "file_name" : "h10 (1).jpg", "create_at" : 1539853645, "permission" : 1, "status" : 0 } { "_id" : ObjectId("5bab1fc5b83567b81e4d4c74"), "attach_id" : "0000-4794-1661-aa53-eb7", "remote_path" : "chinasoft/files/0000-4794-1661-aa53-eb7.zip", "rmfile" : false, "code" : "88103", "name" : "Resume - Elegant Green", "file_name" : "elegant_green.zip", "status" : 1, "create_at" : 1539676616, "permission" : 4 } # 导入文件 # cat attach.json {"_id":{"$oid":"5bab1fc5b83567b81e4d4c6c"},"attach_id":"0000-4794-1661-4b23-eb7","remote_path":"files/0000-15b7-1668-d074-605.pdf","rmfile":false,"code":"sdsds","name":"1212121","file_name":"1 (11) - 副本.pdf","download_inc":13,"create_at":1539853656,"permission":1,"status":0} {"_id":{"$oid":"5bab1fc5b83567b81e4d4c6e"},"attach_id":"0000-4794-1661-11ae-eb7","remote_path":"files/0000-3083-1665-be96-249.jpg","rmfile":false,"code":"sdasd","name":"asdasdas","file_name":"h10 (1).jpg","create_at":1539853645,"permission":1,"status":0} {"_id":{"$oid":"5bab1fc5b83567b81e4d4c70"},"attach_id":"0000-4794-1661-173b-eb7","remote_path":"chinasoft/files/0000-4794-1661-173b-eb7.zip","rmfile":false,"code":"88101","name":"Resume - Timeless Lite","file_name":"timeless_lite.zip","status":1,"download_inc":4,"create_at":1539676578,"permission":4} {"_id":{"$oid":"5bab1fc5b83567b81e4d4c72"},"attach_id":"0000-4794-1661-b22c-eb7","remote_path":"chinasoft/files/0000-4794-1661-b22c-eb7.zip","rmfile":false,"code":"88102","name":"Resume - Timeless Dark","file_name":"timeless_dark.zip","status":1,"create_at":1539676594,"permission":4,"download_inc":2} {"_id":{"$oid":"5bab1fc5b83567b81e4d4c74"},"attach_id":"0000-4794-1661-aa53-eb7","remote_path":"chinasoft/files/0000-4794-1661-aa53-eb7.zip","rmfile":false,"code":"88103","name":"Resume - Elegant Green","file_name":"elegant_green.zip","status":1,"create_at":1539676616,"permission":4} {"_id":{"$oid":"5bab1fc5b83567b81e4d4c76"},"attach_id":"0000-4794-1661-f3c5-eb8","remote_path":"chinasoft/files/0000-4794-1661-f3c5-eb8.zip","rmfile":false,"code":"88104","name":"Resume - Elegant Gold","file_name":"elegant_gold.zip","status":1,"create_at":1539676640,"permission":4,"download_inc":2} ...... # 忘记mongodb数据库管理员密码的处理方法 vim /etc/mongodb.conf # 修改 mongodb 配置,将 auth = true 注释掉,或者改成 false service mongodb restart # 重启 mongodb 服务 mongo # 运行客户端(也可以去mongodb安装目录下运行这个) use admin # 切换到系统帐户表 db.system.users.find() # 查看当前帐户(密码有加密过) db.system.users.remove({}) # 删除所有帐户 db.addUser('admin','password') # 添加新帐户 vim /etc/mongodb.conf # 恢复 auth = true service mongodb restart # 重启 mongodb 服务