<?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>Mysql – Yeri Tiete</title>
    <link>https://yeri.be/tag/mysql/</link>
    <description>Yeri Tiete&#39;s blog</description>
    <language>en</language>
    <copyright>© Yeri Tiete</copyright>
    <lastBuildDate>Thu, 22 Feb 2024 11:24:50 +0100</lastBuildDate>
    <atom:link href="https://yeri.be/tag/mysql/index.xml" rel="self" type="application/rss+xml" />
    
    <item>
      <title>WordPress unable to connect to SQL database</title>
      <link>https://yeri.be/wordpress-unable-to-connect-to-sql-database/</link>
      <pubDate>Thu, 22 Feb 2024 11:24:50 +0100</pubDate>
      <author>Yeri Tiete</author>
      <guid isPermaLink="true">https://yeri.be/wordpress-unable-to-connect-to-sql-database/</guid><enclosure url="https://static.yeri.be/2024/02/crashed_database.jpg" length="0" type="image/jpeg" />
      <description>&lt;p&gt;Yesterday, three blogs I&#39;m hosting suddenly went offline and &lt;a href=&#34;https://yeri.be/?s=betteruptime&#34;&gt;alerts&lt;/a&gt; went off.&lt;/p&gt;&#xA;&lt;p&gt;They all had the same error that they couldn&#39;t connect to their SQL database, and it seemed that &lt;a href=&#34;https://hub.docker.com/_/mariadb&#34; target=&#34;_blank&#34; rel=&#34;noreferrer noopener&#34;&gt;the container&lt;/a&gt; recently was auto-updated. &lt;/p&gt;&#xA;&lt;p&gt;Docker logs also showed (which may or may not have been related):&lt;/p&gt;&#xA;&lt;pre class=&#34;wp-block-code&#34;&gt;&lt;code&gt;2024-02-21  7:30:29 97 &amp;#91;Warning] Aborted connection 97 to db: &#39;unconnected&#39; user: &#39;unauthenticated&#39; host: &#39;192.168.200.4&#39; (This connection closed normally without authentication)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;This was odd. I started the usual troubleshooting:&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p><img src="https://static.yeri.be/2024/02/crashed_database.jpg" alt="WordPress unable to connect to SQL database"></p><p>Yesterday, three blogs I'm hosting suddenly went offline and <a href="https://yeri.be/?s=betteruptime">alerts</a> went off.</p>
<p>They all had the same error that they couldn't connect to their SQL database, and it seemed that <a href="https://hub.docker.com/_/mariadb" target="_blank" rel="noreferrer noopener">the container</a> recently was auto-updated. </p>
<p>Docker logs also showed (which may or may not have been related):</p>
<pre class="wp-block-code"><code>2024-02-21  7:30:29 97 &#91;Warning] Aborted connection 97 to db: 'unconnected' user: 'unauthenticated' host: '192.168.200.4' (This connection closed normally without authentication)</code></pre>
<p>This was odd. I started the usual troubleshooting:</p>
<ul>
<li>As these containers have their own network, maybe the IPs changed and as the SQL users can only access from a certain IP range, something broke there -- nothing seemed to have changed and I could connect from the nginx container to the other just fine</li>
<li>Password somehow changed -- nah, connections were accepted</li>
<li>Roll back to a previous version of MariaDB -- didn't do anything, odd</li>
<li>This was not actually a MariaDB problem but a WordPress problem? Was there an auto-update? Didn't make sense because one of the blogs is running a very old WP version</li>
<li>Worth noting that I also host two Ghosts blogs using the same setup (nginx+mariadb in container) and they were unaffected and humming along just fine...</li>
<li>At this point I was starting to think something was massively corrupt and this was gonna be a long day.</li>
</ul>
<p>I then continued and created a php (<code>testconnection.php</code>) file on the nginx:</p>
<pre class="wp-block-code"><code>&lt;?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// Connection Details
$hostname = "container-mariadb";  // Your MariaDB hostname 
$username = "your_username"; // Your MariaDB username
$password = "your_password"; // Your MariaDB password
$database = "your_database_name"; // The database

