1. 程式人生 > >MongoDB學習筆記之Installing and Starting the Server

MongoDB學習筆記之Installing and Starting the Server

Mongodb

1、創建YUM軟件倉庫
[root@hdp04 ~]# vi /etc/yum.repos.d/mongodb.repo 
[mongodb-org-3.6]
name=MongoDB Repository
baseurl=https://mirrors.aliyun.com/mongodb/yum/redhat/7Server/mongodb-org/3.6/x86_64
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc
[root@hdp04 ~]# yum repolist

2、操作系統設置

[root@hdp04 ~]# echo "mongod     soft    nofiles   64000" >> /etc/security/limits.conf
[root@hdp04 ~]# echo "mongod     soft    nproc     64000" >> /etc/security/limits.conf

3、安裝mongodb

[root@hdp04 ~]# yum -y install mongodb-org

技術分享圖片
編輯mongodb配置文件,如下:

[root@hdp04 ~]# vi /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:
#  mmapv1:
#  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: 0.0.0.0  # Listen to local interface only, comment to listen on all interfaces.

security:
   authorization: enabled
#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:

4、啟動MongoDB服務

[root@hdp04 ~]# systemctl enable mongod.service
[root@hdp04 ~]# systemctl start mongod.service

5、訪問mongo服務

通過mongo命令接口直接訪問,默認的數據庫是test,可以通過db驗證。

[root@hdp04 ~]# mongo
MongoDB shell version v3.6.5
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.5
> db
test
>

6、創建數據庫和用戶

以下命令創建一個可以管理任意用戶的用戶管理員(安全起見,請自行更改user和password)。

> use admin
switched to db admin
> db.createUser({user: "madmin", pwd: "redhat", roles:[{role: "userAdminAnyDatabase", db: "admin"}]})
Successfully added user: {
        "user" : "madmin",
        "roles" : [
                {
                        "role" : "userAdminAnyDatabase",
                        "db" : "admin"
                }
        ]
}
> quit()

使用新建的madmin用戶進行驗證,如下:

[root@hdp04 ~]# mongo -u madmin -p --authenticationDatabase admin
MongoDB shell version v3.6.5
Enter password: 
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.5
> show dbs
admin  0.000GB
local  0.000GB
> quit()

或者使用下面的方法驗證:

[root@hdp04 ~]#  mongo --port 27017
MongoDB shell version v3.6.5
connecting to: mongodb://127.0.0.1:27017/
MongoDB server version: 3.6.5
> use admin
switched to db admin
> db.auth("madmin","redhat")
--輸出1表示成功
1
> exit
bye

7、通過JavaScript訪問MongoDB

[root@hdp04 ~]# vi hello.js
function sayHello(name) {
print(‘Hello ‘ + name + ‘, how are you?‘)
}
[root@hdp04 ~]# mongo --shell /root/hello.js 
MongoDB shell version v3.6.5
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.5
type "help" for help
> db
test
> sayHello(‘candon‘)
Hello candon, how are you?
> exit
bye

MongoDB學習筆記之Installing and Starting the Server