Nội dung bài viết Connect server MongoDB từ xa hay từ một server khác đó là một mô hình bình thường hiện
nay. Bài viết này sẽ hướng dẫn cho người mới làm quen với mongodb có thể làm được điều mà chỉ có những chuyên về admin mới làm được. Bài viết này có trong Series - Mongodb. Để hiểu được và có thể thao tác được, trước tiên tôi mong muốn về cơ bản, bạn phải hiểu MongoDB là gì? Và đương nhiên là phải có những thao tác trên Đầu tiên chính xác là bạn truy cập và Đến bước này thì việc duy nhất là bạn tìm tới file config mongodb Sau khi mở ra thì tìm đến line này: Warning: Đừng có comment lại Giờ bạn muốn thêm một Server A truy cập đến Server chứa MongoDB thì hãy làm như sau: Đầu tiên bạn phải làm là sử dụng lệnh: Để làm gì? Để bạn có thể thấy được ip address đang xài là gì? Ở đây chính là dòng thứ 3 Nghĩa là server khác sẽ truy cập và connect mongodb qua Sau khi edit thành
công thì đương nhiên phải reload lại mongo. Sau đó check lệnh Để theo dõi log... Kết nối mongodb từ xa Và sau cùng là ta lấy server A connect tới bằng nhiều cách như Connect mongodb Nodejs, nhưng ở đâu tôi dùng Sell cho nhanh: Ở đây Và đó là tất cả bài hướng dẫn của chủ đề ngày hôm nay. Xin chào!Video học lập trình mỗi ngày
LINUX
.Bài học này bạn được những gì?
MongoDB create user
ssh
, và tạo một user trên mongodb mà chúng ta đã nói đến ở
Mongodb create database and user terminal:> db.createUser[
{
user: "new_user",
pwd: "some_password",
roles: [ { role: "readWrite", db: "anonystick" } ]
}
]
Enable MongoDB Auth
/etc/mongod.conf
. Sau đó sửa file này như sau:sudo vim /etc/mongod.conf
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1
bindIp
: chính là nơi chứa những ip address được cho
phép truy cập. Mặc định là chỉ có connect bằng localhost
.Đến đây không thể không nhắc đến vấn đề bảo mật trong mongodb, đó là mỗi lần config vui lòng backup mongodb sử dụng mongodump. Cẩn thận trên hết nhé các đồng chí.#bindIp
. Nếu bạn làm như vậy đồng nghĩa với việc bạn sẽ cho phép tất cả các nơi truy cập vào admin
MongoDB của bạn.anonystick:~# ip a |grep net
inet 127.0.0.1/3 scope host lo
inet6 xxxx scope host
inet 12.13.14.15/23 brd xxxxx scope global dynamic ens3
inet6 xxxxx/64 scope link
12.13.14.15
. Khi lấy được rồi bạn sửa lại file config trên như tôi đã nói :# network interfaces
net:
port: 27017
bindIp: 127.0.0.1,12.13.14.15
12.13.14.15
này.Restart mongo daemon [mongod]
sudo service mongod restart
mongo
lên xem. Hoặc bạn có thể sử dụngtail -f /var/log/mongodb/mongod.log
mongo -u ian -p secretPassword 12.13.14.15/abc
MongoDB security
12.13.14.15
chính là ip mình đã config ở trên kia. Tới bước này rồi, có thể là tạm thời nói rằng bạn đã thành công. Nhưng còn một chuyện
đó là vấn đề bảo mật. Có ai đặt câu hỏi rằng, nếu như vậy thì tất cả có thể truy cập vào database từ xa, cho dù là ai? Đúng là như vậy, chính vì vậy một bước cuối cùng để MongoDB security
chính là phải cấu hình lại, ip address
nào được phép truy cập. Đó là việc điều chỉnh lại Firewall trong hệ thống. Bài viết này đã dài, cho nên tôi nghĩ sẽ để vấn đề này cho một bài viết khác có tên là "Adjusting the Firewall'.
On this page
- Database deployment Connect button is disabled
- Connecting IP address not in IP Access List
- Authentication to the database deployment failed
- Too many open connections to your database deployment
- Attempting to connect to a database deployment from behind a firewall
- Database Deployment Availability
- MongoDB Compass Troubleshooting
- Connection String Issues
This page outlines common connection issues and possible resolutions.
To learn more about connecting to an Atlas cluster, see the Get Started with Atlas tutorial.
Note
Note
Your database deployment's Connect button may be disabled if your database deployment is in the provisioning state. Your database deployment needs to provision when it is first deployed. Clusters also must provision when you scaled them up or down. The provisoning process can take up to 10 minutes, after which the Connect button will become enabled.
Before connecting to your Atlas database deployment, check that you added your host's IP address to the IP access list for your database deployment's project. Atlas allows client connections only from IP addresses and CIDR address ranges in the IP access list.
To connect to Atlas, you must authenticate with a MongoDB database user. To create a database user for your database deployment, see Configure Database Users.
If you have created a user and are having trouble authenticating, try the following:
Check that you are using the correct username and password for your database user, and that you are connecting to the correct database deployment.
Check that you are specifying the correct
authSource
database in your connection string.If you have a special character in your password, see Special characters in connection string password.
Atlas sets limits for concurrent incoming connections to a database deployment. For clusters, this is based on the cluster tier. If you try to connect when you are at this limit, MongoDB displays an error stating connection refused because too many open connections
.
For a detailed comparision of cluster tiers and their maximum concurrent connections, see Connection Limits and Cluster Tier.
Close any open connections to your database deployment not currently in use.
Scale your cluster to a higher tier to support more concurrent connections.
Restart your application.
To prevent this issue in the future, consider using the
maxPoolSize
connection string option to limit the number of connections in the connection pool.
To learn how to fix this issue, see Fix Connection Issues.
Atlas database deployments operate on port 27017
. You must be able to reach this port to connect to your database deployments. Additionally, ensure that the appropriate ports are open for the following:
For sharded clusters, grant access to port 27016.
For BI Connector, grant access to port 27015.
You can check your ability to reach a port using the third-party Outgoing port tester.
Example
If you can't access these ports, check your system firewall settings and ensure that they are not blocking access to these ports.
If you are using a mongodb+srv://
connection string and your driver or shell can't find the DNS
host of the Atlas database deployment, the database deployment might be paused or deleted. Check that the database deployment exists. If this is a paused cluster, you can resume the cluster if necessary.
Note
Atlas automatically pauses idle M0
clusters after 60 days with no connections.
If you use MongoDB Compass to connect to your cluster and experience issues, see:
Connection Refused using SRV Connection String in this section.
Compass Connection Errors in the MongoDB Compass documentation.
If you use a self-managed X.509 certificate or an auto-generated X.509 certificate managed by Atlas to authenticate to the MongoDB database, when you connect to MongoDB Compass, you must:
In MongoDB Compass, choose Fill in connection fields individually.
In the Authentication dropdown, select
X.509
.Select More Options.
In the SSL dropdown, select Server and Client Validation.
Add the same path to the downloaded Atlas-managed certificate, or the self-managed certificate [depending on which you use] to each of these fields: Certificate Authority, Client Certificate, and Client Private Key.
To learn more, see Connect to MongoDB in the MongoDB Compass documentation.
The connection string format you use to connect to Atlas depends on several factors, including:
Your
mongosh
version. To learn more, see Connect viamongosh
Your driver version. To learn more, see Connect via Your Application.
Verify your connection string in a test environment before putting it into production.
If your password includes special characters, and you are using your password in a connection string URI, encode the special characters.
Note
The following characters must be converted using percent encoding if included in a username or password:
For example, if your password in plain-text is p@ssw0rd'9'!
, you need to encode your password as:
➤ Use the Select your language drop-down menu to set the language of the encoding example in this section.
Important
Do not encode
special characters in your password if you are using your password outside of a connection string URI [for example, pasting it into mongosh
].
If you see this error message, your driver is likely out of date. For instructions on updating your driver, refer to your specific Driver Documentation.
When you use the DNS seed list connection string format to connect to Atlas, you might see the following error:
DNSHostNotFound: Failed to look up service “”
This error may occur when using the default DNS server that your ISP provides. That DNS server might not support SRV lookups that the DNS seed list connection string format uses.
To resolve the issue, you can try changing your DNS configuration to use a public DNS server.
Example
After you update your network settings to use a public DNS server, connect to the database deployment.
If running Ubuntu 18.04 and using the DNS seed list connection string format
[mongodb+srv://
] to connect to Atlas from one of the MongoDB Database Tools [mongodump
, mongorestore
, etc], you might see the following error:
lookup nta8e.mongodb.net on 123.45.67.8:27017: cannot unmarshal DNS message
If so, use one of the following connection options instead:
use the
--uri
option with a non-SRV connection string [mongodb://
].use the
--host
option to specify the host to connect.
When using the
DNS seed list connection string format [mongodb+srv://
] with a driver or Compass, you may receive in the following error:
Error: querySrv ECONNREFUSED _mongodb._tcp.
To remedy this issue, use the Standard Connection String format with Compass or that driver. With Compass, don't set the SRV Record value, set the Hostname and Port values instead.