原文出处:http://blog.csdn.net/anxpp/article/details/52453134 本人租用的云服务器,为了节约成本,各项配置都是比较低的,但是又运行了很多的服务,所以要经常查看内存占用情况以检查哪些服务存在问题并调整参数作调优。当然,实际的生成服务器,也可能会需要做些类似的操作的。 1、 top命令 top命令经常用来监控linux的系统状况,比如cpu、内存的使用,程序员基本都知道这个命令。 进入监控界面后按M后可以看到以内存占用大小排序的视图: 2、free -m 使用这个命令可以直接看内存的整个使用情况: 3、ps -e -o 'pid,comm,args,pcpu,rsz,vsz,stime,user,uid' | grep oracle | sort -nrk5 rsz为实际内存占用: 命令的完整用法大家还是度娘吧,这里只是给出方法,够用就好。 from:http://blog.csdn.net/anxpp/article/details/52453134
View Details引言 最近博客又抽风了,打开主页后提示 Error Establishing a Database Connection。仔细想想,应该就是数据库服务器 mariadb 挂了;以前也遇到过类似的问题。经过分析日志,并结合网上的资料最终解决了问题。 日志 以下是 mariadb 服务器挂掉时的比较关键的日志信息,从下面的日志信息中,我们可以很容易地看出由于内存不足,从而导致数据库服务器启动时崩溃。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
InnoDB: Starting crash recovery. InnoDB: Reading tablespace information from the .ibd files... InnoDB: Restoring possible half-written data pages from the doublewrite InnoDB: buffer... 160919 2:47:12 InnoDB: Waiting for the background threads to start 160919 2:47:13 Percona XtraDB (http://www.percona.com) 5.5.46-MariaDB-37.6 started; log sequence number 352718445 160919 2:47:13 [ERROR] mysqld: Out of memory (Needed 128917504 bytes) 160919 2:47:13 [Note] Plugin 'FEEDBACK' is disabled. 160919 2:47:13 [Note] Server socket created on IP: '0.0.0.0'. 160919 2:47:13 [Note] Event Scheduler: Loaded 0 events 160919 2:47:13 [Note] /usr/libexec/mysqld: ready for connections. Version: '5.5.47-MariaDB' socket: '/var/lib/mysql/mysql.sock' port: 3306 MariaDB Server 160919 02:47:35 mysqld_safe Number of processes running now: 0 160919 02:47:35 mysqld_safe mysqld restarted 160919 2:47:35 [Note] /usr/libexec/mysqld (mysqld 5.5.47-MariaDB) starting as process 28614 ... 160919 2:47:35 InnoDB: The InnoDB memory heap is disabled 160919 2:47:35 InnoDB: Mutexes and rw_locks use GCC atomic builtins 160919 2:47:35 InnoDB: Compressed tables use zlib 1.2.7 160919 2:47:35 InnoDB: Using Linux native AIO 160919 2:47:35 InnoDB: Initializing buffer pool, size = 128.0M InnoDB: mmap(137756672 bytes) failed; errno 12 160919 2:47:35 InnoDB: Completed initialization of buffer pool 160919 2:47:35 InnoDB: Fatal error: cannot allocate memory for the buffer pool 160919 2:47:35 [ERROR] Plugin 'InnoDB' init function returned error. 160919 2:47:35 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed. 160919 2:47:35 [ERROR] mysqld: Out of memory (Needed 128917504 bytes) 160919 2:47:35 [ERROR] mysqld: Out of memory (Needed 96681984 bytes) 160919 2:47:35 [ERROR] mysqld: Out of memory (Needed 72499200 bytes) 160919 2:47:35 [Note] Plugin 'FEEDBACK' is disabled. 160919 2:47:35 [ERROR] Unknown/unsupported storage engine: InnoDB 160919 2:47:35 [ERROR] Aborting |
解决 在使用 free -m 查看内存信息时,发现 swap 分区大小为 0。难怪说数据库服务器无法启动呢,在内存不够用的情况下,又无法使用 swap 分区,自然崩溃了。由于 VPS 使用了 SSD,性能自然不错。下面我们给服务器系统 CentOS 7 添加 1024M 的 swap 分区,采用的方法是创建一个 swap 文件: 使用下面的命令创建 swapfile:
1 2 |
# 1048576 = 1024 * 1024 dd if=/dev/zero of=/swapfile bs=1024 count=1048576 |
使用下面的命令配置 swap 文件:
1 |
mkswap /swapfile |
接下来,使用下面的命令立即启用 swapfile,这样就不用等到下次重启时自动启用:
1 |
swapon /swapfile |
最后,我们在 /etc/fstab 中添加下面一行,这样可以在系统下次重启时自动生效创建的 swapfile:
1 |
/swapfile swap swap defaults 0 0 |
使用 cat /proc/swaps 或 free -m 查看 swapfile 的生效情况,如下图所示: 在完成上面的步骤后,我们还可以在 /etc/my.cnf 配置文件中添加一些配置信息,降低 mariadb 资源需求,具体的配置请参考文末给出的链接。 启动 启动 apache 服务器:systemctl start httpd.service; 启动 mariadb 服务器:systemctl start mariadb.service。 启动完成后,再次打开网站主页,bingo,问题解决了! 总结 低配 VPS 最好还是要多增加 swap 分区大小,尤其对于使用 SSD 的 VPS 而言,swap […]
View Details