Centos7.5部署mongodb集群

基础环境介绍

环境中采用的是两个虚拟机部署,对应信息如下

	主节点		:ipaddr: 192.168.200.101		hostname: mongo-master
备节点&仲裁节点	:ipaddr:192.168.200.102			hostname:mongo-node

基础环境准备

master-configure
[root@localhost ~]# hostnamectl set-hostname mongo-master && bash				//设置主机名
[root@mongo-master ~]# hostname --all-ip-address								//查看本机IP地址
192.168.200.101 
[root@mongo-master ~]# systemctl stop firewalld									//关闭firewall防火墙
[root@mongo-master ~]# systemctl disable firewalld								//关闭开机自动
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@mongo-master ~]# iptables -F
[root@mongo-master ~]# setenforce 0
setenforce: SELinux is disabled
[root@mongo-master ~]# ping -c 4 csdn.net
PING csdn.net (47.95.164.112) 56(84) bytes of data.
64 bytes from 47.95.164.112 (47.95.164.112): icmp_seq=1 ttl=128 time=4.74 ms
64 bytes from 47.95.164.112 (47.95.164.112): icmp_seq=2 ttl=128 time=5.97 ms
64 bytes from 47.95.164.112 (47.95.164.112): icmp_seq=3 ttl=128 time=4.76 ms
64 bytes from 47.95.164.112 (47.95.164.112): icmp_seq=4 ttl=128 time=4.90 ms

--- csdn.net ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3007ms
rtt min/avg/max/mdev = 4.741/5.096/5.979/0.518 ms

slave-configure
[root@localhost ~]# hostnamectl set-hostname mongo-node  && bash
[root@mongo-node ~]# systemctl stop firewalld
[root@mongo-node ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@mongo-node ~]# iptables -F
[root@mongo-node ~]# setenforce 0
setenforce: SELinux is disabled
[root@mongo-node ~]# ping -c 4 csdn.net
PING csdn.net (47.95.164.112) 56(84) bytes of data.
64 bytes from 47.95.164.112 (47.95.164.112): icmp_seq=1 ttl=128 time=4.66 ms
64 bytes from 47.95.164.112 (47.95.164.112): icmp_seq=2 ttl=128 time=6.18 ms
64 bytes from 47.95.164.112 (47.95.164.112): icmp_seq=3 ttl=128 time=5.47 ms
64 bytes from 47.95.164.112 (47.95.164.112): icmp_seq=4 ttl=128 time=8.51 ms

--- csdn.net ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 4.662/6.209/8.513/1.435 ms

mongo环境部署

1.下载mongo软件包

[root@mongo-master ~]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.4.10.tgz

2.创建ssh-key秘钥【没有其他作用只是为了方便传包及配置文件】

[root@mongo-master ~]# ssh-keygen -t rsa									//生成秘钥
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:zuXmZBo5/eDNgxQegMDIxiHCqFMeL9rHUYSgFCRDGSE root@mongo-master
The key's randomart image is:
+---[RSA 2048]----+
|EXB=.+..         |
|**B o o .        |
|o+ o .   .       |
|o o o     o      |
| + o .  S..o     |
|. . o  o =o      |
|   .    *.B.     |
|         X.=.    |
|        . o +.   |
+----[SHA256]-----+
[root@mongo-master ~]# ssh-copy-id 192.168.200.102				//将秘钥copy过去
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.200.102 (192.168.200.102)' can't be established.
ECDSA key fingerprint is SHA256:UaQUa6bWaxIBXBPc+2XSvvlDRjdj/TbpXOz7x2eJyWI.
ECDSA key fingerprint is MD5:b6:e5:99:6b:ea:e8:fe:29:ad:e3:4d:7f:6e:74:71:e3.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.200.102'"
and check to make sure that only the key(s) you wanted were added.
[root@mongo-master ~]# scp mongodb-linux-x86_64-rhel70-3.4.10.tgz 192.168.200.102:/root/
mongodb-linux-x86_64-rhel70-3.4.10.tgz                                                     100%   96MB  61.1MB/s   00:01    

3.mongodb安装部署

