Mongo

Summary: Author: 张亚飞 | 阅读时间: 4 minute read | Published: 2016-08-11
Filed under Categories: LinuxTags: Note,

Mongo 安装指南


MongoDB 数据库兼容升级到 3.6

db.adminCommand( { setFeatureCompatibilityVersion: "3.4" } )
  • 安装 MongoDB 升级到 4.0 后启动再报错:
2018-11-25T09:21:29.199Z F CONTROL  [initandlisten] ** IMPORTANT: UPGRADE PROBLEM: The data files need to be fully upgraded to version 3.6 before attempting an upgrade to 4.0; see http://dochub.mongodb.org/core/4.0-upgrade-fcv for more details.

提示要讲数据库升级到 3.6 兼容版本,登录终端,通过以下命令查看当前兼容版本

Tue Jan 08 15:29:59 coam@a.us.1:/var/log/mongodb$ mongo
MongoDB shell version v3.6.8
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.8
> use admin
switched to db admin
> db.auth("zyf","yafei312")
> db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } )
{ "featureCompatibilityVersion" : { "version" : "3.4" }, "ok" : 1 }
> db.shutdownServer()

可以看到虽然当前运行的 Mongodb 是 3.6,但当前最低兼容的版本是 3.4,而运行 4.0 最低要求数据版本兼容到 3.6, 于是在 Mongodb 环境手动升级:

> db.adminCommand( { setFeatureCompatibilityVersion: "3.6" } )
{ "ok" : 1 }
> db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } )
{ "featureCompatibilityVersion" : { "version" : "3.6" }, "ok" : 1 }
> db.shutdownServer()

修改成功,再关闭 Mongodb 3.6,启动 Mongodb4.0 解决问题


Mongodb 安装

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
echo "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
sudo apt update
sudo apt install -y mongodb-org

Mongodb 升级

sudo apt purge mongodb-org*
...重复以上安装步骤...
sudo apt install -y mongodb-org

加入开机自运行

sudo vi /etc/rc.local
  • mongo 不加 sudo 启动不了
/usr/bin/mongod -f /etc/mongod.conf

Sun Nov 20 11:43:00 coam@as:~$ /usr/bin/mongod -f /etc/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 2205
ERROR: child process failed, exited with error number 1

增加用户 mongodb 日志及数据文件夹的写权限

sudo chown -R mongodb.mongodb /data/home/data/mongodb
sudo chown -R 777 /data/home/data/mongodb
sudo chown -R mongodb.mongodb /var/log/mongodb/
sudo chown -R 777 /var/log/mongodb/

ERROR: child process failed, exited with error number 1

原因: 上次 mongodb 未正常关闭

  • mongod.lock :该文件将 mongodb 锁上防止用户操作
rm -rf /data/home/data/mongodb/mongod.lock
#@ 删除当天日志文件
rm -rf /var/log/mongodb/log.datetime

ERROR: child process failed, exited with error number 100

大部分是因为mongodb 服务的不正常关闭,导致mongod 被锁,想想可能是上次服务器断电维护时,没有手动关闭MongoDb服务造成的.

sudo /usr/bin/mongod -port 27017 --repair -f /etc/mongodb.conf
sudo /usr/bin/mongod -port 27017 --repair --dbpath /data/home/data/mongodb/
sudo /usr/bin/mongod -port 27017 --repair --dbpath /data/home/data/mongodb/ --repairpath /data/home/data/tmp
./mongod --repair --dbpath /storage/mongodb/data/ --repairpath /storage/mongodb/tmp/

/usr/bin/mongod -f /etc/mongod.conf --repair

安装 MongoDB3 参考 http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/#install-the-mongodb-packages

卸载 MongoDB

sudo apt purge mongodb mongodb-clients mongodb-server mongodb-dev
sudo apt purge mongodb-10gen
sudo apt autoremove

MongoDB的备份与恢复 http://weizhifeng.net/backup-and-restore-data-of-mongodb.html

安装稳定版本,否则会出现连接502错误

参考 * http://php.net/manual/zh/mongo.installation.php#mongo.installation.nix * 升级到MongoDB 3.0


