<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>环境与服务 on 凡·间</title>
    <link>https://16bh.github.io/categories/%E7%8E%AF%E5%A2%83%E4%B8%8E%E6%9C%8D%E5%8A%A1/</link>
    <description>Recent content in 环境与服务 on 凡·间</description>
    <generator>Hugo</generator>
    <language>zh-cn</language>
    <lastBuildDate>Fri, 27 Apr 2018 14:28:17 +0800</lastBuildDate>
    <atom:link href="https://16bh.github.io/categories/%E7%8E%AF%E5%A2%83%E4%B8%8E%E6%9C%8D%E5%8A%A1/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>使用pecl终端命令安装php扩展</title>
      <link>https://16bh.github.io/2018/04/27/brew-php-7-0-and-pecl-install-php-extension/</link>
      <pubDate>Fri, 27 Apr 2018 14:28:17 +0800</pubDate>
      <guid>https://16bh.github.io/2018/04/27/brew-php-7-0-and-pecl-install-php-extension/</guid>
      <description>&lt;p&gt;brew不支持php70扩展的安装了，改用pecl&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-shell&#34; data-lang=&#34;shell&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;➜  conf.d brew search &lt;span class=&#34;nv&#34;&gt;php70&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;o&#34;&gt;==&lt;/span&gt;&amp;gt; Searching &lt;span class=&#34;nb&#34;&gt;local&lt;/span&gt; taps...
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;o&#34;&gt;==&lt;/span&gt;&amp;gt; Searching taps on GitHub...
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;o&#34;&gt;==&lt;/span&gt;&amp;gt; Searching blacklisted, migrated and deleted formulae...
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;It was migrated from josegonzalez/php to homebrew/core.
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
      <content:encoded><![CDATA[<p>brew不支持php70扩展的安装了，改用pecl</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">➜  conf.d brew search <span class="nv">php70</span>
</span></span><span class="line"><span class="cl"><span class="o">==</span>&gt; Searching <span class="nb">local</span> taps...
</span></span><span class="line"><span class="cl"><span class="o">==</span>&gt; Searching taps on GitHub...
</span></span><span class="line"><span class="cl"><span class="o">==</span>&gt; Searching blacklisted, migrated and deleted formulae...
</span></span><span class="line"><span class="cl">It was migrated from josegonzalez/php to homebrew/core.
</span></span></code></pre></div><h2 id="安装php70">安装PHP@7.0</h2>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">brew install php@7.0
</span></span></code></pre></div><p>安装完要配置环境变量,加入<code>.zshrc</code>文件</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl"><span class="nb">export</span> <span class="nv">PATH</span><span class="o">=</span><span class="s2">&#34;</span><span class="k">$(</span>brew --prefix php@7.0<span class="k">)</span><span class="s2">/sbin:</span><span class="nv">$PATH</span><span class="s2">&#34;</span>
</span></span><span class="line"><span class="cl"><span class="nb">export</span> <span class="nv">PATH</span><span class="o">=</span><span class="s2">&#34;/usr/local/bin:/usr/local/sbin:</span><span class="nv">$PATH</span><span class="s2">&#34;</span>
</span></span></code></pre></div><p>启动关闭的命令也变了</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">//启动php-fpm
</span></span><span class="line"><span class="cl">sudo php-fpm
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">//关闭php-fpm
</span></span><span class="line"><span class="cl">sudo pkill php-fpm
</span></span></code></pre></div><h2 id="安装pear">安装pear</h2>
<p>参考一下的教程，安装pear，安装完之后就可以使用pecl命令安装管理php扩展了</p>
<p><a href="https://www.jianshu.com/p/598c0fd84719">在Mac上安装pecl</a></p>
<h2 id="pecl命令">pecl命令</h2>
<ul>
<li>pecl search xxxx 查找扩展</li>
<li>pecl install xxxx 安装扩展</li>
<li>pecl list xxxx  查看已安装扩展列表</li>
</ul>
<h2 id="扩展的配置">扩展的配置</h2>
<p>以前brew安装php70的扩展都是放在conf.d文件夹下的，而现在php@7.0的扩展安装完之后之前会自动在<code>php.ini</code>文件中开启</p>
<p>以protobuf扩展为例，我们通过<code>pecl install protobuf</code>安装完成之后,在终端日志最后可以看到以下内容</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">Build process completed successfully
</span></span><span class="line"><span class="cl">Installing <span class="s1">&#39;/usr/local/Cellar/php@7.0/7.0.29_1/pecl/20151012/protobuf.so&#39;</span>
</span></span><span class="line"><span class="cl">install ok: channel://pecl.php.net/protobuf-3.5.1.1
</span></span><span class="line"><span class="cl">Extension protobuf enabled in php.ini
</span></span></code></pre></div><p><code>/usr/local/Cellar/php@7.0/7.0.29_1/pecl/20151012/protobuf.so</code>是扩展所在的目录</p>
<p>打开<code>php.ini</code>文件,
第一行新增了</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-ini" data-lang="ini"><span class="line"><span class="cl"><span class="na">extension</span><span class="o">=</span><span class="s">&#34;protobuf.so&#34;</span>
</span></span></code></pre></div><p>将其注释掉</p>
<p>在conf.d目录下新建文件<code>ext-protobuf.ini</code>，编辑如下：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-ini" data-lang="ini"><span class="line"><span class="cl"><span class="k">[protobuf]</span>
</span></span><span class="line"><span class="cl"><span class="na">extension</span><span class="o">=</span><span class="s">&#34;/usr/local/Cellar/php@7.0/7.0.29_1/pecl/20151012/protobuf.so&#34;</span>
</span></span></code></pre></div><h2 id="php的配置">php的配置</h2>
<p>brew安装的php70时，php.ini文件是不生效的
但是brew安装php@7.0，php.ini文件是生效的</p>
<h3 id="obcache">obcache</h3>
<p>默认打开了obcache，影响本地测试，需要关闭</p>
<p>修改php.ini</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-ini" data-lang="ini"><span class="line"><span class="cl"><span class="k">[opcache]</span>
</span></span><span class="line"><span class="cl"><span class="c1">; Determines if Zend OPCache is enabled</span>
</span></span><span class="line"><span class="cl"><span class="na">opcache.enable</span><span class="o">=</span><span class="s">0</span>
</span></span></code></pre></div><h2 id="补充这套流程在今天的-mac-上怎么走">补充：这套流程在今天的 Mac 上怎么走</h2>
<p>原文的坑主要来自当时 Homebrew 的两套机制并存（<code>php70</code> 与 <code>php@7.0</code>、brew 自带扩展与 pecl 扩展）。现在统一了，整个流程可以缩短成四步：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl"><span class="c1"># 1. 装 PHP（版本 formula 形式，php@7.0 早已停更，换 8.x）</span>
</span></span><span class="line"><span class="cl">brew install php@8.3
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1"># 2. 让 shell 用对 PHP（不要手写 /usr/local，Apple Silicon 是 /opt/homebrew）</span>
</span></span><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s1">&#39;export PATH=&#34;$(brew --prefix php@8.3)/bin:$PATH&#34;&#39;</span> &gt;&gt; ~/.zshrc
</span></span><span class="line"><span class="cl"><span class="nb">source</span> ~/.zshrc
</span></span><span class="line"><span class="cl">php -v
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1"># 3. 装扩展，优先用 brew，编译依赖少、升级省事</span>
</span></span><span class="line"><span class="cl">brew install imagemagick
</span></span><span class="line"><span class="cl">pecl install imagick
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1"># 4. 单独一个 ini，别改主 php.ini</span>
</span></span><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s1">&#39;extension=imagick.so&#39;</span> &gt; <span class="s2">&#34;</span><span class="k">$(</span>brew --prefix<span class="k">)</span><span class="s2">/etc/php/8.3/conf.d/ext-imagick.ini&#34;</span>
</span></span><span class="line"><span class="cl">php -m <span class="p">|</span> grep imagick
</span></span></code></pre></div><p>几个和原文不同的点：</p>
<ul>
<li><strong><code>pecl install</code> 不会再自动往 <code>php.ini</code> 里写 <code>extension=</code></strong>。老版本 pecl 会改主 ini，新版只提示 <code>You should add &quot;extension=xxx.so&quot; to php.ini</code>。原文“把它注释掉再建 conf.d”的绕法现在不需要了。</li>
<li><strong>路径前缀变了</strong>：Intel Mac 是 <code>/usr/local</code>，Apple Silicon 是 <code>/opt/homebrew</code>。写死 <code>/usr/local/Cellar/...</code> 的配置换机器就失效，用 <code>brew --prefix</code> 代替。</li>
<li><strong>php-fpm 不要用 <code>sudo</code> 起</strong>。用 <code>brew services start php@8.3</code> 管理，日志和 pid 都由 brew 负责；<code>sudo php-fpm</code> 起出来的进程属主是 root，写入日志和 session 目录时会出现权限错乱。</li>
<li><strong>opcache 在 CLI 下默认就是关的</strong>，只有 fpm 会按 ini 里的 <code>opcache.enable</code> 生效。本地要彻底关掉，改 <code>opcache.enable=0</code> 之外还要确认 <code>opcache.enable_cli=0</code>。</li>
</ul>
<hr>
<p><em>本文整理于 2018 年，PHP 7.0 已停止维护，文中路径与命令需按上方补充调整。</em></p>]]></content:encoded>
    </item>
    <item>
      <title>mysql datetime默认值设置为0000-00-00 00:00:00时报错</title>
      <link>https://16bh.github.io/2017/07/13/mysql-error-datetime-dafault-value-0000-00-00-00-00-00/</link>
      <pubDate>Thu, 13 Jul 2017 18:46:44 +0800</pubDate>
      <guid>https://16bh.github.io/2017/07/13/mysql-error-datetime-dafault-value-0000-00-00-00-00-00/</guid>
      <description>&lt;p&gt;出错原因：mysql5.7之后的版本默认设置不允许日期为空&lt;/p&gt;
