<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0">
<channel>
<title>Fun with ones and zeros - mysql</title>
<description><![CDATA[Barry's notes on computer software and hardware]]></description>
<link>/blog/tags/mysql</link>
<lastBuildDate>Fri, 17 Apr 2026 13:43:49 -0700</lastBuildDate>
<item>
<title>Automatically restarting Percona XtraDB cluster</title>
<link>/blog/entries/automatically-restarting-percona-xtradb-cluster</link>
<pubDate>Wed, 01 Feb 2023 10:05:00 -0800</pubDate>
<author>bp@barryp.org (Barry Pederson)</author>
<description><![CDATA[<body><p>I've been experimenting with Percona XtraDB cluster, and found that by default it requires manual intervention
to restart the cluster from an all-nodes-down state when the nodes were gracefully shutdown.  The docs talk about
identifying which node has <code>safe_to_bootstrap: 1</code> in it's <code>/var/lib/mysql/grastate.dat</code> file, and on that node starting
the <code>mysql@boostrap</code> service instead of just plain <code>mysql</code>.</p>
<p>Looking at a file and acting on what's found seems like something that could be automated, so here's my take for an
Ubuntu 22.04 setup:</p>
<p>On each node (yay Ansible!) I added this script as <code>/usr/local/sbin/choose-mysql-service.sh</code></p>
<div class="source"><pre><span></span><span class="ch">#!/bin/bash</span>

<span class="nv">GRASTATE</span><span class="o">=</span><span class="s2">"/var/lib/mysql/grastate.dat"</span>

<span class="nv">service</span><span class="o">=</span><span class="s2">"mysql"</span>

<span class="c1"># Start a different service if grastate.dat is present</span>
<span class="c1"># with safe_to_bootstrap: 1</span>
<span class="c1">#</span>
<span class="k">if</span><span class="w"> </span><span class="o">[</span><span class="w"> </span>-f<span class="w"> </span><span class="nv">$GRASTATE</span><span class="w"> </span><span class="o">]</span><span class="p">;</span><span class="w"> </span><span class="k">then</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span>grep<span class="w"> </span>--quiet<span class="w"> </span><span class="s2">"^safe_to_bootstrap: 1"</span><span class="w"> </span><span class="nv">$GRASTATE</span><span class="p">;</span><span class="w"> </span><span class="k">then</span>
<span class="w">        </span><span class="nv">service</span><span class="o">=</span><span class="s2">"mysql@bootstrap"</span>
<span class="w">    </span><span class="k">fi</span>
<span class="k">fi</span>

<span class="nb">echo</span><span class="w"> </span><span class="s2">"Starting </span><span class="nv">$service</span><span class="s2">"</span>
systemctl<span class="w"> </span>start<span class="w"> </span><span class="nv">$service</span>
</pre></div>
<p>Then I added a one-shot systemd unit to execute at boot time, as <code>/etc/systemd/system/choose-mysql-service.service</code></p>
<div class="source"><pre><span></span><span class="k">[Unit]</span>
<span class="na">Description</span><span class="o">=</span><span class="s">Choose MySQL service</span>
<span class="na">After</span><span class="o">=</span><span class="s">network.target</span>

<span class="k">[Service]</span>
<span class="na">Type</span><span class="o">=</span><span class="s">oneshot</span>
<span class="na">ExecStart</span><span class="o">=</span><span class="s">/usr/local/sbin/choose-mysql-service.sh</span>
<span class="na">RemainAfterExit</span><span class="o">=</span><span class="s">true</span>

<span class="k">[Install]</span>
<span class="na">WantedBy</span><span class="o">=</span><span class="s">multi-user.target</span>
</pre></div>
<p>And the disabled the default <code>mysql</code> service and enabled my new unit with:</p>
<pre><code>systemctl daemon-reload
systemctl disable mysql
systemctl enable choose-mysql-service</code></pre>
<p>So now when the OS boots, instead of just blindly trying to start <code>mysql</code>, it looks at the <code>grastate.dat</code> and if it has <code>safe_to_bootstrap: 1</code> it starts <code>mysql@bootstrap</code> instead - or otherwise falls back to the default of starting <code>mysql</code></p>
<p>I also shared this on the <a href="https://forums.percona.com/t/pxc-8-auto-restart-after-graceful-shutdown/19850">Percona Forum</a>, look for feedback there</p></body>]]></description>
</item>
<item>
<title>mysqldump: Got error: 1049: Unknown database when using LOCK TABLES</title>
<link>/blog/entries/mysqldump-error-unknown-database</link>
<pubDate>Thu, 22 Dec 2022 14:56:00 -0800</pubDate>
<author>bp@barryp.org (Barry Pederson)</author>
<description><![CDATA[<p>Ran in to a confusing error message today:</p>
<pre><code>mysqldump: Got error: 1049: Unknown database 'mydb' when using LOCK TABLES</code></pre>
<p>I did have a database named <code>mydb</code> and then deleted it the other day.  Later when doing
some scripted mysqldumps of other databases, I got that message.  Why in the world was it trying
to lock a database I knew to be gone?</p>
<p>Turns out one of my other dbs was referencing the deleted db in a VIEW.  Hope that helps</p>]]></description>
</item>
</channel>
</rss>