// Create the connection
$conn = new mysqli($hostname, $username, $password, $database);

// Check for errors
if ($conn-&gt;connect_error) {
    die("Connection failed: " . $conn-&gt;connect_error);
}

// Print Debug Information
echo "Connected successfully\n"; 
echo "Host information: " . $conn-&gt;host_info . "\n";
echo "Client information: " . $conn-&gt;client_info . "\n";

// Close the connection
$conn-&gt;close();
?&gt;</code></pre>
<p>This threw the following error... We were getting somewhere:</p>
<pre class="wp-block-code"><code>Fatal error: Uncaught mysqli_sql_exception: Server sent charset (0) unknown to the client. Please, report to the developers in /var/www/domain/testconnection.php:13 Stack trace: #0 /var/www/domain/testconnection.php(13): mysqli-&gt;__construct() #1 {main} thrown in /var/www/domain/testconnection.php on line 13</code></pre>
<p>The charset I was using was <code>utf8mb4</code> with a collate of <code>utf8mb4_general_ci</code>:</p>
<pre class="wp-block-code"><code>SHOW CREATE DATABASE your_database_name;</code></pre>
<p>My <code>wp-config.php</code> file already had <code>DB_CHARSET</code>, but <code>DB_COLLATE</code> was not set. I added that in the config as well:</p>
<pre class="wp-block-code"><code>define( 'DB_CHARSET', 'utf8mb4' );
define( 'DB_COLLATE', 'utf8mb4_general_ci' );</code></pre>
<p>Sadly, that didn't fix it either. Still saying it can't connect to the database. Grmbl.</p>
<p>I then went ahead and checked <code>/etc/mysql/mariadb.conf</code> and the files in <code>/etc/mariadb.conf.d/</code>. It included <code>character-set-server = utf8mb4</code>, but not <code>collation-server = utf8mb4_general_ci</code>. The line was specifically in <code>/etc/mariadb.conf.d/50-server.cnf</code>.</p>
<p>Adding the latter solved the problem... 2hrs of my life gone. </p>
<p>As this file (likely) is managed in the container and changed by the OS (it's apparently based on Ubuntu, ewwww) or the MariaDB version, the easiest way was just creating another file: <code>/etc/mariadb.conf.d/51-&lt;something&gt;.cnf</code>.</p>
<p>This is my <strong>very dirty</strong> hack (as I'm not using Docker Compose for this):</p>
<pre class="wp-block-code"><code>docker exec -it container-mariadb bash -c "echo -e '&#91;mysqld]\ncollation-server = utf8mb4_general_ci'> /etc/mysql/mariadb.conf.d/51-yeri.cnf"
docker restart container-mariadb</code></pre>
<p>It now works fine again... </p>
]]></content:encoded>
      <category>linux</category><category>software</category><category>www</category>
      <category>mysql</category><category>nginx</category><category>wordpress</category>
    </item>
    
    <item>
      <title>Agenda / Todo page</title>
      <link>https://yeri.be/agenda-todo-page/</link>
      <pubDate>Tue, 09 Feb 2010 13:29:35 +0100</pubDate>
      <author>Yeri Tiete</author>
      <guid isPermaLink="true">https://yeri.be/agenda-todo-page/</guid>
      <description>&lt;p&gt;This summer I created a simple &amp;ldquo;agenda&amp;rdquo; for myself. I&amp;rsquo;ve been using it every now &amp;amp; then, but forgot to &amp;ldquo;share&amp;rdquo; it. So here it goes.&lt;/p&gt;&#xA;&lt;p&gt;Screenshots below&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;Features&lt;/strong&gt;:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;    &lt;li&gt;Add todo items&lt;/li&gt;&#xA;    &lt;li&gt;Change todo items to completed&lt;/li&gt;&#xA;    &lt;li&gt;List of (completed) todo items&lt;/li&gt;&#xA;    &lt;li&gt;PHP + MySQL&lt;/li&gt;&#xA;    &lt;li&gt;Basic &lt;a href=&#34;http://prowl.weks.net/&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Prowl&lt;/a&gt; support&lt;/li&gt;&#xA;    &lt;li&gt;Example SQL &lt;a href=&#34;https://yeri.be/8g&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;create script&lt;/a&gt;&lt;/li&gt;&#xA;    &lt;li style=&#34;text-align: left;&#34;&gt;There&#39;s NO login. Use &lt;a href=&#34;http://www.javascriptkit.com/howto/htaccess3.shtml&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;.htaccess files&lt;/a&gt; to disable world wide access.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;&lt;strong&gt;Screenshots&lt;/strong&gt;:&lt;/p&gt;&#xA;&lt;p style=&#34;text-align: center;&#34;&gt;&lt;a href=&#34;https://static.yeri.be/2010/02/Screen-shot-2010-02-08-at-13.27.49.png&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;&lt;img class=&#34;size-medium wp-image-951 aligncenter&#34; title=&#34;Todo list&#34; src=&#34;https://static.yeri.be/2010/02/Screen-shot-2010-02-08-at-13.27.49-300x194.png&#34; alt=&#34;&#34; width=&#34;300&#34; height=&#34;194&#34; /&gt;&lt;/a&gt;&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>This summer I created a simple &ldquo;agenda&rdquo; for myself. I&rsquo;ve been using it every now &amp; then, but forgot to &ldquo;share&rdquo; it. So here it goes.</p>
