记一次 MySQL 崩溃的修复经历

这一天网站突然打不开报 MySQL 连接错误,上服务器后发现 MySQL 已经停止服务,各种启动无效。经历了一系列搜索和无效的工作后,去云服务上后台看监控,发现硬盘满了。于是得出结论硬盘空间不足会导致 MySQL 服务停止。

切到根目录下使用 du -sh * 查看各目录的占用情况,发现 /root/.pm2 在捣鬼。

20K    dump.pm2
16K    dump.pm2.bak
33G    logs
4.0K    module_conf.json
4.0K    modules
8.0K    pids
16G    pm2.log
4.0K    pm2.pid
0    pub.sock
0    reload.lock
0    rpc.sock
4.0K    touch

于是先删除日志文件,启动 MySQL 服务即可。

rm -rf logs/*
rm -rf pm2.log
service mysqld start

查看错误日志发现 Node 服务的某个模块不存在...

用 Nginx 反向代理 NodeJS

安装 NodeJS

yum install nodejs -y

node -v

安装 pm2

npm install pm2 -g

ln -s /home/download/node-v8.11.1-linux-x64/lib/node_modules/pm2/bin/pm2 /usr/local/bin/pm2

修改 package.json

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "pm2": "/home/download/node-v8.11.1-linux-x64/lib/node_modules/pm2/bin/pm2 start /web/mazey.cn/server/app.js"
}

or

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "pm2": "pm2 start app.js"
}

启动 pm2

npm run pm2

开机启动 pm2

pm2 save

pm2 startup centos

注意

pm2 startup centos失败,可尝试pm2 startup

 PM2 detected systemv but you precised centos
 Please verify that your choice is indeed your init system
 If you arent sure, just run : pm2 startup

修改 Nginx 配置

vim /etc/nginx/conf.d/*.conf

upstream nodejs {
    server 127.0.0.1:3000;
    keepalive 64;
}
server {
    listen 80;
    server_name domain.cn;
    root /web/mazey.cn;
    index index.html index.htm;
    # 网站切到/server下时走nodejs
    location /server {
        proxy_set_header    X-Real-IP $remote_addr;
        proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header    Host  $http_host;
        proxy_set_header    X-Nginx-Proxy true;
        proxy_set_header    Connection "";
        proxy_pass http://nodejs;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
        expires 30d;
    }
    location ~ .*\.(js|css)?$ {
        expires 1h;
    }
}

相应的 app.js

const express = require('express')
const app = express()
let hi = 'hi'

app.get('/server', (req, res, next) => {
  hi = `Hello Mazey!\n`
  next()
}, (req, res) => {
  res.send(`
  ${hi}
  ${req.method}\n
  ${req.originalUrl}\n
  ${req.query.id}\n
  `)
})

const server = app.listen(3000, function () {
  let host = server.address().address
  let port = server.address().port

  console.log('Example app listening at http://%s:%s', host, port)
})

注意

若报错 Cannot GET /xxx 说明 Express 的路由没配好。