I am working with the C-API to MYSQL Ver 12.16 Distrib 4.0.5a-beta,
for pc-linux (i686).
I have noticed the following problem. Within my C program I issue
---
conn = mysql_init(NULL);
conn = mysql_real_connect(conn,"localhost",...etc...);
if (conn == NULL)
{
printf("Connection unsuccessful...\n");
}
else
{
printf("Connection successful...\n");
}
---
conn is thankfully non-NULL and I can verify a successful connection
using
mysql_ping(conn) for example. However, if instead I do (using
loopback TCP/IP)
conn = mysql_init(NULL);
conn = mysql_real_connect(conn,"127.0.0.1",...etc...);
no connection is made (conn is NULL)! and of course the ping results
in a segmentation fault. :(
I have also tried this using the IP address of the machine.
Question 1: Why does "localhost" work but "127.0.0.1 (the loopback
interface) or IP address etc not work?? NULL also works so I assume it
has something to do with the differences between a UNIX socket and
TCP/IP connection. Is there a way to verify??
I should mention that I am using mysql on a server that I use ssh to
get to.
I ssh to the server compile C programs etc all on the same server
where the mysql is I just use ssh to get there. However, running
mysql from the prompt and from within Bourne shell scripts has worked
without any problems.
Question 2: When using the "localhost" designation (UNIX socket) I
can issue a mysql_init and mysql_real_connect as mentioned above in a
sub-function called from my main() program. This connection is
verified using mysql_ping within the subfunction, however the conn
(MYSQL connection). However, after the call to the subfunction, the
conn is lost. Why does the conn fall out of scope?? Is there
something about a UNIX socket that falls out of scope??
Similarly, if I issue mysql_init in main() but not in the subfunction
the mysql_real_connect call in the subfunction fails. Thus, it seems
mysql_init and mysql_real_connect must be issued together and that
conn somehow falls out of scope when switching between subfunction or
main program. Obviously this has drastic consequences in terms how I
can program so I'm hoping there is a way to have conn not fall out of
scope.
-------------Here is full code for reference -------------
#include <stdio.h>
#include <mysql.h>
void MYSQL_OPEN(MYSQL *conn)
{
int chk;
char *ERR_MSG;
printf("In subfunction MYSQL_OPEN");
printf("Opening connection to MYSQL server....\n");
conn = mysql_init(NULL);
conn = mysql_real_connect(conn,"localhost","blah","blah_b lah","blah_blah_blah",0,NULL,0);
if (conn == NULL)
{
printf("Connection unsuccessful...\n");
}
else
{
printf("Connection successful...\n");
}
if( mysql_ping(conn) == 0)
{
printf("Ping of server successful...\n");
}
else
{
printf("Ping of server unsuccessful...\n");
}
if (mysql_errno(conn) == 0)
{
printf("Connection made to MYSQL server.\n");
}
else
{
ERR_MSG=mysql_error(conn);
printf("%s\n",ERR_MSG);
}
chk = mysql_select_db(conn,"dpdoughe");
if (chk != 0)
{
printf("Unsuccessful database select\n");
}
else
{
printf("Successful database select\n");
}
chk=mysql_query(conn,"SHOW TABLES");
if (chk != 0)
{
printf("Unsuccessful query\n");
}
else
{
printf("Successful query\n");
}
}
void MYSQL_CLOSE(MYSQL *conn)
{
printf("Closing connection to MYSQL server...\n");
mysql_close(conn);
}
int main(int argc, char *argv[])
{
int chk=0;
MYSQL *conn;
char *ERR_MSG;
MYSQL_RES *STACK;
MYSQL_ROW ROW;
MYSQL_OPEN(conn);
chk = mysql_select_db(conn,"dpdoughe");
if (chk != 0)
{
printf("Unsuccessful database select\n");
}
conn = mysql_init(NULL);
chk=mysql_query(conn,"SHOW TABLES");
if (chk != 0)
{
printf("Unsuccessful query\n");
}
conn = mysql_init(NULL);
STACK=mysql_use_result(conn);
if (STACK==NULL)
{
conn = mysql_init(NULL);
ERR_MSG=mysql_error(conn);
printf("Query result is NULL...\n");
printf("%s\n",ERR_MSG);
}
else
{
printf("Sucessful query...\n");
}
/*conn = mysql_init(conn);
ROW=mysql_fetch_row(STACK);
*/
/*(if (chk!=0)
{
printf("Unsuccessful query.\n");
}
*/
MYSQL_CLOSE(conn);
return(0);
}