[root@mongo-master ~]# tar xf mongodb-linux-x86_64-rhel70-3.4.10.tgz 
[root@mongo-master ~]# mv mongodb-linux-x86_64-rhel70-3.4.10/ /usr/local/mongo-3.4.10
[root@mongo-master ~]# cd /usr/local/mongo-3.4.10/
[root@mongo-master mongo-3.4.10]# mkdir config						//创建配置文件目录
[root@mongo-master mongo-3.4.10]# vim config/mongo-master.conf
[root@mongo-master mongo-3.4.10]# cat config/mongo-master.conf 
port=27017
bind_ip=192.168.200.101
dbpath=/data/mongodb
logpath=/data/logs/mongodb/mongodb.log
logappend=true
fork=true
maxConns=5000
storageEngine=mmapv1
pidfilepath=/var/run/mongodb/mongo.pid
directoryperdb=true
noprealloc=true
oplogSize=10000
#replSet=mslinux
配置文件讲解
port:指的是mongodb启动监听的端口
bind_ip:指的是监听地址
dbpath:数据目录存储位置
logpath:日志文件位置
logappend:以追加的方式记录日志
fork:以后台的方式运行
maxConns:最大连接数
storageEngine:设置mongodb存储引擎【MMAPv1 是mongodb 在3.2以前默认的存储引擎,在3.2 之后默认的存储引擎为WiredTiger,MMAPv1存储引擎基于内存映射文件,它擅长高容量的插入,读取和更新。】
pidfilepath:mongodb启动后pid文件路径
directoryperdb:为每一个数据库按照数据库名建立文件夹存放
noprealloc:不预先分配存储
oplogsize:mongodb操作日志文件的最大大小,单位为Mb,默认为硬盘剩余空间的5%
#replSet:设置replSet的名字,集群会用到,三个配置必须一致

4.设置环境变量创建对应文件目录并启动

[root@mongo-master ~]# vim /etc/profile						//设置环境变量,末行添加				
export MONGO_HOME=/usr/local/mongo-3.4.10/
export PATH=$PATH:$MONGO_HOME/bin
[root@mongo-master ~]# source /etc/profile				
[root@mongo-master ~]# which mongo
/usr/local/mongo-3.4.10/bin/mongo
[root@mongo-master ~]# mkdir /data/{mongodb,logs} -pv
mkdir: 已创建目录 "/data"
mkdir: 已创建目录 "/data/mongodb"
mkdir: 已创建目录 "/data/logs"
[root@mongo-master ~]# mkdir /var/run/mongodb
[root@mongo-master ~]# mkdir /data/logs/mongodb/ -pv
mkdir: 已创建目录 "/data/logs/mongodb/"
[root@mongo-master ~]# touch /data/logs/mongodb/mongodb.log
启动mongodb
[root@mongo-master ~]# mongod -f /usr/local/mongo-3.4.10/config/mongo-master.conf 
note: noprealloc may hurt performance in many applications
about to fork child process, waiting until server is ready for connections.
forked process: 1899
child process started successfully, parent exiting

5.mongodb连接测试

[root@mongo-master ~]# mongo --host 192.168.200.101
MongoDB shell version v3.4.10
connecting to: mongodb://192.168.200.101:27017/
MongoDB server version: 3.4.10
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: 
2019-08-21T15:28:53.360+0800 I STORAGE  [initandlisten] 
2019-08-21T15:28:53.361+0800 I STORAGE  [initandlisten] ** WARNING: Readahead for /data/mongodb is set to 4096KB
2019-08-21T15:28:53.361+0800 I STORAGE  [initandlisten] **          We suggest setting it to 256KB (512 sectors) or less
2019-08-21T15:28:53.361+0800 I STORAGE  [initandlisten] **          http://dochub.mongodb.org/core/readahead
2019-08-21T15:28:53.417+0800 I CONTROL  [initandlisten] 
2019-08-21T15:28:53.417+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-08-21T15:28:53.417+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-08-21T15:28:53.417+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2019-08-21T15:28:53.417+0800 I CONTROL  [initandlisten] 
2019-08-21T15:28:53.417+0800 I CONTROL  [initandlisten] 
2019-08-21T15:28:53.417+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-08-21T15:28:53.417+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-08-21T15:28:53.417+0800 I CONTROL  [initandlisten] 
2019-08-21T15:28:53.417+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-08-21T15:28:53.417+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-08-21T15:28:53.417+0800 I CONTROL  [initandlisten] 
> show dbs
admin  0.078GB
local  0.078GB
> db.getMongo()
connection to 192.168.200.101:27017
> exit
bye
查看一下目录,确认是否按照配置文件要求
[root@mongo-master ~]# tree /data/
/data/
├── logs
│   └── mongodb
│       └── mongodb.log
└── mongodb
    ├── admin
    │   ├── admin.0
    │   ├── admin.ns
    │   └── _tmp
    ├── diagnostic.data
    │   ├── metrics.2019-08-21T07-28-54Z-00000
    │   └── metrics.interim
    ├── journal
    │   └── j._0
    ├── local
    │   ├── local.0
    │   ├── local.ns
    │   └── _tmp
    ├── mongod.lock
    └── storage.bson

