安装并迁移mongodb数据
之前通过golang连接安装好的mongodb一直不成功,今天重新安装部署下mongodb,并且做了一次数据迁移,结果是成功的。
参考文档:
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/
操作步骤
- 移除已经安装的mogodb
yum erase $(rpm -qa | grep mongodb-org)
移除之前的日志目录,数据目录
rm -rf /app/mongodb/log/*
rm -rf /app/mongodb/data/* - 配置yum源:
创建文件:
/etc/yum.repos.d/mongodb-org-4.4.repo
内容如下:
[mongodb-org-4.4]
name=MongoDB Repository
baseurl=http://mirrors.aliyun.com/mongodb/yum/redhat/7Server/mongodb-org/4.4/x86_64/
gpgcheck=0
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc
- 安装: mongodb-org
yum install -y mongodb-org
4.修改默认配置文件/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: /app/mongodb/log/mongod.log
# Where and how to store data.
storage:
dbPath: /app/mongodb/data
journal:
enabled: true
# engine:
# 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 5
- 授权
mkdir -p /app/mongodb/log/
mkdir -p /app/mongodb/data
chown -R mongodb:mongodb /app/mongodb/log/
chown -R mongodb:mongodb /app/mongodb/data - 关闭selinux ,并重启主机
setenforce 0
sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config
reboot ##重启主机
- 启动服务:
systemctl daemon-reload
systemctl start mongod
8.启动报错:
提示无法操作/tmp/mongodb-27017.sock
{"t":{"$date":"2020-09-21T12:19:08.370+08:00"},"s":"E", "c":"NETWORK", "id":23024, "ctx":"initandlisten","msg":"Failed to unlink socket file","attr":{"path":"/tmp/mongodb-27017.sock","error":"Operation not permitted"}}
增加权限
chown -R mongod:mongod /tmp/mongodb-27017.sock
- 创建用户以及集合
[root@prd-server-000834 log]# mongo
MongoDB shell version v4.4.0
> use admin
switched to db admin
> db.createUser({user: "root",pwd: "root",roles: [ { role: "root", db: "admin" } ]})
> db.createUser(
{
user: "XXX",
pwd: "XXX", // or cleartext password
roles: [
{ role: "clusterAdmin", db: "admin" },
{ role: "userAdminAnyDatabase", db: "admin" },
{ role: "root", db: "admin" }
]
}
> use XXX
>db.XXX.insert({"name":"check"})
- 检查db是否可以正常提供服务
mongo mongodb://XXX:XXX@localhost/XXX
- 数据导出:
mongoexport -h localhost -d oms -c publish_log -o /app/backup/publish_log.json
mongoexport -h localhost -d oms -c publish_state -o /app/backup/publish_state.json
mongoexport -h localhost -d oms -c publish_task -o /app/backup/publish_task.json
mongoexport -h localhost -d oms -c service -o /app/backup/service.json
mongoexport -h localhost -d oms -c serviceVersion -o /app/backup/serviceVersion.json
mongoexport -h localhost -d oms -c user -o /app/backup/user.json
- 数据导入:
mongoimport -d oms -c publish_log --file publish_log.json
mongoimport -d oms -c publish_state --file publish_state.json
mongoimport -d oms -c publish_task --file publish_task.json
mongoimport -d oms -c service --file service.json
mongoimport -d oms -c serviceVersion --file serviceVersion.json
mongoimport -d oms -c user --file user.json
12.导入导出,中间涉及tar 以及解压,scp,就不写了~~
有疑问加站长微信联系(非本文作者)