&lt;p&gt;解决方法:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;修改mysql的sql mode&lt;/li&gt;
&lt;li&gt;默认值设置为非空值&lt;/li&gt;
&lt;/ul&gt;</description>
      <content:encoded><![CDATA[<p>出错原因：mysql5.7之后的版本默认设置不允许日期为空</p>
<p>解决方法:</p>
<ul>
<li>修改mysql的sql mode</li>
<li>默认值设置为非空值</li>
</ul>
<h2 id="查看sql-mode">查看sql mode</h2>
<pre tabindex="0"><code>SELECT @@GLOBAL.sql_mode;
SELECT @@SESSION.sql_mode;
</code></pre><h2 id="sql_mode常用值">sql_mode常用值</h2>
<ul>
<li>
<p>ONLY_FULL_GROUP_BY：
对于GROUP BY聚合操作，如果在SELECT中的列，没有在GROUP BY中出现，那么这个SQL是不合法的，因为列不在GROUP BY从句中</p>
</li>
<li>
<p>NO_AUTO_VALUE_ON_ZERO：
该值影响自增长列的插入。默认设置下，插入0或NULL代表生成下一个自增长值。如果用户 希望插入的值为0，而该列又是自增长的，那么这个选项就有用了。</p>
</li>
<li>
<p>STRICT_TRANS_TABLES：
在该模式下，如果一个值不能插入到一个事务表中，则中断当前的操作，对非事务表不做限制</p>
</li>
<li>
<p>NO_ZERO_IN_DATE：
在严格模式下，不允许日期和月份为零</p>
</li>
<li>
<p>NO_ZERO_DATE：
设置该值，mysql数据库不允许插入零日期，插入零日期会抛出错误而不是警告。</p>
</li>
<li>
<p>ERROR_FOR_DIVISION_BY_ZERO：
在INSERT或UPDATE过程中，如果数据被零除，则产生错误而非警告。如 果未给出该模式，那么数据被零除时MySQL返回NULL</p>
</li>
<li>
<p>NO_AUTO_CREATE_USER：
禁止GRANT创建密码为空的用户</p>
</li>
<li>
<p>NO_ENGINE_SUBSTITUTION：
如果需要的存储引擎被禁用或未编译，那么抛出错误。不设置此值时，用默认的存储引擎替代，并抛出一个异常</p>
</li>
<li>
<p>PIPES_AS_CONCAT：
将&quot;||&ldquo;视为字符串的连接操作符而非或运算符，这和Oracle数据库是一样的，也和字符串的拼接函数Concat相类似</p>
</li>
<li>
<p>ANSI_QUOTES：启用ANSI_QUOTES后，不能用双引号来引用字符串，因为它被解释为识别符</p>
</li>
</ul>
<h2 id="设置sql-mode">设置sql mode</h2>
<pre tabindex="0"><code>SET GLOBAL sql_mode = &#39;modes...&#39;;
SET SESSION sql_mode = &#39;modes...&#39;;
</code></pre><h2 id="补充完整错误长什么样怎么改才对">补充：完整错误长什么样，怎么改才对</h2>
<p>这个错误在后端日志里通常是这样一串：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">ERROR 1067 (42000): Invalid default value for &#39;created_at&#39;
</span></span></code></pre></div><p>或者在建表时：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sql" data-lang="sql"><span class="line"><span class="cl"><span class="k">CREATE</span><span class="w"> </span><span class="k">TABLE</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">(</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="n">id</span><span class="w"> </span><span class="nb">INT</span><span class="w"> </span><span class="k">PRIMARY</span><span class="w"> </span><span class="k">KEY</span><span class="w"> </span><span class="n">AUTO_INCREMENT</span><span class="p">,</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="n">created_at</span><span class="w"> </span><span class="n">DATETIME</span><span class="w"> </span><span class="k">NOT</span><span class="w"> </span><span class="k">NULL</span><span class="w"> </span><span class="k">DEFAULT</span><span class="w"> </span><span class="s1">&#39;0000-00-00 00:00:00&#39;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="p">);</span><span class="w">
</span></span></span></code></pre></div><p>MySQL 5.7 起默认开启了 <code>NO_ZERO_DATE</code> 与 <code>STRICT_TRANS_TABLES</code>，零日期不再被接受。三种改法，推荐程度从高到低：</p>
<p><strong>① 用 <code>NULL</code> 表达“未设置”（推荐）</strong></p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sql" data-lang="sql"><span class="line"><span class="cl"><span class="k">ALTER</span><span class="w"> </span><span class="k">TABLE</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="k">MODIFY</span><span class="w"> </span><span class="n">created_at</span><span class="w"> </span><span class="n">DATETIME</span><span class="w"> </span><span class="k">NULL</span><span class="w"> </span><span class="k">DEFAULT</span><span class="w"> </span><span class="k">NULL</span><span class="p">;</span><span class="w">
</span></span></span></code></pre></div><p>语义最清楚，也和大多数 ORM 的行为一致。统计时要 <code>IS NULL</code>，不要拿 <code>= 0</code> 去比。</p>
<p><strong>② 用 <code>CURRENT_TIMESTAMP</code> 做默认值</strong></p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sql" data-lang="sql"><span class="line"><span class="cl"><span class="k">ALTER</span><span class="w"> </span><span class="k">TABLE</span><span class="w"> </span><span class="n">t</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="k">MODIFY</span><span class="w"> </span><span class="n">created_at</span><span class="w"> </span><span class="n">DATETIME</span><span class="w"> </span><span class="k">NOT</span><span class="w"> </span><span class="k">NULL</span><span class="w"> </span><span class="k">DEFAULT</span><span class="w"> </span><span class="k">CURRENT_TIMESTAMP</span><span class="p">,</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="k">MODIFY</span><span class="w"> </span><span class="n">updated_at</span><span class="w"> </span><span class="n">DATETIME</span><span class="w"> </span><span class="k">NOT</span><span class="w"> </span><span class="k">NULL</span><span class="w"> </span><span class="k">DEFAULT</span><span class="w"> </span><span class="k">CURRENT_TIMESTAMP</span><span class="w"> </span><span class="k">ON</span><span class="w"> </span><span class="k">UPDATE</span><span class="w"> </span><span class="k">CURRENT_TIMESTAMP</span><span class="p">;</span><span class="w">
</span></span></span></code></pre></div><p><strong>③ 仅当兼容老代码时才临时放宽 sql_mode</strong></p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sql" data-lang="sql"><span class="line"><span class="cl"><span class="k">SET</span><span class="w"> </span><span class="k">SESSION</span><span class="w"> </span><span class="n">sql_mode</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">REPLACE</span><span class="p">(</span><span class="o">@@</span><span class="k">SESSION</span><span class="p">.</span><span class="n">sql_mode</span><span class="p">,</span><span class="w"> </span><span class="s1">&#39;NO_ZERO_DATE&#39;</span><span class="p">,</span><span class="w"> </span><span class="s1">&#39;&#39;</span><span class="p">);</span><span class="w">
</span></span></span></code></pre></div><p>注意 <code>SET SESSION</code> 只对当前连接有效、<code>SET GLOBAL</code> 只对新连接有效且重启后失效。永久生效要改 <code>my.cnf</code> 的 <code>[mysqld] sql_mode=</code>，<strong>而且会影响这个实例上的所有库</strong>——如果线上不止一个业务，这一步要评估。</p>
<p>原文列的那张 sql_mode 取值表值得留着当字典，但要提醒一点：<code>NO_AUTO_CREATE_USER</code> 在 MySQL 8.0 已被移除，写进 <code>sql_mode</code> 会让 8.0 起不来。</p>
<hr>
<p><em>本文整理于 2017 年，MySQL 5.7 时代；<code>NO_AUTO_CREATE_USER</code> 在 8.0 已移除。</em></p>]]></content:encoded>
    </item>
    <item>
      <title>crontab入门</title>
      <link>https://16bh.github.io/2017/07/12/get-started-to-crontab/</link>
      <pubDate>Wed, 12 Jul 2017 17:43:43 +0800</pubDate>
      <guid>https://16bh.github.io/2017/07/12/get-started-to-crontab/</guid>
      <description>&lt;p&gt;crontab用来定时执行脚本&lt;/p&gt;
&lt;p&gt;为什么要定时执行脚本：&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;异步处理，如下单赠品，计算用户会员等&lt;/li&gt;
&lt;li&gt;自动化，如整点时间让用户可以签到，凌晨五点恢复游戏内体力等。&lt;/li&gt;
&lt;/ul&gt;</description>
      <content:encoded><![CDATA[<p>crontab用来定时执行脚本</p>
<p>为什么要定时执行脚本：</p>
<ul>
<li>异步处理，如下单赠品，计算用户会员等</li>
<li>自动化，如整点时间让用户可以签到，凌晨五点恢复游戏内体力等。</li>
</ul>
<h2 id="crontab文件格式">crontab文件格式</h2>
<pre tabindex="0"><code>分钟 小时 日 月 星期 [command]
</code></pre><p>一条crontab执行的shell脚本分为6部分</p>
<ol>
<li>分钟,默认*</li>
<li>小时,默认*</li>
<li>日,默认*</li>
<li>月,默认*</li>
<li>星期,默认*</li>
<li>命令</li>
</ol>
<p>前面五个部分用于指定脚本的执行时间和频率，默认<code>* * * * *</code>代表每分钟执行一次</p>
<pre tabindex="0"><code>//输入精确时间
3 * * * * do_sth	   //每3分钟执行一次
//输入范围
* 5-23 * * * do_sth   //从凌晨5点到晚上11点每分钟执行一次（普通人作息时间）
//输入可选项
* * * * 6,7          //在周六和周日每分钟执行一次
</code></pre><h2 id="启用cron">启用cron</h2>
<p>新建一个cron文件 <code>$HOME/jimcron</code>
编辑如下：</p>
<pre tabindex="0"><code>#log time every min
* * * * * /bin/echo date &gt; ~/cron.log
</code></pre><p>然后执行下面的命令启用cron</p>
<pre tabindex="0"><code>crontab $HOME/jimcron
</code></pre><h2 id="查看所有cron">查看所有cron</h2>
<pre tabindex="0"><code>crontab -l
</code></pre><p><img alt="20170712149985458536665.png" loading="lazy" src="/2017/07/12/get-started-to-crontab/20170712149985458536665.png"></p>
<h2 id="cron备份与恢复">cron备份与恢复</h2>
<pre tabindex="0"><code>crontab -l &gt; $HOME/jimcron  //备份cron
crontab $HOME/jimcron	//恢复cron
</code></pre><h2 id="编辑当前用户的cron">编辑当前用户的cron</h2>
<pre tabindex="0"><code>crontab -e
</code></pre><h2 id="删除cron文件">删除cron文件</h2>
<pre tabindex="0"><code>crontab -r
</code></pre><h2 id="ubuntu下cron命令">ubuntu下cron命令</h2>
<pre tabindex="0"><code>sudo /etc/init.d/cron start	//启动
sudo /etc/init.d/cron stop 	//关闭
sudo /etc/init.d/cron restart		//重启
</code></pre><h2 id="crontab模拟工具">crontab模拟工具</h2>
<p><a href="https://tool.lu/crontab">https://tool.lu/crontab</a></p>
<p><img alt="image-20181015222003501" loading="lazy" src="/2017/07/12/get-started-to-crontab/image-20181015222003501.png"></p>
<h2 id="补充crontab-用之前要知道的几件事">补充：crontab 用之前要知道的几件事</h2>
<p>原文把格式讲清楚了，但漏掉了几个<strong>实际用起来一定会踩的坑</strong>。</p>
<h3 id="1-星号位置的直觉错误">1. 星号位置的直觉错误</h3>
<p>原文的例子里有这么一行：</p>
<pre tabindex="0"><code>3 * * * * do_sth   //每3分钟执行一次
</code></pre><p><strong>这是错的。</strong> 分钟位的 <code>3</code> 表示「每小时的第 3 分钟」，也就是每小时只执行一次，不是每 3 分钟。每 3 分钟要写成 <code>*/3 * * * *</code>。同理 <code>5-23</code> 是范围，<code>*/5</code> 才是步长。</p>
<h3 id="2-crontab-里的-path-和你终端里的不一样">2. crontab 里的 PATH 和你终端里的不一样</h3>
<p>这是最容易踩的坑：你在终端敲 <code>php</code> 能用，crontab 里报 <code>command not found</code>。原因是 cron 启动时只有 <code>/usr/bin:/bin</code> 这类极简 PATH，<strong>不会读你的 <code>.zshrc</code></strong>。</p>
<p>两种解法，选一种：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl"><span class="c1"># 写法一：命令写绝对路径</span>
</span></span><span class="line"><span class="cl">*/3 * * * * /opt/homebrew/bin/php /path/to/script.php
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1"># 写法二：在 crontab 顶部声明环境</span>
</span></span><span class="line"><span class="cl"><span class="nv">PATH</span><span class="o">=</span>/opt/homebrew/bin:/usr/bin:/bin
</span></span><span class="line"><span class="cl"><span class="nv">HOME</span><span class="o">=</span>/Users/your-name
</span></span></code></pre></div><p>Apple Silicon 上的 brew 在 <code>/opt/homebrew/bin</code>，Intel 是 <code>/usr/local/bin</code>——这个差异在 crontab 里尤其容易忘。</p>
<h3 id="3-分钟级是调度粒度不是保底频率">3. 「分钟级」是调度粒度，不是保底频率</h3>
<p>cron 是<strong>无状态</strong>的：它只看当前时间对不对得上表达式，不管上一次跑完没有。所以：</p>
<ul>
<li>一个跑 5 分钟的任务配成 <code>*/1</code>，会<strong>叠起来并发执行</strong></li>
<li>机器休眠唤醒后，错过的任务<strong>不会补跑</strong>（除非用 <code>anacron</code>）</li>
</ul>
<p>要防重叠，自己加锁：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">* * * * * flock -n /tmp/myjob.lock /path/to/job.sh
</span></span></code></pre></div><h3 id="4-macos-上的-crontab-需要授权">4. macOS 上的 crontab 需要授权</h3>
<p>从 macOS Catalina 起，cron 访问「完全磁盘访问」「文件和文件夹」需要手动授权，否则表现为任务执行了但读不到文件、静默失败。授权位置：<strong>系统设置 → 隐私与安全性 → 完全磁盘访问</strong>，把 <code>/usr/sbin/cron</code> 加进去。</p>
<h3 id="5-现在的替代方案">5. 现在的替代方案</h3>
<table>
	<thead>
			<tr>
					<th>场景</th>
					<th>建议</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td>个人机器上的简单定时</td>
					<td>crontab 依然够用，胜在零依赖</td>
			</tr>
			<tr>
					<td>需要日志、重试、错过补跑</td>
					<td><code>launchd</code>（macOS 原生）或 systemd timer（Linux）</td>
			</tr>
			<tr>
					<td>「每隔 N 秒」「任务间有依赖」</td>
					<td>crontab 做不到，用调度框架或自写的循环</td>
			</tr>
			<tr>
					<td>想要可视化</td>
					<td>原文提到的 tool.lu 可以继续用，用于校验表达式</td>
			</tr>
	</tbody>
</table>
<p>原文给的备份恢复两条命令（<code>crontab -l &gt; file</code> / <code>crontab file</code>）非常实用，<strong>建议把它加入版本控制</strong>，因为 <code>crontab -r</code> 没有回收站，敲错一次就全没了。</p>
<p><em>本文整理于 2017 年，2026 年 11 月补充。</em></p>]]></content:encoded>
    </item>
    <item>
      <title>从零搭建mac开发环境</title>
      <link>https://16bh.github.io/2017/05/10/rebuild-mac-develop-environment/</link>
      <pubDate>Wed, 10 May 2017 21:48:02 +0800</pubDate>
      <guid>https://16bh.github.io/2017/05/10/rebuild-mac-develop-environment/</guid>
      <description>&lt;p&gt;从零安装开发环境&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>从零安装开发环境</p>
<h1 id="系统设置">系统设置</h1>
<ul>
<li>登录apple账号</li>
<li>将不常用的app折叠到文件夹中
<img alt="20170628149862006734533.png" loading="lazy" src="/2017/05/10/rebuild-mac-develop-environment/20170628149862006734533.png"></li>
<li>设置快捷键</li>
<li>设置触摸板（三指拖拽功能在<code>辅助功能</code>中设置）</li>
<li>设置触发角
<img alt="20170624149830172871625.png" loading="lazy" src="/2017/05/10/rebuild-mac-develop-environment/20170624149830172871625.png"></li>
<li>下载已购项目</li>
<li>设置dock</li>
</ul>
<p><img alt="201706261498466531224.png" loading="lazy" src="/2017/05/10/rebuild-mac-develop-environment/201706261498466531224.png">
置于左侧
将窗口最小化为应用程序图标</p>
<h1 id="环境搭建">环境搭建</h1>
<h2 id="安装homebrew">安装homebrew</h2>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">ruby -e <span class="s2">&#34;</span><span class="k">$(</span>curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install<span class="k">)</span><span class="s2">&#34;</span>
</span></span></code></pre></div><h2 id="通过homebrew安装homebrew-cask">通过homebrew安装homebrew-cask</h2>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">brew tap caskroom/cask
</span></span></code></pre></div><blockquote>
<p>正常的软件通过appstore多dmg安装文件安装，正常渠道无法安装的采用brew cask</p>
</blockquote>
<h2 id="shadowsocksx和chrome">shadowsocksx和chrome</h2>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">brew cask install shadowsocksx
</span></span></code></pre></div><ul>
<li>开启shadowsocksx,登录chrome浏览器，同步书签和插件</li>
<li>安装alfred并安装chrome书签的workflow</li>
</ul>
<h2 id="git">git</h2>
<ul>
<li>安装xcode</li>
<li>安装git</li>
</ul>
<blockquote>
<p>安装了xcode后，在终端输入git命令即可安装git</p>
</blockquote>
<p><a href="https://github.com/xirong/my-git/blob/master/use-gitlab-github-together.md">怎样在一台电脑上同时使用公司 GitLab 和 Github 的服务</a></p>
<h2 id="iterm2和oh-my-zsh">iterm2和oh my zsh</h2>
<p>下载安装iterm2</p>
<p><a href="https://www.iterm2.com/downloads.html">https://www.iterm2.com/downloads.html</a></p>
<ul>
<li>安装oh my zsh</li>
</ul>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">sh -c <span class="s2">&#34;</span><span class="k">$(</span>curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh<span class="k">)</span><span class="s2">&#34;</span>
</span></span></code></pre></div><ul>
<li>安装wd和zsh-wakatime插件</li>
<li>下载powerline主题</li>
</ul>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">git clone git://github.com/jeremyFreeAgent/oh-my-zsh-powerline-theme ~/.ohmyzsh-powerline
</span></span><span class="line"><span class="cl"><span class="nb">cd</span> ~/.ohmyzsh-powerline
</span></span><span class="line"><span class="cl">cp powerline.zsh-theme ~/.oh-my-zsh/themes/powerline.zsh-theme
</span></span><span class="line"><span class="cl">rm -rf ~/.ohmyzsh-powerline
</span></span></code></pre></div><ul>
<li>
<p>设置主题</p>
</li>
<li>
<p>安装powerline等宽字体</p>
</li>
</ul>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">git clone https://github.com/powerline/fonts.git
</span></span><span class="line"><span class="cl"><span class="nb">cd</span> fonts
</span></span><span class="line"><span class="cl">./install.sh
</span></span><span class="line"><span class="cl"><span class="nb">cd</span> .. <span class="o">&amp;&amp;</span> rm -rf fonts
</span></span></code></pre></div><ul>
<li>
<p>设置等宽字体
Profiles - Text - Font</p>
</li>
<li>
<p>iterm2也可以使用<code>robbyrussell</code>主题和<code>Arthur</code>配色方案
（Laravel作者推荐）</p>
</li>
<li>
<p>使用zsh</p>
</li>
</ul>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">chsh -s /bin/zsh
</span></span></code></pre></div><h2 id="编译安装vim">编译安装vim</h2>
<h2 id="安装nginx">安装nginx</h2>
<pre><code>```brew install nginx```
</code></pre>
<ul>
<li>配置servers文件</li>
<li>设置pid和log路径</li>
</ul>
<h2 id="安装php">安装php</h2>
<pre><code>```brew install php70```
</code></pre>
<ul>
<li>
<p>设置环境变量</p>
</li>
<li>
<p>设置pid和log路径</p>
</li>
<li>
<p>安装扩展</p>
<p>编译安装</p>
</li>
</ul>
<h2 id="安装phpstorm">安装phpstorm</h2>
<ul>
<li>下载安装</li>
<li>导入配置（安装前导出备份）</li>
<li>安装必要的插件</li>
</ul>
<h2 id="安装redis">安装redis</h2>
<p><code>brew install redis</code></p>
<ul>
<li>
<p>查看启动命令</p>
<p><code>brew info redis</code></p>
</li>
<li>
<p>设置pid和log路径</p>
</li>
<li>
<p>关闭持久化</p>
</li>
</ul>
<h2 id="建议pid和log目录">建议pid和log目录</h2>
<p>pid: /usr/local/var/run
log: /usr/local/var/log</p>
<h2 id="安装mysql">安装mysql</h2>
<ul>
<li>下载</li>
</ul>
<p><a href="http://www.cnblogs.com/macro-cheng/archive/2011/10/25/mysql-001.html">下载安装：http://www.cnblogs.com/macro-cheng/archive/2011/10/25/mysql-001.html</a></p>
<blockquote>
<p>注意：安装的某一步中会给出mysql的密码，要记录一下，用户后面登录cli并修改密码</p>
</blockquote>
<ul>
<li>
<p>安装
<a href="http://www.jianshu.com/p/fd3aae701db9">配置：http://www.jianshu.com/p/fd3aae701db9
</a></p>
</li>
<li>
<p>修改mysql密码</p>
</li>
</ul>
<h2 id="tree">tree</h2>
<ul>
<li>安装tree</li>
</ul>
<p><code>brew install tree</code></p>
<h2 id="node">node</h2>
<ul>
<li>安装node</li>
</ul>
<p><code>brew install node</code></p>
<ul>
<li>用淘宝的源
<code>npm install -g cnpm --registry=https://registry.npm.taobao.org</code>
然后就可以用cnpm代替npm安装包了</li>
</ul>
<h1 id="重要软件">重要软件</h1>
<ul>
<li>安装reeder，登录inoreader账号</li>
<li>安装quiver、paste、yonik、bartender、moom、dash等常用软件
<img alt="20170626149846649474604.png" loading="lazy" src="/2017/05/10/rebuild-mac-develop-environment/20170626149846649474604.png"></li>
</ul>
<h1 id="设置开启启动app">设置开启启动app</h1>
<p>系统偏好设置-用户和群组-登录项</p>
<p><img alt="20170626149846660040418.png" loading="lazy" src="/2017/05/10/rebuild-mac-develop-environment/20170626149846660040418.png"></p>
<h1 id="其他">其他</h1>
<ul>
<li>安装hexo</li>
</ul>
<blockquote>
<p>前提是安装了node</p>
</blockquote>
<p><a href="https://www.zhihu.com/question/21193762">迁移hexo</a></p>
<h2 id="补充这篇流程今天还值得照着走吗">补充：这篇流程今天还值得照着走吗</h2>
<p>这是一篇「换新 Mac 时的清单」，流程本身的价值还在——<strong>先配环境再用机器，比边用边补要快得多</strong>。但其中过半命令已经失效。</p>
<h3 id="已经失效的步骤">已经失效的步骤</h3>
<table>
	<thead>
			<tr>
					<th>原文</th>
					<th>现状</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td><code>ruby -e &quot;$(curl -fsSL .../Homebrew/install/master/install)&quot;</code></td>
					<td><strong>Ruby 一次性安装脚本已下线</strong>（Homebrew 官方宣布 Ruby 版安装方式停止支持）。现在用 <code>/bin/bash -c &quot;$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)&quot;</code></td>
			</tr>
			<tr>
					<td><code>brew tap caskroom/cask</code>、<code>brew cask install xxx</code></td>
					<td><strong>cask 早已并入 Homebrew 主仓库</strong>，不再需要 tap；<code>brew cask</code> 子命令改成了 <code>brew install --cask xxx</code></td>
			</tr>
			<tr>
					<td><code>brew install php70</code></td>
					<td>php 各版本 formula 陆续移除，今天装 PHP 走 <code>brew install php</code>（当前主版本）或 <code>shivammathur/php</code> tap</td>
			</tr>
			<tr>
					<td><code>npm install -g cnpm --registry=https://registry.npm.taobao.org</code></td>
					<td><strong>taobao 域名已停用</strong>，改为 <code>https://registry.npmmirror.com</code>。而且今天大多数场景不需要 cnpm，配个镜像源即可</td>
			</tr>
			<tr>
					<td><code>git clone git://github.com/...</code></td>
					<td><strong><code>git://</code> 协议在 2022 年被 GitHub 关闭</strong>，必须换成 <code>https://</code></td>
			</tr>
			<tr>
					<td><code>brew tap homebrew/php</code>、<code>brew tap homebrew/dupes</code></td>
					<td>这些 tap 均已在 2018 年前后废弃</td>
			</tr>
	</tbody>
</table>
<h3 id="需要换位置的系统设置">需要换位置的系统设置</h3>
<ul>
<li>「三指拖拽」在 <strong>系统设置 → 辅助功能 → 指针控制 → 触控板选项</strong> 里，和原文的「辅助功能」路径一致，但面板名字从「系统偏好设置」改成了「系统设置」（macOS 13 起）。</li>
<li>「登录项」在 <strong>系统设置 → 通用 → 登录项</strong>，不再是「用户与群组」下的标签页。</li>
<li>「触发角」在 <strong>系统设置 → 桌面与程序坞 → 触发角</strong>。</li>
</ul>
<h3 id="今天我会怎么排这个清单">今天我会怎么排这个清单</h3>
<ol>
<li><strong>先装 Homebrew，再装其它一切</strong>。brew 是后续所有安装的基础。</li>
<li><strong>跳过「编译安装 vim」</strong>。用 Neovim 或 <code>brew install vim</code> 即可，源码编译在 Apple Silicon 上还要额外处理架构。</li>
<li><strong>PHP 扩展不要一个个 <code>brew install phpXX-xxx</code></strong>，用 <code>pecl install</code> 或 <code>brew install php</code> 自带的扩展，避免版本对不上。</li>
<li><strong>软件用 <code>Brewfile</code> 统一管理</strong>，比手敲几十条命令靠谱：</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">brew bundle dump --file<span class="o">=</span>~/Brewfile    <span class="c1"># 旧机器导出</span>
</span></span><span class="line"><span class="cl">brew bundle install --file<span class="o">=</span>~/Brewfile  <span class="c1"># 新机器还原</span>
</span></span></code></pre></div><p>这一条是原文明明可以做到、但当时还没普及的做法：<strong>机器配置也应该进版本库</strong>。</p>
<p><em>本文整理于 2017 年，2026 年 11 月补充。</em></p>]]></content:encoded>
    </item>
    <item>
      <title>通过package安装包安装mysql</title>
      <link>https://16bh.github.io/2017/04/13/install-mysql-via-package/</link>
      <pubDate>Thu, 13 Apr 2017 18:46:08 +0800</pubDate>
      <guid>https://16bh.github.io/2017/04/13/install-mysql-via-package/</guid>
      <description>&lt;p&gt;通过brew安装mysql是很简单的，但是在使用的时候经常出现奇怪的错误