9 directories, 10 files

至此master上数据库已经就创建好了,现在处理配置安装slave

7.slave&arbiter(从节点和仲裁节点配置)

[root@mongo-master ~]# scp -r /usr/local/mongo-3.4.10/ 192.168.200.102:/usr/local/
README                                                                        100% 2266     1.2MB/s   00:00    
THIRD-PARTY-NOTICES                                                           100%   54KB   2.1MB/s   00:00    
MPL-2                                                                         100%   16KB  10.5MB/s   00:00    
GNU-AGPL-3.0                                                            	  100%   34KB   5.4MB/s   00:00    
mongodump   																  100%   12MB  29.9MB/s   00:00    
mongorestore                                                                  100%   13MB  52.0MB/s   00:00    
mongoexport                                                                   100%   10MB  41.9MB/s   00:00    
mongoimport																	  100%   10MB  46.1MB/s   00:00    
mongostat                                                                 	  100%   10MB  49.0MB/s   00:00    
mongotop                                                                	  100%   10MB  50.4MB/s   00:00    
bsondump                                                             		  100%   10MB  46.3MB/s   00:00    
mongofiles 		                                                              100%   10MB  48.2MB/s   00:00    
mongooplog 	                                                                  100%   10MB  44.9MB/s   00:00    
mongoreplay                                                                   100%   13MB  47.5MB/s   00:00    
mongoperf                                                                     100%   51MB  55.8MB/s   00:00    
mongod                                                                        100%   52MB  59.3MB/s   00:00    
mongos                                                                        100%   29MB  55.1MB/s   00:00    
mongo                                                                         100%   29MB  54.4MB/s   00:00    
mongo-master.conf                                                             100%  263    32.4KB/s   00:00 

8.配置mongodb从节点及仲裁节点

slave

[root@mongo-node config]# vim mongo-slave.conf 
port=27017
bind_ip=192.168.200.102											#改IP地址
dbpath=/data/mongodb/mongo-slave/								#改数据存储路径
logpath=/data/logs/mongodb/mongodb-slave.log					#改log名称
logappend=true
fork=true
maxConns=5000
storageEngine=mmapv1
pidfilepath=/var/run/mongodb/mongo-slave.pid					#改pid名称
directoryperdb=true
noprealloc=true
oplogSize=10000
#replSet=mslinux

arbiter

[root@mongo-node config]# vim mongo-arbiter.conf 
[root@mongo-node config]# cat mongo-arbiter.conf
port=27018													#改port
bind_ip=192.168.200.102										#改ip
dbpath=/data/mongodb/mongo-arbiter/							#改数据存储位置
logpath=/data/logs/mongodb/mongodb-arbiter.log				#改日志位置
logappend=true
fork=true
maxConns=5000
storageEngine=mmapv1
pidfilepath=/var/run/mongodb/mongo-arbiter.pid				#改pid位置
directoryperdb=true
noprealloc=true
oplogSize=10000
#replSet=mslinux

9.创建对应文件路径位置&环境变量

