Darksaint2014
Darksaint2014

Reputation: 172

libmysql mysql_real_connect fails with IP address but works with localhost

Okay so my issue is pretty straight forward, I am relatively new to C++ so this may be an easy answer for some, but I am using libmysql to connect to a database and so far it works just as I need it too. But I want to be able to connect to a database not found at local host, the database will still be on my LAN but just not on the local machine. I am pretty sure that the unix_socket parameter is all that i need to change but i have no clue how or what to change it to or how to make a "unix_socket". Below i will post my code and thank you in advanced for help thanks!

void readFromDB(){
skip:
     MYSQL *connect; // Create a pointer to the MySQL instance
connect=mysql_init(NULL); // Initialise the instance
/* This If is irrelevant and you don't need to show it. I kept it in for Fault Testing.*/
if(!connect)    /* If instance didn't initialize say so and exit with fault.*/
{
    fprintf(stderr,"MySQL Initialization Failed");


}
/* Now we will actually connect to the specific database.*/

connect=mysql_real_connect(connect,"10.1.3.253",USER,PASSWORD,DATABASE,3306, NULL,0);
/* Following if statements are unneeded too, but it's worth it to show on your
first app, so that if your database is empty or the query didn't return anything it
will at least let you know that the connection to the mysql server was established. */

if(connect){
    printf("First Connection Succeeded\n");
            system("PAUSE");
}
else{
    printf("Connection Failed!\n");
            Sleep(800);
            goto skip;
}
MYSQL_RES *result; /* Create a pointer to recieve the return value.*/
MYSQL_ROW row;  /* Assign variable for rows. */

//mysql_query(connect,"SELECT * FROM locationTime");
/* Send a query to the database. */
    if(!(mysql_query(connect,"SELECT * FROM locationtime") == 0)){
            mysql_close(connect);
            Sleep(300);
            goto skip;
    }
result = mysql_store_result(connect); /* Receive the result and store it in res_set */


unsigned int numrows = mysql_num_rows(result); /* Create the count to print all rows */

/* This while is to print all rows and not just the first row found, */
    if(numrows != 0){
            while ((row = mysql_fetch_row(result)) != NULL){
                    ids.push_back(row[0]);
                    dateTime.push_back(row[1]);
                    locs.push_back(setLocation(row[2]));
                    strLocs.push_back(row[2]);
                    imgPaths.push_back(row[3]);

                    //for(int i = 0; i<(sizeof(row)  ); i++){
                    //        //printf("%s\n",row[i] != NULL ? row[i] : "NULL"); /* Print the     row data */
                    //}
                    //cout<<endl<<endl;
            }
    }else if(numrows <= 0){
            mysql_close(connect);   /* Close and shutdown */
            mysql_free_result(result);
            Sleep(200);
            goto skip;
    }
mysql_close(connect);   /* Close and shutdown */
    mysql_free_result(result);
return;
}

and here is my header file:

#include "my_global.h" // Include this file first to avoid problems
#include "mysql.h" // MySQL Include File
#define SERVER "10.1.3.253"
#define USER "user"
#define PASSWORD "pass"
#define DATABASE "DBName"

Upvotes: 1

Views: 3942

Answers (1)

tadman
tadman

Reputation: 211600

Usually if you're having a problem connecting via IP it's that your user account is not allowed to connect from a "remote" address.

Normally this is fixed by:

GRANT ALL PRIVILEGES ON *.* TO `user`@`%` IDENTIFIED BY '...'

Where user is your username. You may want to lock down access more, obviously.

localhost is what's used for "local" connections via UNIX socket. All others are treated as remote.

Upvotes: 2

Related Questions