一直以来负责的都是MySQL的运维,最近入职了一家新公司,虽然日志量比较少但是mongodb业务很重要,之前学习过阿里云的mongodb课程,工作没有运维的机会,趁着这个机会回忆一下之前的学习在添加一些工作中遇到的问题和解决办法。方便自己成长和积累业务能力。
1.Standalone 单机部署
1.安装依赖包
yum install lrzsz libcurl openssl xz-libs wget git docker tree -y
2.下载mongodb
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.23.tgz
tar -zxvf mongodb-linux-x86_64-rhel70-4.4.23.tgz
3.建议用户组
groupadd mongodb
useradd -G mongodb mongodb
4.更改mongodb软件目录
mv /root/mongodb-linux-x86_64-rhel70-4.4.23/ /usr/local/mongodb/
5.讲执行文件软连接到/usr/bin,这样可以不用配置.bash_profile
ln -s /usr/local/mongodb/bin/* /usr/local/bin
which mongo
6.安装mongosh(可以不安装)
如果服务器不支持avx可以不安装,安装了也不能用
grep avx /proc/cpuinfo
wget https://downloads.mongodb.com/compass/mongodb-mongosh-1.10.1.x86_64.rpm
rpm -ivh mongodb-mongosh-1.10.1.x86_64.rpm
7.创建mongodb数据目录(data单独挂载)(默认端口为27017,为了不与副本集冲突所以使用27000端口)
mkdir -p /data/mongodb/27000/
chown -R mongodb.mongodb /data/mongodb/27000
8.配置mongodb参数文件
cat /data/mongodb/27000.conf
#日志相关
systemLog:
destination: file #日志输出为文件
logAppend: true #日志为追加模式,重启后将在现有日志末尾添加日志。
logRotate: rename #日志回转,重命名日志。
timeStampFormat: iso8601-local #为本地时间格式
path: /data/mongodb/27000/mongodb.log #日志存储路径
#存储引擎相关
storage:
journal: #开启journal日志并持久存储,用于故障恢复。
enabled: true
dbPath: /data/mongodb/27000/ #数据存储目录
directoryPerDB: true #将不同的数据存储在不同的目录中
wiredTiger: #存储引擎配置,wiredTiger3.2之后为默认存储引擎
engineConfig:
cacheSizeGB: 1 #最大的缓存大小,3.4后50%的ram-1GB
directoryForIndexes: true #将索引和集合分别存储在单独的目录中
collectionConfig:
blockCompressor: snappy #集合默认的压缩算法
indexConfig:
prefixCompression: false #不对索引使用前缀压缩。 这个默认值是true,默认值可以减少索引数据内存的使用量。
#进程管理相关
processManagement:
fork: true #以守护进程运行mongodb,也就是后台运行mongodb。
pidFilePath: /data/mongodb/27000/mongodb.pid #存储进程id的文件路径。
#网络相关
net:
port: 27000
9.启动与关闭
启动
mongod -f /data/mongodb/27000.conf
登入
mongo --port 27000 或者 mongosh --port 27000
关闭
mongod -f /data/mongodb/27000.conf --shutdown
2.Replication 副本集部署(启用认证)
| IP地址 | 端口 | 角色 |
|---|---|---|
| 192.168.56.105 | 27010 | Primary |
| 192.168.56.107 | 27010 | SECONDARY |
| 192.168.56.108 | 27010 | SECONDARY |
如果Standalone1-6的步骤没有执行,则需要在执行一下1-6的步骤。如果执行了就无需重复执行,从第7个步骤开始执行。如果做测试可以使用一台机器不同端口来实现。
1.创建mongodb数据目录
3个节点都需要执行,默认端口27017,为不与分片冲突使用28000端口
mkdir -p /data/mongodb/28000
chown -R mongodb.mongodb /data/mongodb/28000
2.配置mongodb参数文件
3个节点都需配置
cat /data/mongodb/28000.conf
#日志相关
systemLog:
destination: file #日志输出为文件
logAppend: true #日志为追加模式,重启后将在现有日志末尾添加日志。
logRotate: rename #日志回转,重命名日志。
timeStampFormat: iso8601-local #为本地时间格式
path: /data/mongodb/28000/mongodb.log #日志存储路径
#存储引擎相关
storage:
journal: #开启journal日志并持久存储,用于故障恢复。
enabled: true
dbPath: /data/mongodb/28000/ #数据存储目录
directoryPerDB: true #将不同的数据存储在不同的目录中
wiredTiger: #存储引擎配置,wiredTiger3.2之后为默认存储引擎
engineConfig:
cacheSizeGB: 1 #最大的缓存大小,3.4后50%的ram-1GB
directoryForIndexes: true #将索引和集合分别存储在单独的目录中
collectionConfig:
blockCompressor: snappy #集合默认的压缩算法
indexConfig:
prefixCompression: false #不对索引使用前缀压缩。 这个默认值是true,默认值可以减少索引数据内存的使用量。
#进程管理相关
processManagement:
fork: true #以守护进程运行mongodb,也就是后台运行mongodb。
pidFilePath: /data/mongodb/28000/mongodb.pid #存储进程id的文件路径。
#网络相关
net:
port: 28000
bindIp: 0.0.0.0
#副本集
replication:
oplogSizeMB: 4096 #oplog日志最大的尺寸。如果太小则全量同步
replSetName: test #副本集名称,副本集合里面mongod必须使用相同名字。测试环境使用test做演示
#安全相关,如果只是测试不想验证则去掉下面3行
security:
authorization: enabled #开启认证功能
keyFile: /data/mongodb/key.file #副本集身份验证的秘钥文件。
3.密钥文件配置
任一节点执行即可,执行完成后传送另外2个节点
openssl rand -base64 756 > /data/mongodb/key.file
cat /data/mongodb/key.file
chmod 400 /data/mongodb/key.file
chmod mongodb. /data/mongodb/key.file
将密钥文件传送给其他的服务器
scp /data/mongodb/key.file root@192.168.56.107:/data/mongodb/
scp /data/mongodb/key.file root@192.168.56.108:/data/mongodb/
4.启动mongodb与配置副本集
3个节点都需执行启动
mongod -f /data/mongodb/28000.conf
配置副本集
任一节点执行即可,这里使用105来执行
mongo --port 28000 (如果安装了mongosh --port 28000)
执行副本集初始化
rs.initiate({
_id: "test",
"members" : [
{
"_id": 0,
"host" : "192.168.56.105:28000"
},
{
"_id": 1,
"host" : "192.168.56.107:28000"
},
{
"_id": 2,
"host" : "192.168.56.108:28000"
}
]
});
等待几秒后,会变化变化为
test:PRIMARY>
查看副本集状态
test:PRIMARY> rs.status()
创建管理用户(如果副本集初始化完成但是没有创建用户就退出,则一定use admin才能创建用户)
use admin
db.createUser({user: "root", pwd: "123456", roles: [{role: "root", db: "admin" }]})
5.登录启用认证的Mongodb
参数文件中开启了认证则必须使用账户密码方式登录。不然会报错
mongo --authenticationDatabase -u root -p '123456' admin --port 28000
如果安装了mongosh
mongosh --authenticationDatabase -u root -p '123456' admin --port 28000
3.Sharding 分片集群部署(启用认证)
1.角色分工
| IP地址 | 端口 | 角色 |
|---|---|---|
| 192.168.56.105 | 27010 | Shard1-Primary |
| 192.168.56.107 | 27010 | Shard1-SECONDARY |
| 192.168.56.108 | 27010 | Shard1-SECONDARY |
| 192.168.56.105 | 27019 | Config-Primary |
| 192.168.56.107 | 27019 | Config-SECONDARY |
| 192.168.56.108 | 27019 | Config-SECONDARY |
| 192.168.56.105 | 27017 | Mongos |
| 192.168.56.107 | 27017 | Mongos |
| 192.168.56.108 | 27017 | Mongos |
2.部署前准备
3个节点都执行
1.安装依赖包
yum install lrzsz libcurl openssl xz-libs wget git docker tree -y
2.下载mongodb
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.23.tgz
tar -zxvf mongodb-linux-x86_64-rhel70-4.4.23.tgz
3.建议用户组
groupadd mongodb
useradd -G mongodb mongodb
4.更改mongodb软件目录
mv /root/mongodb-linux-x86_64-rhel70-4.4.23/ /usr/local/mongodb/
5.讲执行文件软连接到/usr/bin,这样可以不用配置.bash_profile
ln -s /usr/local/mongodb/bin/* /usr/local/bin
which mongo
6.创建目录
3个节点都使用root用户来执行
mkdir -p /data/mongodb/
chown -R mongodb. /data/mongodb/
3个节点都使用mongodb用户来执行(也可以一直使用root用户来操作)
mkdir -p /data/mongodb/{shard1,config,mongos}
cd /data/mongodb/
touch shard1.conf config.conf mongos.conf
3.shard1/config/mongos 参数配置
3个节点除了bindip为对应的ip,其余参数都一样
cat shard1.conf
systemLog:
destination: file
logAppend: true
path: /data/mongodb/shard1/mongod.log
storage:
dbPath: /data/mongodb/shard1/
journal:
enabled: true
processManagement:
fork: true # fork and run in background
pidFilePath: /data/mongodb/shard1/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
net:
port: 27010
bindIp: 127.0.0.1,192.168.56.105
security:
authorization: enabled
keyFile: /data/mongodb/key.file
replication:
replSetName: shard1
sharding:
clusterRole: shardsvr
cat config.conf
systemLog:
destination: file
logAppend: true
path: /data/mongodb/config/mongod.log
storage:
dbPath: /data/mongodb/config/
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
processManagement:
fork: true # fork and run in background
pidFilePath: /data/mongodb/config/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
net:
port: 27019
bindIp: 127.0.0.1,192.168.56.105
security:
authorization: enabled
keyFile: /data/mongodb/key.file
replication:
replSetName: config
sharding:
clusterRole: configsvr
cat mongos.conf
systemLog:
destination: file
logAppend: true
path: /data/mongodb/mongos/mongos.log
processManagement:
fork: true # fork and run in background
pidFilePath: /data/mongodb/mongos/mongos.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
net:
port: 27017
bindIp: 127.0.0.1,192.168.56.105
security:
# authorization: enabled
keyFile: /data/mongodb/key.file
sharding:
configDB: config/192.168.56.105:27019,192.168.56.105:27019,192.168.56.105:27019 #这是config集群的ip地址。注意
4.配置shard1副本集
3个节点启动shard1
mongod -f /data/mongodb/shard1.conf
登录任一节点初始化副本集
mongo --port 27010
rs.initiate({
_id: "shard1",
"members" : [
{
"_id": 0,
"host" : "192.168.56.105:27010"
},
{
"_id": 1,
"host" : "192.168.56.107:27010"
},
{
"_id": 2,
"host" : "192.168.56.108:27010"
}
]
});
等待几秒后,会变化变化为
shard1:PRIMARY>
查看副本集状态
shard1:PRIMARY> rs.status()
创建管理用户
use admin
db.createUser({user: "root", pwd: "123456", roles: [{role: "root", db: "admin" }]})
5.配置config副本集
3个节点启动config
mongod -f /data/mongodb/config.conf
登录任一节点初始化config副本集
mongo --port 27019
rs.initiate({
_id: "config",
"members" : [
{
"_id": 0,
"host" : "192.168.56.105:27019"
},
{
"_id": 1,
"host" : "192.168.56.107:27019"
},
{
"_id": 2,
"host" : "192.168.56.108:27019"
}
]
});
等待几秒后,会变化变化为
config:PRIMARY>
查看副本集状态
config:PRIMARY> rs.status()
创建管理用户
use admin
db.createUser({user: "root", pwd: "123456", roles: [{role: "root", db: "admin" }]})
6.配置mongos
3个节点启动mongos
mongos -f /data/mongodb/mongos.conf
登录192.168.56.105节点
mongo --port 27017
mongos> use admin
switched to db admin
mongos> db.auth('root','123456')
1
将shard1副本集和加入到mongos中。
mongos>sh.addShard("shard1/192.168.56.105:27010,192.168.56.107:27010,192.168.56.108:27010");
查看状态
mongos> sh.status()
添加一个分片表
sh.enableSharding("foo");
sh.shardCollection("foo.bar", {_id: 'hashed'});
sh.status();
插入随机数据
use foo
for (var i = 0; i < 10000; i++) {
db.bar.insert({i: i});
}
查看状态即可
sh.status();
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("64dbe4d31b7a336790cc3bf3")
}
shards:
{ "_id" : "shard1", "host" : "shard1/192.168.56.105:27010,192.168.56.107:27010,192.168.56.108:27010", "state" : 1 }
active mongoses:
"4.4.23" : 1
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases:
{ "_id" : "config", "primary" : "config", "partitioned" : true }
config.system.sessions
shard key: { "_id" : 1 }
unique: false
balancing: true
chunks:
shard1 1024
too many chunks to print, use verbose if you want to force print
{ "_id" : "foo", "primary" : "shard1", "partitioned" : true, "version" : { "uuid" : UUID("50f35ad1-7f6b-4757-8cfc-be85985d3349"), "lastMod" : 1 } }
foo.bar
shard key: { "_id" : "hashed" }
unique: false
balancing: true
chunks:
shard1 2
{ "_id" : { "$minKey" : 1 } } -->> { "_id" : NumberLong(0) } on : shard1 Timestamp(1, 0)
{ "_id" : NumberLong(0) } -->> { "_id" : { "$maxKey" : 1 } } on : shard1 Timestamp(1, 1)