1. Install in Yum mode
1. Add a Yum source
Create a / etc / yum.com file repos. d/mongodb-org-5.0. Repo file
[mongodb-org-5.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/5.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-5.0.asc
2. Install mongodb package
sudo yum install -y mongodb-org
Default configuration file: / etc / mongod conf
Default data file directory: / var / lib / Mongo
Default log file directory: / var / log / mongodb
3. You can customize the configuration file and change some settings
For example, specify a new data storage directory (for example: / some / data / directory) or specify a new log file path (for example: / some / log / directory / mongod. Log)
Finally, make sure that the user running mongodb has access to these directories. Suppose we create a new user named Zhangsan, and we intend to run mongodb with Zhangsan, then we must ensure that Zhangsan can access / some / data / directory and / some / log / directory / mongod log
Thus, this can be done
sudo chown -R zhangsan:zhangsan mongod --config /etc/mongod.conf
PS: if you change the user running the mongodb process, you must grant the new user access to these directories.
4. Start mongodb
sudo systemctl start mongod
5. View mongodb running status
sudo systemctl status mongod
6. Stop mongodb
sudo systemctl stop mongod
7. Restart mongodb
sudo systemctl restart mongod
8. Using mongodb
mongosh
Start a mongosh session on the same host as mongod. You can run mongosh without any parameters. In this way, you will connect mongod running on the local localhost. The default port is 27017.
9、help
10. Uninstall mongodb
#Stop service
sudo systemctl stop mongod
#Remove installation package
sudo yum erase $(rpm -qa | grep mongodb-org)
#Delete database and log files
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongo
11. Quick start
Mongodb stores documents in collections. A collection is similar to a table in a relational database. If the collection does not exist, mongodb creates the collection when the data of the collection is first stored.
12. Package name and description
- Mongodb org database: it is a metapackage that will automatically install the following component packages
- Mongodb org server: contains mongod daemon, related initialization scripts and configuration files (/ etc / mongod. CONF)
- Mongodb ORG mongos: contains the mongos daemon
- Mongodb org shell: contains the Mongo shell left over from history
- Mongodb mongosh: including mongodb shell (mongosh)
- Mongodb org tools: it is a metapackage, which will automatically install the following component packages
- Mongodb database tools: includes the following mongodb database tools (mongodump, mongorestore, bsondump, mongoimport, mongoexport, mongostat, mongotop, and mongofiles)
- Mongodb org database tools extra: includes install_ Compass script
13. Turn off firewall
systemctl stop|start|status firewalld
2. Docker installation
Download Image
docker pull mongo
Specifies the version run container
1. Start a Mongo server instance
docker run --name some-mongo -d mongo:tag
2. Connect to mongodb from another docker container
The following example starts another mongodb container instance and runs the Mongo command line client against the original mongodb container in the above example, allowing you to execute mongodb statements on the database instance:
docker run -it --network some-network --rm mongo mongo --host some-mongo test
(PS: to explain here, this is equivalent to remotely connecting to container a in container B, and the local Mongo client is equivalent to connecting to the remote Mongo server. Since Mongo now runs in the docker container, we can understand the container running Mongo server as a machine A. in order to connect to a in another machine B, at least Mo needs to be installed on B NGO shell, which is Mongo client connecting to remote Mongo server)
3. The container shell accesses and views mongodb logs
The docker exec command allows you to run commands in the docker container. The following command line will provide you with the bash shell inside the Mongo container:
docker exec -it some-mongo bash
Mongodb server logs can be obtained from docker’s container logs:
docker logs some-mongo
4. Help
Check the help to know which parameters the Mongo container can take when it runs
docker run -it --rm mongo --help
5. Set wiredtiger cache size limit
docker run --name some-mongo -d mongo --wiredTigerCacheSizeGB 1.5
6. Use a custom mongodb configuration file
Mongod does not read the configuration file by default, so you need to specify the path of the configuration file through the — config option. Create a custom configuration file by creating a custom dockerfile from Mongo or mounting it from the host to the container and put it into the container.
For example, suppose / my / custom / mongod Conf is a user-defined configuration file. When starting the mongodb container, you can do the following:
docker run --name some-mongo -v /my/custom:/etc/mongo -d mongo --config /etc/mongo/mongod.conf
The default configuration file (/ etc / mongod. CONF) is as follows:
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# Where and how to store data.
storage:
dbPath: /var/lib/mongo
journal:
enabled: true
# engine:
# wiredTiger:
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
#security:
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options
#auditLog:
#snmp:
7. Environmental variables
When you start Mongo image, you can adjust the initialization of mongodb instance by passing one or more environment variables in the docker run command line.
MONGO_INITDB_ROOT_USERNAME, MONGO_INITDB_ROOT_PASSWORDThese two variables are used together to create a new user and set the user’s password. The user is created in the administrator authentication database and assigned the root role, which is a “superuser” role.
The following is an example. Use these two variables to create a mongodb instance, and then use Mongo cli to connect to the admin authentication database.
First, start the container and create a mongodb instance
docker run -d --network some-network --name some-mongo \
-e MONGO_INITDB_ROOT_USERNAME=mongoadmin \
-e MONGO_INITDB_ROOT_PASSWORD=secret \
mongo
Connect to the Mongo server instance container created earlier
docker run -it --rm --network some-network mongo \
mongo --host some-mongo \
-u mongoadmin \
-p secret \
--authenticationDatabase admin \
some-db
(PS: Mongo in the command line is the name of the image, and mongod is the main daemon of the mongodb system)
Both variables are required to create a user. If both exist, mongodb will enable authentication (mongod — auth)
Authentication in mongodb is quite complex, so more complex user settings are set through / docker entrypoint initdb D / leave it to the user to set it explicitly
MONGO_INITDB_DATABASE This variable allows you to specify / docker entrypoint initdb d/*. The database name of the script created in JS
The most basic principle of mongodb design is “create on first use”
As an alternative to passing sensitive information through environmental variables_ File can be attached to the environment variables listed earlier, causing the initialization script to load the values of these variables from the files that exist in the container. In particular, this can be used from stored in / run / secrets/Load the password in the docker key store in the file. Currently, only Mongo is supported_ INITDB_ ROOT_ Username and Mongo_ INITDB_ ROOT_ PASSWORD 。
docker run --name some-mongo -e MONGO_INITDB_ROOT_PASSWORD_FILE=/run/secrets/mongo-root -d mongo
8. Authentication
The authentication in mongodb is quite complex. See:
Security > Role-Based Access Control
Security > Role-Based Access Control > Built-In Roles
Security > Enable Auth (tutorial)
9. Where is the data stored
There are two ways to store data used by applications running in the docker container.
Method 1: let docker use its own internal volume management to write database files to the disk on the host system, so as to manage the storage of database data. This is the default setting, which is very simple and transparent to users. The disadvantage is that it may be difficult for tools and Applications (i.e. external containers) running directly on the host system to find these files.
Method 2: create a data directory on the host system (outside the container) and mount it to the visible directory inside the container. This places the database files in a known location on the host system and makes them easily accessible to tools and applications on the host system. The disadvantage is that the user needs to ensure that the directory exists. For example, the directory permissions and other security mechanisms on the host system are set correctly.
(PS: To sum up, there are two methods, one is to put it inside the container and the other is to put it outside the container)
The default behavior of being managed by docker is not demonstrated here. The following is a demonstration of how to mount directories outside the container to the container:
First, create a directory on the host, such as / my / own / dataDir
Then, mount the directory with the – V option when starting the container
docker run --name some-mongo -v /my/own/datadir:/data/db -d mongo
In this command, – V / my / own / dataDir: / data / DB mounts the / my / own / dataDir directory on the host system to / data / DB of the container. When mongodb is installed in the docker container, by default, mongodb will store its data in the/data/dbIn the directory.
memorandum
docker network create my-network
docker network ls
docker run -d --network my-network --name my-mongo mongo
docker run -it --rm --network my-network mongo mongo --host my-mongo test
docker exec -it my-mongo bash
10. Create database dump
docker exec some-mongo sh -c 'exec mongodump -d --archive' > /some/path/on/your/host/all-collections.archive
11. Memorandum
docker run --network some-network --name some-mongo -v /my/custom:/etc/mongo -p 27017:27017 -d mongo --config /etc/mongo/mongod.conf
docker run --name some-mongo -p 27017:27017 -d mongo --auth
docker exec -it some-mongo mongo admin
> db.createUser({user:"root",pwd:"123456",roles:["root"]})
docker run -d --name some-mongo \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=123456 \
mongo
docker exec -it some-mongo mongo
file:
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/
https://registry.hub.docker.com/_/mongo
https://docs.docker.com/engine/reference/commandline/network/