Bug #5007 Can't get C API to recognize Option File

Submitted: 11 Aug 2004 23:02 Modified: 11 Aug 2004 23:44
Reporter: David Johnson Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server Severity:S3 (Non-critical)
Version:4.0.20 OS:Solaris (Solaris 9 (aka SunOS 2.9))
Assigned to: Matthew Lord CPU Architecture:Any

[11 Aug 2004 23:02] David Johnson
Description:

	In spite of many different calls to mysql_options(), I have not
        been able to get the C API to recognize an Option File.  I have
        also tried many different calls to mysql_real_connect().  I can
        use the standard .my.cnf with the mysql client, but the only way
        I can get to my data with the C API is to "hard code" user and
        password values into the mysql_real_connect() call.  the error
        message I get with the C API is:
     Access denied for user: 'ccnjdcj@localhost' (Using password: NO)

How to repeat:
/**********************************************************************
*
* File:      myslistdb.c
* Version:   1.0
* Modified:  08/10/04
* Author:    David Johnson
*
* Description:
* This program lists databases that the user who runs the program
* has access to on this 'localhost' MySQL server.
*
* Environment Variables Required:
*    none
*
* Parameters:
*    none
*
* Sample Call:  myslistdb
*
* INPUTS:
*    Choices made by the user
*
* OUTPUTS:
*    Based on the inputs, the appropriate database is
*    queried.  If the user chooses, the results of each
*    query are displayed.
*
* Modifications:
*  07/29/04  D.Johnson          Original Version
**********************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <mysql.h>

#define    MAX_CHARS    80     /* maximum number of characters in input line */
#define    ECHO_QUERY    1
#define    SHOW_RESULTS  1
#define    NO_ECHO       0
#define    NO_RESULTS    0

MYSQL mysql;

main (argc, argv)

int   argc;     /* argument count */
char *argv[];   /* list of arguments */

{
  int        max = MAX_CHARS;
  int        res;              /* result of stat call */
  char       line [MAX_CHARS];
  char      *hostname = line;         /* pointer to buffer */
  char       query_string [MAX_CHARS];

  int do_query(char *query_string, int echo_query, int show_results);
  res=gethostname(hostname, max);
  if (res == 0) {
       /******   printf (">>Hostname=%s<<.\n", hostname);   ******/
  } else {
     printf ("  Result of FAILED gethostname call = %d\n", res);
  }

  mysql_init(&mysql);
/*************************   OPTIONS TO TRY
  strcpy(line, argv[0]);
  strcpy(line, "client");
  strcpy(line, "myslistdb");
  strcpy(line, "");
  strcpy(line, "qzjxv");
***************************/
  strcpy(line, "client");
  res = mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP, line);
  if (res != 0) {
     printf ("  FAILED mysql_options = %d for Grp=%s\n", res, line);
  }
