Bug #15998 Sometimes, mysql_real_connect() fails.
Submitted: 27 Dec 2005 4:36 Modified: 23 Feb 2006 15:03
Reporter: Yasufumi Kinoshita Email Updates:
Status: Can't repeat Impact on me:
None 
Category:MySQL Server Severity:S3 (Non-critical)
Version:5.0.16 OS:Linux (SuSE Linux Professional 9.3)
Assigned to: CPU Architecture:Any

[27 Dec 2005 4:36] Yasufumi Kinoshita
Description:
Sometimes, mysql_real_connect() fails in like this, though the server works normally.
errno:2003
error:"Can't connect to MySQL server on 'localhost' (111)"

Server parameter "thread_cache" don't affect this phenomena.

How to repeat:
Make following source.

Execute again and again.
(e.g.) bash> while (true); do ./mysql_session_test ; done

Wait for several minutes.

And, you'll get these messages..
3:2003 Can't connect to MySQL server on 'localhost' (111) HY000
0:2003 Can't connect to MySQL server on 'localhost' (111) HY000
2:2003 Can't connect to MySQL server on 'localhost' (111) HY000
4:2003 Can't connect to MySQL server on 'localhost' (111) HY000
1:2003 Can't connect to MySQL server on 'localhost' (111) HY000
2:2003 Can't connect to MySQL server on 'localhost' (111) HY000
....

--------------------------<mysql_session_test.c>-------------------------
/*
 * gcc -O2 -o mysql_session_test mysql_session_test.c `mysql_config --cflags` `mysql_config --libs_r`
 *
 * (eg) bash> while (true); do ./mysql_session_test ;done
 */

#include <stdio.h>
#include <pthread.h>
#include <mysql.h>

#define USER     "test"
#define PASS     "test"
#define DATABASE "test"
#define SESSIONS 20

int thread_main(int t_num);

int main( int argc, char *argv[] )
{
    int i, t_num;
    pthread_t *t;
    int num_conn;

    num_conn = SESSIONS;

    t = malloc( sizeof(pthread_t) * num_conn );

    for( t_num=0; t_num < num_conn; t_num++ ){
        pthread_create( &t[t_num], NULL, (void *)thread_main, (void *)t_num );
    }

    for( i=0; i < num_conn; i++ ){
        pthread_join( t[i], NULL );
    }

    free(t);
}

int thread_main (int t_num)
{
    MYSQL mysql_object;
    MYSQL *mysql;
    MYSQL *resp;

    mysql = &mysql_object;
    mysql = mysql_init(mysql);

    resp = mysql_real_connect(mysql, "localhost", USER, PASS, DATABASE, 0, NULL, 0);
    if(!resp){
        printf("%d:%d %s %s\n",t_num,mysql_errno(mysql),mysql_error(mysql),mysql_sqlstate(mysql));
    }

    sleep(1);

    mysql_close(mysql);

}
-------------------------------------------------------------------------------
[10 Feb 2006 15:57] Valeriy Kravchuk
Thank you for a problem report. Sorry, but even after waiting for 15 minutes and many iterations of your test program on SuSE 9.3 connecting to 5.0.19-BK I've got no connection errors (with default server variables - empty my.cnf):

openxs@suse:~/dbs/5.0> while true; do ./15998; echo -n '.'; done
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
openxs@suse:~/dbs/5.0> uname -a
Linux suse 2.6.11.4-20a-default #1 Wed Mar 23 21:52:37 UTC 2005 i686 i686 i386 GNU/Linux

Should I simply wait longer? Can you describe your hardware also? Is it SMP?
[22 Feb 2006 1:24] Yasufumi Kinoshita
Thank you for your kind response.
I've tried to reproduce this phenomenon by 5.0.18 too.
And I've got only a little error. I think it's all right.
But when I tried by 5.0.16, I got many errors certainly.

Can I understand as has already corrected in 5.0.18?

To make sure, I'll test at the intrinsical environment by 5.0.18 if I'm having a chance.
[22 Feb 2006 7:53] Valeriy Kravchuk
So, it looks like you was not able to repeat the same erroneous behaviour with 5.0.18. Please, inform about the results of your further testing. 

You had not answered my question:

"Can you describe your hardware also? Is it SMP?"

My test proved is that there is no bug in 5.0.19 running on single CPU.
[22 Feb 2006 8:14] Yasufumi Kinoshita
In the both of cases SMP and not-SMP, I've got the errors (5.0.16).

not-SMP: Pentium 4 2.0A x 1
SMP:       Xeon MP 3.6(64bit) x 4 
              Xeon MP ?GHz (64bit) x 2
[22 Feb 2006 8:19] Valeriy Kravchuk
OK, so SMP does not matter. Then my test proves the bug was somehow fixed in 5.0.19. Please, inform about the results of your further tests with 5.0.18.
[23 Feb 2006 15:03] Valeriy Kravchuk
I can not repeat the behaviour described on 5.0.19 on Linux.