问题记录

按以上步骤安装完后,我按照官方的默认路径配置的 /etc/mongod.conf 将 dbPath 目录指向 /data/home/data/mongodb

sudo service mongod start
或者测试重启 sudo reboot
sudo vi /var/log/mongod/mongod.log

提示如下错误

2016-05-15T22:36:09.913+0800 D -        [initandlisten] User Assertion: 98:Unable to create/open lock file: /data/home/data/mongodb/mongod.lock errno:13 Permission denied Is a mongod instance already running?
2016-05-15T22:36:09.913+0800 I STORAGE  [initandlisten] exception in initAndListen: 98 Unable to create/open lock file: /data/home/data/mongodb/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating
2016-05-15T22:36:09.913+0800 I CONTROL  [initandlisten] dbexit:  rc: 100

默认 /var/log/mongodb 日志文件没有修改,用户为 mongodb;而查看默认 dbPath/var/lib/mongodb 下用户都是 mongodb

cd /var/lib/mongodb
ls -al
...
-rw-r--r--  1 mongodb mongodb 98304 May 13 21:05 collection-0--2561765191871685279.wt
-rw-r--r--  1 mongodb mongodb 36864 May 13 21:05 collection-0--6028622490541801156.wt
drwxr-xr-x  2 mongodb mongodb  4096 May 13 21:05 diagnostic.data
-rw-r--r--  1 mongodb mongodb 53248 May 13 21:05 index-1--2561765191871685279.wt
...

于是手动将 /data/home/data/mongodb 全部修改为 mongodb 用户

sudo chown mongodb.mongodb /data/home/data/mongodb
sudo chown mongodb.mongodb /data/home/data/mongodb/*

MongoDB 管理笔记摘要

Mongodb 日常管理

sudo service mongod start
sudo service mongod stop
sudo service mongod restart

Mongo 用户管理

创建超级用户 [#超级root帐号]

db.createUser(
  {
    user: "zyfmix",
    pwd: "yafei312",
    roles: [
       { role: "root", db: "admin" }
    ]
  }
 )

MongoDB 修改重命名字段名

通过命令行批量修改
  • 修改一级字段名
CoamReSet:PRIMARY> db.TruckRoadsPath.update({},{$rename:{"Lng":"lng"}},{multi:true})
WriteResult({ "nMatched" : 2702582, "nUpserted" : 0, "nModified" : 2702582 })
  • 修改二级字段名
CoamReSet:PRIMARY> db.TruckRoadsPath.update({},{$rename:{"PathInfo.Speed":"PathInfo.speed"}},{multi:true})

出现以下错误,提示不能更新 PathInfo 为 null 的数据,提示更新失败,但检查数据发现已全部正确更新过来了

WriteResult({
        "nMatched" : 0,
        "nUpserted" : 0,
        "nModified" : 0,
        "writeError" : {
                "code" : 16837,
                "errmsg" : "cannot use the part (PathInfo of PathInfo.Speed) to traverse the element ({PathInfo: null})"
        }
})

正确的检查非 null 条件下的写法应该是:

CoamReSet:PRIMARY> db.TruckRoadsPath.update({"PathInfo":{$ne:null}},{$rename:{"PathInfo.State":"PathInfo.state"}},{multi:true})
WriteResult({ "nMatched" : 2702158, "nUpserted" : 0, "nModified" : 2702156 })

参考 How do you query this in Mongo? (is not null)


CentOS 相关

  • 注意, mongodb 可以通过命令 sudo mongod -f /etc/mongod.conf 启动,但是却不能通过 systemctl start mongod 启动,注意 mongodb 用户权限及 cs-mongod.service 中启动的用户组配置

CentOS 安装 mongodb 的用户及组名称为 mongod 而 Ubuntu 系统安装的用户及组名称为 mongodb,导致 配置不兼容.


参考 * 如何在MongoDB 修改字段名称,重命名字段 * Mongo docs - $rename * Error while upgrading Mongodb from 3.2 to 3.6

Comments

Cor-Ethan, the beverage → www.iirii.com