背景
公司开发要用mongodb,一开始使用apt-get安装的是3.6版本,而存数据的服务器用的是5.0版本,新旧版本的shell和server是不兼容的,需要使用同一版本,mongo-tools也是。
步骤
卸载旧版本
sudo -i
#卸载mongo server
apt-get remove mongodb
#卸载mongodb-clients
apt-get remove mongodb-clients
#卸载mongo clients
apt-get remove mongo-tools
#卸载依赖
apt-get autoremove
下载新版本
#创建下载路径
mkdir /home/mongoDowmload
cd /home/mongoDowmload
#下载相关安装包
wget https://repo.mongodb.org/apt/ubuntu/dists/focal/mongodb-org/5.0/multiverse/binary-amd64/mongodb-org-server_5.0.5_amd64.deb
wget https://repo.mongodb.org/apt/ubuntu/dists/focal/mongodb-org/5.0/multiverse/binary-amd64/mongodb-org-mongos_5.0.5_amd64.deb
wget https://repo.mongodb.org/apt/ubuntu/dists/focal/mongodb-org/5.0/multiverse/binary-amd64/mongodb-org-shell_5.0.5_amd64.deb
wget https://fastdl.mongodb.org/tools/db/mongodb-database-tools-ubuntu2004-x86_64-100.5.1.deb
#安装
sudo dpkg -i mongodb-org-server_5.0.5_amd64.deb
sudo dpkg -i mongodb-org-shell_5.0.5_amd64.deb
sudo dpkg -i mongodb-database-tools-ubuntu2004-x86_64-100.5.1.deb
sudo dpkg -i mongodb-org-mongos_5.0.5_amd64.deb
配置
vim /etc/mongod.conf
修改bindip为0.0.0.0
service mongod start
#mongo shell 中
>use admin
>db.createUser({ user:'root',pwd:'123456',roles:[ { role:'userAdminAnyDatabase', db: 'admin'},"readWriteAnyDatabase"]});
>db.auth('root','123456')
#若提示1则添加管理员成功
#报错大概率是忘记输入use admin却换到admin数据库了
>exit
service mongod restart
数据导入与导出(备份与恢复)
参数
[root@localhost mongodb]# ./bin/mongoimport --help
options:
--help produce help message
-v [ --verbose ] be more verbose (include multiple times for more
verbosity e.g. -vvvvv)
--version print the program's version and exit
-h [ --host ] arg mongo host to connect to ( <set name>/s1,s2 for sets)
--port arg server port. Can also use --host hostname:port
--ipv6 enable IPv6 support (disabled by default)
-u [ --username ] arg username
-p [ --password ] arg password
--dbpath arg directly access mongod database files in the given
path, instead of connecting to a mongod server -
needs to lock the data directory, so cannot be used
if a mongod is currently accessing the same path
--directoryperdb if dbpath specified, each db is in a separate
directory
--journal enable journaling
-d [ --db ] arg database to use
-c [ --collection ] arg collection to use (some commands)
-f [ --fields ] arg comma separated list of field names e.g. -f name,age
--fieldFile arg file with fields names - 1 per line
--ignoreBlanks if given, empty fields in csv and tsv will be ignored
--type arg type of file to import. default: json (json,csv,tsv)
--file arg file to import from; if not specified stdin is used
--drop drop collection first
--headerline CSV,TSV only - use first line as headers
--upsert insert or update objects that already exist
--upsertFields arg comma-separated fields for the query part of the
upsert. You should make sure this is indexed
--stopOnError stop importing at first error rather than continuing
--jsonArray load a json array, not one item per line. Currently
limited to 4MB.
导出
mongodump -h 你要导入的数据库地址:端口 -u 帐号 -p 密码 -d 数据库 -o --authenticationDatabase admin 文件路径
例子
mongodump -h 192.168.2.58:27017 -u=root -p=123456 -d reading -o --authenticationDatabase admin /home/文档/mongoback
注意,如果你没有提前创建好对应库名的文件夹,请加上sudo
前缀,不然会提示mkdir失败,没有权限
导入
导入json
mongoimport -h 你要导入的数据库地址:端口 -u 管理员帐号 -p 管理员密码 --authenticationDatabase admin -d 数据库 -c 集合(表) 文件路径
例子
mongoreimport -h 192.168.2.22 -u root -p 123456 --authenticationDatabase admin -d reading -c wordResult /home/dianhui/文档/mongoback/reading/wordResult.json
导入bson
mongostore -h 你要导入的数据库地址:端口 -u 管理员帐号 -p 管理员密码 --authenticationDatabase admin -d 数据库 -c 集合(表) 文件路径
例子
mongorestore -h 192.168.2.22 -u root -p 123456 --authenticationDatabase admin -d reading -c wordResult /home/dianhui/文档/mongoback/reading/wordResult.bson