Install MongoDB 4.4.4 offline on Linux
0. Preface
Version: Centos7+MongoDB 4.4.4
Official website: https://www.mongodb.com/
The installation configuration of this article is
单个机器单个实例
If you install
单个机器多个实例
or多台机器集群
, this article is for reference only.
1. Download
Click here to download the customized version, or click here to download
version:4.4.4
Platform:RedHat / CentOS 7.0
Package:tgz
2. Installation and Configuration
Upload to linux
After decompression, move the file storage location and rename it
tar -zxvf mongodb-linux-x86_64-rhel70-4.4.4.tgz
mv mongodb-linux-x86_64-rhel70-4.4.4 /usr/local/mongodb
Add configuration file
cd /usr/local/mongodb/
vi mongodb.conf
The following is the content of the configuration file mongodb.conf
#数据文件存放目录
dbpath = /usr/local/mongodb/data/db
#日志文件存放目录
logpath = /usr/local/mongodb/logs/mongodb.log
#默认端口27017
port = 27017
#以守护程序的方式启用,即在后台运行
fork = true
#允许远程连接,127.0.0.1只允许本地连接
bind_ip=0.0.0.0
#是否需要认证,如果启用,则需要创建mongodb账号密码,使用账号密码才可以远程访问
#auth = true
Then create the data file directory and log file configured in the mongodb.conf configuration file
mkdir -p /usr/local/mongodb/data/db
mkdir -p /usr/local/mongodb/logs
touch /usr/local/mongodb/logs/mongodb.log
3. Configure environment variables
vi /etc/profile
Then add the following
export PATH=$PATH:/usr/local/mongodb/bin
Make the changes you just made effective immediately
source /etc/profile
4. Start the mongo service
mongod -f /usr/local/mongodb/mongodb.conf
Successful startup is as follows
[root@iZbp185opgs9ov4aretwdxZ ~]# mongod -f /usr/local/mongodb/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 11622
child process started successfully, parent exiting
5. Connect to the mongo client
#未开启认证
mongo
#开启认证使用账号密码连接
mongo -u admin -p admin
The connection is successful as follows
[root@iZbp185opgs9ov4aretwdxZ ~]# mongo
MongoDB shell version v4.4.4
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session {
"id" : UUID("14909903-578d-4023-83cd-dec9703c904e") }
MongoDB server version: 4.4.4
---
The server generated these startup warnings when booting:
2021-03-04T15:00:41.842+08:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
2021-03-04T15:00:42.732+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2021-03-04T15:00:42.732+08:00: You are running this process as the root user, which is not recommended
2021-03-04T15:00:42.732+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
2021-03-04T15:00:42.732+08:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'
---
---
Enable MongoDB's 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()
---
>
You can also
ps -ef | grep mongo
check whether the process is started, the display of two is the start
6. Turn off the mongo service
mongod --dbpath /usr/local/mongodb/data/db --logpath /usr/local/mongodb/logs/mongodb.log --shutdown
or
rs:PRIMARY> use admin
rs:PRIMARY> db.shutdownServer()
#退出后即关闭
7. Create user [recommendation]
# 切换到admin数据库
use admin
# 使用db.createUser() 创建一个管理员用户admin,角色为root,指定具体数据库
db.createUser({
user:'admin',pwd:'admin',roles:[{
role:'root',db:'admin'}]})
# db.auth()认证一下获取权限,认证通过后可以操作数据库,成功则会输出1
db.auth("admin","admin")
# use 命令也可以是创建数据库,当指定数据库不存在时即为创建,切换到一个新的数据库xtest
use xtest
# 创建用户xtest dbOwner权限
db.createUser({
user:'xtest',pwd:'xtest',roles:['dbOwner']})
# 查看有哪儿些用户
show users
# 删除用户
db.dropUser("user_name")
One must be created
管理员账号
. After creating the account, you needmongodb.conf
to enable auth authentication in the configuration file , and then restart themongo
serviceAfter restarting, you need to log in to the client through the administrator account and password you just created, otherwise the
show dbs
command cannot view any database because you don’t have permission.
The client connection format is:
mongo -u 账号 --authenticationDatabase 数据库名 -p 密码
--authenticationDatabase 数据库名
Optional
mongo -u admin --authenticationDatabase admin -p admin
mongo -u admin -p admin
role:
read 允许用户读取指定数据库
readWrite 允许用户读写指定数据库
dbAdmin 允许用户在指定数据库中执行管理函数,如索引创建、删除,查看统计或访问system.profile
dbOwner 数据库拥有者,包含readWrite、dbAdmin、userAdmin
userAdmin 允许用户向system.users集合写入,可以找指定数据库里创建、删除和管理用户
clusterAdmin 只能切换在admin数据库中可用,赋予用户所有分片和复制集相关函数的管理权限。
readAnyDatabase 只能切换在admin数据库中可用,赋予用户所有数据库的读权限
readWriteAnyDatabase 只能切换在admin数据库中可用,赋予用户所有数据库的读写权限
userAdminAnyDatabase 只能切换在admin数据库中可用,赋予用户所有数据库的userAdmin权限
dbAdminAnyDatabase 只能切换在admin数据库中可用,赋予用户所有数据库的dbAdmin权限。
root 只能切换在admin数据库中可用。超级账号,超级权限
8. Add replica set and keyfile file [optional]
(1) Generate a keyfile file that stores authentication information
At the same time, the auth authentication and the replica set require a keyfile file. If it is not configured, an error will be reported when starting:BadValue: security.keyFile is required when authorization is enabled with replica sets
cd /usr/local/mongodb/data/db
openssl rand -base64 741 > mongodb.key
#修改权限,不修改之后启动时会报错
chmod 400 mongodb.key
(2) MongoDB uses only one node to enable replica set in MongoDB
Modify the configuration file to add replica set information and keyfile location information
cd /usr/local/mongodb/
vi mongodb.conf
The following is the content of the configuration file mongodb.conf
#数据文件存放目录
dbpath = /usr/local/mongodb/data/db
#日志文件存放目录
logpath = /usr/local/mongodb/logs/mongodb.log
#默认端口27017
port = 27017
#以守护程序的方式启用,即在后台运行
fork = true
#允许远程连接,127.0.0.1只允许本地连接
bind_ip=0.0.0.0
#是否需要认证,如果启用,则需要创建mongodb账号密码,使用账号密码才可以远程访问
auth = true
#副本集名称
replSet=rs
#存储身份信息的秘钥文件
keyFile=/usr/local/mongodb/data/db/mongodb.key
(3) Restart the mongo service
mongod --dbpath /usr/local/mongodb/data/db --logpath /usr/local/mongodb/logs/mongodb.log --shutdown
mongod -f /usr/local/mongodb/mongodb.conf
(4) Use the following command to initialize the replica set rs.initiate()
Log in to the MongoDB shell and run the commandrs.initiate()
> rs.initiate()
{
"info2" : "no configuration specified. Using a default configuration for the set",
"me" : "iZbp185opgs9ov4aretwdxZ:27017",
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1615022835, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1615022835, 1)
}
rs:SECONDARY>
View replica set information
rs.status()
So far complete.
9. Visualization tools
Click NoSQLBooster for MongoDB to download the tool. Some functions of this tool are free for 30-day trial.
How to connect:
When using this tool to connect to MongoDB
If the MongoDB setting does not require password authentication, just directly Connections->Create->Basic->Enter the ip and port in the Server->Click Test Connecting->If a green ok appears, it will succeed, and a red font will report an error and fail (the reason for failure is possible There is an error in the ip or port ) -> After the connection is successful, click Save & Connect -> Finish
If the MongoDB setting requires password authentication, that is, the configuration file is set
auth = true
, then Connections->Create->Basic->Enter the ip and port in the Server->Click Authentication->Mode to select Basic (Username/Password), Auth DB adds the database name , User Name add account name, Password fill in the password -> click Test Connecting -> if a green ok appears, it will succeed, and a red font will report an error, then it will fail -> after the connection is successful, click Save & Connect -> Finish(The reason for the failure may be an error in the ip port, or an error in the database account password. If you still cannot connect after all checks are correct, you should check whether it is configured in the configuration file
auth = true
, and then shut down the mongo service and restart it. After starting, pass the account in the black window Password to connect to the mongo client, re-create the account password, use the newly created account password in the tool to connect)
If the Alibaba Cloud server cannot be connected, it may be blocked by Cloud Shield. Configure the whitelist: click here
Or change the phone hotspot to try