Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/anteqkois/public-mongo-container
Selfhosted mongo replica-set
https://github.com/anteqkois/public-mongo-container
database mongo mongo-cluster mongo-selfhosted mongodb replica-set
Last synced: 25 days ago
JSON representation
Selfhosted mongo replica-set
- Host: GitHub
- URL: https://github.com/anteqkois/public-mongo-container
- Owner: anteqkois
- Created: 2023-10-31T16:01:58.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2024-12-10T00:27:34.000Z (about 1 month ago)
- Last Synced: 2024-12-10T01:21:01.708Z (about 1 month ago)
- Topics: database, mongo, mongo-cluster, mongo-selfhosted, mongodb, replica-set
- Language: TypeScript
- Homepage:
- Size: 576 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Commands to create local mongo replica set (in PROD databases should be placed on different servers)
## Run with MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOT_PASSWORD
or add admin when database is running without authentication mode on
```
use admindb.createUser({
user: "admin",
pwd: "password",
roles:["root"]
})
```## Connect to main db container
```
docker exec -it mongodb bash
```## Login as a admin
```
mongo --host mongodb:27017 -u admin -p --authenticationDatabase admin
```## Check replica set status
`rs.status()`
`rs.conf()`## Setup replica set config (add secondary database)
```
config = {
"_id" : "rsMain",
"members" : [
{
"_id" : 0,
"host" : "mongodb:27017"
},
{
"_id" : 1,
"host" : "mongodb2:27017"
},
]
};
```## Initialize cluster
```
rs.initiate(config);
```How to connect:
You must use `directConnection=true` connection option (only for test purpose)
```
mongodb://user:password@mongodb:27017/on_chain_data?replicaSet=rs0&authSource=on_chain_data&directConnection=true
```Properly connection string
```
mongodb://user:password@:27017/on_chain_data?replicaSet=rs0&authSource=on_chain_data
```### Additional warnings
When i try to initialize databse using docker compose, the `.js` scirpt isn't run. No solution found.
So i throw away idea to use
```
volumes:
- ./docker-entrypoint-replicaset-onedb:/docker-entrypoint-initdb.d
```and back to use simple env veriables and then manually enter to docker container and run the scripts to initialize replica set etc.
# Helpfull docker compose config from other service
```
mongodb:
image: mongo:5.0.5
container_name: mongodb
# environment:
# MONGO_INITDB_ROOT_PASSWORD:
# MONGO_INITDB_ROOT_USERNAME: admin
ports:
- 27017:27017
entrypoint:
- bash
- -c
- |
chmod 400 /etc/mongo/authKey
chown 999:999 /etc/mongo/authKey
exec docker-entrypoint.sh "$@"
command: ["mongod", "--config", "/etc/mongo/mongod.conf", "--replSet", "rs0"]
logging:
driver: "json-file"
options:
max-size: 100m
max-file: "3"
volumes:
- ./raw_db:/data/db
- ./mongo_conf:/etc/mongo
restart: unless-stopped
```