MongoDB 2019(1)Install 4.2.1 Single and Cluster
程序员文章站
2022-03-30 19:23:40
...
MongoDB 2019(1)
Follow this https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat-tarball/
Prepare the package for mongodb
> sudo yum install libcurl openssl
Select Version 4.2.1 current release version, OS RHEL 7.0 Linux 64-bit x64 for my CentOS7, Package TGZ
> wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.1.tgz
> tar -zxvf mongodb-linux-x86_64-rhel70-4.2.1.tgz
> mv mongodb-linux-x86_64-rhel70-4.2.1 ~/tool/mongodb-4.2.1
Prepare the directory and add to PATH
> sudo ln -s /home/carl/tool/mongodb-4.2.1 /opt/mongodb-4.2.1
> sudo ln -s /opt/mongodb-4.2.1 /opt/mongodb
> vi ~/.bash_profile
PATH=$PATH:/opt/mongodb/bin
> . ~/.bash_profile
Check version
> mongod -version
db version v4.2.1
git version: edf6d45851c0b9ee15548f0f847df141764a317e
OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
allocator: tcmalloc
modules: none
build environment:
distmod: rhel70
distarch: x86_64
target_arch: x86_64
Follow the document, it should use
/var/lib/mongo —> Data Directory
/var/log/mongodb —> Log Directory
> sudo mkdir -p /var/lib/mongo
> sudo mkdir -p /var/log/mongodb
Add User mongod
> sudo adduser mongod
Set Up password
> sudo passwd mongod
> sudo usermod -aG wheel mongod
Set Up the permission for the directories
> sudo chown -R mongod:mongod /var/lib/mongo
> sudo chown -R mongod:mongod /var/log/mongodb
Try to start it
> cd /opt/mongodb
> mkdir conf
> cat conf/mongodb.conf
bind_ip=0.0.0.0
port=27017
It failed to start with command
> mongod -f conf/mongodb.conf
Data directory /data/db not found
Check the Configuration and prepare the directories
> cat conf/mongodb.conf
fork=true
bind_ip=0.0.0.0
port=27017
quiet=true
dbpath=/data/db/mongodb
logpath=/var/log/mongodb/mongod.log
logappend=true
journal=true
> sudo mkdir -p /data/db/mongodb
> sudo mkdir -p /var/log/mongodb
> sudo chown -R mongod:mongod /data/db/mongodb
> sudo chown -R mongod:mongod /var/log/mongodb
> sudo touch /var/log/mongodb/mongod.log
> sudo chown -R mongod:mongod /var/log/mongodb/mongod.log
I am using user carl, so change all these command to user carl. It works.
> mongod -f conf/mongodb.conf
Check the process
> ps -ef | grep mongo
Try to connect
> mongo -host 127.0.0.1 -port 27017
Some warnings, I may deal with them later
2019-11-10T10:07:27.754-0600 I CONTROL [initandlisten]
2019-11-10T10:07:27.754-0600 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-11-10T10:07:27.754-0600 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2019-11-10T10:07:27.754-0600 I CONTROL [initandlisten]
2019-11-10T10:07:27.755-0600 I CONTROL [initandlisten]
2019-11-10T10:07:27.755-0600 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-11-10T10:07:27.755-0600 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-11-10T10:07:27.755-0600 I CONTROL [initandlisten]
2019-11-10T10:07:27.755-0600 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-11-10T10:07:27.755-0600 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-11-10T10:07:27.755-0600 I CONTROL [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
Test the current things
> db.test.save({a:1,b:2})
WriteResult({ "nInserted" : 1 })
> db.test.find()
{ "_id" : ObjectId("5dc836add99ae664c7d5bf69"), "a" : 1, "b" : 2 }
> exit
bye
Let me try to do another new installation on rancher-master
> sudo yum install libcurl openssl
> wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.1.tgz
> tar zxvf mongodb-linux-x86_64-rhel70-4.2.1.tgz
> mv mongodb-linux-x86_64-rhel70-4.2.1 ~/tool/mongodb-4.2.1
Prepare the directory and add to PATH
> sudo ln -s /home/carl/tool/mongodb-4.2.1 /opt/mongodb-4.2.1
> sudo ln -s /opt/mongodb-4.2.1 /opt/mongodb
> vi ~/.bash_profile
PATH=$PATH:/opt/mongodb/bin
> . ~/.bash_profile
Check version
> mongod -version
db version v4.2.1
git version: edf6d45851c0b9ee15548f0f847df141764a317e
OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
allocator: tcmalloc
modules: none
build environment:
distmod: rhel70
distarch: x86_64
target_arch: x86_64
> sudo mkdir -p /data/db/mongodb
> sudo mkdir -p /var/log/mongodb
> sudo chown -R carl:carl /data/db/mongodb
> sudo chown -R carl:carl /var/log/mongodb
> cd /opt/mongodb
> mkdir conf
> cat conf/mongodb.conf
fork=true
bind_ip=0.0.0.0
port=27017
quiet=true
dbpath=/data/db/mongodb
logpath=/var/log/mongodb/mongod.log
logappend=true
journal=true
Start the Service
> mongod -f conf/mongodb.conf
Try to connect
> mongo -host 127.0.0.1 -port 27017
Test the current things
> db.test.save({a:1,b:2})
WriteResult({ "nInserted" : 1 })
> db.test.find()
{ "_id" : ObjectId("5dc836add99ae664c7d5bf69"), "a" : 1, "b" : 2 }
> exit
bye
Do the smilier installation on rancher-worker2
Set Up Replication on 3 Nodes
On all the services
> vi conf/mongodb.conf
replSet=sillycat
Start all the services on 3 nodes
> mongod -f conf/mongodb.conf
> mongod -f conf/mongodb.conf
> mongod -f conf/mongodb.conf
On the master machine, rancher-home, do this as follow:
> mongo -host rancher-home -port 27017
Check status
> rs.status()
> rs.in
> rs.initiate()
sillycat:PRIMARY> rs.conf()
{
"_id" : "sillycat",
"version" : 1,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 0,
"host" : "rancher-home:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : -1,
"catchUpTakeoverDelayMillis" : 30000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("5dc8788113bce2f21790db39")
}
}
Add the worker1 to the Cluster
sillycat:PRIMARY> rs.add("rancher-worker1:27017")
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1573419389, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1573419389, 1)
}
Add the Second Worker to the Cluster
sillycat:PRIMARY> rs.add("rancher-worker2:27017")
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1573419409, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1573419409, 1)
}
We can check the status, I think it runs well.
On the primary
sillycat:PRIMARY> db.users.insert({name:"Carl", age:31})
sillycat:PRIMARY> db.users.find();
{ "_id" : ObjectId("5dc87a3b872719ffb1b50d26"), "name" : "Carl", "age" : 31 }
On the worker machine
> mongo -host rancher-worker1 -port 27017
sillycat:SECONDARY> rs.slaveOk();
sillycat:SECONDARY> db.users.find();
{ "_id" : ObjectId("5dc87a3b872719ffb1b50d26"), "name" : "Carl", "age" : 31 }
We can do some proxy on top of that
2. Configure the HA Proxy in front of Mongo Servers
My mongo servers are running as follow:
rancher-home 27017
rancher-worker1 27017
rancher-worker2 27017
The configuration should be as follow:
listen mongodb_local_cluster 127.0.0.1:27010
#配置TCP模式
mode tcp
#balance url_param userid
#balance url_param session_id check_post 64
#balance hdr(User-Agent)
#balance hdr(host)
#balance hdr(Host) use_domain_only
#balance rdp-cookie
#balance leastconn
#balance source //ip
#简单的轮询
balance roundrobin
#集群节点配置
server mongo1 127.0.0.1:27017 check inter 5000 rise 2 fall 2
server mongo2 127.0.0.1:27018 check inter 5000 rise 2 fall 2
server mongo3 127.0.0.1:27019 check inter 5000 rise 2 fall 2
References:
https://juejin.im/post/5d525b1af265da03b31bc2d5
https://learnku.com/articles/31096
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat-tarball/
https://zeppelin.apache.org/docs/0.8.2/setup/storage/storage.html
Follow this https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat-tarball/
Prepare the package for mongodb
> sudo yum install libcurl openssl
Select Version 4.2.1 current release version, OS RHEL 7.0 Linux 64-bit x64 for my CentOS7, Package TGZ
> wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.1.tgz
> tar -zxvf mongodb-linux-x86_64-rhel70-4.2.1.tgz
> mv mongodb-linux-x86_64-rhel70-4.2.1 ~/tool/mongodb-4.2.1
Prepare the directory and add to PATH
> sudo ln -s /home/carl/tool/mongodb-4.2.1 /opt/mongodb-4.2.1
> sudo ln -s /opt/mongodb-4.2.1 /opt/mongodb
> vi ~/.bash_profile
PATH=$PATH:/opt/mongodb/bin
> . ~/.bash_profile
Check version
> mongod -version
db version v4.2.1
git version: edf6d45851c0b9ee15548f0f847df141764a317e
OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
allocator: tcmalloc
modules: none
build environment:
distmod: rhel70
distarch: x86_64
target_arch: x86_64
Follow the document, it should use
/var/lib/mongo —> Data Directory
/var/log/mongodb —> Log Directory
> sudo mkdir -p /var/lib/mongo
> sudo mkdir -p /var/log/mongodb
Add User mongod
> sudo adduser mongod
Set Up password
> sudo passwd mongod
> sudo usermod -aG wheel mongod
Set Up the permission for the directories
> sudo chown -R mongod:mongod /var/lib/mongo
> sudo chown -R mongod:mongod /var/log/mongodb
Try to start it
> cd /opt/mongodb
> mkdir conf
> cat conf/mongodb.conf
bind_ip=0.0.0.0
port=27017
It failed to start with command
> mongod -f conf/mongodb.conf
Data directory /data/db not found
Check the Configuration and prepare the directories
> cat conf/mongodb.conf
fork=true
bind_ip=0.0.0.0
port=27017
quiet=true
dbpath=/data/db/mongodb
logpath=/var/log/mongodb/mongod.log
logappend=true
journal=true
> sudo mkdir -p /data/db/mongodb
> sudo mkdir -p /var/log/mongodb
> sudo chown -R mongod:mongod /data/db/mongodb
> sudo chown -R mongod:mongod /var/log/mongodb
> sudo touch /var/log/mongodb/mongod.log
> sudo chown -R mongod:mongod /var/log/mongodb/mongod.log
I am using user carl, so change all these command to user carl. It works.
> mongod -f conf/mongodb.conf
Check the process
> ps -ef | grep mongo
Try to connect
> mongo -host 127.0.0.1 -port 27017
Some warnings, I may deal with them later
2019-11-10T10:07:27.754-0600 I CONTROL [initandlisten]
2019-11-10T10:07:27.754-0600 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-11-10T10:07:27.754-0600 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2019-11-10T10:07:27.754-0600 I CONTROL [initandlisten]
2019-11-10T10:07:27.755-0600 I CONTROL [initandlisten]
2019-11-10T10:07:27.755-0600 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-11-10T10:07:27.755-0600 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-11-10T10:07:27.755-0600 I CONTROL [initandlisten]
2019-11-10T10:07:27.755-0600 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-11-10T10:07:27.755-0600 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-11-10T10:07:27.755-0600 I CONTROL [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
Test the current things
> db.test.save({a:1,b:2})
WriteResult({ "nInserted" : 1 })
> db.test.find()
{ "_id" : ObjectId("5dc836add99ae664c7d5bf69"), "a" : 1, "b" : 2 }
> exit
bye
Let me try to do another new installation on rancher-master
> sudo yum install libcurl openssl
> wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.1.tgz
> tar zxvf mongodb-linux-x86_64-rhel70-4.2.1.tgz
> mv mongodb-linux-x86_64-rhel70-4.2.1 ~/tool/mongodb-4.2.1
Prepare the directory and add to PATH
> sudo ln -s /home/carl/tool/mongodb-4.2.1 /opt/mongodb-4.2.1
> sudo ln -s /opt/mongodb-4.2.1 /opt/mongodb
> vi ~/.bash_profile
PATH=$PATH:/opt/mongodb/bin
> . ~/.bash_profile
Check version
> mongod -version
db version v4.2.1
git version: edf6d45851c0b9ee15548f0f847df141764a317e
OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
allocator: tcmalloc
modules: none
build environment:
distmod: rhel70
distarch: x86_64
target_arch: x86_64
> sudo mkdir -p /data/db/mongodb
> sudo mkdir -p /var/log/mongodb
> sudo chown -R carl:carl /data/db/mongodb
> sudo chown -R carl:carl /var/log/mongodb
> cd /opt/mongodb
> mkdir conf
> cat conf/mongodb.conf
fork=true
bind_ip=0.0.0.0
port=27017
quiet=true
dbpath=/data/db/mongodb
logpath=/var/log/mongodb/mongod.log
logappend=true
journal=true
Start the Service
> mongod -f conf/mongodb.conf
Try to connect
> mongo -host 127.0.0.1 -port 27017
Test the current things
> db.test.save({a:1,b:2})
WriteResult({ "nInserted" : 1 })
> db.test.find()
{ "_id" : ObjectId("5dc836add99ae664c7d5bf69"), "a" : 1, "b" : 2 }
> exit
bye
Do the smilier installation on rancher-worker2
Set Up Replication on 3 Nodes
On all the services
> vi conf/mongodb.conf
replSet=sillycat
Start all the services on 3 nodes
> mongod -f conf/mongodb.conf
> mongod -f conf/mongodb.conf
> mongod -f conf/mongodb.conf
On the master machine, rancher-home, do this as follow:
> mongo -host rancher-home -port 27017
Check status
> rs.status()
> rs.in
> rs.initiate()
sillycat:PRIMARY> rs.conf()
{
"_id" : "sillycat",
"version" : 1,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 0,
"host" : "rancher-home:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : -1,
"catchUpTakeoverDelayMillis" : 30000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("5dc8788113bce2f21790db39")
}
}
Add the worker1 to the Cluster
sillycat:PRIMARY> rs.add("rancher-worker1:27017")
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1573419389, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1573419389, 1)
}
Add the Second Worker to the Cluster
sillycat:PRIMARY> rs.add("rancher-worker2:27017")
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1573419409, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1573419409, 1)
}
We can check the status, I think it runs well.
On the primary
sillycat:PRIMARY> db.users.insert({name:"Carl", age:31})
sillycat:PRIMARY> db.users.find();
{ "_id" : ObjectId("5dc87a3b872719ffb1b50d26"), "name" : "Carl", "age" : 31 }
On the worker machine
> mongo -host rancher-worker1 -port 27017
sillycat:SECONDARY> rs.slaveOk();
sillycat:SECONDARY> db.users.find();
{ "_id" : ObjectId("5dc87a3b872719ffb1b50d26"), "name" : "Carl", "age" : 31 }
We can do some proxy on top of that
2. Configure the HA Proxy in front of Mongo Servers
My mongo servers are running as follow:
rancher-home 27017
rancher-worker1 27017
rancher-worker2 27017
The configuration should be as follow:
listen mongodb_local_cluster 127.0.0.1:27010
#配置TCP模式
mode tcp
#balance url_param userid
#balance url_param session_id check_post 64
#balance hdr(User-Agent)
#balance hdr(host)
#balance hdr(Host) use_domain_only
#balance rdp-cookie
#balance leastconn
#balance source //ip
#简单的轮询
balance roundrobin
#集群节点配置
server mongo1 127.0.0.1:27017 check inter 5000 rise 2 fall 2
server mongo2 127.0.0.1:27018 check inter 5000 rise 2 fall 2
server mongo3 127.0.0.1:27019 check inter 5000 rise 2 fall 2
References:
https://juejin.im/post/5d525b1af265da03b31bc2d5
https://learnku.com/articles/31096
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat-tarball/
https://zeppelin.apache.org/docs/0.8.2/setup/storage/storage.html