title: mysql_real_connect() in C fails with "Access denied for user"
body: <p>I have been dealing all day with this problem, and I can't find a solution.
  I can't connect to mariadb from C (fedora24) using mysql_real_connect(). It always fails with Access denied for user, Error Code 1045, SQL State 28000</p>
  
  <p>All proposed solutions in internet are related to wrong password, wrong interface (localhost and not 127.0.0.1, for instance) or privileges, and so people could not connect using sql client.
  But the fact is that I can connect using the sql client, with my user and also with root. I just can't with the C function. Using mysql -u -p works for both users, root and my user (userdb). I am even using phpMyAdmin, and it works with both users.</p>
  
  <p>I am connecting explicitly to 127.0.0.1, to use TCP socket and not Unix socket, in order to monitor messages using Wireshark. The response is always :</p>
  
  <pre><code>MySQL Protocol
  Packet Length: 72
  Packet Number: 2
  Error Code: 1045
  SQL state: 28000
  Error message: Access denied for user 'root'@'localhost' (using password: YES)
  </code></pre>
  
  <p>root and localhost changing, depending if I use root, userdb or a diffeent interface.</p>
  
  <p>If I check users and passwords I have this :</p>
  
  <pre><code>MariaDB [mysql]&gt; select user,host,password from mysql.user;
  +--------+-----------+-------------------------------------------+
  | user   | host      | password                                  |
  +--------+-----------+-------------------------------------------+
  | root   | localhost | *B94FFB596FCEC97E64467B3274F7C739C42FFA7A |
  | root   | 127.0.0.1 | *B94FFB596FCEC97E64467B3274F7C739C42FFA7A |
  | root   | ::1       | *B94FFB596FCEC97E64467B3274F7C739C42FFA7A |
  | userdb | %         | *A2CF089828EE5EEA40C84C6454E97650B87345A9 |
  +--------+-----------+-------------------------------------------+
  </code></pre>
  
  <p>My simplest test is this :</p>
  
  <pre><code>void finish_with_error( MYSQL *con )
  {
      fprintf( stderr, "%s\n", mysql_error( con ) );
      mysql_close( con );
      exit( EXIT_FAILURE );
  }
  
  int main( int argc, char **argv )
  {
      MYSQL *con = mysql_init( NULL );
  
      if ( con == NULL )
      {
          fprintf( stderr, "%s\n", mysql_error( con ) );
          exit( EXIT_FAILURE );
      }
  
      if ( mysql_real_connect( con, "127.0.0.1", "root", "asd8814*",
                               "mysql", 0, NULL, 0 ) == NULL )
      {
          finish_with_error( con );
      }
  </code></pre>
  
  <p>Any ideas?</p>
  
  <p>Thanks in advance.</p>
  
  <p>Luis</p>
  
  <pre><code>******** UPDATE *********
  </code></pre>
  
  <p>As I was sure privileges and password were OK, as I could connect with phyMyAdmin and also mysql client, I started checking the password validation. That was the only thing left, in my opinion, that could cause problems only with C client and not the others. Both Mariadb and my C client are using mysql_native_password, as I could verify with Wireshark.</p>
  
  <p>I deleted the password in MariaDB for that user and it connected successfully.
  After that I created a new password using phpMyAdmin and still connects successfully.
  It may seem as I had a wrong password, but then, why could I connect with phpMyAdmin and mysql client?</p>
  
  <p>I repeated the procedure with root user. I could connect to phpMyAdmin with root, so I deleted the password for root@127.0.0.1 and still could not connect from C. I deleted password for root@localhost and then C client connected.</p>
  
  <p>First question : why it connected with no password at root@localhost if I am calling mysql_real_connect() with 127.0.0.1 instead of localhost (and I confirm I am using TCP using Wireshark)</p>
  
  <p>Second question : I re-created same previous password for root@localhost using phpMyAdmin and now C client can also connect (as so does mysql client and phpMyAdmin).</p>
  
  <p>Unknown answers for those questions. I have no idea why password validation did not work before and why it works now.</p>