/*************************   OPTIONS TO TRY
  if (!mysql_real_connect(&mysql,hostname,"","","",0,NULL,0)) {
  if (!mysql_real_connect(&mysql,"host","user","passwd","database",0,NULL,0)) {
  if (!mysql_real_connect(&mysql,"","","","",0,NULL,0)) {
  if (!mysql_real_connect(&mysql,"localhost","ccnjdcj","","",0,NULL,0)) {
***************************/
  if (!mysql_real_connect(&mysql,"","","","",0,NULL,0)) {
    fprintf(stderr, "Failed to connect to database: Error= %s\n",
         mysql_error(&mysql));
  } else {
/*************************
    fprintf(stderr, "Successful connect to MySQL Server %s\n", p);
***************************/

    strcpy(query_string, "select user()");
    if (do_query(query_string, ECHO_QUERY, SHOW_RESULTS)) {
       fprintf(stderr, "Failed to select user(): Error= %s\n",
         mysql_error(&mysql));
    }

    strcpy(query_string, "show databases");
    if (do_query(query_string, ECHO_QUERY, SHOW_RESULTS)) {
       fprintf(stderr, "Failed to list databases: Error= %s\n",
         mysql_error(&mysql));
    }

    strcpy(query_string, "use test");
    if (do_query(query_string, ECHO_QUERY, SHOW_RESULTS)) {
       fprintf(stderr, "Failed to select test: Error= %s\n",
    if (do_query(query_string, ECHO_QUERY, SHOW_RESULTS)) {
       fprintf(stderr, "Failed to select test: Error= %s\n",
         mysql_error(&mysql));
    }

    strcpy(query_string, "show tables");
    if (do_query(query_string, ECHO_QUERY, SHOW_RESULTS)) {
       fprintf(stderr, "Failed to show tables: Error= %s\n",
         mysql_error(&mysql));
    }

/*************************
    strcpy(query_string, "show table status like '%'");
    if (do_query(query_string, ECHO_QUERY, SHOW_RESULTS)) {
       fprintf(stderr, "Failed to show table status: Error= %s\n",
         mysql_error(&mysql));
    }
***************************/

  mysql_close(&mysql);
  } /*** Successful connect to database ***/
}

int do_query(char *query_string, int echo_query, int show_results) {
  MYSQL_RES *result;
  MYSQL_ROW  row;
  int        num_fields;
  int        num_rows;
  long      *lengths;
  int        i;
    if (echo_query) {
       fprintf(stderr, "==MYSQL Query=%s\n", query_string);
    }
    if (mysql_query(&mysql, query_string)) {
       return 1;  /* query failed */
    } else { /*** Successful query ***/
/***************
       fprintf(stderr, "Successful Query\n");
******************/
       result = mysql_store_result(&mysql);
       if (result) {   // there are rows
          num_fields = mysql_num_fields(result);
/***************
          fprintf(stderr, "There were %d fields.\n", num_fields);
******************/
          if (show_results) {
             // retrieve rows
             while((row = mysql_fetch_row(result))) {
                 // do something with data
                num_fields = mysql_num_fields(result);
                lengths = mysql_fetch_lengths(result);
                for (i = 0; i < num_fields; i++) {
/***************
                   printf("Column %u is %lu bytes in length.\n", i, lengths[i]);
******************/
                   printf("    %.*s\n", (int) lengths[i], row[i] ? row[i] : "NUL
L");
                }
             }
          }
          if (mysql_errno(&mysql)) { // mysql_fetch_row() failed due to an error
             fprintf(stderr, "Fetch_Row Error: %s\n", mysql_error(&mysql));
          }
          mysql_free_result(result);
       } else { // mysql_store_result() returned nothing; should it have?
          if (mysql_field_count(&mysql) == 0) {
                 // query does not return data
                 // (it was not a SELECT)
                 num_rows = mysql_affected_rows(&mysql);
          } else {   // mysql_store_result() should have returned data
            fprintf(stderr, "Store_Result Error: %s\n", mysql_error(&mysql));
          }
       }
       return 0;  /* query succeeded */
    } /*** Successful query ***/
}

>Fix:
         unknown

>Submitter-Id:	David.C.Johnson@eds.com
>Originator:	David C. Johnson
>Organization:
  EDS
>MySQL support: none
>Synopsis:	Can't get C API to recognize Option File
>Severity:	non-critical
>Priority:	low
>Category:	mysql
>Class:		sw-bug
>Release:	mysql-4.0.20 (binary Solaris distribution)

>C compiler:    2.95.3
>C++ compiler:  2.95.3
>Environment:
System: SunOS blade2 5.9 Generic_112233-12 sun4u sparc SUNW,Sun-Blade-100
Architecture: sun4

Some paths:  /usr/bin/perl /usr/ccs/bin//make

Compilation info: CC='gcc'  CFLAGS='-Wimplicit -Wreturn-type -Wswitch -Wtrigraphs -Wcomment -W -Wchar-subscripts -Wformat -Wparentheses -Wsign-compare -Wwrite-strings -Wunused -mcpu=pentiumpro -O3 -fno-omit-frame-pointer'  CXX='ccache gcc'  CXXFLAGS='-Wimplicit -Wreturn-type -Wswitch -Wtrigraphs -Wcomment -W -Wchar-subscripts -Wformat -Wparentheses -Wsign-compare -Wwrite-strings -Woverloaded-virtual -Wsign-promo -Wreorder -Wctor-dtor-privacy -Wnon-virtual-dtor -felide-constructors -fno-exceptions -fno-rtti -mcpu=pentiumpro -O3 -fno-omit-frame-pointer'  LDFLAGS=''  ASFLAGS=''
LIBC: 
-rw-r--r--   1 root     bin      1857244 Feb 10  2004 /lib/libc.a
lrwxrwxrwx   1 root     root          11 Jul 13 09:54 /lib/libc.so -> ./libc.so.1
-rwxr-xr-x   1 root     bin       867112 Feb 10  2004 /lib/libc.so.1
-rw-r--r--   1 root     bin      1857244 Feb 10  2004 /usr/lib/libc.a
lrwxrwxrwx   1 root     root          11 Jul 13 09:54 /usr/lib/libc.so -> ./libc.so.1
-rwxr-xr-x   1 root     bin       867112 Feb 10  2004 /usr/lib/libc.so.1
Configure command: ./configure '--prefix=/usr/local/mysql' '--enable-assembler' '--with-extra-charsets=complex' '--with-innodb' '--with-berkeley-db' '--with-embedded-server' '--enable-thread-safe-client' '--with-openssl' '--with-vio' '--with-raid' '--enable-local-infile' 'CFLAGS=-Wimplicit -Wreturn-type -Wswitch -Wtrigraphs -Wcomment -W -Wchar-subscripts -Wformat -Wparentheses -Wsign-compare -Wwrite-strings -Wunused -mcpu=pentiumpro -O3 -fno-omit-frame-pointer' 'CXXFLAGS=-Wimplicit -Wreturn-type -Wswitch -Wtrigraphs -Wcomment -W -Wchar-subscripts -Wformat -Wparentheses -Wsign-compare -Wwrite-strings -Woverloaded-virtual -Wsign-promo -Wreorder -Wctor-dtor-privacy -Wnon-virtual-dtor -felide-constructors -fno-exceptions -fno-rtti -mcpu=pentiumpro -O3 -fno-omit-frame-pointer' 'CXX=ccache gcc'

Suggested fix:
none
[11 Aug 2004 23:44] Matthew Lord
Dear Sir,

Thank you for your bug report!

I don't see any bug here.  Please read this closely:
http://dev.mysql.com/doc/mysql/en/mysql_options.html
You should also look at the my_print_defaults application.

If you do not have a support contract you can take advantage of our
community based resources:
mysql.com/IRC
lists.mysql.com

Best Regards

Portions of this website are copyright © 2001, 2002 The PHP Group

Page generated in 0.051 sec. using MySQL 8.4.7-u6-cloud

Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.