# MongoDB Diagnostics and Debugging (Version 3.4) <br />Ch 4: Connectivity
###### tags: `MongoDB University M312`
## What Connectivity Issues Look Like in an Application
* Time Outs
* Connection Error
* Incorrect URI
* Non-static IP Addresses
* Firewall
* Out of conrtrol
* DDoS attacks
* ISP Performing Poorly
## Timeout
### Timeout Errors
* Server Selection TimeoutError
* WTimeError ( 僅表示 writeConcern 沒有在時間內完成,不代表 query 沒有完成 )
```javascript=
db.foo.insertOne( {hello: 'world'}, { writeConcern: { w: 3, wtimeout: 2} } )
```
* Execution Timeout
```javascript=
db.collection.find({})._addSpecial("$maxTimeMS", 100)
```
* Network Timeout
## Closing and Dropping Connections
* Greedy Connection Applications
* Implication of Connection in Memory allocation
* How to detect poorly configured hosts
* Election of new Primary
### Tools
* Mongostat
* db.serverStatus()
* free -h (OS)
* mongod log file
:::info
MongoDB allows incoming connections up to ***65536***.
Since our process require RAM, too many connections is not the normal behavior for a well written application.
:::
* **net.maxIncomingConnections** configuration file parameter affect the number of available incoming connections
* Cuz one connection is regard as a file to mongod, we need to set higher limit for more connections.
``` bash=
# allow open file to 2048
ulimit -n 2048
```
```javascript=
// shows the available connections of mongod
db.serverStatus().connections
```
### Election of Primary
## Write Concern and Timeouts
[Doc](https://docs.mongodb.com/manual/reference/write-concern/#write-concern-specification)
:::warning
Replica set with 'w:majority' may have some bad situation when there are arbiters or delay servers in the set.
:::
### Hostnames and Cluster Configuration
edit host file
```bash=
# OS like
/etc/host
# windows
C:\WINDOWS\system32\drivers\etc\hosts
```
### Networking Best Practices
* All Replica Set nodes should be addressable from client hosts and replica set hosts
* All mongos nodes should be addressable
* Use DNS names instead of IP address to avoid IP address renew resolution conflicts
* Make use of ping command to check if node can reach each other
* Use telnet to make sure that the ports are available
## Sharding Issues
split chunk manually
```javascript=
sh.splitAt('m312.example', { last_name: 'E', first_name: Minkey })
```
Increase chunk size
```javascript=
use config
db.setting.save( { _id: 'chunksize', value: 150 } )
```
# MongoDB Diagnostics and Debugging (Version 3.4) <br />Ch 5: Schema Issues
### Schema Investigation
1. turn on the profiler
```javascript=
db.setProfilingLevel(2)
```
2. turn off the profiler and see how many queries were made
```javascript=
db.setProfilingLevel(0)
db.system.profile.count()
```
3. Get deeper information
```javascript=
db.system.profile.find({}, { planSummary: 1, op:1, ns: 1, query: 1 }).pretty()
```