您的位置:首页 > 数据库 > Mongodb

mongodb-linux-x86_64-rhel62-3.4.0部署,主从部署

2016-12-14 19:56 489 查看
1、下载mongodb-linux-x86_64-rhel62-3.4.0软件
http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.4.0.tgz
linux 下命令输入 wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.4.0.tgz
2、解压

tar -xvf mongodb-linux-x86_64-rhel62-3.4.0.tgz

cd mongodb-linux-x86_64-rhel62-3.4.0

创建logs和db文件夹

mkdir logs db

[root@localhost mongodb-linux-x86_64-rhel62-3.4.0]# ll

总用量 132

drwxrwxr-x. 2 sh sh  4096 12月 13 06:44 bin

drwxrwxr-x. 4 sh sh  4096 12月 13 06:49 db

-rw-r--r--. 1 sh sh 34520 11月 26 06:40 GNU-AGPL-3.0

drwxrwxr-x. 2 sh sh  4096 12月 13 06:13 logs

-rw-rw-r--. 1 sh sh     6 12月 13 06:44 mongo.pid

-rw-r--r--. 1 sh sh 16726 11月 26 06:40 MPL-2

-rw-r--r--. 1 sh sh  1359 11月 26 06:40 README

-rw-r--r--. 1 sh sh 55625 11月 26 06:40 THIRD-PARTY-NOTICES

cd bin/

创建mongodb的配置文件

vim mongodb.conf

输入以下配置

port = 27017
logpath = /home/sh/local/mongodb-linux-x86_64-rhel62-3.4.0/logs/mongodb.log
dbpath = /home/sh/local/mongodb-linux-x86_64-rhel62-3.4.0/db
fork = true
pidfilepath = /home/sh/local/mongodb-linux-x86_64-rhel62-3.4.0/mongo.pid
maxConns = 20000
rest = true
#auth = true
logappend = true
配置详解看下面

--quiet	# 安静输出
--port arg	# 指定服务端口号,默认端口27017
--bind_ip arg	# 绑定服务IP,若绑定127.0.0.1,则只能本机访问,不指定默认本地所有IP
--logpath arg	# 指定MongoDB日志文件,注意是指定文件不是目录
--logappend	# 使用追加的方式写日志
--pidfilepath arg	# PID File 的完整路径,如果没有设置,则没有PID文件
--keyFile arg	# 集群的私钥的完整路径,只对于Replica Set 架构有效
--unixSocketPrefix arg	# UNIX域套接字替代目录,(默认为 /tmp)
--fork	# 以守护进程的方式运行MongoDB,创建服务器进程
--auth	# 启用验证
--cpu	# 定期显示CPU的CPU利用率和iowait
--dbpath arg	# 指定数据库路径
--diaglog arg	# diaglog选项 0=off 1=W 2=R 3=both 7=W+some reads
--directoryperdb	# 设置每个数据库将被保存在一个单独的目录
--journal	# 启用日志选项,MongoDB的数据操作将会写入到journal文件夹的文件里
--journalOptions arg	# 启用日志诊断选项
--ipv6	# 启用IPv6选项
--jsonp	# 允许JSONP形式通过HTTP访问(有安全影响)
--maxConns arg	# 最大同时连接数 默认2000
--noauth	# 不启用验证
--nohttpinterface	# 关闭http接口,默认关闭27018端口访问
--noprealloc	# 禁用数据文件预分配(往往影响性能)
--noscripting	# 禁用脚本引擎
--notablescan	# 不允许表扫描
--nounixsocket	# 禁用Unix套接字监听
--nssize arg (=16)	# 设置信数据库.ns文件大小(MB)
--objcheck	# 在收到客户数据,检查的有效性,
--profile arg	# 档案参数 0=off 1=slow, 2=all
--quota	# 限制每个数据库的文件数,设置默认为8
--quotaFiles arg	# number of files allower per db, requires --quota
--rest	# 开启简单的rest API
--repair	# 修复所有数据库run repair on all dbs
--repairpath arg	# 修复库生成的文件的目录,默认为目录名称dbpath
--slowms arg (=100)	# value of slow for profile and console log
--smallfiles	# 使用较小的默认文件
--syncdelay arg (=60)	# 数据写入磁盘的时间秒数(0=never,不推荐)
--sysinfo	# 打印一些诊断系统信息
--upgrade	# 如果需要升级数据库  * Replicaton 参数

--------------------------------------------------------------------------------

--fastsync	# 从一个dbpath里启用从库复制服务,该dbpath的数据库是主库的快照,可用于快速启用同步
--autoresync	# 如果从库与主库同步数据差得多,自动重新同步,
--oplogSize arg	# 设置oplog的大小(MB)  * 主/从参数

--------------------------------------------------------------------------------

--master	# 主库模式
--slave	# 从库模式
--source arg	# 从库 端口号
--only arg	# 指定单一的数据库复制
--slavedelay arg	# 设置从库同步主库的延迟时间  * Replica set(副本集)选项:

--------------------------------------------------------------------------------

--replSet arg	# 设置副本集名称  * Sharding(分片)选项

--------------------------------------------------------------------------------
--configsvr	# 声明这是一个集群的config服务,默认端口27019,默认目录/data/configdb
--shardsvr	# 声明这是一个集群的分片,默认端口27018
--noMoveParanoia	# 关闭偏执为moveChunk数据保存


