MongoDB的安装及连接

注:本文基于CentOS 7.2编写

1、安装

使用yum方式安装,因此需要先添加repo配置,

[root@centos7 yum.repos.d]# cat mongodb-org-4.2.repo 
[mongodb-org-4.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc

然后安装mongodb-org即可,

yum install -y mongodb-org

2、配置

默认配置如下:

[root@centos7 ~]# grep -vE "^$|^#" /etc/mongod.conf 
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log
storage:
  dbPath: /var/lib/mongo
  journal:
    enabled: true
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo
net:
  port: 27017
  bindIp: 127.0.0.1  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.

通过rpm方式安装,默认数据库存放位置为/var/lib/mongo,除了该配置,还有systemd服务文件里有一些系统层面的配置,

[root@centos7 mongo]# grep -vE "^$|^#" /usr/lib/systemd/system/mongod.service
...
[Service]
User=mongod
Group=mongod
Environment="OPTIONS=-f /etc/mongod.conf"
EnvironmentFile=-/etc/sysconfig/mongod
ExecStart=/usr/bin/mongod $OPTIONS
ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb
ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb
ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb
PermissionsStartOnly=true
PIDFile=/var/run/mongodb/mongod.pid
Type=forking
LimitFSIZE=infinity
LimitCPU=infinity
LimitAS=infinity
LimitNOFILE=64000
LimitNPROC=64000
LimitMEMLOCK=infinity
TasksMax=infinity
TasksAccounting=false
...

3、启动服务

在默认配置下即可运行服务,并设置开启启动,

systemctl start mongod
systemctl enable mongod

4、连接数据库

如果只是连接本地数据库的话,直接输入mongo即可,

[root@centos7 mongo]# mongo
MongoDB shell version v4.2.6
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("2aae8ae7-0add-4fa2-95c2-04658a71f699") }
MongoDB server version: 4.2.6
Server has startup warnings: 
2020-05-09T11:54:13.505-0400 I  STORAGE  [initandlisten] 
2020-05-09T11:54:13.505-0400 I  STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2020-05-09T11:54:13.505-0400 I  STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2020-05-09T11:54:17.641-0400 I  CONTROL  [initandlisten] 
2020-05-09T11:54:17.641-0400 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-05-09T11:54:17.641-0400 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2020-05-09T11:54:17.641-0400 I  CONTROL  [initandlisten] 
2020-05-09T11:54:17.641-0400 I  CONTROL  [initandlisten] 
2020-05-09T11:54:17.641-0400 I  CONTROL  [initandlisten] ** WARNING: You are running on a NUMA machine.
2020-05-09T11:54:17.641-0400 I  CONTROL  [initandlisten] **          We suggest launching mongod like this to avoid performance problems:
2020-05-09T11:54:17.641-0400 I  CONTROL  [initandlisten] **              numactl --interleave=all mongod [other options]
2020-05-09T11:54:17.641-0400 I  CONTROL  [initandlisten] 
2020-05-09T11:54:17.641-0400 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2020-05-09T11:54:17.641-0400 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-05-09T11:54:17.641-0400 I  CONTROL  [initandlisten] 
2020-05-09T11:54:17.641-0400 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2020-05-09T11:54:17.641-0400 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-05-09T11:54:17.641-0400 I  CONTROL  [initandlisten] 
---
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()
---

> 

默认情况下会有一系列系统检测,并提示用户修改,

  • 由于本机是ext4文件系统,MongoDB建议使用xfs性能更优
  • 由于是新装数据库,还未创建用户,因此Access control会提示
  • 由于本机是numa架构机器,开启numa特性会使得mongod进程内存只能分配到某一个node中,即出现系统还有内存却开始使用swap分区的现象,因此需要关闭该限制
  • MongoDB建议关闭透明大页,否则可能会有一些异常性能问题

如果不想有这些提示启动时加上–quiet即可,

[root@centos7 mongo]# mongo --quiet
> 

另外,可通过以下格式命令连接任意一台MongoDB,

mongo 'mongodb://username:password@ipaddr/dbname'

其中,
username为用户名
password为密码
ipaddr为数据库ip地址
dbname为所要连接的数据库名称

或者使用参数,

mongo -u me -p --host localhost --authenticationDatabase test --quiet
Enter password: 

其中,
-u 指定用户名
-p 指定密码,可以输入明文,也可以键盘输入
–host 指定数据库ip地址
–authenticationDatabase 指定需要连接的数据库名称

你可能感兴趣的:(MongoDB)