环境准备:
确保某台机器有httpd服务,如果没有先安装
## 先yum list看看有没有你可以的包
yum list httpd
## 如果有的话,比如我用x86_64版,就可以安装
yum install httpd.x86_64
## 看一下端口 默认是80
vi /etc/httpd/conf/httpd.conf
## 启动服务
apachectl start
下载该目录下的两个文件夹文件
https://repo.mongodb.org/yum/redhat/7Server/mongodb-org/4.0/x86_64/
将下载的文件拷贝到 /var/www/html/mongodb
mkdir -p /var/www/html/mongodb
cd /var/www/html/mongodb
[root@mq-73 mongodb]# tree
.
├── repodata
│ ├── 151a9f7fa5425dae0d39afd18926d92ad009c923-other.sqlite.bz2
│ ├── 48c57d1b32996e0c19fcffc956342475c890d370-primary.sqlite.bz2
│ ├── 8bd3b3c3315d8ff8bd5c1610220db79a59b950f1-other.xml.gz
│ ├── 93027440579c723a0d0463ec3c212c753ee952c9-primary.xml.gz
│ ├── bb6f7245857edd2f0d68e21b1e6f53380d9c51c7-filelists.sqlite.bz2
│ ├── d7172c61643be7bfa3171d9be710b2be2966e0b2-filelists.xml.gz
│ ├── repomd.xml
│ └── repomd.xml.asc
└── RPMS
├── mongodb-org-4.0.0-1.el7.x86_64.rpm
├── mongodb-org-4.0.1-1.el7.x86_64.rpm
├── mongodb-org-4.0.2-1.el7.x86_64.rpm
├── mongodb-org-4.0.3-1.el7.x86_64.rpm
├── mongodb-org-mongos-4.0.0-1.el7.x86_64.rpm
├── mongodb-org-mongos-4.0.1-1.el7.x86_64.rpm
├── mongodb-org-mongos-4.0.2-1.el7.x86_64.rpm
├── mongodb-org-mongos-4.0.3-1.el7.x86_64.rpm
├── mongodb-org-server-4.0.0-1.el7.x86_64.rpm
├── mongodb-org-server-4.0.1-1.el7.x86_64.rpm
├── mongodb-org-server-4.0.2-1.el7.x86_64.rpm
├── mongodb-org-server-4.0.3-1.el7.x86_64.rpm
├── mongodb-org-shell-4.0.0-1.el7.x86_64.rpm
├── mongodb-org-shell-4.0.1-1.el7.x86_64.rpm
├── mongodb-org-shell-4.0.2-1.el7.x86_64.rpm
├── mongodb-org-shell-4.0.3-1.el7.x86_64.rpm
├── mongodb-org-tools-4.0.0-1.el7.x86_64.rpm
├── mongodb-org-tools-4.0.1-1.el7.x86_64.rpm
├── mongodb-org-tools-4.0.2-1.el7.x86_64.rpm
└── mongodb-org-tools-4.0.3-1.el7.x86_64.rpm
2 directories, 28 files
下面开始切换到准备运行mongodb的服务器上
配置repo(注意需要删除其他的repo安装源,不然会提示连接服务器失败,/etc/yum.repos.d
)
添加源配置 下面那个IP是上面服务器IP
vim /etc/yum.repos.d/mongodb-org-4.0.repo
添加如下内容:
[mongodb-org-4.0]
name=MongoDB Repository
baseurl=http://192.168.1.20/mongodb/
gpgcheck=0
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
安装mongodb,推荐使用4.0.3
yum install -y mongodb-org-4.0.1 mongodb-org-server-4.0.1 mongodb-org-shell-4.0.1 mongodb-org-mongos-4.0.1 mongodb-org-tools-4.0.1
4.0.2
yum install -y mongodb-org-4.0.2 mongodb-org-server-4.0.2 mongodb-org-shell-4.0.2 mongodb-org-mongos-4.0.2 mongodb-org-tools-4.0.2
4.0.3
yum install -y mongodb-org-4.0.3 mongodb-org-server-4.0.3 mongodb-org-shell-4.0.3 mongodb-org-mongos-4.0.3 mongodb-org-tools-4.0.3
服务器以及服务信息
服务器 Ip 服务及端口
CentosA 192.168.1.3 mongod shard1_1:27017
mongod shard2_1:27018
mongod config:20000
Mongs:30000
CentosB 192.168.1.4 mongod shard1_2:7017
mongod shard2_2:7018
mongod config:20000
Mongs:30000
centosC 192.168.1.5 mongod shard1_3:27017
mongod shard2_3:27018
mongod config:20000
Mongs:30000
建立数据和日志的目录
centosA:
mkdir -p /data1/mongodb/shard1_1
mkdir -p /data1/mongodb/shard2_1
mkdir -p /data1/mongodb/config
centosB:
mkdir -p /data1/mongodb/shard1_2
mkdir -p /data1/mongodb/shard2_2
mkdir -p /data1/mongodb/config
centosC:
mkdir -p /data1/mongodb/shard1_3
mkdir -p /data1/mongodb/shard2_3
mkdir -p /data1/mongodb/config
启动分片mongod服务
centosA:
service mongod stop
mongod --shardsvr --replSet shard1 --port 27017 --dbpath /data1/mongodb/shard1_1 --logpath /data1/mongodb/shard1_1/shard1_1.log --logappend --fork --bind_ip_all
mongod --shardsvr --replSet shard2 --port 27018 --dbpath /data1/mongodb/shard2_1 --logpath /data1/mongodb/shard2_1/shard2_1.log --logappend --fork --bind_ip_all
centosB:
service mongod stop
mongod --shardsvr --replSet shard1 --port 27017 --dbpath /data1/mongodb/shard1_2 --logpath /data1/mongodb/shard1_2/shard1_2.log --logappend --fork --bind_ip_all
mongod --shardsvr --replSet shard2 --port 27018 --dbpath /data1/mongodb/shard2_2 --logpath /data1/mongodb/shard2_2/shard2_2.log --logappend --fork --bind_ip_all
centosC:
service mongod stop
mongod --shardsvr --replSet shard1 --port 27017 --dbpath /data1/mongodb/shard1_3 --logpath /data1/mongodb/shard1_3/shard1_3.log --logappend --fork --bind_ip_all
mongod --shardsvr --replSet shard2 --port 27018 --dbpath /data1/mongodb/shard2_3 --logpath /data1/mongodb/shard2_3/shard2_3.log --logappend --fork --bind_ip_all
任意连接一台centos配置分片和数据集
//注意这里的shard1要与上面 启动节点配置文件中 --replSet shard1名称一致
mongo --port 27017
config={_id:'shard1',members:[{_id:0,host:'192.168.1.3:27017'},{_id:1,host:'192.168.1.4:27017'},{_id:2,host:'192.168.1.5:27017'}]}
rs.initiate(config)
//注意 这里是第二个分片,别和上面一起执行了
mongo --port 27018
config={_id:'shard2',members:[{_id:0,host:'192.168.1.3:27018'},{_id:1,host:'192.168.1.4:27018'},{_id:2,host:'192.168.1.5:27018'}]}
rs.initiate(config)
config server配置服务器
mongodb3.4以后要求配置服务器也创建副本集,不然集群搭建不成功。
(三台机器)添加配置文件
mkdir -p /data1/mongodb/conf/
cd /data1/mongodb/conf/
mkdir log
mkdir data
vi config.conf
## 配置文件内容
pidfilepath = /data1/mongodb/conf/log/configsrv.pid
dbpath = /data1/mongodb/conf/data
logpath = /data1/mongodb/conf/log/congigsrv.log
logappend = true
bind_ip = 0.0.0.0
port = 20000
fork = true
#declare this is a config db of a cluster;
configsvr = true
#副本集名称
replSet = configs
#设置最大连接数
maxConns = 20000
启动三台服务器的config server
mongod -f /data1/mongodb/conf/config.conf
登录任意一台配置服务器,初始化配置副本集 注意 其中,”_id” : “configs”应与配置文件中配置的 replicaction.replSetName 一致,”members” 中的 “host” 为三个节点的 ip 和 port , 注意这里连接的端口是config的
连接 MongoDB
mongo --port 20000
config = {
_id : "configs",
configsvr: true,
members : [
{_id : 0, host : "192.168.1.3:20000" },
{_id : 1, host : "192.168.1.4:20000" },
{_id : 2, host : "192.168.1.5:20000" }
]
}
rs.initiate(config)
常用停止命令
#如果非要用kill命令,别用 -9 避免数据丢失
#查看相关 进程
ps -ef|grep mongodb
#停止配置服务
mongod -f /data1/mongodb/conf/config.conf --shutdown
#关闭分片
mongo --port 27017
use admin
db.shutdownServer();
#关闭分片
mongo --port 27018
use admin
db.shutdownServer();