[root@mongo-node config]# mkdir -pv /data/{mongodb,logs}
mkdir: 已创建目录 "/data"
mkdir: 已创建目录 "/data/mongodb"
mkdir: 已创建目录 "/data/logs"
[root@mongo-node config]# mkdir -pv /data/mongodb/{mongo-slave,mongo-arbiter}
mkdir: 已创建目录 "/data/mongodb/mongo-slave"
mkdir: 已创建目录 "/data/mongodb/mongo-arbiter"
[root@mongo-node logs]# mkdir /data/logs/mongodb
[root@mongo-node config]# touch /data/logs/mongodb-arbiter.log
[root@mongo-node config]# touch /data/logs/mongodb-slave.log
[root@mongo-node config]# mkdir /var/run/mongodb/
[root@mongo-node logs]# cd /data/logs/mongodb
[root@mongo-node mongodb]# touch mongodb-arbiter.log
[root@mongo-node mongodb]# touch mongodb-slave.log
[root@mongo-node ~]# vim /etc/profile
[root@mongo-node ~]# tail -2 /etc/profile
export MONGO_HOME=/usr/local/mongo-3.4.10/
export PATH=$PATH:$MONGO_HOME/bin
[root@mongo-node ~]# source /etc/profile
[root@mongo-node ~]# which mongo
/usr/local/mongo-3.4.10/bin/mongo

10.启动连接测试
启动

[root@mongo-node ~]# mongod -f /usr/local/mongo-3.4.10/config/mongo-slave.conf 
note: noprealloc may hurt performance in many applications
about to fork child process, waiting until server is ready for connections.
forked process: 2518
child process started successfully, parent exiting
[root@mongo-node ~]# mongod -f /usr/local/mongo-3.4.10/config/mongo-arbiter.conf 
note: noprealloc may hurt performance in many applications
about to fork child process, waiting until server is ready for connections.
forked process: 2535
child process started successfully, parent exiting
[root@mongo-node ~]# netstat -lnpt | grep mongo
tcp        0      0 192.168.200.102:27017   0.0.0.0:*               LISTEN      2518/mongod         
tcp        0      0 192.168.200.102:27018   0.0.0.0:*               LISTEN      2535/mongod  

连接测试
slave

[root@mongo-node ~]# mongo --host 192.168.200.102
MongoDB shell version v3.4.10
connecting to: mongodb://192.168.200.102:27017/
MongoDB server version: 3.4.10
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: 
2019-08-21T16:14:39.062+0800 I STORAGE  [initandlisten] 
2019-08-21T16:14:39.063+0800 I STORAGE  [initandlisten] ** WARNING: Readahead for /data/mongodb/mongo-slave/ is set to 4096KB
2019-08-21T16:14:39.063+0800 I STORAGE  [initandlisten] **          We suggest setting it to 256KB (512 sectors) or less
2019-08-21T16:14:39.063+0800 I STORAGE  [initandlisten] **          http://dochub.mongodb.org/core/readahead
2019-08-21T16:14:39.240+0800 I CONTROL  [initandlisten] 
2019-08-21T16:14:39.240+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-08-21T16:14:39.240+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-08-21T16:14:39.240+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2019-08-21T16:14:39.240+0800 I CONTROL  [initandlisten] 
2019-08-21T16:14:39.240+0800 I CONTROL  [initandlisten] 
2019-08-21T16:14:39.240+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-08-21T16:14:39.240+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-08-21T16:14:39.240+0800 I CONTROL  [initandlisten] 
2019-08-21T16:14:39.240+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-08-21T16:14:39.240+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-08-21T16:14:39.240+0800 I CONTROL  [initandlisten] 
> show dbs
admin  0.078GB
local  0.078GB
> db.getMongo()
connection to 192.168.200.102:27017
> exit
bye

arbiter

