MongoDB Community Edition 安装笔记

安装数据库系统三步走:

  • 安装数据库程序本身
  • 设置账户/密码/安全性等等
  • (可选)导入数据

安装就非常简单的,按照官方程序复制粘贴都能过:

https://docs.mongodb.com/manual/administration/install-on-linux/

大致就是如此(没错,截至今天最新依然是buster源版本):

wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
echo "deb http://repo.mongodb.org/apt/debian buster/mongodb-org/5.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
sudo apt-get update
sudo apt-get install -y mongodb-org
echo "mongodb-org hold" | sudo dpkg --set-selections
echo "mongodb-org-database hold" | sudo dpkg --set-selections
echo "mongodb-org-server hold" | sudo dpkg --set-selections
echo "mongodb-org-shell hold" | sudo dpkg --set-selections
echo "mongodb-org-mongos hold" | sudo dpkg --set-selections
echo "mongodb-org-tools hold" | sudo dpkg --set-selections

在Debian系统似乎遇到权限问题,人工调整了一下:

chown -R mongodb:mongodb /var/lib/mongodb

设置账户密码:

默认提供的是无认证,直接可以连接,使用起来总有那么一丝丝不安,可以给超级管理员先套个密码,运行mongo进入本机服务器命令控制台.

use admin  
db.createUser({
  user: 'taterli',
  pwd: '5XbdVH3V8LQgE4',
  roles:[{
    role: 'root',
    db: 'admin'
  }]
})

设置完成后修改/etc/mongod.conf开启认证,重启服务器:

security:
  authorization: enabled

第三,如果是服务器迁移,还要考虑转移老数据呢,默认都是dump目录就不改了,临时移动嘛,不是要单独指定.

在源服务器进行导出:

mongodump --uri "mongodb://user:xxoo@127.0.0.1:27017/?retryWrites=true&w=majority"

把JSON文件(admin除外)挪到新服务器,在新服务器导入:

待恢复机目录结构:

root@debian:~# tree
.
└── dump
    └── torrent
        ├── comments.bson
        └── comments.metadata.json

2 directories, 2 files
root@debian:~# 

恢复命令:

mongorestore --uri "mongodb://user:xxoo@127.0.0.1:27017/?retryWrites=true&w=majority"

具体输出:

2022-01-02T06:56:42.055+0800    using default 'dump' directory
2022-01-02T06:56:42.056+0800    preparing collections to restore from
2022-01-02T06:56:42.056+0800    reading metadata for torrent.comments from dump/torrent/comments.metadata.json
2022-01-02T06:56:42.065+0800    restoring torrent.comments from dump/torrent/comments.bson
2022-01-02T06:56:42.081+0800    finished restoring torrent.comments (952 documents, 0 failures)
2022-01-02T06:56:42.081+0800    no indexes to restore for collection torrent.comments
2022-01-02T06:56:42.081+0800    952 document(s) restored successfully. 0 document(s) failed to restore.

发表评论

您的电子邮箱地址不会被公开。