，比如这样的&lt;code&gt;The server quit without updating PID file&lt;/code&gt;,或者这样的&lt;/p&gt;
&lt;p&gt;改用package安装&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>通过brew安装mysql是很简单的，但是在使用的时候经常出现奇怪的错误
，比如这样的<code>The server quit without updating PID file</code>,或者这样的</p>
<p>改用package安装</p>
<h2 id="下载安装包">下载安装包</h2>
<p>访问MySQL的官网<a href="http://www.mysql.com/downloads/">http://www.mysql.com/downloads/</a> 然后在页面中会看到“MySQL Community Server”下方有一个“download”点击</p>
<h2 id="设置新密码">设置新密码</h2>
<pre tabindex="0"><code>set PASSWORD = PASSWORD(&#39;new_password&#39;);
</code></pre><p>通过pkg安装的mysql会在mac的偏好设置面板里出现哦</p>
<h2 id="补充装完之后第一件事不是改密码">补充：装完之后第一件事不是改密码</h2>
<p>原文是通过官方 dmg 包安装 MySQL。补充几条装完必做、但原文没提的：</p>
<p><strong>1. 系统级 MySQL 和 brew 装的会打架。</strong> 官方包安装在 <code>/usr/local/mysql</code>，并且会注册一个 <code>LaunchDaemon</code> 开机自启。如果之前用 brew 装过，两者会争 3306 端口，表现为&quot;刚装的 MySQL 起不来&quot;。检查：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">lsof -nP -iTCP:3306 -sTCP:LISTEN
</span></span><span class="line"><span class="cl">sudo launchctl list <span class="p">|</span> grep -i mysql
</span></span></code></pre></div><p><strong>2. 8.0 的默认认证插件变了。</strong> 老客户端（以及很多 PHP 7.x 编译出来的 mysqlnd）连 MySQL 8 会报 <code>caching_sha2_password</code> 相关错误。解决方式要么升级客户端，要么把用户改成 <code>mysql_native_password</code>（新版 MySQL 已不推荐）。</p>
<p><strong>3. 初始化密码在安装界面的弹窗里</strong>，一旦关掉就找不回来了，只能改配置文件重置。装之前先准备好记录的地方。</p>
<p><strong>4. 数据目录在 <code>/usr/local/mysql/data</code></strong>，卸载不等于删除数据，重装前确认要不要备份。</p>
<p><em>本文整理于 2017 年，2026 年 11 月补写。</em></p>
<h2 id="补充这篇文章的结论今天还成立吗">补充：这篇文章的结论今天还成立吗</h2>
<h3 id="1-核心现象依然存在但原因不同">1. 核心现象依然存在，但原因不同</h3>
<p>原文说「brew 装 mysql 经常报 <code>The server quit without updating PID file</code>，所以改用官方安装包」。这个<strong>现象今天依然存在</strong>，但根因比「brew 不好」更具体：</p>
<ul>
<li>机器上曾经装过另一个 MySQL（旧 brew 版本、MAMP、官方包）→ <strong>数据目录冲突</strong>，新实例找不到自己的 <code>mysql</code> 库。</li>
<li>老版本的 <code>my.cnf</code> 残留（<code>/etc/my.cnf</code>、<code>/usr/local/etc/my.cnf</code>）指向了不存在的路径。</li>
<li>权限问题：数据目录属主不对，mysqld 无法写入。</li>
</ul>
<p><strong>也就是说，换用官方安装包并没有解决根因，只是绕开了冲突。</strong> 如果继续用 brew，正确做法是先彻底清干净：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">brew services stop mysql
</span></span><span class="line"><span class="cl">rm -rf /opt/homebrew/var/mysql      <span class="c1"># 确认这里没有你要的数据！</span>
</span></span><span class="line"><span class="cl">brew install mysql
</span></span><span class="line"><span class="cl">brew services start mysql
</span></span></code></pre></div><h3 id="2-password-函数已被移除">2. <code>PASSWORD()</code> 函数已被移除</h3>
<p>原文的改密码语句：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sql" data-lang="sql"><span class="line"><span class="cl"><span class="k">set</span><span class="w"> </span><span class="n">PASSWORD</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PASSWORD</span><span class="p">(</span><span class="s1">&#39;new_password&#39;</span><span class="p">);</span><span class="w">
</span></span></span></code></pre></div><p><code>PASSWORD()</code> 函数在 <strong>MySQL 5.7.6 被废弃，8.0 彻底移除</strong>。今天的写法：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sql" data-lang="sql"><span class="line"><span class="cl"><span class="k">ALTER</span><span class="w"> </span><span class="k">USER</span><span class="w"> </span><span class="s1">&#39;root&#39;</span><span class="o">@</span><span class="s1">&#39;localhost&#39;</span><span class="w"> </span><span class="n">IDENTIFIED</span><span class="w"> </span><span class="k">BY</span><span class="w"> </span><span class="s1">&#39;new_password&#39;</span><span class="p">;</span><span class="w">
</span></span></span></code></pre></div><h3 id="3-官方安装包的现状">3. 官方安装包的现状</h3>
<ul>
<li>MySQL 官网仍提供 macOS 的 <code>.dmg</code>（内含 <code>.pkg</code>），安装后会出现在「系统设置」底部。</li>
<li><strong>Apple Silicon 需要选 arm64 包</strong>，选错会出现「无法打开，因为无法验证开发者」或以 Rosetta 运行。</li>
<li>官方包不会自动加 PATH，需要在 <code>~/.zshrc</code> 里加 <code>/usr/local/mysql/bin</code>。</li>
</ul>
<h3 id="4-更推荐的第三条路">4. 更推荐的第三条路</h3>
<p>无论 brew 还是官方包，都会污染系统环境。今天更干净的选择是：</p>
<ul>
<li><strong>Docker</strong>：<code>docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=xxx mysql:8</code>，用完即删，不碰宿主机。</li>
<li><strong><code>dbngin</code> / <code>OrbStack</code></strong>：macOS 上图形化管理多个数据库版本。</li>
</ul>
<p>如果这篇文章让你「每次重装都要折腾一遍」，那容器才是真正的解法。</p>
<p><em>本文整理于 2017 年，2026 年 11 月补充。</em></p>]]></content:encoded>
    </item>
    <item>
      <title>[mysql error]The server quit without updating PID file </title>
      <link>https://16bh.github.io/2017/04/12/mysql-error-the-server-quit-without-updating-pid-file/</link>
      <pubDate>Wed, 12 Apr 2017 19:06:00 +0800</pubDate>
      <guid>https://16bh.github.io/2017/04/12/mysql-error-the-server-quit-without-updating-pid-file/</guid>
      <description>&lt;p&gt;mysql是通过homebrew安装的,重启电脑后启动mysql报错&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>mysql是通过homebrew安装的,重启电脑后启动mysql报错</p>
