title: "No route to host" (HY000/2002) mysqli error on a remote server every now and then - what can I do besides contacting the host?
body: <p>Every now and then, I get following error: <code>Warning: mysqli_connect(): (HY000/2002): No route to host in</code> <em>(path to my mysqli_connect)</em>. My site is on a remote server owned by my webhost, so the problem might be on their side, but before accusing them of causing such an error, I want to make sure it's not my fault. In fact, I expect it to be my fault, from several reasons:</p>
  
  <ul>
  <li>the hosting company is one of the most popular in my country, and still Google search of HY000/2002 and its name found nothing relevant. Seems others don't have this problem with them.</li>
  <li>I first encountered this problem after previous major change of my code (php, mysql, few javascript snippets). It could be just a coincidence, but more probably it is not.</li>
  <li>if it was a server fault, I would expect the connection to crash and remain dead for some time. However, after each such error I can just reload the page and the problem is over... for few more reloads.</li>
  </ul>
  
  <p><strong>The pattern I have found yet is that there is some (~1%) chance of triggering the bug while running mysqli_connect.</strong> Each function working with database has its own include of the <code>db.php</code> file (where the database is initialized), so parts of code where lots of nested database-calling functions are called are quite dangerous (up to about 10% that the reload will lead to a crash), while those where all the queries are done without function calls are "safe".</p>
  
  <p>I can think of few workarounds minimizing the probability of the crash, but they wouldn't solve the main problem: that mysqli_connect fails sometimes and I have no idea why, or what does trigger it.</p>
  
  <p>What I have changed before I first encountered this problem:</p>
  
  <ul>
  <li>I turned most of my mysqli queries into prepared statements. Few functions were versatile - if I wanted to feed them with parametrizable arguments instead of custom SQL code chunk, I would have to split them into 10+ other functions. I know keeping non-parametrized queries is a bad practice, but I made sure I sanitized every content that might get to the function, so changing them to 100% safe parametrized queries seems like a small priority task. Unless it causes the problems somehow - I thought so for some time, because my "mysqli_query" functions tend to be called in the most "dangerous" parts of my site.</li>
  <li>upgrade from static to JavaScript Google Maps API</li>
  <li>some changes to the tables in my database, but not to the database itself</li>
  <li>cleaning .htaccess file (previously I have forbidden caching for testing reasons - no reason for this on live site)</li>
  </ul>
  
  <p>Here is my <code>db.php</code> file - the same as it was before the problems started (the password is not real):</p>
  
  <pre><code>&lt;?php
    $spojeni=mysqli_connect('mysql01','pavel','mypasswd');
    if (!$spojeni) die('Nepodařilo se připojit k databázi.');
    mysqli_query($spojeni, "USE ehistory");
    mysqli_set_charset($spojeni,'utf8');
    mysqli_query($spojeni, "SET COLLATION_CONNECTION = 'utf8_czech_ci';");
  ?&gt;
  </code></pre>
  
  <p>I can't post whole site (or even all queries) here, so comment and ask if you think some part of my code should be important; in such a case, add explanation why it might matter.</p>
  
  <p>EDIT: I asked my webhost and received the IP address that should be substituted for "mysql01". I replaced "mysqli01" with the IP address and nothing changed. What else can I do?</p>