Thread: access denied!

  1. #1
    Registered User joybanerjee39's Avatar
    Join Date
    Oct 2011
    Location
    kolkata
    Posts
    106

    access denied!

    i am starting to learn to connect a cprogram to a mysql database.

    i have got this elementary program to connect to server , create a database and disconnect from it.
    Code:
    #include <stdio.h>
    #include<windows.h>
    #include <mysql.h>
    static char *opt_host_name = NULL; /* server host (default=localhost) */
    static char *opt_user_name =NULL; /* username (default=login name) */
    static char *opt_password = NULL; /* password (default=none) */
    static unsigned int opt_port_num = 0; /* port number (use built-in value) */
    static char *opt_socket_name = NULL; /* socket name (use built-in value) */
    static char *opt_db_name = NULL; /* database name (default=none) */
    static unsigned int opt_flags = 0; /* connection flags (none) */
    static MYSQL *conn; /* pointer to connection handler */
    int
    main (int argc, char *argv[])
    {
    /* initialize connection handler */
    conn = mysql_init (NULL);
    if (conn == NULL)
    {
    fprintf ( stderr,"mysql_init() failed (probably out of memory)\n");
    exit (1);
    }
    /* connect to server */
    if (mysql_real_connect (conn, opt_host_name, opt_user_name, opt_password,
    opt_db_name, opt_port_num, opt_socket_name, opt_flags) == NULL)
    {
    fprintf (stderr, "mysql_real_connect() failed:\nError %u (%s)\n",
    mysql_errno (conn), mysql_error (conn));
    mysql_close (conn);
    exit (1);
    }
    
    //create database
    if (mysql_query(conn, "create database firstdata")) {
          printf("xxxError %u: %s\n", mysql_errno(conn), mysql_error(conn));
          exit(1);
      }
    
    /* disconnect from server */
    mysql_close (conn);
    exit (0);
    }

    but, when i execute it there is error message
    Code:
    access denied for user ''@'localhost'to database firstdata.
    please help!

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Looks like you have something in there that needs either an actual IP Address or the name of the host computer.

    Don't know about Linux but on Windows... a computer's name, a computer's IP address and Localhost are three different things.

  3. #3
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Do you have to set up a web server like Apache on localhost first? If so, make sure it's set up properly.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I'm far from an expert on MySQL, but I would bet you haven't properly configured database users and/or permissions. Also note that you have an empty user name. That may not be equivalent to anonymous access, and/or you may not have anonymous access allowed.

  5. #5
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Code:
    if (mysql_real_connect (conn, opt_host_name, opt_user_name, opt_password, opt_db_name, opt_port_num, opt_socket_name, opt_flags) == NULL)
    Well there's your problem. You're not giving it a host name, neither are you giving it a user name or password to log in with.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. writing to registry access denied
    By Elkvis in forum C# Programming
    Replies: 7
    Last Post: 11-21-2011, 04:30 PM
  2. Access is denied when listen on a port
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-29-2008, 09:01 AM
  3. c:\boot.ini access denied!
    By Bajanine in forum Tech Board
    Replies: 8
    Last Post: 11-17-2005, 11:52 AM
  4. access denied
    By laasunde in forum C++ Programming
    Replies: 4
    Last Post: 10-25-2002, 02:38 PM
  5. class member access denied
    By chiqui in forum C++ Programming
    Replies: 2
    Last Post: 05-27-2002, 02:02 PM