都说 MongoDB 是个内存大户,但是怎么知道它到底用了多少内存呢? 先 ps 一下看看。 $ ps aux|grep mongodmongo 26994 9.0 20.0 797264324 13243052 ? Sl May16 117:03 /path/to/mongodb/bin/mongod 总共 760G 多的虚拟内存,但是物理内存就只有 12.6G 。这
都说 MongoDB 是个内存大户,但是怎么知道它到底用了多少内存呢?
先 ps 一下看看。
$ ps aux|grep mongod mongo 26994 9.0 20.0 797264324 13243052 ? Sl May16 117:03 /path/to/mongodb/bin/mongod
总共 760G 多的虚拟内存,但是物理内存就只有 12.6G 。这个机器可是有 64G 内存的哦,这看起来 MongoDB 完全没用多少内存嘛。
再看看 free 的结果。
$ free -m total used free shared buffers cached Mem: 64544 64279 265 0 134 60413 -/+ buffers/cache: 3731 60813 Swap: 31999 0 31999
内存倒是占得差不多了,基本都是 cached ,也就是文件系统缓存。MongoDB 是通过 mmap 方式让操作系统来处理持久化和缓存的。每个数据文件都直接映射到某个虚拟内存地址。访问的时候如果这一页不在内存中,系统就会尝试把这一页加载进来。这些内存都是算进 cache 里的。在 mongodb 的官方文档里有这样一个说法,top 或 ps 里的 RSIZE 段显示的是机器的全部内存大小,因为 mongodb 会尽可能占用全部内存。但是事实上,这些缓存并没有算在里面。因此在 top 或 ps 中是看不出 MongoDB 的实际内存使用情况的。而 free 虽然可以看到系统的内存使用情况,但是没法确定这些内存里究竟有多少真的是 MongoDB 使用的。
还好有人做了 vmtouch 这个工具。可以检查文件在缓存中的情况,另外也可以把文件直接加载进缓存或者踢出去。只需要对 MongoDB 的所有数据文件检查一下缓存加载情况,就可以知道 MongoDB 到底缓存了多少数据了。
$ vmtouch -m4G /path/to/mongodb/data/ Files: 256 Directories: 3 Resident Pages: 15465901/100219772 58G/382G 15.4% Elapsed: 4.072 seconds
这里 -m4G 是 vmtouch 检查的文件大小限制。MongoDB 的数据文件比较大,通常会超过默认的 500M。这样看来,缓存用了 58G,这还差不多。Resident Pages 左侧的数字是页的数量,页的数量乘以文件系统页大小才是内存使用量。页的大小可以通过
getconf PAGESIZE
查看,通常是 4096,也就是 4KB。
MongoDB 在 NUMA 的机器上运行,并且内存被固定到一个 node 的时候,会有一个警告
WARNING: You are running on a NUMA machine. We suggest launching mongod like this to avoid performance problems: numactl –interleave=all mongod [other options]
也许是认为,这种情况下只能用上一个节点的内存。但 MongoDB 的缓存是由操作系统管理的。NUMA 似乎对此并没有影响。而内存不太小的时候 MongoDB 本身很难用掉一个节点的内存。这种情况下,是否开启 numactl –interleave=all 作用已经不大了。能做的也许只能是加内存,sharding,或者换 ssd 了。
原文地址:MongoDB 内存使用, 感谢原作者分享。

MySQLoffersvariousstorageengines,eachsuitedfordifferentusecases:1)InnoDBisidealforapplicationsneedingACIDcomplianceandhighconcurrency,supportingtransactionsandforeignkeys.2)MyISAMisbestforread-heavyworkloads,lackingtransactionsupport.3)Memoryengineis

Common security vulnerabilities in MySQL include SQL injection, weak passwords, improper permission configuration, and unupdated software. 1. SQL injection can be prevented by using preprocessing statements. 2. Weak passwords can be avoided by forcibly using strong password strategies. 3. Improper permission configuration can be resolved through regular review and adjustment of user permissions. 4. Unupdated software can be patched by regularly checking and updating the MySQL version.

Identifying slow queries in MySQL can be achieved by enabling slow query logs and setting thresholds. 1. Enable slow query logs and set thresholds. 2. View and analyze slow query log files, and use tools such as mysqldumpslow or pt-query-digest for in-depth analysis. 3. Optimizing slow queries can be achieved through index optimization, query rewriting and avoiding the use of SELECT*.

To monitor the health and performance of MySQL servers, you should pay attention to system health, performance metrics and query execution. 1) Monitor system health: Use top, htop or SHOWGLOBALSTATUS commands to view CPU, memory, disk I/O and network activities. 2) Track performance indicators: monitor key indicators such as query number per second, average query time and cache hit rate. 3) Ensure query execution optimization: Enable slow query logs, record and optimize queries whose execution time exceeds the set threshold.

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

MySQL uses a GPL license. 1) The GPL license allows the free use, modification and distribution of MySQL, but the modified distribution must comply with GPL. 2) Commercial licenses can avoid public modifications and are suitable for commercial applications that require confidentiality.

The situations when choosing InnoDB instead of MyISAM include: 1) transaction support, 2) high concurrency environment, 3) high data consistency; conversely, the situation when choosing MyISAM includes: 1) mainly read operations, 2) no transaction support is required. InnoDB is suitable for applications that require high data consistency and transaction processing, such as e-commerce platforms, while MyISAM is suitable for read-intensive and transaction-free applications such as blog systems.

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Zend Studio 13.0.1
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Notepad++7.3.1
Easy-to-use and free code editor