<p>update: 2017年7月11日17:42</p>
<p>终极解决方案：
用brew安装mysql经常会碰到这些问题，直接用安装包安装后，从未复现</p>
<p>debug前须知：
mysql配置文件依次读取：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-ini" data-lang="ini"><span class="line"><span class="cl"><span class="na">/etc/my.cnf</span>
</span></span><span class="line"><span class="cl"><span class="na">/etc/mysql/my.cnf</span>
</span></span><span class="line"><span class="cl"><span class="na">~/my.cnf</span>
</span></span></code></pre></div><p><img alt="2017041214919952361252.png" loading="lazy" src="/2017/04/12/mysql-error-the-server-quit-without-updating-pid-file/2017041214919952361252.png"></p>
<p>网上提供的解决方法：</p>
<h2 id="查看报错信息有两处报错">查看报错信息，有两处报错</h2>
<p>err文件位于<code>/usr/local/var/mysql/data</code>
<img alt="20170412149199540435226.png" loading="lazy" src="/2017/04/12/mysql-error-the-server-quit-without-updating-pid-file/20170412149199540435226.png"></p>
<pre><code>- Can't open the mysql.plugin table. Please run mysql_upgrade to create it.
按照提示运行`mysql_upgrade`报错
</code></pre>
<p><img alt="20170412149199546335602.png" loading="lazy" src="/2017/04/12/mysql-error-the-server-quit-without-updating-pid-file/20170412149199546335602.png">
​    &gt;mysql_upgrade: Got error: 2002: Can&rsquo;t connect to local MySQL server through socket &lsquo;/tmp/mysql.sock&rsquo; (2) while connecting to the MySQL server
Upgrade process encountered error and will not continue.</p>
<pre><code>- Fatal error: Can't open and lock privilege tables: Table 'mysql.user' doesn't exist
</code></pre>
<h2 id="检查是否有实例正在运行">检查是否有实例正在运行</h2>
<pre tabindex="0"><code>ps -ef | grep mysql
</code></pre><p><img alt="20170412149199567567857.png" loading="lazy" src="/2017/04/12/mysql-error-the-server-quit-without-updating-pid-file/20170412149199567567857.png">
结果：没有</p>
<h2 id="删除err文件">删除.err文件</h2>
<p><img alt="20170412149199529841695.png" loading="lazy" src="/2017/04/12/mysql-error-the-server-quit-without-updating-pid-file/20170412149199529841695.png">
结果：无效</p>
<h2 id="改变权限">改变权限</h2>
<pre tabindex="0"><code>sudo chown -R _mysql:mysql /usr/local/var/mysql
</code></pre><p>结果：无效</p>
<h2 id="重新初始化">重新初始化</h2>
<pre tabindex="0"><code>unset TMPDIR
mysql_install_db --verbose --user=`whoami` --basedir=&#34;$(brew --prefix mysql)&#34; --datadir=/usr/local/var/mysql/data --tmpdir=/tmp
</code></pre><p><img alt="20170412149199560432948.png" loading="lazy" src="/2017/04/12/mysql-error-the-server-quit-without-updating-pid-file/20170412149199560432948.png"></p>
<p>因为/usr/local/var/mysql/data目录不为空，所以没法初始化，那么先清空目录，再初始化</p>
<pre tabindex="0"><code>unset TMPDIR
cd /usr/local/var/mysql/data
sudo rm *
mysql_install_db
</code></pre><p>再运行<code>sudo mysql.server start</code>命令启动，依然报错，查看目录文件权限，原来初始化的时候又改变权限了</p>
<pre tabindex="0"><code>sudo chown -R _mysql:mysql /usr/local/var/mysql
</code></pre><p><img alt="2017041214919969007399.png" loading="lazy" src="/2017/04/12/mysql-error-the-server-quit-without-updating-pid-file/2017041214919969007399.png"></p>
<p>DONE！</p>
<p>brew services start mysql与mysql.server start</p>
<p>2017-04-12T12:00:55.701485Z 1 [Note] A temporary password is generated for root@localhost: ZCBp1r-K0RlE</p>
<p>If you lose this password, please consult the section How to Reset the Root Password in the MySQL reference manual.</p>
<h2 id="补充先看-err-日志别先清数据目录">补充：先看 err 日志，别先清数据目录</h2>
<p>原文的排查顺序是“试各种江湖办法 → 最后清空数据目录重新初始化”。这里有个风险：</p>
<p><strong><code>cd /usr/local/var/mysql/data &amp;&amp; sudo rm *</code> 会删掉整个数据库目录。</strong> 原文之所以能这么干，是因为那是本地练习库；如果有真实数据，这一步直接毁库。正确的入口永远只有一个——先读错误日志。</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl"><span class="c1"># 1. 错误日志在哪（brew 装的 MySQL 通常在 datadir 下）</span>
</span></span><span class="line"><span class="cl">brew --prefix mysql
</span></span><span class="line"><span class="cl">ls -lt /usr/local/var/mysql/*.err   <span class="c1"># Apple Silicon: /opt/homebrew/var/mysql</span>
</span></span><span class="line"><span class="cl">tail -50 /usr/local/var/mysql/*.err
</span></span></code></pre></div><p>对照日志查根因：</p>
<table>
	<thead>
			<tr>
					<th>日志里的关键字</th>
					<th>真正原因</th>
					<th>处理</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td><code>Can't open and lock privilege tables</code></td>
					<td>数据目录被别的东西覆盖过，或 <code>basedir</code> 指错</td>
					<td>检查 <code>my.cnf</code> 的 <code>datadir</code></td>
			</tr>
			<tr>
					<td><code>Another process with pid … is using unix socket file</code></td>
					<td>上次异常退出，<code>.pid</code> / <code>.sock</code> 残留</td>
					<td>删 <code>*.pid</code>、<code>*.sock</code> 后重启，<strong>不要删数据</strong></td>
			</tr>
			<tr>
					<td><code>Permission denied</code> / <code>Operating system error number 13</code></td>
					<td>目录属主不是 <code>_mysql</code></td>
					<td><code>sudo chown -R _mysql:mysql &lt;datadir&gt;</code></td>
			</tr>
			<tr>
					<td><code>Table 'mysql.user' doesn't exist</code></td>
					<td>初始化没跑完</td>
					<td>重新 <code>mysqld --initialize</code> <strong>之前先备份现有数据</strong></td>
			</tr>
			<tr>
					<td><code>Version mismatch</code> / <code>unknown variable</code></td>
					<td><code>my.cnf</code> 里写了新版不认的参数</td>
					<td>逐条注释掉试</td>
			</tr>
	</tbody>
</table>
<p>原文遇到的 <code>Can't open the mysql.plugin table</code> 加 <code>Table 'mysql.user' doesn't exist</code> 同时出现，说明<strong>系统表整体缺失</strong>，也就是初始化没做成，改权限、删 err 文件都不会有用——这才是原文“试了都不管用”的原因，不是玄学。</p>
<p><code>mysql_upgrade</code> 是 MySQL 5.x 的工具，<strong>8.0 起已移除</strong>，现在升级表结构是 <code>mysqld --upgrade=FORCE</code> 或者干脆重装。原文最后那句“改用安装包安装后再没复现”，本质是把 Homebrew 的权限/初始化问题绕过去了，换个工具而已。</p>
<hr>
<p><em>本文整理于 2017 年，MySQL 版本为 5.7 时代，命令与工具在 8.0 后已有变化。</em></p>]]></content:encoded>
    </item>
    <item>
      <title>关于缓存的一些思考</title>
      <link>https://16bh.github.io/2017/04/12/my-thinking-about-redis/</link>
      <pubDate>Wed, 12 Apr 2017 14:12:41 +0800</pubDate>
      <guid>https://16bh.github.io/2017/04/12/my-thinking-about-redis/</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;性能有问题怎么办，加机器，做缓存&lt;/p&gt;
&lt;/blockquote&gt;</description>
      <content:encoded><![CDATA[<blockquote>
<p>性能有问题怎么办，加机器，做缓存</p>
</blockquote>
<h2 id="redis和memcached">redis和memcached</h2>
<p>区别：memcached只支持简单的key-value格式，redis支持更多的格式，如hash,</p>
<h1 id="什么时候要用到缓存">什么时候要用到缓存</h1>
<p>列表页、详情页等经常请求而且变化频率不高的场景</p>
<h1 id="缓存基本用法">缓存基本用法</h1>
<ol>
<li>从缓存中取数据</li>
<li>缓存命中，直接返回</li>
<li>缓存miss，从db中取数据，将数据存入缓存，返回</li>
</ol>
<h1 id="缓存穿透">缓存穿透</h1>
<p>指的是访问缓存中的某个不存在的key，没有命中，然后直接请求db，同时因为返回的结果为空，不会存入缓存中，故而下次还是无法命中的场景</p>
<p>解决方案：将不存在的key也存入缓存中，value设置为null</p>
<h1 id="持久化">持久化</h1>
<p>这个好像是运维做的，生产环境没有试过</p>
<p>不过在设计技术方案的时候，都要考虑redis挂掉的风险</p>
<h1 id="缓存并发">缓存并发</h1>
<p>redis有原子性操作，可以防止并发的时候读写出错</p>
<h1 id="缓存过期时间">缓存过期时间</h1>
<p>存入缓存时可以设置缓存的过期时间</p>
<p>有一个要注意的地方在于如果有一批过期时间相同的key同时过期并请求db时，会使db的压力骤增，如果扛不住，就会导致缓存雪崩</p>
<p>处理方法：设置过期时间时加上一个随机的小数字</p>
<h1 id="缓存是否过期">缓存是否过期</h1>
<p>是否给缓存设置过期时间，也是个值得思考的问题？</p>
<p>一般有两种思路：
一种是设置过期时间，到期后重新生成新的db
另一种是不设置过期时间，手动更新缓存</p>
<p>一般是采用第一种，因为redis存储的空间是有限的</p>
<p>此外，一些需要持久的缓存是不会设置过期时间的，如最新的订单号等</p>
<h1 id="redis缓存的其他应用场景">redis缓存的其他应用场景</h1>
<h2 id="队列">队列</h2>
<p>使用<code>redis</code>可以构建简单的一对一的队列，简单的说，就是使用<code>lpush</code>入列，<code>rpop</code>出列（也可以右进左出）</p>
<h2 id="数据库">数据库</h2>
<p>是的，你没有看错，<code>redis</code>除了当缓存使用，作为<code>NoSql</code>的成员之一，它还可以当独立的数据库使用</p>
<h2 id="代理">代理</h2>
<p>开源的tw框架</p>
<h1 id="参考">参考</h1>
<blockquote>
<p><a href="http://mp.weixin.qq.com/s?__biz=MzA5Nzc4OTA1Mw==&amp;mid=2659597537&amp;idx=1&amp;sn=9c91d231315b507b5eaea0e465a01423&amp;scene=21#wechat_redirect">缓存穿透、并发和失效，来自一线架构师的解决方案</a></p>
<p><a href="http://mp.weixin.qq.com/s/CCRa-qbgnNYSI4b10q4F9g">如何解决常见的缓存穿透、并发和失效问题？</a></p>
</blockquote>
<h2 id="补充缓存那几问今天仍是最常被追问的">补充：缓存那几问，今天仍是最常被追问的</h2>
<p>原文把缓存的几个基本问题列了出来（穿透、雪崩、持久化、一致性），但没有展开。这几条在面试和线上排障里仍然是最高频的，补上判断标准：</p>
<h3 id="穿透--击穿--雪崩三者别混">穿透 / 击穿 / 雪崩，三者别混</h3>
<table>
	<thead>
			<tr>
					<th>现象</th>
					<th>根因</th>
					<th>常见解法</th>
					<th>解法的代价</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td><strong>穿透</strong></td>
					<td>查的是数据库里也不存在的 key</td>
					<td>缓存空值；布隆过滤器</td>
					<td>空值缓存要设短 TTL；布隆过滤器有假阳性</td>
			</tr>
			<tr>
					<td><strong>击穿</strong></td>
					<td><strong>单个</strong>热点 key 过期瞬间，大量请求同时落到库上</td>
					<td>互斥锁回源；逻辑过期</td>
					<td>互斥锁会牺牲一部分可用性</td>
			</tr>
			<tr>
					<td><strong>雪崩</strong></td>
					<td><strong>大批</strong> key 同时过期，或缓存整体不可用</td>
					<td>TTL 加随机抖动；多级缓存；限流降级</td>
					<td>抖动只能缓解同时过期，挡不住整体挂掉</td>
			</tr>
	</tbody>
</table>
<p>原文说的&quot;不存在的 key 也存 null&quot;只是穿透的一种解法，而且<strong>必须设短 TTL</strong>——否则有人用随机 key 刷接口，会把内存打满。</p>
<h3 id="缓存与数据库的一致性">缓存与数据库的一致性</h3>
<p>原文没提这一块，但它是实践中最容易出问题的部分。业界常用的做法是 <strong>Cache Aside</strong>：</p>
<ol>
<li>读：先读缓存，miss 再读库，然后回填缓存。</li>
<li>写：<strong>先更新数据库，再删除缓存</strong>（不是更新缓存）。</li>
</ol>
<p>为什么是&quot;删除&quot;而不是&quot;更新&quot;：并发写时更新缓存会出现两个请求的写入顺序与数据库相反，缓存里留下旧值；删除则让下一次读自然回填。</p>
<p>为什么是&quot;先库后删&quot;：如果反过来（先删缓存再写库），在删除到写库之间的窗口里，其他请求会把旧值重新读进缓存，然后一直错到过期。</p>
<p>即便如此，这个模式仍有极小概率不一致窗口，所以<strong>缓存必须有过期时间兜底</strong>，把它当&quot;最终一致&quot;而不是&quot;强一致&quot;。</p>
<h3 id="持久化-1">持久化</h3>
<p>RDB 是快照，AOF 是追加日志。两个关键事实：</p>
<ul>
<li><strong>RDB 会 fork 子进程</strong>，写时复制（COW）期间内存可能翻倍，这是&quot;Redis 内存突然涨了一倍&quot;的常见原因。</li>
<li><strong>AOF 的 <code>everysec</code> 最多丢 1 秒数据</strong>，<code>always</code> 每条都刷盘但吞吐骤降。生产上 <code>everysec</code> 是默认选择。</li>
</ul>
<p>Redis 4.0 起有混合持久化（<code>aof-use-rdb-preamble yes</code>），重启恢复快且数据丢失少，是现在的新装推荐配置。</p>
<p><em>本文整理于 2017-04，2026-11 补写。原文的问题清单依然成立，补充的是判断标准与代价权衡。</em></p>]]></content:encoded>
    </item>
    <item>
      <title>[xml error]Unescaped &amp; or nonterminated character/entity reference</title>
      <link>https://16bh.github.io/2017/04/01/xml-error-unescaped-or-nonterminated-character-entity-reference/</link>
      <pubDate>Sat, 01 Apr 2017 15:48:36 +0800</pubDate>
      <guid>https://16bh.github.io/2017/04/01/xml-error-unescaped-or-nonterminated-character-entity-reference/</guid>
      <description>&lt;p&gt;xml的某个标签中的值包含了&amp;amp;，会报下面的错误
&lt;code&gt;Unescaped &amp;amp; or nonterminated character/entity reference&lt;/code&gt;&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>xml的某个标签中的值包含了&amp;，会报下面的错误
<code>Unescaped &amp; or nonterminated character/entity reference</code></p>
<p>网上流传的解决方法如下：</p>
<p>用<code>&amp;amp;</code>代替<code>&amp;</code></p>
<p>但是stackoverflow上有个小哥表示自己不想这么干，就发了个帖子：<a href="http://stackoverflow.com/questions/3493405/do-i-really-need-to-encode-as-amp">《我一定要用<code>&amp;amp;</code>替换<code>&amp;</code>符号吗》</a></p>
<p>下面是大佬的回复：</p>
<blockquote>
</blockquote>
<p>Yes. Just as the error said, in HTML, attributes are #PCDATA meaning they&rsquo;re parsed. This means you can use character entities in the attributes. Using &amp; by itself is wrong and if not for lenient browsers and the fact that this is HTML not XHTML, would break the parsing. Just escape it as &amp; and everything would be fine.</p>
<blockquote>
<p>HTML5 allows you to leave it unescaped, but only when the data that follows does not look like a valid character reference. However, it&rsquo;s better just to escape all instances of this symbol than worry about which ones should be and which ones don&rsquo;t need to be.</p>
</blockquote>
<blockquote>
<p>Keep this point in mind; if you&rsquo;re not escaping &amp; to &amp;, it&rsquo;s bad enough for data that you create (where the code could very well be invalid), you might also not be escaping tag delimiters, which is a huge problem for user-submitted data, which could very well lead to HTML and script injection, cookie stealing and other exploits.</p>
</blockquote>
<blockquote>
<p>Please just escape your code. It will save you a lot of trouble in the future.</p>
</blockquote>
<h2 id="补充这个错误的本质是裸的-">补充：这个错误的本质是&quot;裸的 &amp;&quot;</h2>
<p>原文讲的是 XML 里的 <code>Unescaped or nonterminated character entity reference</code>。这个报错只有一种根因：<strong>正文里出现了 <code>&amp;</code>，但不是合法实体（<code>&amp;amp;</code>、<code>&amp;lt;</code> 这种）的开头</strong>。</p>
<p>最常见的三种场景：</p>
<ol>
<li><strong>URL 里的参数没转义</strong>：<code>&lt;link&gt;https://example.com/?a=1&amp;b=2&lt;/link&gt;</code> —— 这里的 <code>&amp;b</code> 会被当成实体开头。</li>
<li><strong>手写 <code>&amp;</code> 又忘了分号</strong>：<code>&amp;nbsp</code> 少了 <code>;</code>。</li>
<li><strong>中文标点被误输入</strong>：全角 <code>＆</code> 其实没问题，但复制粘贴时常混进半角。</li>
</ol>
<p>修法就是转义成 <code>&amp;amp;</code>。命令行快速定位：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">xmllint --noout 文件.xml
</span></span></code></pre></div><p>会直接给出<strong>行号</strong>，比在浏览器里看一片红快得多。批量检查一个目录：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">find . -name <span class="s2">&#34;*.xml&#34;</span> -exec xmllint --noout <span class="o">{}</span> <span class="se">\;</span>
</span></span></code></pre></div><p><em>本文整理于 2017 年，2026 年 11 月补写。</em></p>]]></content:encoded>
    </item>
    <item>
      <title>常用sql操作</title>
      <link>https://16bh.github.io/2016/10/28/some-sql/</link>
      <pubDate>Fri, 28 Oct 2016 12:08:42 +0800</pubDate>
      <guid>https://16bh.github.io/2016/10/28/some-sql/</guid>
      <description>&lt;p&gt;几个sql操作&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>几个sql操作</p>
<ol>
<li>
<p>统计查询结果的数量</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sql" data-lang="sql"><span class="line"><span class="cl"><span class="k">select</span><span class="w"> </span><span class="k">count</span><span class="p">(</span><span class="o">*</span><span class="p">)</span><span class="w"> </span><span class="k">from</span><span class="w">  </span><span class="k">table</span><span class="w">
</span></span></span></code></pre></div></li>
<li>
<p>查询时间时加8小时</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sql" data-lang="sql"><span class="line"><span class="cl"><span class="k">select</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">from</span><span class="w"> </span><span class="k">table</span><span class="w"> </span><span class="k">where</span><span class="w"> </span><span class="n">date_add</span><span class="p">(</span><span class="n">time</span><span class="p">,</span><span class="nb">interval</span><span class="w"> </span><span class="mi">8</span><span class="w"> </span><span class="n">hour</span><span class="p">)</span><span class="o">&gt;</span><span class="w"> </span><span class="s1">&#39;2016-10-01 10:00:00&#39;</span><span class="w">
</span></span></span></code></pre></div></li>
<li>
<p>修改字段属性</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sql" data-lang="sql"><span class="line"><span class="cl"><span class="k">alter</span><span class="w"> </span><span class="k">table</span><span class="w"> </span><span class="o">`</span><span class="err">{$</span><span class="n">installer</span><span class="o">-&gt;</span><span class="n">getTable</span><span class="p">(</span><span class="s1">&#39;example_module/device&#39;</span><span class="p">)</span><span class="err">}</span><span class="o">`</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">       </span><span class="k">modify</span><span class="w"> </span><span class="o">`</span><span class="n">device_id</span><span class="o">`</span><span class="w"> </span><span class="nb">varchar</span><span class="p">(</span><span class="mi">256</span><span class="p">);</span><span class="w">
</span></span></span></code></pre></div></li>
<li>
<p>联合查询join</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sql" data-lang="sql"><span class="line"><span class="cl"><span class="k">select</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">from</span><span class="w"> </span><span class="n">A</span><span class="w"> </span><span class="k">left</span><span class="w"> </span><span class="k">join</span><span class="w"> </span><span class="n">B</span><span class="w"> </span><span class="k">on</span><span class="w"> </span><span class="n">A</span><span class="p">.</span><span class="n">id</span><span class="o">=</span><span class="n">B</span><span class="p">.</span><span class="n">id</span><span class="w"> </span><span class="k">where</span><span class="w"> </span><span class="n">A</span><span class="p">.</span><span class="n">ctime</span><span class="o">&gt;</span><span class="s1">&#39;2016-01-01&#39;</span><span class="w"> </span><span class="k">and</span><span class="w"> </span><span class="n">B</span><span class="p">.</span><span class="n">status</span><span class="o">=</span><span class="mi">0</span><span class="w">
</span></span></span></code></pre></div></li>
<li>
<p>分组group by</p>
<pre tabindex="0"><code class="language- sql" data-lang=" sql">select status,count(*) from order group by status
</code></pre></li>
</ol>
<h2 id="补充这几条在今天的写法与坑">补充：这几条在今天的写法与坑</h2>
<p>这篇是操作备忘，语句本身都还能用，但有几条在现代 MySQL（8.0+）下会踩新坑，补上说明。</p>
<h3 id="left-join-加-where-条件会变回内连接"><code>left join</code> 加 <code>where</code> 条件会变回内连接</h3>
<p>原文第 4 条 <code>left join ... where A.ctime &gt; ... and B.status = 0</code> 是 2016 年最常见的写法，但它有一个隐蔽语义问题：<strong><code>where</code> 里对右表字段加条件，会让外连接退化成内连接</strong>。</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sql" data-lang="sql"><span class="line"><span class="cl"><span class="c1">-- B.status = 0 写在 where：B 表没匹配到（B.* 全是 NULL）时这个条件为 NULL，整行被过滤掉
</span></span></span><span class="line"><span class="cl"><span class="k">select</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">from</span><span class="w"> </span><span class="n">A</span><span class="w"> </span><span class="k">left</span><span class="w"> </span><span class="k">join</span><span class="w"> </span><span class="n">B</span><span class="w"> </span><span class="k">on</span><span class="w"> </span><span class="n">A</span><span class="p">.</span><span class="n">id</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">B</span><span class="p">.</span><span class="n">id</span><span class="w"> </span><span class="k">where</span><span class="w"> </span><span class="n">B</span><span class="p">.</span><span class="n">status</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="c1">-- 正确：把右表条件放进 on，左边没匹配的行仍然保留
</span></span></span><span class="line"><span class="cl"><span class="k">select</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">from</span><span class="w"> </span><span class="n">A</span><span class="w"> </span><span class="k">left</span><span class="w"> </span><span class="k">join</span><span class="w"> </span><span class="n">B</span><span class="w"> </span><span class="k">on</span><span class="w"> </span><span class="n">A</span><span class="p">.</span><span class="n">id</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">B</span><span class="p">.</span><span class="n">id</span><span class="w"> </span><span class="k">and</span><span class="w"> </span><span class="n">B</span><span class="p">.</span><span class="n">status</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w">
</span></span></span></code></pre></div><p>如果本意就是“只要匹配上的”，写成 <code>inner join</code> 更直白。这类语句在生产里表现为“明明有 A 行没出来”，排查时极易忽略。</p>
<h3 id="group-by-的严格模式"><code>group by</code> 的严格模式</h3>
<p>原文第 5 条 <code>select status, count(*) from order group by status</code> 是标准写法，没问题。但要注意 <code>ONLY_FULL_GROUP_BY</code> 从 <strong>MySQL 5.7 起默认开启</strong>，如果哪天想多查一列：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sql" data-lang="sql"><span class="line"><span class="cl"><span class="c1">-- 5.6 能跑，5.7+ 报错：Expression #2 of SELECT list is not in GROUP BY clause
</span></span></span><span class="line"><span class="cl"><span class="k">select</span><span class="w"> </span><span class="n">status</span><span class="p">,</span><span class="w"> </span><span class="n">id</span><span class="p">,</span><span class="w"> </span><span class="k">count</span><span class="p">(</span><span class="o">*</span><span class="p">)</span><span class="w"> </span><span class="k">from</span><span class="w"> </span><span class="k">order</span><span class="w"> </span><span class="k">group</span><span class="w"> </span><span class="k">by</span><span class="w"> </span><span class="n">status</span><span class="p">;</span><span class="w">
</span></span></span></code></pre></div><p>这时要么把 <code>id</code> 加进 <code>group by</code>（会改变分组粒度），要么改用聚合函数（<code>MAX(id)</code>），不能靠关掉 <code>ONLY_FULL_GROUP_BY</code> 蒙混——它是防止结果不确定的保护。</p>
<h3 id="alter-table--modify-会丢属性"><code>alter table ... modify</code> 会丢属性</h3>
<p>原文第 3 条改字段属性：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sql" data-lang="sql"><span class="line"><span class="cl"><span class="k">alter</span><span class="w"> </span><span class="k">table</span><span class="w"> </span><span class="o">`</span><span class="n">xxx</span><span class="o">`</span><span class="w"> </span><span class="k">modify</span><span class="w"> </span><span class="o">`</span><span class="n">device_id</span><span class="o">`</span><span class="w"> </span><span class="nb">varchar</span><span class="p">(</span><span class="mi">256</span><span class="p">);</span><span class="w">
</span></span></span></code></pre></div><p><strong><code>MODIFY</code> 是整体替换列定义，没写的属性会回到默认值</strong>：原来的 <code>NOT NULL</code>、<code>DEFAULT</code>、<code>COMMENT</code>、字符集全部丢失。正确做法是先 <code>show create table</code> 看完整定义，再一并在 <code>modify</code> 里写全：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sql" data-lang="sql"><span class="line"><span class="cl"><span class="k">alter</span><span class="w"> </span><span class="k">table</span><span class="w"> </span><span class="o">`</span><span class="n">xxx</span><span class="o">`</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">    </span><span class="k">modify</span><span class="w"> </span><span class="o">`</span><span class="n">device_id</span><span class="o">`</span><span class="w"> </span><span class="nb">varchar</span><span class="p">(</span><span class="mi">256</span><span class="p">)</span><span class="w"> </span><span class="k">NOT</span><span class="w"> </span><span class="k">NULL</span><span class="w"> </span><span class="k">DEFAULT</span><span class="w"> </span><span class="s1">&#39;&#39;</span><span class="w"> </span><span class="k">COMMENT</span><span class="w"> </span><span class="s1">&#39;设备ID&#39;</span><span class="p">;</span><span class="w">
</span></span></span></code></pre></div><p>只想改一部分属性时，<code>CHANGE</code> 和 <code>MODIFY</code> 的语义一样，都是整列重定义。<strong>这个坑在 Magento 项目里尤其危险</strong>，因为字段上常有注释和默认值，丢了之后模型层行为会变。</p>
<h3 id="order-是保留字"><code>order</code> 是保留字</h3>
<p>原文表名写成 <code>order</code>，没加反引号。MySQL 8.0 对保留字更严格，某些语境下会直接报语法错误。表名一律加反引号，或者干脆避免用 <code>order</code>、<code>group</code>、<code>key</code> 这类词做表名。</p>
<h3 id="count-与-count字段-不是一回事"><code>count(*)</code> 与 <code>count(字段)</code> 不是一回事</h3>
<p>原文第 1 条 <code>select count(*) from table</code> 正确。提醒一下：<code>count(字段)</code> 会<strong>跳过该字段为 NULL 的行</strong>，两者结果不同。统计总数永远用 <code>count(*)</code>，InnoDB 会选最优索引，不是真的逐行数。</p>
<p><em>本文整理于 2016-10，语句基于当时的 MySQL 5.6/5.7。上述补充按 MySQL 8.0 书写。</em></p>
<p>原文里的 <code>left join</code> 那一条前面已经专门补过（<code>where</code> 里过滤右表会退化成内连接），这里再补两条同样常见但更隐蔽的：</p>
<h3 id="count-和-count字段-不是一回事"><code>count(*)</code> 和 <code>count(字段)</code> 不是一回事</h3>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sql" data-lang="sql"><span class="line"><span class="cl"><span class="k">select</span><span class="w"> </span><span class="k">count</span><span class="p">(</span><span class="o">*</span><span class="p">)</span><span class="w"> </span><span class="k">from</span><span class="w"> </span><span class="n">orders</span><span class="p">;</span><span class="w">          </span><span class="c1">-- 行数
</span></span></span><span class="line"><span class="cl"><span class="k">select</span><span class="w"> </span><span class="k">count</span><span class="p">(</span><span class="n">status</span><span class="p">)</span><span class="w"> </span><span class="k">from</span><span class="w"> </span><span class="n">orders</span><span class="p">;</span><span class="w">     </span><span class="c1">-- status 非 null 的行数
</span></span></span></code></pre></div><p>差在 null 上。统计&quot;有状态字段的记录数&quot;时，<code>count(status)</code> 会漏掉所有 null 行，这类错误通常表现为&quot;统计口径经常和报表对不上&quot;。</p>
<h3 id="order-by-里用列序号是危险的"><code>order by</code> 里用列序号是危险的</h3>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sql" data-lang="sql"><span class="line"><span class="cl"><span class="k">select</span><span class="w"> </span><span class="n">id</span><span class="p">,</span><span class="w"> </span><span class="n">name</span><span class="p">,</span><span class="w"> </span><span class="n">created_at</span><span class="w"> </span><span class="k">from</span><span class="w"> </span><span class="n">users</span><span class="w"> </span><span class="k">order</span><span class="w"> </span><span class="k">by</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="k">desc</span><span class="p">;</span><span class="w">  </span><span class="c1">-- 按 created_at 排
</span></span></span></code></pre></div><p>序号指的是 select 列表里的位置。<strong>只要中间插一列、或者 <code>select *</code> 的字段顺序变了，排序就静默错掉</strong>——不报错，只是结果不对。永远写列名。</p>
<p><em>本文整理于 2017 年，2026 年 11 月补写。</em></p>]]></content:encoded>
    </item>
    <item>
      <title>XML报错:XML declaration allowed only at the start of the document</title>
      <link>https://16bh.github.io/2016/07/20/xml-error-xml-declaration-allowed-only-at-the-start-of-the-document/</link>
      <pubDate>Wed, 20 Jul 2016 11:01:15 +0800</pubDate>
      <guid>https://16bh.github.io/2016/07/20/xml-error-xml-declaration-allowed-only-at-the-start-of-the-document/</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;错误信息:
Warning: simplexml_load_string(): Entity: line 1: parser error : XML declaration allowed only at the start of the document&lt;/p&gt;
&lt;/blockquote&gt;</description>
      <content:encoded><![CDATA[<blockquote>
<p>错误信息:
Warning: simplexml_load_string(): Entity: line 1: parser error : XML declaration allowed only at the start of the document</p>
</blockquote>
<p>检查后发现，<code>xml</code>文件的头文件开头处出现了多余的空格</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="line"><span class="cl">    <span class="cp">&lt;?xml version=&#34;1.0&#34;?&gt;</span>
</span></span></code></pre></div><p>修改为：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="line"><span class="cl"><span class="cp">&lt;?xml version=&#34;1.0&#34;?&gt;</span>
</span></span></code></pre></div><p>问题解决</p>
<h2 id="补充为什么一个空格就会炸以及怎么批量查">补充：为什么一个空格就会炸，以及怎么批量查</h2>
<p>XML 规范要求 <code>&lt;?xml ...?&gt;</code> 声明必须是文件的<strong>第一个字节</strong>。<code>simplexml_load_string()</code> 报 “XML declaration allowed only at the start of the document” 时，说明声明前面已经存在别的内容，而空白字符（空格、Tab、换行）在解析器眼里同样是内容。</p>
<p>三种比“肉眼翻文件”更可靠的方式：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-php" data-lang="php"><span class="line"><span class="cl"><span class="c1">// 1. 精确定位前几个字节
</span></span></span><span class="line"><span class="cl"><span class="nv">$raw</span> <span class="o">=</span> <span class="nx">file_get_contents</span><span class="p">(</span><span class="nv">$file</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="k">echo</span> <span class="nx">bin2hex</span><span class="p">(</span><span class="nx">substr</span><span class="p">(</span><span class="nv">$raw</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">8</span><span class="p">));</span>   <span class="c1">// XML 文件应以 3c3f 78 6d 6c（&lt;?xml）开头，
</span></span></span><span class="line"><span class="cl">                                    <span class="c1">// 若以 20/09/0a/0d 开头就是空白；EFBBBF 是 UTF-8 BOM
</span></span></span></code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl"><span class="c1"># 2. 批量找出所有以空白或 BOM 开头的 xml（忽略大小写扩展名）</span>
</span></span><span class="line"><span class="cl">find . -iname <span class="s1">&#39;*.xml&#39;</span> -exec sh -c <span class="s1">&#39;head -c 4 &#34;$1&#34; | grep -qP &#34;^\s|^\xEF\xBB\xBF&#34; &amp;&amp; echo &#34;$1&#34;&#39;</span> _ <span class="o">{}</span> <span class="se">\;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1"># 3. 编辑器里看不到的 BOM 用十六进制确认</span>
</span></span><span class="line"><span class="cl">file --mime <span class="s2">&#34;</span><span class="nv">$file</span><span class="s2">&#34;</span>
</span></span></code></pre></div><p>除了头部的空格，第二常见的元凶是 <strong>UTF-8 BOM</strong>——很多 Windows 编辑器（记事本、旧版 Excel 导出）会默认写入 BOM。用 <code>sed -i '1s/^\xEF\xBB\xBF//' file.xml</code> 去掉即可。</p>
<p>如果是拼接出来的 XML 字符串，注意 <code>&lt;?php</code> 闭合标签 <code>?&gt;</code> <strong>之后的换行也会被输出</strong>，这会在拼接内容前多出一个 <code>\n</code>，同样触发这个报错。<strong>PHP 文件末尾不要写 <code>?&gt;</code></strong> 是规避这类问题最省事的习惯。</p>
<hr>
<p><em>本文整理于 2016 年。</em></p>]]></content:encoded>
    </item>
    <item>
      <title>mac下php开发环境搭建:nginx&#43;mysql&#43;php&#43;php-fpm</title>
      <link>https://16bh.github.io/2016/06/30/buid-php-develope-environment-on-mac-include-nginx-mysql-php-php-fpm/</link>
      <pubDate>Thu, 30 Jun 2016 18:21:57 +0800</pubDate>
      <guid>https://16bh.github.io/2016/06/30/buid-php-develope-environment-on-mac-include-nginx-mysql-php-php-fpm/</guid>
      <description>如果要使用apache服务器的话，可以考虑windows环境下可以安装集成环境，phpstudy、xampp mac下也有集成环境：MAMP,基础功能不能切换php版本，高级功能收费 另外，mac自带了apache和ph</description>
      <content:encoded><![CDATA[<p>#关于集成环境</p>
<p>如果要使用apache服务器的话，可以考虑windows环境下可以安装集成环境，phpstudy、xampp
mac下也有集成环境：<a href="https://www.mamp.info/en/">MAMP</a>,基础功能不能切换php版本，高级功能收费</p>
<p>另外，mac自带了apache和php，如果对php版本要求不高，线上服务器使用的是apache的话，只需要启用并进行简单的配置，搭配上mysql数据库就拥有一套开发环境了</p>
<p>至于<em><strong>到底是安装集成环境还是逐项安装</strong></em>，我的建议是跟线上服务器保持一致：
线上使用nginx，那我们本地就使用nginx,不然远程服务器上的nginx日志都不知道在哪里
线上使用php5，我们也用php5，就不要图新鲜安装php7了（可以在虚拟机里单独搭建一套测试环境，那想装什么样的都ok）</p>
<h2 id="一-准备工作">一 准备工作</h2>
<p>安装homebrew</p>
<h2 id="二-安装配置mysql">二 安装配置mysql</h2>
<ol>
<li>在终端通过命令行安装</li>
</ol>
<pre tabindex="0"><code>brew install mysql
</code></pre><ol start="2">
<li>设置mysql开机启动</li>
</ol>
<pre tabindex="0"><code>ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
</code></pre><ol start="3">
<li>通过安全方式设置mysql的密码
输入以下命令后</li>
</ol>
<pre tabindex="0"><code> /usr/local/opt/mysql/bin/mysql_secure_installation
</code></pre><p>根据提示输入密码,密码要记住，下面登录的时候要用</p>
<ol start="4">
<li>登录mysql数据库</li>
</ol>
<p><img alt="数据库登录" loading="lazy" src="/2016/06/30/buid-php-develope-environment-on-mac-include-nginx-mysql-php-php-fpm/1240-20181015214455236.png"></p>
<ol start="5">
<li>安装mysql客户端
推荐使用<code>Sequel</code>
安装方法：搜索下载安装文件或用命令行下载</li>
</ol>
<pre tabindex="0"><code>brew cask install sequel-pro
</code></pre><p>效果图：
<img alt="sequel界面" loading="lazy" src="/2016/06/30/buid-php-develope-environment-on-mac-include-nginx-mysql-php-php-fpm/1240.jpeg"></p>
<p>#三  安装配置php及php-fpm</p>
<p>系统自带的php版本不一定能满足我们的需要，一般我们会选择重新安装php，安装的版本应当与线上环境的php版本保持一致。<em><strong>这样就可以避免本地与线上php版本不一致产生的bug</strong></em>，比如老版本的一些方法在新版本中已经被淘汰了，而新版本增加了许多实用的功能老版本并不支持
举个小例子：
php5.4之后，可以用<code>[]</code>表示数组</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-php" data-lang="php"><span class="line"><span class="cl"><span class="nv">$a</span> <span class="o">=</span> <span class="p">[</span>
</span></span><span class="line"><span class="cl">    <span class="s1">&#39;a&#39;</span> <span class="o">=&gt;</span> <span class="mi">1</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="s1">&#39;b&#39;</span> <span class="o">=&gt;</span> <span class="mi">2</span><span class="p">,</span>
</span></span><span class="line"><span class="cl"><span class="p">];</span>
</span></span></code></pre></div><ol>
<li>安装php(若要使用系统自带的php可跳过这一步)，以php5.5为例</li>
</ol>
<pre tabindex="0"><code>  brew install php55 --with-fpm --with-gmp --with-imap --with-tidy --with-debug --with-mysql --with-libmysql
</code></pre><ol start="2">
<li>安装php扩展，也可以先不急，等后面要用到什么扩展的时候再安装也可以
安装扩展的方法(以xdebug扩展为例)：</li>
</ol>
<pre tabindex="0"><code>brew install php55-xdebug --build-from-source;
</code></pre><p>最后的<code>--build-from-source</code>是一定要加上的，我看很多教程里都漏了这一句。加上这句可以保证安装的扩展与php版本保持一致</p>
<blockquote>
<p>备注：要卸载通过homebrew安装的php扩展，参考这篇文章：<a href="http://www.jianshu.com/p/ef92888fd5f4">mac环境下通过homebrew安装的php报错解决</a></p>
</blockquote>
<ol start="3">
<li>配置环境变量，使我们安装的5.5版本的php覆盖系统自带的php</li>
</ol>
<p>在文件中写入下面的内容</p>
<pre tabindex="0"><code>export PATH=&#34;$(brew --prefix php55)/bin:$PATH&#34;
export PATH=&#34;$(brew --prefix php55)/sbin:$PATH&#34;
export PATH=&#34;/usr/local/bin:/usr/local/sbin:$PATH&#34;
</code></pre><p>觉得这样麻烦的，可以直接删除mac自带的老版本的php后再安装</p>
<ol start="4">
<li>
<p>php-fpm配置
打开你的php目录，一般在<code>usr/local/etc/php/5.5</code>，需要配置<code>php-fpm.conf</code>和<code>php.ini</code></p>
</li>
<li>
<p>php-fpm开机启动</p>
</li>
</ol>
<pre tabindex="0"><code>ln -sfv /usr/local/opt/php55/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php55.plist
</code></pre><h2 id="四-安装配置nginx">四 安装配置nginx</h2>
<ol>
<li>安装nginx</li>
</ol>
<pre tabindex="0"><code>brew install nginx --with-http_geoip_module
</code></pre><ol start="2">
<li>启动nginx</li>
</ol>
<pre tabindex="0"><code>sudo nginx
</code></pre><ol start="3">
<li>重新加载配置|重启|停止|退出 nginx</li>
</ol>
<pre tabindex="0"><code>nginx -s reload/reopen/stop/quit
</code></pre><ol start="4">
<li>设置nginx开机启动</li>
</ol>
<pre tabindex="0"><code>ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
</code></pre><h2 id="五-配置快捷键">五 配置快捷键</h2>
<pre tabindex="0"><code>alias nginx.start=&#34;sudo nginx&#34;
alias fpm.start=&#34;sudo php-fpm -D&#34;
alias redis.start=&#34;redis-server /usr/local/etc/redis.conf&#34;

alias nginx.stop=&#34;sudo nginx -s quit&#34;
alias fpm.stop=&#39;sudo pkill php-fpm&#39;
</code></pre><h2 id="补充这套-php-55--nginx-的装法今天怎么改">补充：这套 PHP 5.5 + nginx 的装法今天怎么改</h2>
<p>原文的核心判断——<strong>「本地环境要和线上一致」</strong>——至今仍然是最重要的原则。但命令层面几乎全变了。</p>
<h3 id="1-逐条对照">1. 逐条对照</h3>
<table>
	<thead>
			<tr>
					<th>原文</th>
					<th>今天</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td><code>brew install php55 --with-fpm ...</code></td>
					<td><strong>php 各版本 formula 已移除，且 <code>--with-xxx</code> 选项被 Homebrew 全面弃用</strong>（2020 年起只支持默认选项，需要定制要自己写 Formula）。今天用 <code>brew install php</code>，扩展用 <code>pecl</code></td>
			</tr>
			<tr>
					<td><code>brew install php55-xdebug --build-from-source</code></td>
					<td>同上，改用 <code>pecl install xdebug</code></td>
			</tr>
			<tr>
					<td><code>brew install nginx --with-http_geoip_module</code></td>
					<td><code>--with-*</code> 也不再支持。需要 geoip 就用 <code>brew install nginx-full</code> 或自己编译</td>
			</tr>
			<tr>
					<td><code>ln -sfv /usr/local/opt/xxx/*.plist ~/Library/LaunchAgents</code> + <code>launchctl load</code></td>
					<td><strong>推荐统一用 <code>brew services</code></strong>：<code>brew services start nginx</code></td>
			</tr>
			<tr>
					<td><code>sudo nginx</code></td>
					<td>不要用 sudo 启动 brew 装的 nginx，否则日志/配置文件的属主会变成 root，后面各种权限问题。用 <code>brew services start nginx</code></td>
			</tr>
			<tr>
					<td><code>/usr/local/opt/...</code> 路径</td>
					<td>Apple Silicon 是 <code>/opt/homebrew/opt/...</code></td>
			</tr>
	</tbody>
</table>
<h3 id="2---with--的消失值得单独说">2. <code>--with-*</code> 的消失值得单独说</h3>
<p>很多人照抄老教程时装 PHP 失败，报的是：</p>
<pre tabindex="0"><code>Error: invalid option: --with-fpm
</code></pre><p>根因不是网络，而是 <strong>Homebrew 从 2020 年起移除了 Formula 的选项机制</strong>。今天要「打开某个扩展」，思路变成：</p>
<ul>
<li>默认公式已经带上常用扩展（<code>php</code> 公式默认就有 fpm、opcache 等）</li>
<li>额外的用 <code>pecl install xxx</code></li>
<li>需要完全定制才写自己的 formula 或用 <code>shivammathur/php</code> tap</li>
</ul>
<h3 id="3-今天的完整流程">3. 今天的完整流程</h3>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">brew install nginx php mysql redis
</span></span><span class="line"><span class="cl">brew services start nginx php mysql redis
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1"># 验证 fpm 是否在跑</span>
</span></span><span class="line"><span class="cl">brew services list <span class="p">|</span> grep php
</span></span><span class="line"><span class="cl">lsof -i :9000
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1"># 扩展</span>
</span></span><span class="line"><span class="cl">pecl install xdebug redis
</span></span></code></pre></div><h3 id="4-仍然值得保留的两点">4. 仍然值得保留的两点</h3>
<ul>
<li><strong>「装 php-fpm 后要确认 9000 端口在监听」</strong>——原文没明说，但这是 nginx 502 的第一排查项。</li>
<li><strong>「PATH 要覆盖系统自带的 php」</strong>——macOS 不再自带 PHP（从 macOS 12 起移除了内置 PHP），所以今天不会有这个冲突，但如果你用 nvm 之类的版本管理工具，仍然要注意 PATH 顺序。</li>
</ul>
<p><em>本文整理于 2016 年，2026 年 11 月补充。</em></p>
]]></content:encoded>
    </item>
    <item>
      <title>mac环境下通过homebrew安装的php报错解决：PHP Startup: Unable to load dynamic library</title>
      <link>https://16bh.github.io/2016/06/23/brew-install-php-error-php-startup-unable-to-load-dynamic-library/</link>
      <pubDate>Thu, 23 Jun 2016 18:22:39 +0800</pubDate>
      <guid>https://16bh.github.io/2016/06/23/brew-install-php-error-php-startup-unable-to-load-dynamic-library/</guid>
      <description>&lt;em&gt;注：可以直接查看最下面的解决方案&lt;/em&gt; 事情的起因是这样的： 通过下面的命令安装imagic扩展（不要问我为什么装这玩意儿，抽风了可能） brew install php55-imagick 运行 php &amp;ndash;versio</description>
      <content:encoded><![CDATA[<h1 id="背景">背景</h1>
<blockquote>
<p><em>注：可以直接查看最下面的解决方案</em></p>
</blockquote>
<p>事情的起因是这样的：
通过下面的命令安装imagic扩展（不要问我为什么装这玩意儿，抽风了可能）</p>
<pre><code>   brew install php55-imagick
</code></pre>
<p>运行 <code>php --version</code>发现报错信息
<img alt="安装imagick报错" loading="lazy" src="/2016/06/23/brew-install-php-error-php-startup-unable-to-load-dynamic-library/1240.png"></p>
<blockquote>
<p>注：此处解决方案参考了这里
<a href="https://github.com/Homebrew/homebrew-php/issues/1556">https://github.com/Homebrew/homebrew-php/issues/1556</a></p>
</blockquote>
<p>报错原因是因为安装的<em><strong>imagick扩展与php版本不匹配</strong></em>，通过下面的命令重新安装</p>
<pre tabindex="0"><code> brew reinstall php55-imagick --build-from-source
</code></pre><p>运行 <code>php --version</code>发现新的报错信息
<img alt="imagick报错信息" loading="lazy" src="/2016/06/23/brew-install-php-error-php-startup-unable-to-load-dynamic-library/1240-20181015214159584.png"></p>
<p>这个原因一直没找到</p>
<p>然后我一想：我管它什么原因，这玩意儿我压根就不需要吧，卸载了就是了
执行</p>
<pre tabindex="0"><code>brew uninstall php55-imagick
</code></pre><p>果然，又报错了：
<img loading="lazy" src="/2016/06/23/brew-install-php-error-php-startup-unable-to-load-dynamic-library/1240-20181015214210796.png"></p>
<p>在<code>stackoverflow</code>上搜索了半天，几乎所有提供的解决方案都是重装<code>imagick</code>
but，前面装过了还是会报错啊</p>
<p>先是在<code>php.ini</code>文件中看看在哪里启用了imagick这个扩展，并没有发现</p>
<blockquote>
<p>附：<code>php.ini</code>的路径是：
<code>/usr/local/etc/php/5.5</code>(如果你跟我一样是通过homebrew安装的php的话)</p>
</blockquote>
<h1 id="最终解决方案">最终解决方案</h1>
<blockquote>
<p>注：最后的解决方法参考这里
<a href="http://stackoverflow.com/questions/5282264/php-warning-php-startup-unable-to-load-dynamic-library">http://stackoverflow.com/questions/5282264/php-warning-php-startup-unable-to-load-dynamic-library</a></p>
</blockquote>
<p>先执行下面的命令，获取所有跟php配置相关的文件的路径，终于找到了通过homebrew安装的扩展的位置</p>
<pre tabindex="0"><code>php -i | grep ini
</code></pre><p><img loading="lazy" src="/2016/06/23/brew-install-php-error-php-startup-unable-to-load-dynamic-library/1240-20181015214214845.png"></p>
<p>接下来就简单了,打开ext-imagick.ini文件</p>
<pre tabindex="0"><code>cd /usr/local/etc/php/5.5/conf.d/
vim ext-imagick.ini
</code></pre><p>注销掉<code>extentions=&quot;&quot;</code>这行就可以了
<img loading="lazy" src="/2016/06/23/brew-install-php-error-php-startup-unable-to-load-dynamic-library/1240-20181015214222889.png"></p>
<p>这下，世界终于安静了，每次启动php的时候静悄悄的，再也没有烦人的warning提示了</p>
<h2 id="补充这个报错的通用修法">补充：这个报错的通用修法</h2>
<p>原文最后“注销掉 <code>extensions=&quot;&quot;</code> 那一行”能用，但属于对着现象改。真正的规律是：</p>
<p><strong><code>PHP Startup: Unable to load dynamic library</code> 永远是两种情况之一</strong>——<code>.so</code> 文件不存在，或者 <code>.so</code> 存在但和当前 PHP 的 API 版本（<code>php70</code> / <code>php71</code> …）不匹配。<code>pecl install</code> 装出来的扩展编译时会绑定当时的 PHP 版本，换了 PHP 版本，扩展就废了。</p>
<p>按这个顺序排查，比逐个注释 ini 文件快得多：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl"><span class="c1"># 1. 当前 PHP 的版本与扩展目录</span>
</span></span><span class="line"><span class="cl">php -v
</span></span><span class="line"><span class="cl">php -i <span class="p">|</span> grep -E <span class="s1">&#39;extension_dir|Loaded Configuration File|Scan this dir&#39;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1"># 2. 这个扩展到底有没有安装、装在哪</span>
</span></span><span class="line"><span class="cl">php -m <span class="p">|</span> grep imagick          <span class="c1"># 加载成功的才会出现</span>
</span></span><span class="line"><span class="cl">ls -l <span class="s2">&#34;</span><span class="k">$(</span>php -r <span class="s1">&#39;echo ini_get(&#34;extension_dir&#34;);&#39;</span><span class="k">)</span><span class="s2">&#34;</span> <span class="p">|</span> grep imagick
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1"># 3. 找出所有启用了该扩展的 ini（含 conf.d）</span>
</span></span><span class="line"><span class="cl">php --ini <span class="p">|</span> grep -i imagick
</span></span><span class="line"><span class="cl">grep -rl <span class="s1">&#39;imagick&#39;</span> <span class="s2">&#34;</span><span class="k">$(</span>php --ini <span class="p">|</span> awk -F<span class="s1">&#39;: *&#39;</span> <span class="s1">&#39;/Scan this dir/{print $2}&#39;</span><span class="k">)</span><span class="s2">&#34;</span>
</span></span></code></pre></div><p>三个常见根因：</p>
<table>
	<thead>
			<tr>
					<th>现象</th>
					<th>根因</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td>卸载了扩展但启动仍报警告</td>
					<td>只删了 <code>.so</code>，<code>conf.d/ext-*.ini</code> 还留着 <code>extension=</code> 行</td>
			</tr>
			<tr>
					<td>装了新版扩展仍报警告</td>
					<td>旧版本 <code>.so</code> 还在，<code>extension_dir</code> 里有两个同名文件，加载的是旧的那个</td>
			</tr>
			<tr>
					<td>Mac 上编译失败</td>
					<td>缺 <code>pkg-config</code> / <code>autoconf</code>，<code>brew install pkg-config autoconf</code></td>
			</tr>
	</tbody>
</table>
<p><strong>关于 Homebrew 的现状</strong>：<code>php55-imagick</code> 这类“按 PHP 版本打补丁的扩展 formula”已经全部从 Homebrew 移除，现在的做法是 <code>pecl install imagick</code>，或用 <code>shivammathur/extensions</code> 这个 tap（<code>brew tap shivammathur/extensions</code> 后 <code>brew install imagick@8.3</code>）。原文里的命令在今天的 Homebrew 上会直接报 “No available formula”。</p>
<hr>
<p><em>本文整理于 2016 年，当时用的是 Homebrew 的 php55 系列 formula，现已不存在。</em></p>
]]></content:encoded>
    </item>
    <item>
      <title>mac下安装redis</title>
      <link>https://16bh.github.io/2016/06/20/install-redis-on-mac/</link>
      <pubDate>Mon, 20 Jun 2016 18:12:38 +0800</pubDate>
      <guid>https://16bh.github.io/2016/06/20/install-redis-on-mac/</guid>
      <description>&lt;p&gt;在mac上安装redis服务。&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>在mac上安装redis服务。</p>
<h2 id="一-准备工作">一 准备工作</h2>
<p>安装了homebrew
安装了php、nginx（或apache）或集成环境</p>
<h2 id="二-安装redis服务器">二 安装redis服务器</h2>
<ol>
<li>通过homebrew安装</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">brew install redis
</span></span></code></pre></div><ol start="2">
<li>直接下载安装
本人是用这种方法安装的，安装完后才发现可以用homebrew安装</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">curl -O http://redis.googlecode.com/files/redis-2.8.7.tar.gz
</span></span><span class="line"><span class="cl">sudo tar -zxf redis-2.8.7.tar.gz
</span></span><span class="line"><span class="cl">mv redis-2.8.7 /usr/local/redis
</span></span><span class="line"><span class="cl"><span class="nb">cd</span> redis
</span></span><span class="line"><span class="cl">sudo make
</span></span><span class="line"><span class="cl">sudo make <span class="nb">test</span>
</span></span><span class="line"><span class="cl">sudo make install
</span></span><span class="line"><span class="cl">mv redis.conf /etc/reds.conf
</span></span></code></pre></div><p>安装成功</p>
<h2 id="三-redis服务器的启动使用和退出">三 redis服务器的启动、使用和退出</h2>
<p>*** 1 启动redis服务***
执行以下命令</p>
<pre tabindex="0"><code>/usr/local/bin/redis-server /etc/redis.conf
</code></pre><p>出现下面的界面说明redis服务器安装成功
<img loading="lazy" src="/2016/06/20/install-redis-on-mac/1469498761643.png"></p>
<p>进入<code>/usr/local/bin</code>目录可以看到以下文件</p>
<blockquote>
<p>dump.rdb  用于将缓存以文件的形式存储在硬盘中，需要设置权限，见文末
redis-cli    用于启动redis客户端</p>
</blockquote>
<p><em><strong>2 查看redis服务是否启动</strong></em></p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">➜  16bh.github.io git:<span class="o">(</span>gh-pages<span class="o">)</span> ✗ ps aux <span class="p">|</span>grep redis
</span></span><span class="line"><span class="cl">your-user          <span class="m">3196</span>  96.9  0.0  <span class="m">4321720</span>   <span class="m">1500</span>   ??  Rs   11:02PM   0:02.34 redis-server 127.0.0.1:6379
</span></span><span class="line"><span class="cl">your-user          <span class="m">3207</span>   0.0  0.0  <span class="m">4258736</span>    <span class="m">192</span> s000  R+   11:02PM   0:00.00 grep --color<span class="o">=</span>auto --exclude-dir<span class="o">=</span>.bzr --exclude-dir<span class="o">=</span>CVS --exclude-dir<span class="o">=</span>.git --exclude-dir<span class="o">=</span>.hg --exclude-dir<span class="o">=</span>.svn redis
</span></span></code></pre></div><p><em><strong>3 使用redis服务</strong></em></p>
<blockquote>
<p>注：启动redis服务器后终端所在的窗口就不能输入别的命令了(如下图所示)，需要在终端打开新的窗口才能使用客户端功能</p>
</blockquote>
<p><img loading="lazy" src="/2016/06/20/install-redis-on-mac/1469498796319.png"></p>
<p>2018-10-15：补充，可以设置redis后台，这样就不影响终端窗口的使用</p>
<p>配置文件redis.conf</p>
<pre tabindex="0"><code class="language-conf" data-lang="conf">daemonize yes
</code></pre><p>通过redis-cli命令可以启动redis客户端</p>
<pre tabindex="0"><code>redis-cli
</code></pre><p>常用命令</p>
<blockquote>
<p>keys * 查看所有键值
set (key) (value) 设置键key的值为value
append (key) (value2) 在键key的值后面加上value2
get (key) 查看键key的值</p>
</blockquote>
<p>redis客户端使用举例：
<img loading="lazy" src="/2016/06/20/install-redis-on-mac/1469498811208.png"></p>
<p>6.23补充：如何设置和查看缓存时间</p>
<blockquote>
<p>set a 123;//设置缓存：a=&gt;123
EXPIRE a 3600;//设置缓存时间（秒）
TTL a；//查看缓存剩余时间</p>
</blockquote>
<p>6.24补充：如何清空所有缓存</p>
<blockquote>
<p>flushall  //执行该命令后会清空redis服务器的所有缓存，一般用于应急处理，不应该作为常用命令</p>
</blockquote>
<p><em><strong>4 退出redis服务</strong></em>
（1）客户端退出
执行</p>
<pre tabindex="0"><code>redis-cli shutdown
</code></pre><p>（2）关闭pid
先运行</p>
<pre tabindex="0"><code>ps -u jim(替换成你的用户名) -o pid,rss,command | grep redis-server
</code></pre><p>查看所有redis服务的pid号</p>
<p><img loading="lazy" src="/2016/06/20/install-redis-on-mac/1469498829072.png"></p>
<p>16.6.24日补充：还可以通过mac自带的活动监视器查看pid
如下图所示
通过Spotlight或alfred搜索<code>activity monitor</code>打开活动监视器
在活动监视器中搜索<code>redis-server</code>，即可得到pid号
<img loading="lazy" src="/2016/06/20/install-redis-on-mac/1469498837473.png"></p>
<p>补充：如果你的电脑安装了oh my zsh
那么只需要在终端输入</p>
<pre tabindex="0"><code>kill redis
</code></pre><p>按tab，会自动替换成对应的pid（喜大普奔啊，各位）</p>
<p>再运行</p>
<pre tabindex="0"><code>kill -9 27355
</code></pre><p>关闭redis服务对应的pid号，即可关闭redis服务</p>
<h2 id="四-配置php使用redis服务">四 配置php使用redis服务</h2>
<ol>
<li>安装php的redis扩展</li>
</ol>
<pre tabindex="0"><code>  brew install php55-redis --build-from-source
</code></pre><p><code>php55</code>是本机安装的php的版本（5.5）,<code>--build-from-source</code>是让安装的扩展与php的版本保持一致</p>
<p>查看phpinfo()，出现redis选项说明redis配置成功
<img loading="lazy" src="/2016/06/20/install-redis-on-mac/1469498850108.png"></p>
<ol start="2">
<li>在php代码中使用redis服务</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-php" data-lang="php"><span class="line"><span class="cl"><span class="nv">$redis</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Redis</span><span class="p">();</span>
</span></span><span class="line"><span class="cl"><span class="nv">$redis</span><span class="o">-&gt;</span><span class="na">connect</span><span class="p">(</span><span class="s1">&#39;127.0.0.1&#39;</span><span class="p">,</span><span class="s1">&#39;host&#39;</span><span class="p">);</span><span class="c1">//redis服务器ip及端口号
</span></span></span><span class="line"><span class="cl"><span class="nv">$redis</span><span class="o">-&gt;</span><span class="na">set</span><span class="p">(</span><span class="nv">$key</span><span class="p">,</span><span class="nv">$value</span><span class="p">,</span><span class="nv">$timeout</span><span class="p">);</span><span class="c1">//设置缓存:键-值-缓存时间
</span></span></span><span class="line"><span class="cl"><span class="nv">$redis</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="nv">$key</span><span class="p">);</span><span class="c1">//查找缓存
</span></span></span><span class="line"><span class="cl"><span class="nv">$redis</span><span class="o">-&gt;</span><span class="na">del</span><span class="p">(</span><span class="nv">$key</span><span class="p">);</span><span class="c1">//删除缓存
</span></span></span><span class="line"><span class="cl"><span class="nv">$redis</span><span class="o">-&gt;</span><span class="na">delete</span><span class="p">(</span><span class="nv">$key</span><span class="p">);</span><span class="c1">//删除缓存
</span></span></span></code></pre></div><p>##五 常见问题
（1）Redis: Failed opening .rdb for saving: Permission denied
redis服务器会生成<code>dump.rdb</code>文件存储缓存，如果文件权限不够则无法读写该文件</p>
<pre tabindex="0"><code>cd /usr/loal/bin
</code></pre><p>在<code>/usr/local/bin/</code>（默认文件目录）下执行命令</p>
<pre tabindex="0"><code>chmod 777 dump.rdb
</code></pre><h2 id="补充十年后再装-redis原文这几步都要改">补充：十年后再装 Redis，原文这几步都要改</h2>
<p>本文写于 2016 年，装的是 Redis 2.8.7。今天再按原文操作，第一条下载命令就会失败。</p>
<h3 id="哪些变了">哪些变了</h3>
<table>
	<thead>
			<tr>
					<th>原文</th>
					<th>今天</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td><code>curl http://redis.googlecode.com/files/redis-2.8.7.tar.gz</code></td>
					<td><strong>googlecode 已于 2016 年关闭</strong>，域名解析失败。源码改为 <code>https://download.redis.io/releases/redis-x.y.z.tar.gz</code></td>
			</tr>
			<tr>
					<td><code>sudo make install</code> 装到 <code>/usr/local/bin</code></td>
					<td>用 Homebrew 即可，Apple Silicon 上路径是 <code>/opt/homebrew</code>，不是 <code>/usr/local</code></td>
			</tr>
			<tr>
					<td><code>brew install php55-redis --build-from-source</code></td>
					<td><strong>php55 已于 2018 年从 Homebrew 移除</strong>，<code>--build-from-source</code> 这条命令对任何新版本都不可用。改用 <code>pecl install redis</code>，或在 <code>php.ini</code> 里启用随 PHP 分发的 <code>redis.so</code></td>
			</tr>
			<tr>
					<td><code>mv redis.conf /etc/reds.conf</code>（原文第 26 行）</td>
					<td>这是笔误，应为 <code>/etc/redis.conf</code>。后文启动命令里的 <code>/etc/redis.conf</code> 才是对的</td>
			</tr>
			<tr>
					<td><code>/usr/local/bin/redis-server /etc/redis.conf</code></td>
					<td>Homebrew 装完用 <code>brew services start redis</code> 即可，配置文件在 <code>$(brew --prefix)/etc/redis.conf</code></td>
			</tr>
			<tr>
					<td><code>kill redis</code> + Tab 补全 pid</td>
					<td>这是 oh-my-zsh 的 <code>kill</code> 插件按进程名找 pid，和「杀 redis 服务」不是一回事，容易误杀别的进程。用 <code>brew services stop redis</code> 更稳</td>
			</tr>
	</tbody>
</table>
<h3 id="原文仍然成立的部分">原文仍然成立的部分</h3>
<ul>
<li><code>daemonize yes</code> 让服务后台运行，这条至今有效，Homebrew 安装的默认配置已经开启了。</li>
<li><code>flushall</code> 会清空所有库，线上慎用，这条提醒今天更需要强调：<strong>很多线上事故就是有人把测试环境的 <code>flushall</code> 敲到了线上终端</strong>。</li>
<li><code>dump.rdb</code> 权限问题，本质是启动用户和文件属主不一致，今天的表现依然一样。</li>
</ul>
<h3 id="今天的建议装法">今天的建议装法</h3>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">brew install redis
</span></span><span class="line"><span class="cl">brew services start redis   <span class="c1"># 开机自启，等价于原文的 launchctl 那套</span>
</span></span><span class="line"><span class="cl">redis-cli ping              <span class="c1"># 返回 PONG 即可用</span>
</span></span></code></pre></div><p>如果只是本机调试，<strong>不要设置密码，也不要把 6379 端口暴露到局域网</strong>——Redis 默认无鉴权，公网暴露是被挖矿的重灾区。</p>
<h3 id="踩过的一个坑">踩过的一个坑</h3>
<p>原文里 <code>chmod 777 dump.rdb</code> 能解决问题，但副作用是文件权限被放宽到所有人可写。更稳妥的做法是把 Redis 的工作目录和启动用户对齐：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">sudo chown -R <span class="k">$(</span>whoami<span class="k">)</span> <span class="k">$(</span>brew --prefix<span class="k">)</span>/var/db/redis
</span></span></code></pre></div><p><em>本文整理于 2016 年，2026 年 11 月补充。</em></p>]]></content:encoded>
    </item>
  </channel>
</rss>