[root@mongo-node ~]# mongo --host 192.168.200.102 --port 27018
MongoDB shell version v3.4.10
connecting to: mongodb://192.168.200.102:27018/
MongoDB server version: 3.4.10
Server has startup warnings: 
2019-08-21T16:14:45.373+0800 I STORAGE  [initandlisten] 
2019-08-21T16:14:45.373+0800 I STORAGE  [initandlisten] ** WARNING: Readahead for /data/mongodb/mongo-arbiter/ is set to 4096KB
2019-08-21T16:14:45.373+0800 I STORAGE  [initandlisten] **          We suggest setting it to 256KB (512 sectors) or less
2019-08-21T16:14:45.373+0800 I STORAGE  [initandlisten] **          http://dochub.mongodb.org/core/readahead
2019-08-21T16:14:46.251+0800 I CONTROL  [initandlisten] 
2019-08-21T16:14:46.251+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-08-21T16:14:46.251+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-08-21T16:14:46.251+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2019-08-21T16:14:46.251+0800 I CONTROL  [initandlisten] 
2019-08-21T16:14:46.251+0800 I CONTROL  [initandlisten] 
2019-08-21T16:14:46.251+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-08-21T16:14:46.251+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-08-21T16:14:46.251+0800 I CONTROL  [initandlisten] 
2019-08-21T16:14:46.251+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-08-21T16:14:46.251+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-08-21T16:14:46.251+0800 I CONTROL  [initandlisten] 
> show dbs
admin  0.078GB
local  0.078GB
> db.getMongo()
connection to 192.168.200.102:27018
> exit
bye

11.编写服务脚本
master

[root@mongo-master ~]# vim /etc/init.d/mongo.service
[root@mongo-master ~]# chmod +x /etc/init.d/mongo.service
[root@mongo-master ~]# cat /etc/init.d/mongo.service
#!/bin/bash
#discription:The script is manager mongo service
#writer by mslinux 

case $1 in 
"start")
	/usr/local/mongo-3.4.10/bin/mongod -f /usr/local/mongo-3.4.10/config/mongo-master.conf 
;;
"stop")
	/usr/local/mongo-3.4.10/bin/mongod -f /usr/local/mongo-3.4.10/config/mongo-master.conf --shutdown
;;
"restart")
	$0 stop 
	$0 start
;;
"status")
	sts=$(netstat -lnpt | grep 27017 | wc -l )
	if [ $sts < 1 ]
	then
		echo "The service File!!!"
	else
		echo "The service Ok!!!!!"
	fi
;;
*)
	echo "Use $0 {start|stop|restart}"
esac

编写Centos7中的sys

[root@mongo-master ~]# vim /usr/lib/systemd/system/mongo.service
[root@mongo-master ~]# cat /usr/lib/systemd/system/mongo.service
[Unit]
Description="this is mongo manager service"					//服务描述
After=network.target														//表示在网络服务后启动

[Service]
Type=forking
ExecStart=/etc/init.d/mongo.service start					//启动
ExecStop=/etc/init.d/mongo.service stop						//停止
PrivateTmp=true

[Install]
WantedBy=multi-user.target									//多用户模式
[root@mongo-master ~]# systemctl daemon-reload				//重载系统服务
[root@mongo-master ~]# systemctl start mongo				//启动设置开机自启动
[root@mongo-master ~]# systemctl enable mongo			
Created symlink from /etc/systemd/system/multi-user.target.wants/mongo.service to /usr/lib/systemd/system/mongo.service.
[root@mongo-master ~]# systemctl status mongo				//查看服务状态
● mongo.service - "this is mongo manager service"
   Loaded: loaded (/usr/lib/systemd/system/mongo.service; enabled; vendor preset: disabled)
   Active: active (running) since 三 2019-08-21 16:42:42 CST; 9s ago
 Main PID: 3112 (mongod)
   CGroup: /system.slice/mongo.service
           └─3112 /usr/local/mongo-3.4.10/bin/mongod -f /usr/local/mongo-3.4.10/config/mongo-master.conf

8月 21 16:42:42 mongo-master systemd[1]: Starting "this is mongo manager service"...
8月 21 16:42:42 mongo-master mongo.service[3109]: note: noprealloc may hurt performance in many applications
8月 21 16:42:42 mongo-master mongo.service[3109]: about to fork child process, waiting until server is ready for connections.
8月 21 16:42:42 mongo-master mongo.service[3109]: forked process: 3112
8月 21 16:42:42 mongo-master mongo.service[3109]: child process started successfully, parent exiting
8月 21 16:42:42 mongo-master systemd[1]: Started "this is mongo manager service".