<p>Screenshots below</p>
<p><strong>Features</strong>:</p>
<ul>
    <li>Add todo items</li>
    <li>Change todo items to completed</li>
    <li>List of (completed) todo items</li>
    <li>PHP + MySQL</li>
    <li>Basic <a href="http://prowl.weks.net/" target="_blank" rel="noopener noreferrer">Prowl</a> support</li>
    <li>Example SQL <a href="https://yeri.be/8g" target="_blank" rel="noopener noreferrer">create script</a></li>
    <li style="text-align: left;">There's NO login. Use <a href="http://www.javascriptkit.com/howto/htaccess3.shtml" target="_blank" rel="noopener noreferrer">.htaccess files</a> to disable world wide access.</li>
</ul>
<p><strong>Screenshots</strong>:</p>
<p style="text-align: center;"><a href="https://static.yeri.be/2010/02/Screen-shot-2010-02-08-at-13.27.49.png" target="_blank" rel="noopener noreferrer"><img class="size-medium wp-image-951 aligncenter" title="Todo list" src="https://static.yeri.be/2010/02/Screen-shot-2010-02-08-at-13.27.49-300x194.png" alt="" width="300" height="194" /></a></p>
<p style="text-align: center;"><a href="https://static.yeri.be/2010/02/Screen-shot-2010-02-08-at-13.28.04.png" target="_blank" rel="noopener noreferrer"><img class="size-medium wp-image-952 aligncenter" title="Add todo item" src="https://static.yeri.be/2010/02/Screen-shot-2010-02-08-at-13.28.04-300x87.png" alt="" width="300" height="87" /></a></p>
<p style="text-align: center;"><a href="https://static.yeri.be/2010/02/Screen-shot-2010-02-08-at-13.28.31.png" target="_blank" rel="noopener noreferrer"><img class="size-medium wp-image-953 aligncenter" title="Archive" src="https://static.yeri.be/2010/02/Screen-shot-2010-02-08-at-13.28.31-300x154.png" alt="" width="300" height="154" /></a></p>
<p style="text-align: left;"><strong>Download</strong>:</p>
<p style="text-align: left;"><a href="https://static.yeri.be/2010/02/todo.tar" target="_blank" rel="noopener noreferrer">version 0.1</a></p>
<p style="text-align: left;">Feel free to edit and use this simple agenda however you want.</p>
]]></content:encoded>
      <category>misc</category><category>www</category>
      <category>agenda</category><category>mysql</category><category>php</category><category>todo</category>
    </item>
    
  </channel>
</rss>
