Press "Enter" to skip to content

MongoDB Atlas简单使用

[root@test01 ~]# uname -a
Linux test01 3.10.0-862.el7.x86_64 #1 SMP Fri Apr 20 16:44:24 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
[root@test01 ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
主机地址:192.168.139.139

注册页面:https://www.mongodb.com/cloud/atlas,填写相关信息,填写完进入如下界面:
Cloud Provider & Region:集群部署的位置
Cluster Tier:服务套餐,选择M0(免费),共享内存CPU,512M存储也够了,M0包含一个3个节点组成的replica set.
Additional settings:M0没有啥可以设置的
Cluster Name:集群的名字,默认为Cluster0不用修改
以上设置完成以后点击Create Cluster开始创建,需要人机校验.
创建新的Cluster需要几分钟时间,初始页面如下,在这个等待时间内,可以按提示创建user和设置白名单,为了方便访问,白名单IP就设置为0.0.0.0好了:
创建完成后页面如下,有一些基本的监控如Operation、Connections、Logical Size等,如下:

连接试用Cluster:
新建mongodb用户以及密码,连接至集群时需要用到:
在clusters这个view点击CONNECT按钮,出现如下界面:
选择“Connect With MongoDB Compass”,MongoDB Compass安装配置参考https://www.tracymc.cn/archives/1426.如果MongoDB Compass已经安装完成,直接选择Compass版本即可,如下:
连接命令为mongodb://admin:PASSWORD@cluster0-shard-00-00-dbppo.mongodb.net:27017,cluster0-shard-00-01-dbppo.mongodb.net:27017,cluster0-shard-00-02-dbppo.mongodb.net:27017/admin?replicaSet=Cluster0-shard-0&ssl=true,把其中的PASSWORD改为自己的命令即可,如下:
MongoDB Compass连接配置如下:
连接成功后页面如下:
如上图,发现连的是SECONDARY不是PRIMARY,退出重新连接,将cluster0-shard-00-00-dbppo.mongodb.net分别改为cluster0-shard-00-01-dbppo.mongodb.net(我这边这个是主)和cluster0-shard-00-02-dbppo.mongodb.net,直到连接到主为止,主的界面如下:
新建完数据库和集合以后就可以自己测试了,如下:

使用Robo 3T连接至集群:
Type选择"Replica Set",Members为cluster0-shard-00-00-dbppo.mongodb.net/cluster0-shard-00-01-dbppo.mongodb.net/cluster0-shard-00-02-dbppo.mongodb.net这三个,如下:
Authentication填写如下内容,"Auth Mechanism"一定要选“SCRAM-SHA-1”,不然会报错:
SSL选项卡,"Use SSL protocol"这个也一定要勾选上不然也会报错,Authentication Method选择"Self-signed Certificate":如下:
点击"Save",选中点击"Connect"即可,连接成功后默认初始页面如下:

使用Studio 3T连接至集群:
连接命令为mongodb+srv://admin:PASSWORD@cluster0-dbppo.mongodb.net/admin
将PASSWORD改成自己的密码,如下:
点击"Save"即可,如下:
选中点击"Connect"即可,如下:
连接成功后初始页面如下:

MongoDB常用导入导出命令如下:
Binary Import and Export Tools,Replace PASSWORD with the password for the admin user and DATABASE with the name of the database you wish to import/export to your cluster.
mongorestore | creates a new database or adds data to an existing database. By default, mongorestore reads the database dump in the dump/ sub-directory of the current directory; to restore from a different directory, pass in the path to the directory as a final argument.
mongorestore --host Cluster0-shard-0/cluster0-shard-00-00-dbppo.mongodb.net:27017,cluster0-shard-00-01-dbppo.mongodb.net:27017,cluster0-shard-00-02-dbppo.mongodb.net:27017 --ssl --username <USERNAME> --password <PASSWORD> --authenticationDatabase admin
mongodump | creates a binary export of the contents of a database
mongodump --host Cluster0-shard-0/cluster0-shard-00-00-dbppo.mongodb.net:27017,cluster0-shard-00-01-dbppo.mongodb.net:27017,cluster0-shard-00-02-dbppo.mongodb.net:27017 --ssl --username <USERNAME> --password <PASSWORD> --authenticationDatabase admin --db <DATABASE>

Data Import and Export Tools,Replace PASSWORD with the password for the admin user, DATABASE with the name of the database you wish to import/export to your cluster, and COLLECTION with the name of the collection you wish to import/export to your cluster. Replace FILETYPE with json or csv to specify the file type. Where applicable, replace FILENAME with the location and name of the output file (for export) or data source (for import).
NOTE: When exporting or importing CSV data, an additional --fields flag is often required. See documentation for the specific tool for additional details.
mongoimport | imports content from an Extended JSON, CSV, or TSV export
mongoimport --host Cluster0-shard-0/cluster0-shard-00-00-dbppo.mongodb.net:27017,cluster0-shard-00-01-dbppo.mongodb.net:27017,cluster0-shard-00-02-dbppo.mongodb.net:27017 --ssl --username <USERNAME> --password <PASSWORD> --authenticationDatabase admin --db <DATABASE> --collection <COLLECTION> --type <FILETYPE> --file <FILENAME>
mongoexport | produces a JSON or CSV export of data stored in a MongoDB instance
mongoexport --host Cluster0-shard-0/cluster0-shard-00-00-dbppo.mongodb.net:27017,cluster0-shard-00-01-dbppo.mongodb.net:27017,cluster0-shard-00-02-dbppo.mongodb.net:27017 --ssl --username <USERNAME> --password <PASSWORD> --authenticationDatabase admin --db <DATABASE> --collection <COLLECTION> --type <FILETYPE> --out <FILENAME>

Be First to Comment

发表评论

电子邮件地址不会被公开。 必填项已用*标注