<?xml version="1.0" encoding="utf-8" ?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Fun with ones and zeros - mysql</title>
<subtitle>Barry&#039;s notes on computer software and hardware</subtitle>
<link href="/blog/tags/mysql"></link>
<updated>2026-04-17T13:47:42-07:00</updated>
<id>urn:uuid:67f1cb9b-ca3a-140a-d6a9-b05176c8ea62</id>
<entry>
<title>Automatically restarting Percona XtraDB cluster</title>
<link href="/blog/entries/automatically-restarting-percona-xtradb-cluster"></link>
<id>urn:uuid:d1a316dd-db76-abce-d158-d28ddf61c8de</id>
<updated>2023-02-01T10:05:00-08:00</updated>
<author><name>Barry Pederson</name>
<email>bp@barryp.org</email>
</author>
<content type="html">&lt;body&gt;&lt;p&gt;I&#039;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 &lt;code&gt;safe_to_bootstrap: 1&lt;/code&gt; in it&#039;s &lt;code&gt;/var/lib/mysql/grastate.dat&lt;/code&gt; file, and on that node starting
the &lt;code&gt;mysql@boostrap&lt;/code&gt; service instead of just plain &lt;code&gt;mysql&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Looking at a file and acting on what&#039;s found seems like something that could be automated, so here&#039;s my take for an
Ubuntu 22.04 setup:&lt;/p&gt;
&lt;p&gt;On each node (yay Ansible!) I added this script as &lt;code&gt;/usr/local/sbin/choose-mysql-service.sh&lt;/code&gt;&lt;/p&gt;
&lt;div class=&quot;source&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;ch&quot;&gt;#!/bin/bash&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;GRASTATE&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;/var/lib/mysql/grastate.dat&quot;&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;service&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;mysql&quot;&lt;/span&gt;

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

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

&lt;span class=&quot;k&quot;&gt;[Service]&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;Type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;oneshot&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;ExecStart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;/usr/local/sbin/choose-mysql-service.sh&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;RemainAfterExit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;true&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;[Install]&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;WantedBy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;multi-user.target&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And the disabled the default &lt;code&gt;mysql&lt;/code&gt; service and enabled my new unit with:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;systemctl daemon-reload
systemctl disable mysql
systemctl enable choose-mysql-service&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So now when the OS boots, instead of just blindly trying to start &lt;code&gt;mysql&lt;/code&gt;, it looks at the &lt;code&gt;grastate.dat&lt;/code&gt; and if it has &lt;code&gt;safe_to_bootstrap: 1&lt;/code&gt; it starts &lt;code&gt;mysql@bootstrap&lt;/code&gt; instead - or otherwise falls back to the default of starting &lt;code&gt;mysql&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;I also shared this on the &lt;a href=&quot;https://forums.percona.com/t/pxc-8-auto-restart-after-graceful-shutdown/19850&quot;&gt;Percona Forum&lt;/a&gt;, look for feedback there&lt;/p&gt;&lt;/body&gt;</content>
</entry>
<entry>
<title>mysqldump: Got error: 1049: Unknown database when using LOCK TABLES</title>
<link href="/blog/entries/mysqldump-error-unknown-database"></link>
<id>urn:uuid:605a9e4c-5df3-8995-4cbd-1d9b69dcf27a</id>
<updated>2022-12-22T14:56:00-08:00</updated>
<author><name>Barry Pederson</name>
<email>bp@barryp.org</email>
</author>
<content type="html">&lt;p&gt;Ran in to a confusing error message today:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;mysqldump: Got error: 1049: Unknown database &#039;mydb&#039; when using LOCK TABLES&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I did have a database named &lt;code&gt;mydb&lt;/code&gt; 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?&lt;/p&gt;
&lt;p&gt;Turns out one of my other dbs was referencing the deleted db in a VIEW.  Hope that helps&lt;/p&gt;</content>
</entry>
</feed>