Files
pages/2020/01/05/devops.html
2026-02-04 02:55:12 +00:00

337 lines
21 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<!-- Begin Jekyll SEO tag v2.8.0 -->
<title>Mayx的运维笔记 | Mayx的博客</title>
<meta name="generator" content="Jekyll v3.9.5" />
<meta property="og:title" content="Mayx的运维笔记" />
<meta name="author" content="mayx" />
<meta property="og:locale" content="zh_CN" />
<meta name="description" content="新年新Bug" />
<meta property="og:description" content="新年新Bug" />
<meta property="og:site_name" content="Mayx的博客" />
<meta property="og:type" content="article" />
<meta property="article:published_time" content="2020-01-05T00:00:00+08:00" />
<meta name="twitter:card" content="summary" />
<meta property="twitter:title" content="Mayx的运维笔记" />
<meta name="google-site-verification" content="huTYdEesm8NaFymixMNqflyCp6Jfvd615j5Wq1i2PHc" />
<meta name="msvalidate.01" content="0ADFCE64B3557DC4DC5F2DC224C5FDDD" />
<meta name="yandex-verification" content="fc0e535abed800be" />
<script type="application/ld+json">
{"@context":"https://schema.org","@type":"BlogPosting","author":{"@type":"Person","name":"mayx"},"dateModified":"2020-01-05T00:00:00+08:00","datePublished":"2020-01-05T00:00:00+08:00","description":"新年新Bug","headline":"Mayx的运维笔记","mainEntityOfPage":{"@type":"WebPage","@id":"/2020/01/05/devops.html"},"publisher":{"@type":"Organization","logo":{"@type":"ImageObject","url":"https://avatars0.githubusercontent.com/u/17966333"},"name":"mayx"},"url":"/2020/01/05/devops.html"}</script>
<!-- End Jekyll SEO tag -->
<link rel="canonical" href="https://mabbs.github.io/2020/01/05/devops.html" />
<link type="application/atom+xml" rel="alternate" href="/atom.xml" title="Mayx的博客" />
<link rel="alternate" type="application/rss+xml" title="Mayx的博客(RSS)" href="/rss.xml" />
<link rel="alternate" type="application/json" title="Mayx的博客(JSON Feed)" href="/feed.json" />
<link rel="stylesheet" href="/assets/css/style.css?v=1770173707" />
<!--[if !IE]> -->
<link rel="stylesheet" href="/Live2dHistoire/live2d/css/live2d.css" />
<!-- <![endif]-->
<link rel="search" type="application/opensearchdescription+xml" href="/opensearch.xml" title="Mayx的博客" />
<link rel="webmention" href="https://webmention.io/mabbs.github.io/webmention" />
<link rel="pingback" href="https://webmention.io/mabbs.github.io/xmlrpc" />
<link rel="preconnect" href="https://summary.mayx.eu.org" crossorigin="anonymous" />
<link rel="prefetch" href="https://www.blogsclub.org/badge/mabbs.github.io" as="image" />
<link rel="blogroll" type="text/xml" href="/blogroll.opml" />
<link rel="me" href="https://github.com/Mabbs" />
<script src="/assets/js/jquery.min.js"></script>
<!--[if lt IE 9]>
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-ajaxtransport-xdomainrequest/1.0.3/jquery.xdomainrequest.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
<script>
var lastUpdated = new Date("Wed, 04 Feb 2026 10:55:07 +0800");
var BlogAPI = "https://summary.mayx.eu.org";
</script>
<script src="/assets/js/main.js"></script>
<!--[if !IE]> -->
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async="async" src="https://www.googletagmanager.com/gtag/js?id=UA-137710294-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-137710294-1');
</script>
<script src="/assets/js/instant.page.js" type="module"></script>
<!-- <![endif]-->
</head>
<body>
<!--[if !IE]> --><noscript><marquee style="top: -15px; position: relative;"><small>发现当前浏览器没有启用JavaScript这不影响你的浏览但可能会有一些功能无法使用……</small></marquee></noscript><!-- <![endif]-->
<!--[if IE]><marquee style="top: -15px; position: relative;"><small>发现当前浏览器为Internet Explorer这不影响你的浏览但可能会有一些功能无法使用……</small></marquee><![endif]-->
<div class="wrapper">
<header class="h-card">
<h1><a class="u-url u-uid p-name" rel="me" href="/">Mayx的博客</a></h1>
<img src="https://avatars0.githubusercontent.com/u/17966333" fetchpriority="high" class="u-photo" alt="Logo" style="width: 90%; max-width: 300px; max-height: 300px;" />
<p class="p-note">Mayx's Home Page</p>
<form action="/search.html">
<input type="text" name="keyword" id="search-input-all" placeholder="Search blog posts.." />&#160;<input type="submit" value="搜索" />
</form>
<br />
<p class="view"><a class="u-url" href="/Mabbs/">About me</a></p>
<ul class="downloads">
<li style="width: 270px; border-right: none;"><a href="/MayxBlog.tgz">Download <strong>TGZ File</strong></a></li>
</ul>
</header>
<section class="h-entry">
<small><time class="date dt-published" datetime="2020-01-05T00:00:00+08:00">5 January 2020</time> - 字数统计2306 - 阅读大约需要7分钟 - Hits: <span id="/2020/01/05/devops.html" class="visitors">Loading...</span></small>
<h1 class="p-name">Mayx的运维笔记</h1>
<p class="view">by <a class="p-author h-card" href="//github.com/Mabbs">mayx</a></p>
<div id="outdate" style="display:none;">
<hr /><p>
这是一篇创建于 <span id="outime"></span> 天前的文章,其中的信息可能已经有所发展或是发生改变。
</p>
</div>
<script>
daysold = Math.floor((new Date().getTime() - new Date("Sun, 05 Jan 2020 00:00:00 +0800").getTime()) / (24 * 60 * 60 * 1000));
if (daysold > 90) {
document.getElementById("outdate").style.display = "block";
document.getElementById("outime").innerHTML = daysold;
}
</script>
<hr />
<b>AI摘要</b>
<p id="ai-output">这篇文章记述了运维人员新年期间遇到的问题花火学园的服务器宕机原因是数据库连接问题。在经历重启失败、故障排查后发现用户表出现问题通过重置解决问题。为了提高稳定性运维决定迁移服务器从Ubuntu迁移到CentOS并使用LEMP应用。迁移过程中包括数据库和文件备份、新服务器部署、DNS设置、CDN配置以及基础环境优化如BBR和Swap安装。在优化过程中运维尝试使用UNIX domain sockets但遇到了权限设置问题后来调整了MySQL连接配置。此外还发现并处理了postfix和日志记录的问题。最后运维反思了之前的问题并计划分开开发环境和主站服务器。</p>
<hr />
<ul><li><a href="#开端">开端</a></li><li><a href="#重建的过程">重建的过程</a></li><li><a href="#之后的优化">之后的优化</a></li><li><a href="#后记">后记</a></li></ul>
<hr />
<main class="post-content e-content" role="main"><p>新年新Bug<!--more--></p>
<h1 id="开端">
<a href="#开端"><svg class='octicon' viewBox='0 0 16 16' version='1.1' width='16' height='32' aria-hidden='true'><path fill-rule='evenodd' d='M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z'></path></svg></a> 开端
</h1>
<p>前天,我维护的<a href="https://www.say-huahuo.com">花火学园</a>宕机了,由于在上面安了乱七八糟的各种各样的东西,所以我以为它只是日常卡机,所以我就随手重启了一下。 </p><p>
重启完之后它凉了不知道为什么数据库连不上了……于是我开始问另外一个同样可以登录服务器的人问他有没有动过数据库结果听起来貌似他好几个月都没登服务器了。于是我慌了试着手动启动MySQL的服务然而黑色的界面里冷冰冰的<code class="language-plaintext highlighter-rouge">Job for mysql.service failed</code>告诉我事情没有那么简单。我按照错误的提示查看了详细信息,然而里面也只是简单的记录了一句<code class="language-plaintext highlighter-rouge">mysql.service: Main process exited, code=exited, status=1/FAILURE</code>这样通俗而又简单的一句话……这和没说有什么区别……谁都知道它启动失败了…… </p><p>
没办法,这时候只能向网站的上一代运维求助了 <del>(向大佬低头)</del> 经过大佬不知道什么样的操作终于找到了真正的错误详细信息看起来好像是数据库中的用户表出了问题。于是大佬重置了用户表总算是勉强把MySQL服务重新启动了。 </p><p>
不过虽然这时候再做一些简单的配置,应该差不多就可以重新上线了,不过考虑了一下,为了让以后不会再出现乱七八糟的事,我打算把网站重新迁移一遍,让系统环境做一个大更新!</p>
<h1 id="重建的过程">
<a href="#重建的过程"><svg class='octicon' viewBox='0 0 16 16' version='1.1' width='16' height='32' aria-hidden='true'><path fill-rule='evenodd' d='M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z'></path></svg></a> 重建的过程
</h1>
<p>首先重新租一台服务器吧之前用的是Ubuntu的服务器现在一年过去了也该考虑考虑稳定性的问题了于是我把系统换成了CentOS。 </p><p>
然后为了之后配置起来方便一些我依然决定使用服务器商已经配置好的LEMP应用这样建好以后我也不用再考虑安装服务器软件的问题了。 </p><p>
在新的服务器部署的过程中,我回到原来的服务器,开始打包网站的数据库和文件,做好向新的服务器转移的准备。 </p><p>
没多久,新的服务器就部署好了,于是我直接一个<code class="language-plaintext highlighter-rouge">scp</code>就把所有的文件传到了新的服务器上。不得不说服务器直接传输文件的速度是真的快几个GiB的文件几分钟就传完了。顺便感叹一下花火学园的数据居然也都好几个GiB了想当年我接手的时候还不到2GiB。 </p><p>
然后就是导入数据库和解压网站文件以及配置Nginx完成以后网站不出所料的可以正常使用了。 </p><p>
然后就是设置DNS之类乱七八糟的事以及Cloudflare的CDN之类的不过还好之前设置过防火墙白名单的配置这次也方便了不少。 </p><p>
不过这还没完,既然换了新的服务器,而这一年里我对服务器的了解也更深了一些,至少应该做一些优化吧。 </p><p>
所以呢我就先把BBR给装上了搞太多东西也很麻烦所以我就直接用了<a href="https://github.com/teddysun/across/blob/master/bbr.sh">一键脚本</a>。顺便又搞了Swap毕竟服务器只有1GiB的内存是应该多搞点内存虽然可能速度会降低但还是求稳嘛而且服务器好像是SSD的硬盘应该影响不大。 </p><p>
到这里,基础的环境部分我就自认为搞好了。</p>
<h1 id="之后的优化">
<a href="#之后的优化"><svg class='octicon' viewBox='0 0 16 16' version='1.1' width='16' height='32' aria-hidden='true'><path fill-rule='evenodd' d='M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z'></path></svg></a> 之后的优化
</h1>
<p>到了第二天我又心血来潮想再搞点别的东西来优化服务器首先我的目标是把对内的TCP/IP sockets全部换成UNIX domain sockets用UNIX domain sockets可以减少不少系统开销所以我首先对PHP-FPM开刀。 </p><p>
其实改起来也很简单就是把PHP-FPM的配置文件中的<code class="language-plaintext highlighter-rouge">listen</code>项从IP地址换成文件然后把Nginx中的<code class="language-plaintext highlighter-rouge">fastcgi_pass</code>改成带<code class="language-plaintext highlighter-rouge">unix:</code>前缀的文件就OK了。我改完之后真就以为OK了但是现实中的<code class="language-plaintext highlighter-rouge">502 Bad Gateway</code>打破了我的幻想,看来没那么简单…… </p><p>
后来我仔细看了一下PHP-FPM文件的注释发现了一句警告</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>; WARNING: If you switch to a unix socket, you have to grant your webserver user
; access to that socket by setting listen.acl_users to the webserver user.
</code></pre></div></div>
<p>哦哦,原来是要设置<code class="language-plaintext highlighter-rouge">listen.acl_users</code>……然后我把nginx加了进去重启之后它就好了。 </p><p>
之后我打算优化一下MySQL不过我也不是很清楚MySQL中怎么设置UNIX domain sockets本来是打算就此放过不过我听说好像数据库连接中用<code class="language-plaintext highlighter-rouge">127.0.0.1</code>要比<code class="language-plaintext highlighter-rouge">localhost</code>要好,因为少了一次解析。我听着感觉好像有那么点道理,然后就去把网站配置的数据库地址给改了。 </p><p>
不过后来发现查了一下发现好像MySQL对<code class="language-plaintext highlighter-rouge">localhost</code>地址有特殊规定在MySQL中如果使用<code class="language-plaintext highlighter-rouge">localhost</code>那么连接就会走UNIX domain sockets而使用<code class="language-plaintext highlighter-rouge">127.0.0.1</code>就会和平常一样走TCP/IP sockets。 </p><p>
看完之后我感觉有那么点不爽啊然后就回去又把我的数据库地址改了回去不仅如此我还打算关掉3306端口后来查了一下发现在配置文件中加入<code class="language-plaintext highlighter-rouge">skip-networking</code>就可以了。 </p><p>
之后我查看了一下端口除了443端口和我的SSH的端口外还有一个奇怪的25端口看着感觉很不爽查了一下是postfix的端口然后我一个不爽就把postfix随手卸载掉了。 </p><p>
再往后我看了看我的Nginx日志发现大小有那么点不对劲才建的服务器日志已经有400-500MiB了虽然花火学园平时流量不算小但也不至于这么夸张吧后来我发现我没有过滤静态资源的日志之后又修改了一下配置文件心里就舒服多了😁。 </p><p>
于是,花火学园的优化就到此结束。</p>
<h1 id="后记">
<a href="#后记"><svg class='octicon' viewBox='0 0 16 16' version='1.1' width='16' height='32' aria-hidden='true'><path fill-rule='evenodd' d='M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z'></path></svg></a> 后记
</h1>
<p>我其实应该仔细看看日志的向莫名其妙出现的BUG说不定是攻击 <del>被害妄想症23333</del> 后来我翻了翻日志好像也没这个倾向。 </p><p>
另外我确实不应该把正在开发中的项目和主站放在同一个服务器,至少鸡蛋也不应该放到同一个篮子里面嘛……这次我打算做好主站的所有配置,保证它的正常运行。</p></main>
<small style="display: block">tags: <a rel="category tag" class="p-category" href="/search.html?keyword=%E8%BF%90%E7%BB%B4"><em>运维</em></a> - <a rel="category tag" class="p-category" href="/search.html?keyword=%E7%AC%94%E8%AE%B0"><em>笔记</em></a> <span style="float: right;"><a href="https://gitlab.com/mayx/mayx.gitlab.io/tree/master/_posts/2020-01-05-devops.md">查看原始文件</a></span></small>
<h4 style="border-bottom: 1px solid #e5e5e5;margin: 2em 0 5px;">推荐文章</h4>
<p id="suggest-container">Loading...</p>
<script>
var suggest = $("#suggest-container");
$.get(BlogAPI + "/suggest?id=/2020/01/05/devops.html&update=" + lastUpdated.valueOf(), function (data) {
if (data.length) {
getSearchJSON(function (search) {
suggest.empty();
var searchMap = {};
for (var i = 0; i < search.length; i++) {
searchMap[search[i].url] = search[i];
}
var tooltip = $('<div class="content-tooltip"></div>').appendTo('body').hide();
for (var j = 0; j < data.length; j++) {
var item = searchMap[data[j].id];
if (item) {
var link = $('<a href="' + item.url + '">' + item.title + '</a>');
var contentPreview = item.content.substring(0, 100);
if (item.content.length > 100) {
contentPreview += "……";
}
link.hover(
function(e) {
tooltip.text($(this).data('content'))
.css({
top: e.pageY + 10,
left: e.pageX + 10
})
.show();
},
function() {
tooltip.hide();
}
).mousemove(function(e) {
tooltip.css({
top: e.pageY + 10,
left: e.pageX + 10
});
}).data('content', contentPreview);
suggest.append(link);
suggest.append(' - ' + item.date + '<br />');
}
}
});
} else {
suggest.html("暂无推荐文章……");
}
});
</script>
<br />
<div class="pagination">
<span class="prev">
<a href="/2019/12/31/summary.html">
上一篇:年终总结
</a>
</span>
<br />
<span class="next">
<a href="/2020/01/21/devops.html">
下一篇Mayx的运维笔记 - 应用优化
</a>
</span>
</div>
<!--[if !IE]> -->
<link rel="stylesheet" href="/assets/css/gitalk.css">
<script src="/assets/js/gitalk.min.js"></script>
<div id="gitalk-container"></div>
<script>
var gitalk = new Gitalk({
clientID: '36557aec4c3cb04f7ac6',
clientSecret: 'ac32993299751cb5a9ba81cf2b171cca65879cdb',
repo: 'mabbs.github.io',
owner: 'Mabbs',
admin: ['Mabbs'],
id: '/2020/01/05/devops', // Ensure uniqueness and length less than 50
distractionFreeMode: false, // Facebook-like distraction free mode
proxy: "https://cors-anywhere.mayx.eu.org/?https://github.com/login/oauth/access_token"
})
gitalk.render('gitalk-container')
</script>
<!-- <![endif]-->
</section>
<!--[if !IE]> -->
<div id="landlord" style="left:5px;bottom:0px;">
<div class="message" style="opacity:0"></div>
<canvas id="live2d" width="500" height="560" class="live2d"></canvas>
<div class="live_talk_input_body">
<form id="live_talk_input_form">
<div class="live_talk_input_name_body" >
<input type="checkbox" id="load_this" />
<input type="hidden" id="post_id" value="/2020/01/05/devops.html" />
<label for="load_this">
<span style="font-size: 11px; color: #fff;">&#160;想问这篇文章</span>
</label>
</div>
<div class="live_talk_input_text_body">
<input name="talk" type="text" class="live_talk_talk white_input" id="AIuserText" autocomplete="off" placeholder="要和我聊什么呀?" />
<button type="submit" class="live_talk_send_btn" id="talk_send">发送</button>
</div>
</form>
</div>
<input name="live_talk" id="live_talk" value="1" type="hidden" />
<div class="live_ico_box" style="display:none;">
<div class="live_ico_item type_info" id="showInfoBtn"></div>
<div class="live_ico_item type_talk" id="showTalkBtn"></div>
<div class="live_ico_item type_music" id="musicButton"></div>
<div class="live_ico_item type_youdu" id="youduButton"></div>
<div class="live_ico_item type_quit" id="hideButton"></div>
<input name="live_statu_val" id="live_statu_val" value="0" type="hidden" />
<audio src="" style="display:none;" id="live2d_bgm" data-bgm="0" preload="none"></audio>
<input id="duType" value="douqilai" type="hidden" />
</div>
</div>
<div id="open_live2d">召唤伊斯特瓦尔</div>
<!-- <![endif]-->
<footer>
<p>
<small>Made with ❤ by Mayx<br />Last updated at 2026-02-04 10:55:07<br /> 总字数614622 - 文章数178 - <a href="/atom.xml" >Atom</a> - <a href="/README.html" >About</a></small>
</p>
</footer>
</div>
<script src="/assets/js/scale.fix.js"></script>
<!--[if !IE]> -->
<script src="/assets/js/main_new.js"></script>
<script src="/Live2dHistoire/live2d/js/live2d.js"></script>
<script src="/Live2dHistoire/live2d/js/message.js"></script>
<!-- <![endif]-->
</body>
</html>