MongoDB official website to download

Platform select RedHat or CentOS 7.0

After downloading it, upload it to the server. You can also Copy the Copy link next to it and download it to the server using the wget command

1. Decompress the download file

[root@localhost software]# tar -zxvf mongodb-linux-x86_64-enterprise-rhel70-4.4.4.tgz
Copy the code

2. Move the decompressed folder to the /usr/local directory and rename it mongodb.

[root@localhost software]# mv mongodb-linux-x86_64-enterprise-rhel70-4.4.4 /usr/local/mongodb
Copy the code

3. Copy the directory to the bin folder and modify environment variables

/usr/local/mongodb/bin
Copy the code

Modifying environment Variables

[root@localhost bin]# vim /etc/profile # export PATH=/usr/local/mongodb/bin:$PATHCopy the code

Save and exit, then refresh

[root@localhost bin]# source /etc/profile
Copy the code

Run the mongo-version command to check whether the configuration is successful

[root@localhost bin]# mongo -version MongoDB shell version v4.4.4 Build Info: {"version": "4.4.4", "gitVersion": "Eight db30a63db1a9d84bdcad0c83369623f708e0397 openSSLVersion", "" :" OpenSSL 1.0.1 e - fips 11 Feb 2013 ", "modules" : [ "enterprise" ], "allocator": "tcmalloc", "environment": { "distmod": "rhel70", "distarch": "x86_64", "target_arch": "x86_64" } } [root@localhost bin]#Copy the code

4. Create directories for storing data and logs

Run the mkdir data/db -p command in the mongodb directory to create a data store directory

[root@localhost mongodb]# pwd /usr/local/mongodb [root@localhost mongodb]# mkdir data/db -p [root@localhost mongodb]# ll Total amount 120 drwxr-xr-x. 2 root root 147 2月 19 17:01 bin drwxr-xr-x. 3 root root 16 2月 19 17:16 data-rw-r --r-- 17699 2月 12 05:57 license-enterprise. TXT -rw-r--r-- 1 root root 16726 2月 12 05:57 mpl-2-rw-r --r-- 1 root root 1977 README drwxr-xr-x. 2 root root 131 2月 19 17:01 snmp-rw-r --r--. 1 root root 75685 2月 12 05:57 THIRD-PARTY-NOTICES [root@localhost mongodb]#Copy the code

Go to the data directory and create a logs log directory

[root@localhost mongodb]# cd data/
[root@localhost data]# ll
总用量 0
drwxr-xr-x. 2 root root 6 2月  19 17:16 db
[root@localhost data]# mkdir logs
[root@localhost data]# ll
总用量 0
drwxr-xr-x. 2 root root 6 2月  19 17:16 db
drwxr-xr-x. 2 root root 6 2月  19 17:18 logs
[root@localhost data]# 
Copy the code

Go to the logs directory and create a log file, mongodb.log

[root@localhost data]# CD logs/ [root@localhost logs]# touch mongodb. Log [root@localhost logs]# ll Root root 0 2月 19 17:19 mongodb.log [root@localhost logs]#Copy the code

5. Configure the mongodb core configuration file

Go back to the mongodb root directory and use the vim command to create and start editing the mongodb.conf file

[root@localhost mongodb]# vim mongodb.conf
Copy the code

Conf file configuration is as follows

# port port = 27017 # database file location dbpath = / usr/local/mongo/data/db # log file location logpath = / usr/local/mongo/data/logs/mongo. Log # Logappend =true Fork =true # maxConns=100 # noauth=true # auth=true # Bind_ip =0.0.0.0; bind_ip=0.0.0.0Copy the code

6. Start the mongo

Run the mongod -f mongodb.conf command to start mongodb

[root@localhost mongodb]# mongod -f mongodb.conf 
Copy the code

If startup error:

mongod: error while loading shared libraries: libnetsnmpmibs.so.31: cannot open shared object file: No such file or directory

Net-snmp needs to be installed first

Install using the yum command

yum install -y net-snmp
Copy the code

Do not start until the installation is complete

[root@localhost mongodb]# mongod -f mongodb.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 45693
child process started successfully, parent exiting
[root@localhost mongodb]# 
Copy the code

The startup succeeded.

Close the mongo

[root@localhost mongodb]# ps -ef | grep mongodb
root      45693      1  1 17:34 ?        00:00:01 mongod -f mongodb.conf
root      47814   5502  0 17:36 pts/0    00:00:00 grep --color=auto mongodb
[root@localhost mongodb]# kill -9 45693
Copy the code

7. Create a user account

[root @ localhost mongo] # mongo mongo shell version v4.4.4 connecting to: mongo: / / 127.0.0.1:27017 /? compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("82489266-3976-4e0b-a87c-3d6238548825") } MongoDB server version: Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see https://docs.mongodb.com/ Questions? Try the MongoDB Developer Community Forums https://community.mongodb.com MongoDB Enterprise > use admin switched to db admin MongoDB Enterprise > db.createUser({"user":"root","pwd":"root",roles:["root"]}) Successfully added user: { "user" : "root", "roles" : [ "root" ] } MongoDB Enterprise > db.auth("root","root") 1 MongoDB Enterprise > show users { "_id" : "admin.root", "userId" : UUID("3559dfaa-9ac2-46d2-a017-aa2102c35b48"), "user" : "root", "db" : "admin", "roles" : [ { "role" : "root", "db" : "admin" } ], "mechanisms" : [ "SCRAM-SHA-1", "SCRAM-SHA-256" ] } MongoDB Enterprise >Copy the code

Using the command

db.createUser({“user”:”root”,”pwd”:”root”,roles:[“root”]})

Example Create an account with user name root, password root, and role permission root.

Navicat connection