从服务器脚本此处就不做介绍了,按照master上进行修改就可以了,根据个人习惯进行编写即可,笔者将会使用一个systemctl start mgcluster启动两个mongo服务slave和arbiter
结果如下

[root@mongo-node ~]# systemctl start mgcluster
[root@mongo-node ~]# systemctl enable mgcluster
Created symlink from /etc/systemd/system/multi-user.target.wants/mgcluster.service to /usr/lib/systemd/system/mgcluster.service.
[root@mongo-node ~]# systemctl status mgcluster
● mgcluster.service - "this is mongo manager service"
   Loaded: loaded (/usr/lib/systemd/system/mgcluster.service; enabled; vendor preset: disabled)
   Active: active (running) since 三 2019-08-21 16:53:29 CST; 12s ago
   CGroup: /system.slice/mgcluster.service
           ├─2728 /usr/local/mongo-3.4.10/bin/mongod -f /usr/local/mongo-3.4.10/config/mongo-slave.conf
           └─2744 /usr/local/mongo-3.4.10/bin/mongod -f /usr/local/mongo-3.4.10/config/mongo-arbiter.conf

8月 21 16:53:29 mongo-node systemd[1]: Starting "this is mongo manager service"...
8月 21 16:53:29 mongo-node mongo.service[2725]: note: noprealloc may hurt performance in many applications
8月 21 16:53:29 mongo-node mongo.service[2725]: about to fork child process, waiting until server is ready for connections.
8月 21 16:53:29 mongo-node mongo.service[2725]: forked process: 2728
8月 21 16:53:29 mongo-node mongo.service[2725]: child process started successfully, parent exiting
8月 21 16:53:29 mongo-node mongo.service[2725]: note: noprealloc may hurt performance in many applications
8月 21 16:53:29 mongo-node mongo.service[2725]: about to fork child process, waiting until server is ready for connections.
8月 21 16:53:29 mongo-node mongo.service[2725]: forked process: 2744
8月 21 16:53:29 mongo-node mongo.service[2725]: child process started successfully, parent exiting
8月 21 16:53:29 mongo-node systemd[1]: Started "this is mongo manager service".

12.集群部署配置
环境

master:192.168.200.101:27017
slave: 192.168.200.102:27017
arbiter:192.168.200.102:27018

master服务器
[root@mongo-master ~]# vim /usr/local/mongo-3.4.10/config/mongo-master.conf 
replSet=mslinux									#取消注释

slave服务器
[root@mongo-node ~]# vim /usr/local/mongo-3.4.10/config/mongo-slave.conf 
replSet=mslinux									#取消注释
[root@mongo-node ~]# vim /usr/local/mongo-3.4.10/config/mongo-arbiter.conf 
replSet=mslinux									#取消注释

13.重启mongo数据库

master
[root@mongo-master ~]# systemctl restart mongo
[root@mongo-master ~]# systemctl status mongo
● mongo.service - "this is mongo manager service"
   Loaded: loaded (/usr/lib/systemd/system/mongo.service; enabled; vendor preset: disabled)
   Active: active (running) since 三 2019-08-21 16:58:44 CST; 37s ago
  Process: 3942 ExecStop=/etc/init.d/mongo.service stop (code=exited, status=0/SUCCESS)
  Process: 3948 ExecStart=/etc/init.d/mongo.service start (code=exited, status=0/SUCCESS)
 Main PID: 3951 (mongod)
   CGroup: /system.slice/mongo.service
           └─3951 /usr/local/mongo-3.4.10/bin/mongod -f /usr/local/mongo-3.4.10/config/mongo-master.conf

8月 21 16:58:44 mongo-master systemd[1]: Starting "this is mongo manager service"...
8月 21 16:58:44 mongo-master mongo.service[3948]: note: noprealloc may hurt performance in many applications
8月 21 16:58:44 mongo-master mongo.service[3948]: about to fork child process, waiting until server is ready for connections.
8月 21 16:58:44 mongo-master mongo.service[3948]: forked process: 3951
8月 21 16:58:44 mongo-master mongo.service[3948]: child process started successfully, parent exiting
8月 21 16:58:44 mongo-master systemd[1]: Started "this is mongo manager service".