然后启动,输入以下命令,地址路径根据自己安装的位置来修改

/home/sh/local/mongodb-linux-x86_64-rhel62-3.4.0/bin/mongod --config /home/sh/local/mongodb-linux-x86_64-rhel62-3.4.0/bin/mongodb.conf

[root@localhost mongodb-linux-x86_64-rhel62-3.4.0]# /home/sh/local/mongodb-linux-x86_64-rhel62-3.4.0/bin/mongod --config /home/sh/local/mongodb-linux-x86_64-rhel62-3.4.0/bin/mongodb.conf

2016-12-14T03:45:26.801-0800 I CONTROL  [main] ** WARNING: --rest is specified without --httpinterface,

2016-12-14T03:45:26.802-0800 I CONTROL  [main] **          enabling http interface

about to fork child process, waiting until server is ready for connections.

forked process: 2459

child process started successfully, parent exiting

看到已经启动成功了,查看一下进程

[root@localhost mongodb-linux-x86_64-rhel62-3.4.0]# ps -aux |grep mongodb

Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ

root      2459  3.2  3.9 362340 39568 ?        Sl   03:45   0:01 /home/sh/local/mongodb-linux-x86_64-rhel62-3.4.0/bin/mongod --config /home/sh/local/mongodb-linux-x86_64-rhel62-3.4.0/bin/mongodb.conf

3、开始测试mongodb

执行一下命令,可以通过linux直接连接

/home/sh/local/mongodb-linux-x86_64-rhel62-3.4.0/bin/mongo

[root@localhost mongodb-linux-x86_64-rhel62-3.4.0]# /home/sh/local/mongodb-linux-x86_64-rhel62-3.4.0/bin/mongo

MongoDB shell version v3.4.0

connecting to: mongodb://127.0.0.1:27017

MongoDB server version: 3.4.0

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-12-14T03:45:26.801-0800 I CONTROL  [main] ** WARNING: --rest is specified without --httpinterface,

2016-12-14T03:45:26.802-0800 I CONTROL  [main] **          enabling http interface

2016-12-14T03:45:27.116-0800 I STORAGE  [initandlisten] 

2016-12-14T03:45:28.006-0800 I CONTROL  [initandlisten] 

2016-12-14T03:45:28.006-0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.

2016-12-14T03:45:28.006-0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.

2016-12-14T03:45:28.006-0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.

2016-12-14T03:45:28.006-0800 I CONTROL  [initandlisten] 

2016-12-14T03:45:28.007-0800 I CONTROL  [initandlisten] 

2016-12-14T03:45:28.007-0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.

2016-12-14T03:45:28.007-0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'

2016-12-14T03:45:28.007-0800 I CONTROL  [initandlisten] 

2016-12-14T03:45:28.008-0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.

2016-12-14T03:45:28.008-0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'

2016-12-14T03:45:28.008-0800 I CONTROL  [initandlisten] 

创建test数据库

> use test

switched to db test

插入数据

> db.testdb.insert({"test2":"test2222"})

WriteResult({ "nInserted" : 1 })

查询库里面全部的数据

> db.testdb.find();

{ "_id" : ObjectId("585005092a9cae014052122c"), "test1" : "testval1" }

{ "_id" : ObjectId("585006142a9cae014052122d"), "test1" : "testval1" }

{ "_id" : ObjectId("585131692c35d3943d9e3d04"), "test2" : "test2222" }



表示已经创建成功了

4、开始配置主从

把上面的第2步重新部署到另外一台服务器

主服务器的启动命令改成下面的

/home/sh/local/mongodb-linux-x86_64-rhel62-3.4.0/bin/mongod --config /home/sh/local/mongodb-linux-x86_64-rhel62-3.4.0/bin/mongodb.conf -master

从服务器的启动命令改成下面的

/home/sh/local/mongodb-linux-x86_64-rhel62-3.4.0/bin/mongod --config /home/sh/local/mongodb-linux-x86_64-rhel62-3.4.0/bin/mongodb.conf -slave -source 192.168.1.130:27017

在从库里面执行

/home/sh/local/mongodb-linux-x86_64-rhel62-3.4.0/bin/mongo

> use test

switched to db test

> db.testdb.find();

Error: error: {

        "ok" : 0,

        "errmsg" : "not master and slaveOk=false",

        "code" : 13435,

        "codeName" : "NotMasterNoSlaveOk"

}



执行查询操作的时候会报not master and slaveOk=false这个错误,这个时候得要执行一下db.getMongo().setSlaveOk();这个命令

再执行一下

db.testdb.find();就可以看到主库里面插入的数据了

> db.testdb.find();

Error: error: {

        "ok" : 0,

        "errmsg" : "not master and slaveOk=false",

        "code" : 13435,

        "codeName" : "NotMasterNoSlaveOk"

}

> db.getMongo().setSlaveOk();

> db.testdb.find();

{ "_id" : ObjectId("585005092a9cae014052122c"), "test1" : "testval1" }

{ "_id" : ObjectId("585006142a9cae014052122d"), "test1" : "testval1" }

{ "_id" : ObjectId("5851329d31dae54a987b2843"), "test2" : "test2222" }

{ "_id" : ObjectId("585132c29f0a9cd9e3310dd3"), "test2" : "test2222" }

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息