[root@xtwj88 ~]# cat /etc/yum.repos.d/mongodb-org-4.2.repo
[mongodb-org-4.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc
[root@xtwj88 ~]# dnf list |grep mongodb
collectd-write_mongodb.x86_64 5.8.1-1.el7 epel
mongocli.x86_64 1.1.0-1 mongodb-org-4.2
mongodb.x86_64 2.6.12-6.el7 epel
mongodb-org.x86_64 4.2.8-1.el7 mongodb-org-4.2
mongodb-org-mongos.x86_64 4.2.8-1.el7 mongodb-org-4.2
mongodb-org-server.x86_64 4.2.8-1.el7 mongodb-org-4.2
mongodb-org-shell.x86_64 4.2.8-1.el7 mongodb-org-4.2
mongodb-org-tools.x86_64 4.2.8-1.el7 mongodb-org-4.2
mongodb-server.x86_64 2.6.12-6.el7 epel
mongodb-test.x86_64 2.6.12-6.el7 epel
nodejs-mongodb.noarch 1.4.7-1.el7 epel
php-mongodb.noarch 1.0.4-1.el7 epel
php-pecl-mongodb.x86_64 1.1.10-1.el7 epel
poco-mongodb.x86_64 1.6.1-3.el7 epel
syslog-ng-mongodb.x86_64 3.5.6-3.el7 epel
[root@xtwj88 ~]# dnf install mongodb-org
Extra Packages for Enterprise Linux 7 - x86_64 0.0 B/s | 0 B 00:00
Docker CE Stable - x86_64 0.0 B/s | 0 B 00:00
Kubernetes 0.0 B/s | 0 B 00:00
CentOS-7 - Base 0.0 B/s | 0 B 00:00
CentOS-7 - Updates 0.0 B/s | 0 B 00:00
CentOS-7 - Extras 0.0 B/s | 0 B 00:00
Dependencies resolved.
==============================================================================================================================================================================================================================================
Package Arch Version Repository Size
==============================================================================================================================================================================================================================================
Installing:
mongodb-org x86_64 4.2.8-1.el7 mongodb-org-4.2 5.9 k
Installing dependencies:
mongodb-org-mongos x86_64 4.2.8-1.el7 mongodb-org-4.2 15 M
mongodb-org-server x86_64 4.2.8-1.el7 mongodb-org-4.2 25 M
mongodb-org-shell x86_64 4.2.8-1.el7 mongodb-org-4.2 17 M
mongodb-org-tools x86_64 4.2.8-1.el7 mongodb-org-4.2 62 M
Transaction Summary
==============================================================================================================================================================================================================================================
Install 5 Packages
Total download size: 119 M
Installed size: 283 M
Is this ok [y/N]: y
Downloading Packages:
(1/5): mongodb-org-4.2.8-1.el7.x86_64.rpm 2.9 kB/s | 5.9 kB 00:02
(2/5): mongodb-org-shell-4.2.8-1.el7.x86_64.rpm 1.2 MB/s | 17 MB 00:13
(3/5): mongodb-org-server-4.2.8-1.el7.x86_64.rpm 1.2 MB/s | 25 MB 00:20
(4/5): mongodb-org-tools-4.2.8-1.el7.x86_64.rpm 1.8 MB/s | 62 MB 00:34
(5/5): mongodb-org-mongos-4.2.8-1.el7.x86_64.rpm 278 kB/s | 15 MB 00:53
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 2.2 MB/s | 119 MB 00:53
warning: /var/cache/dnf/mongodb-org-4.2-083f918c4813188b/packages/mongodb-org-4.2.8-1.el7.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID 058f8b6b: NOKEY
MongoDB Repository 0.0 B/s | 0 B 00:00
Importing GPG key 0x058F8B6B:
Userid : "MongoDB 4.2 Release Signing Key <packaging@mongodb.com>"
Fingerprint: E162 F504 A20C DF15 827F 718D 4B7C 549A 058F 8B6B
From : https://www.mongodb.org/static/pgp/server-4.2.asc
Is this ok [y/N]: y
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : mongodb-org-tools-4.2.8-1.el7.x86_64 1/5
Installing : mongodb-org-shell-4.2.8-1.el7.x86_64 2/5
Running scriptlet: mongodb-org-server-4.2.8-1.el7.x86_64 3/5
Installing : mongodb-org-server-4.2.8-1.el7.x86_64 3/5
Running scriptlet: mongodb-org-server-4.2.8-1.el7.x86_64 3/5
Created symlink from /etc/systemd/system/multi-user.target.wants/mongod.service to /usr/lib/systemd/system/mongod.service.
Installing : mongodb-org-mongos-4.2.8-1.el7.x86_64 4/5
Installing : mongodb-org-4.2.8-1.el7.x86_64 5/5
Verifying : mongodb-org-4.2.8-1.el7.x86_64 1/5
Verifying : mongodb-org-mongos-4.2.8-1.el7.x86_64 2/5
Verifying : mongodb-org-server-4.2.8-1.el7.x86_64 3/5
Verifying : mongodb-org-shell-4.2.8-1.el7.x86_64 4/5
Verifying : mongodb-org-tools-4.2.8-1.el7.x86_64 5/5
Installed:
mongodb-org-4.2.8-1.el7.x86_64 mongodb-org-mongos-4.2.8-1.el7.x86_64 mongodb-org-server-4.2.8-1.el7.x86_64 mongodb-org-shell-4.2.8-1.el7.x86_64 mongodb-org-tools-4.2.8-1.el7.x86_64
Complete!
[root@xtwj88 ~]# cat /etc/mongod.conf
# 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.
bindIp: 0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
#security:
security:
authorization: enabled
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options
#auditLog:
#snmp:
[root@xtwj88 ~]# echo "mongod soft nofiles 64000" >> /etc/security/limits.conf
[root@xtwj88 ~]# echo "mongod soft nproc 64000" >> /etc/security/limits.conf
[root@xtwj88 ~]# systemctl status mongod
● mongod.service - MongoDB Database Server
Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
Active: inactive (dead)
Docs: https://docs.mongodb.org/manual
[root@xtwj88 ~]# systemctl restart mongod
[root@xtwj88 ~]# systemctl status mongod
● mongod.service - MongoDB Database Server
Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2020-07-14 17:30:24 CST; 2s ago
Docs: https://docs.mongodb.org/manual
Process: 13860 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCESS)
Process: 13857 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
Process: 13855 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
Process: 13852 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
Main PID: 13863 (mongod)
Tasks: 32
Memory: 69.4M
CGroup: /system.slice/mongod.service
└─13863 /usr/bin/mongod -f /etc/mongod.conf
Jul 14 17:30:21 xtwj88 systemd[1]: Starting MongoDB Database Server...
Jul 14 17:30:21 xtwj88 mongod[13860]: about to fork child process, waiting until server is ready for connections.
Jul 14 17:30:21 xtwj88 mongod[13860]: forked process: 13863
Jul 14 17:30:24 xtwj88 mongod[13860]: child process started successfully, parent exiting
Jul 14 17:30:24 xtwj88 systemd[1]: Started MongoDB Database Server.
[root@xtwj88 ~]# mongo
MongoDB shell version v4.2.8
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("78b1f7be-dac2-4a29-8cdd-72bbaf68963b") }
MongoDB server version: 4.2.8
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
> use admin
switched to db admin
> db.createUser({user: "dragon", pwd: "Gah6kuP7ohfio4", roles:[{role: "userAdminAnyDatabase", db: "admin"}]})
Successfully added user: {
"user" : "dragon",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
]
}
> quit
function quit() {
[native code]
}
> quit()
[root@xtwj88 ~]#
use dragon to connect mongodb database
[root@xtwj88 ~]# mongo -u dragon -p --authenticationDatabase admin
MongoDB shell version v4.2.8
Enter password:
connecting to: mongodb://127.0.0.1:27017/?authSource=admin&compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("1708573f-835c-43fe-998e-df9bf03246b6") }
MongoDB server version: 4.2.8
> use lanzhou
switched to db lanzhou
> db.createUser({user: "lanzhou", pwd: "Gah6kuP7ohfio4", roles:[{role: "readwrite", db: "lanzhou"}]})
2020-07-14T17:55:47.600+0800 E QUERY [js] uncaught exception: Error: couldn't add user: No role named readwrite@lanzhou :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.createUser@src/mongo/shell/db.js:1370:11
@(shell):1:1
> db.createUser({user: "lanzhou", pwd: "Gah6kuP7ohfio4", roles:[{role: "readWrite", db: "lanzhou"}]})
Successfully added user: {
"user" : "lanzhou",
"roles" : [
{
"role" : "readWrite",
"db" : "lanzhou"
}
]
}
> quit()
[root@xtwj88 ~]#
来源:oschina
链接:https://my.oschina.net/qwfys200/blog/4374745