MongoDB学习笔记之Installing and Starting the Server

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学习笔记之Installing and Starting the Server
编辑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

猜你喜欢

转载自blog.51cto.com/candon123/2125430