Under Mac install methods to configure mongodb and create users
- 2020-10-23 20:20:46
- OfStack
preface
mongodb database believe needless to introduce, everyone not unfamiliar, once installed mongodb before for a long time, in an accidental circumstances be a bosses, landing directly came in first in alarm and the operation, then I found myself not locking the database, and after installation mongodb enabled by default when the default is any ip can don't need a password to access directly.
All right, without further ado, let's get to the main text of the day
Install mongodb
There are three ways to install mongodb:
1. Download and install directly
MongoDB offers a 64-bit installation package for OSX, which you can download from the OSX website.
Download address: [https: / / www. mongodb. com/download - center # community] [1]
2. Install via curl which comes with mac
# Enter the /usr/local
cd /usr/local
# download
sudo curl -O https://fastdl.mongodb.org/osx/mongodb-osx-x86_64-3.4.2.tgz
# Unpack the
sudo tar -zxvf mongodb-osx-x86_64-3.4.2.tgz
# rename mongodb directory
sudo mv mongodb-osx-x86_64-3.4.2 mongodb
3. Install via brew
sudo brew install mongodb
New log file, mongodb configuration file and database storage path
(Add sudo to the command if the following folder is not created successfully or if you are prompted to do not have permissions)
# Enter the mac The root directory
cd /mongoData
# New folder mongoData
mkdir mongoData
# new 3 The folders are respectively db( Store database data ) . etc(mongodb The configuration file ) . logs( The log file )
mkdir db etc logs
in etc and log Next, create a configuration file and a log file, respectively
cd etc
touch mongo.conf
cd logs
touch mongo.log
Next, modify the configuration file for mongodb
#vim Edit configuration file
vim mongo.conf
mongdb has a lot of config file fields, so Let me show you my config file
# Database path
dbpath=/mongoData/db/
# Log output file path
logpath=/mongoData/logs/mongo.log
# The error log is in append mode, after this option is configured mongodb The log is appended to an existing log file instead of being created from scratch 1 A new file
logappend=true
# Enable log files, enabled by default
journal=true
# This option can be filtered out 1 Some useless log information, if you need to debug use please set to false
quiet=false
# Whether the background starts, has this parameter, may realize the background to run
fork=true
# The port number The default is 27017
port=27017
# Specify the storage engine (not required by default)
#storageEngine=mmapv1
# Open the certification
auth = true
Copy the above configuration fields in and save them
Start the mongodb
# Enter the mongdb The installation directory
cd /usr/local/mongodb/bin
# Start as a configuration file mongdb
./mongod -f /mongoData/etc/mongo.conf
The presence of successful indicates that the service has started successfully
Configure the superuser and user
# Enter the mongodb
./mongo
# use admin The database
use admin
# View all the databases
show dbs
Not surprisingly, it will indicate no permissions because we started mongodb as a configuration file, and in the configuration file we turned on authentication and set the auth field to true
At this point we should start configuring users
Create a super administrator user
use admin
db.createUser({user:"admin",pwd:"password",roles:["root"]}) //admin This database is the system's own database, its users can access any other database data, also known as the super administrator
db.auth("admin","password") // => 1 Indicates that the authentication has passed 0 Indicates verification failure
show dbs //=>admin 0.000GB blog 0.000GB config 0.000GB
This shows you all the databases
Create a regular user (a user of a database)
use admin //=> Enter the admin The database
db.auth("admin","password") //=> Verified by super administrator
use blog
db.createUser({user: "blog", pwd: "password", roles: [{ role: "dbOwner", db: "blog" }]})
show dbs => admin 0.000GB blog 0.000GB config 0.000GB local 0.000GB
This creates a separate account for the database blog, with the account blog and password password
One thing to note here is that when creating a normal database user, create it after the super administrator has verified it
conclusion