slave
[root@mongo-node ~]# systemctl restart mgcluster
[root@mongo-node ~]# systemctl status mgcluster
● mgcluster.service - "this is mongo manager service"
   Loaded: loaded (/usr/lib/systemd/system/mgcluster.service; enabled; vendor preset: disabled)
   Active: active (running) since 三 2019-08-21 16:59:09 CST; 41s ago
  Process: 2798 ExecStop=/etc/init.d/mongo.service stop (code=exited, status=0/SUCCESS)
  Process: 2805 ExecStart=/etc/init.d/mongo.service start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/mgcluster.service
           ├─2808 /usr/local/mongo-3.4.10/bin/mongod -f /usr/local/mongo-3.4.10/config/mongo-slave.conf
           └─2829 /usr/local/mongo-3.4.10/bin/mongod -f /usr/local/mongo-3.4.10/config/mongo-arbiter.conf

8月 21 16:59:09 mongo-node systemd[1]: Starting "this is mongo manager service"...
8月 21 16:59:09 mongo-node mongo.service[2805]: note: noprealloc may hurt performance in many applications
8月 21 16:59:09 mongo-node mongo.service[2805]: about to fork child process, waiting until server is ready for connections.
8月 21 16:59:09 mongo-node mongo.service[2805]: forked process: 2808
8月 21 16:59:09 mongo-node mongo.service[2805]: child process started successfully, parent exiting
8月 21 16:59:09 mongo-node mongo.service[2805]: note: noprealloc may hurt performance in many applications
8月 21 16:59:09 mongo-node mongo.service[2805]: about to fork child process, waiting until server is ready for connections.
8月 21 16:59:09 mongo-node mongo.service[2805]: forked process: 2829
8月 21 16:59:09 mongo-node mongo.service[2805]: child process started successfully, parent exiting
8月 21 16:59:09 mongo-node systemd[1]: Started "this is mongo manager service".

14.设置集群
master

[root@mongo-master ~]# mongo --host 192.168.200.101
MongoDB shell version v3.4.10
connecting to: mongodb://192.168.200.101:27017/
MongoDB server version: 3.4.10
> show dbs						//报错为正常现象
2019-08-21T17:00:57.807+0800 E QUERY    [thread1] Error: listDatabases failed:{
	"ok" : 0,
	"errmsg" : "not master and slaveOk=false",
	"code" : 13435,
	"codeName" : "NotMasterNoSlaveOk"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:62:1
shellHelper.show@src/mongo/shell/utils.js:781:19
shellHelper@src/mongo/shell/utils.js:671:15
@(shellhelp2):1:1
设置集群


> use admin
switched to db admin
> cfg={_id:"mslinux",members:[{_id:0,host:'192.168.200.101:27017',priority:2},{_id:1,host:'192.168.200.102:27018',priority:1},{_id:2,host:'192.168.200.102:27019',arbiterOnly:true}]}
{
 "_id" : "mslinux",
 "members" : [
  {
   "_id" : 0,
   "host" : "192.168.0.205:27017",
   "priority" : 2
  },
  {
   "_id" : 1,
   "host" : "192.168.0.205:27018",
   "priority" : 1
  },
  {
   "_id" : 2,
   "host" : "192.168.0.205:27019",
   "arbiterOnly" : true
  }
 ]
}
> rs.initiate(cfg)
{ "ok" : 1 }


注:cfg可以使任意名字,最好不要是mongodb的关键字,conf,config都可以。最外层的_id表示replica set的名字,members里面包含的是所有节点的地址及优先级,优先级最高的成为主节点,即192.168.200.101:2701.特别注意的是,对于仲裁节点,需要有个特别的配置【'arbiterOnly':'true'】。这个必须要配置,不然主备模式不能生效
    建议cfg=后面的直接复制粘贴我的,引号等都可能引起报错!!!!

该文档只是集群部署,后面将引进故障切换后的代码调用方案。
加油,方法总比问题多!

你可能感兴趣的:(mongodb)