在前一篇mongodb atlas初探中创建了一个免费的atlas集群,现在使用mongoimport命令向集群中导入数据。导入数据可以参考官方Import Data to your Free Tier Cluster和mongoimport。
数据来源
mongodb官方提供了一份数据源,使用curl命令下载该json文件:
$ curl -O https://raw.githubusercontent.com/mongodb/docs-assets/primer-dataset/primer-dataset.json
注意,这里是大写的O,表示下载并使用远程文件名保存,详细参考 Linux curl命令详解。终端输出:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 11.3M 100 11.3M 0 0 52940 0 0:03:44 0:03:44 --:--:-- 48113
为了方便下载,我上传了一份到百度云,下载地址为primer-dataset.json
使用mongo shell连接mongodb atlas
使用命令
$ mongo "mongodb://cluster0-shard-00-00-75shm.gcp.mongodb.net:27017,cluster0-shard-00-01-75shm.gcp.mongodb.net:27017,cluster0-shard-00-02-75shm.gcp.mongodb.net:27017/test?replicaSet=Cluster0-shard-0" --ssl --authenticationDatabase admin --username abc --password abc
连接atlas,结果输出
2018-07-08T18:21:56.650+0800 W NETWORK [thread1] No primary detected for set Cluster0-shard-0
谷歌了一下,发现这个问题Can't connect mongo shell to Atlas cluster,有些答案提到是因为mongo shell和mongo server版本不匹配,考虑到atlas中版本是3.6,我本地使用的mongo shell版本是3.2,于是升级mongo shell.
升级mongo shell
直接下载安装包,去下载页面,选择3.6.5,下载以后,编辑.bash_profile,添加PATH环境变量:
export PATH=$PATH:/Users/chenxin/Workspaces/mongo/mongodb-osx-x86_64-3.6.5/bin
重新使用mongo shell连接mongodb atlas
执行命令
mongo "mongodb+srv://cluster0-75shm.gcp.mongodb.net/test" --username abc
终端会提示:
MongoDB shell version v3.6.5
Enter password:
输入密码以后连接成功:
connecting to: mongodb+srv://cluster0-75shm.gcp.mongodb.net/test
2018-07-08T20:29:48.596+0800 I NETWORK [thread1] Starting new replica set monitor for Cluster0-shard-0/cluster0-shard-00-01-75shm.gcp.mongodb.net.:27017,cluster0-shard-00-00-75shm.gcp.mongodb.net.:27017,cluster0-shard-00-02-75shm.gcp.mongodb.net.:27017
2018-07-08T20:29:49.254+0800 I NETWORK [ReplicaSetMonitor-TaskExecutor-0] Successfully connected to cluster0-shard-00-00-75shm.gcp.mongodb.net.:27017 (1 connections now open to cluster0-shard-00-00-75shm.gcp.mongodb.net.:27017 with a 5 second timeout)
2018-07-08T20:29:49.256+0800 I NETWORK [thread1] Successfully connected to cluster0-shard-00-01-75shm.gcp.mongodb.net.:27017 (1 connections now open to cluster0-shard-00-01-75shm.gcp.mongodb.net.:27017 with a 5 second timeout)
2018-07-08T20:29:49.327+0800 I NETWORK [ReplicaSetMonitor-TaskExecutor-0] changing hosts to Cluster0-shard-0/cluster0-shard-00-00-75shm.gcp.mongodb.net:27017,cluster0-shard-00-01-75shm.gcp.mongodb.net:27017,cluster0-shard-00-02-75shm.gcp.mongodb.net:27017 from Cluster0-shard-0/cluster0-shard-00-00-75shm.gcp.mongodb.net.:27017,cluster0-shard-00-01-75shm.gcp.mongodb.net.:27017,cluster0-shard-00-02-75shm.gcp.mongodb.net.:27017
2018-07-08T20:29:49.622+0800 I NETWORK [ReplicaSetMonitor-TaskExecutor-0] Successfully connected to cluster0-shard-00-00-75shm.gcp.mongodb.net:27017 (1 connections now open to cluster0-shard-00-00-75shm.gcp.mongodb.net:27017 with a 5 second timeout)
2018-07-08T20:29:49.628+0800 I NETWORK [thread1] Successfully connected to cluster0-shard-00-01-75shm.gcp.mongodb.net:27017 (1 connections now open to cluster0-shard-00-01-75shm.gcp.mongodb.net:27017 with a 5 second timeout)
2018-07-08T20:29:50.012+0800 I NETWORK [ReplicaSetMonitor-TaskExecutor-0] Successfully connected to cluster0-shard-00-02-75shm.gcp.mongodb.net:27017 (1 connections now open to cluster0-shard-00-02-75shm.gcp.mongodb.net:27017 with a 5 second timeout)
MongoDB server version: 3.6.5
Error while trying to show server startup warnings: user is not allowed to do action [getLog] on [admin.]
MongoDB Enterprise Cluster0-shard-0:PRIMARY>
导入数据
导入数据的命令为:
mongoimport --host Cluster0-shard-0/cluster0-shard-00-00-75shm.gcp.mongodb.net:27017,cluster0-shard-00-01-75shm.gcp.mongodb.net:27017,cluster0-shard-00-02-75shm.gcp.mongodb.net:27017 \
--ssl --username abc --password abc --authenticationDatabase admin --db guidebook --collection restaurants --type json --file /Users/chenxin/Downloads/primer-dataset.json
注意:mongoimport不能在mongo shell中执行,也就是说执行该命令时cmd或者终端不能是连接到mongod的状态,否则会报错。
host参数后面的Cluster0-shard-0
是这个集群的replica set name,了解replica set可以查阅官方的Replication以及Deploy a Replica Set。需要注意的是--host Cluster0-shard-0/cluster0-shard-00-00-75shm.gcp.mongodb.net:27017,cluster0-shard-00-01-75shm.gcp.mongodb.net:27017,cluster0-shard-00-02-75shm.gcp.mongodb.net:27017
这一段必须是连续的,中间不能用\
分隔开,否则会导致命令无法执行,例如下面的命令就无法执行:
mongoimport --host Cluster0-shard-0/cluster0-shard-00-00-75shm.gcp.mongodb.net:27017,cluster0-shard-00-01-75shm.gcp.mongodb.net:27017, \
cluster0-shard-00-02-75shm.gcp.mongodb.net:27017 --ssl --username abc --password abc --authenticationDatabase admin \
--db guidebook --collection restaurants --type json --file /Users/chenxin/Downloads/primer-dataset.json
另外,如果是使用windows,执行的命令和上面的不太一样,可以在windows powershell或者Gitbash中执行下面的命令:
mongoimport --host Cluster0-shard-0/cluster0-shard-00-00-75shm.gcp.mongodb.net:27017,cluster0-shard-00-01-75shm.gcp.mongodb.net:27017,cluster0-shard-00-02-75shm.gcp.mongodb.net:27017 --ssl --username username --password password --authenticationDatabase admin --db guidebook --collection restaurants --type json --file primer-dataset.json
输入上述正确的命令以后,终端输出
2018-07-08T20:48:00.224+0800 connected to: Cluster0-shard-0/cluster0-shard-00-00-75shm.gcp.mongodb.net:27017,cluster0-shard-00-01-75shm.gcp.mongodb.net:27017,cluster0-shard-00-02-75shm.gcp.mongodb.net:27017
2018-07-08T20:48:00.534+0800 [#.......................] guidebook.restaurant544KB/11.3MB (4.7%)
2018-07-08T20:48:03.534+0800 [##############..........] guidebook.restaurant6.71MB/11.3MB (59.2%)
2018-07-08T20:48:06.010+0800 [########################] guidebook.restaurant11.3MB/11.3MB (100.0%)
2018-07-08T20:48:06.011+0800 imported 25359 documents
导入完成以后查看mongodb可以看到多了一个guidebook数据库,并且里面有一个名为restaurants的collection.
网友评论