Skip to content

Instantly share code, notes, and snippets.

@phainamikaze
Last active October 30, 2020 09:29
Show Gist options
  • Select an option

  • Save phainamikaze/d32cba68aeb9992b5a96376c03496367 to your computer and use it in GitHub Desktop.

Select an option

Save phainamikaze/d32cba68aeb9992b5a96376c03496367 to your computer and use it in GitHub Desktop.
install-mongodb-on-ubuntu
wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -
#wget -qO - https://www.mongodb.org/static/pgp/server-4.0.asc | sudo apt-key add -
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list
#echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
sudo apt-get update
sudo apt-get install -y mongodb-org=4.2.3 mongodb-org-server=4.2.3 mongodb-org-shell=4.2.3 mongodb-org-mongos=4.2.3 mongodb-org-tools=4.2.3
#sudo apt-get install -y mongodb-org=4.0.20 mongodb-org-server=4.0.20 mongodb-org-shell=4.0.20 mongodb-org-mongos=4.0.20 mongodb-org-tools=4.0.20
sudo systemctl start mongod
sudo systemctl enable mongod
mongo
use admin
db.createUser(
{
user: "root",
pwd: "Passw0rd",
roles: [ { role: "root", db: "admin" } ]
}
)
#add below script /etc/mongod.comf
security:
authorization: 'enabled'
net:
port: 27017
bindIp: 0.0.0.0
openssl rand -base64 756 > <path-to-keyfile>
chmod 400 <path-to-keyfile>
edit /etc/mongodb.conf
security:
keyFile: <path-to-keyfile>
replication:
replSetName: <replicaSetName>
connect to mongo local and run
rs.initiate(
{
_id : <replicaSetName>,
members: [
{ _id : 0, host : "mongo1.example.net:27017" },
{ _id : 1, host : "mongo2.example.net:27017" },
{ _id : 2, host : "mongo3.example.net:27017" }
]
}
)
Use rs.status() to locate the primary member.
use admin
db.createUser(
{
user: "admin",
pwd: "admin",
roles: [ { "role" : "root", "db" : "admin" } ]
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment