Since the latest version cannot be installed with the standard repository of Ubuntu 18.04, use the repository provided by MongoDB.
$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
gpg: key 68818C72E52529D4: public key "MongoDB 4.0 Release Signing Key <[email protected]>" imported
gpg: Total number processed: 1
gpg: imported: 1
This is the command that imports the GPG key used in the MongoDB repository. If the result shows ** imported: 1 **, it's OK.
Next, add the repository information to apt.
$ 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
After that, in order to recognize the added repository
$ sudo apt update
This completes the repository registration.
Execute the installation with the apt command.
$ sudo apt install mongodb-org
Start MongoDB.
$ sudo systemctl start mongod
If you check the status of MongoDB and it is Active (running), the startup is completed successfully.
$ systemctl status mongod
● mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2020-10-28 02:49:10 UTC; 1 weeks 2 days ago
Docs: https://docs.mongodb.org/manual
Main PID: 17358 (mongod)
CGroup: /system.slice/mongod.service
└─17358 /usr/bin/mongod --config /etc/mongod.conf
This completes the MongoDB installation. Next, set up MongoDB authentication.
Immediately after installing MongoDB, any user can usually see the contents of the database. When using the database in the future, I think it is scary to keep the data exposed, so let's set the authentication at the first stage!
First of all, let's enable the user authentication setting from the configuration file (config). (Default is disabled)
The configuration file is usually located in /etc/mongod.conf
, so open it with a text editor.
Then, when you open it, copy and paste the following as it is, or if there is this description in the configuration file, change it as follows.
$ sudo vim /etc/mongod.conf
(I use vim, but my favorite text editor is fine)
security:
authorization: enabled
First, we have changed the configuration file, so restart mongod.
$ sudo systemctl restart mongod
There is no problem in recognizing a super user as a Linux root. It is OK to recognize it as admin / administrator.
MongoDB doesn't have a fixed user ID, so what determines the superuser is that it's a role. Here, we use root, which is built in as a role.
$ mongo
> use admin
switched to db admin
> db.createUser({user:"mongo", pwd:"password", roles:["root"]})
Successfully added user: { "user" : "mongo", "roles" : [ "root" ] }
users and pwd are optional. This completes the user name and password settings. Go out of MongoDB and enter the user name / password.
When exiting MongoDB
> exit
bye
In the above, the user name is ** mongo ** Password ** password ** Since it was specified, it will be as follows when connecting.
$ mongo -u mongo(User name)
MongoDB shell version v4.2.10
Enter password:
If you can authenticate successfully, it will be as follows.
$ mongo -u mongo
MongoDB shell version v4.2.10
Enter password:
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("xxxxxxxxxxxxxxxxxxxxxxxxx") }
MongoDB server version: 4.2.10
Server has startup warnings:
2020-10-28T02:49:10.604+0000 I STORAGE [initandlisten]
2020-10-28T02:49:10.604+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2020-10-28T02:49:10.604+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
---
Enable MongoDBs 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()
---
>
As a test, enter the following command at the prompt.
> show dbs
admin 0.000GB
app 0.001GB
auth 0.001GB
config 0.000GB
demo 0.000GB
events 0.000GB
hosting 0.000GB
local 0.000GB
log 0.004GB
If the database is displayed like this, it is proof that authentication is successful.
If you connect to MongoDB without specifying a user name, it will be as follows.
$ mongo
MongoDB shell version v4.2.10
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("xxxxxxxxxxxxxxxxxxxxxx") }
MongoDB server version: 4.2.10
> show dbs
>
Please note that the database display will not be displayed in the first place if it is not authenticated.
So, this time, I wrote about everything from MongoDB installation to authentication settings. This is my memorandum. I'm sorry it's hard to read. Next time, I will write about how to connect from PHP to MongoDB.
Recommended Posts