13 Feb, 2009, Zeno wrote in the 1st comment:
Votes: 0
I have this code in a raw.c file that works:
if defined(macintosh)
#include <types.h>
#else
#include <sys/types.h>
#endif
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <mysql/mysql.h>

main()
{
char output[4096];
int db_connect;
short db_last_action;
void check_db_disconnect(void);
MYSQL_RES *query_db(char *query);
MYSQL *db;
char *sql_escape_string( char *source);

if ((db = mysql_init(NULL)) == NULL)
{
printf("connect_db: error on initialize");
return;
}
if (!mysql_real_connect(db, DB_HOST, DB_USER, DB_PASS, DB_DB, 0,NULL,0))
{
/*I have a seperate loging system for boot up
remove or change as needed
*/
printf("connect_db: error on connect");
sprintf(output, "Error for %s %s %s %s %d %d %d: %s", DB_HOST, DB_USER, DB_PASS, DB_DB, DB_PORT, DB_SOCKET, DB_CLIENT_FLAG,
mysql_error(db));
printf(output);
return;
}
db_connect = 1;
db_last_action = 0;
printf("mysql connected");

return;
}

It works:
Quote
mysql connected


And in my MUD, I have this:
void connect_db()
{
char output[MAX_STRING_LENGTH];
if ((db = mysql_init(NULL)) == NULL)
{
SYS_LOG("connect_db: error on initialize");
return;
}
if (!mysql_real_connect(db, DB_HOST, DB_USER, DB_PASS, DB_DB, 0,NULL,0))
{
/*I have a seperate loging system for boot up
remove or change as needed
*/
log_string("connect_db: error on connect");
sprintf(output, "Error for: %s", mysql_error(db));
log_string(output);
return;
}
db_connect = TRUE;
db_last_action = 0;
log_string("mysql connected");

return;
}

It fails:
Quote
Thu Feb 12 20:38:27 2009 :: Error for: Access denied for user 'user'@'localhost' (using password: YES)


Note the constants (DB_USER, etc) are all defined and correct, and the same in both files.

Why is the MUD connection failing? What else could prevent it? I've even tried hard coding the username into into mysql_real_connect().
13 Feb, 2009, quixadhal wrote in the 2nd comment:
Votes: 0
You're running both sets of code on the same machine, targeting the same database?

I ask because MySQL has some very odd ways it defines permissions, and 'user'@'localhost' doesn't always match 'user'@'machinename' or even 'user'@'127.0.0.1'.
13 Feb, 2009, Zeno wrote in the 3rd comment:
Votes: 0
Same machine, same DB user/db/password/localhost.

New code I've tried:
void connect_db()
{
char output[MAX_STRING_LENGTH];
MYSQL mysql;

mysql_init(&mysql);
mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"biyg");
if (!mysql_real_connect(&mysql,"localhost","root","pass","mydb",0,NULL,0))
{
sprintf(output, "Failed to connect to database: Error: %s\n", mysql_error(&mysql));
log_string(output);

}

}

I've copy pasted those user/password and they work fine in bash.
Yet my MUD is giving me
Quote
Log: Failed to connect to database: Error: Access denied for user 'root'@'localhost' (using password: YES)


Tried a MySQL restart, MUD reboot, no help.
13 Feb, 2009, Kline wrote in the 4th comment:
Votes: 0
Just curious (somewhat unrelated): SQL handler code by Kale? If so I use the same (he originally wrote it for a game we were both working on); and my only issue is sometimes losing sync to the DB (that I haven't quite solved).
13 Feb, 2009, Kline wrote in the 5th comment:
Votes: 0
Oh, and you're certain your MySQL user is setup? I note you using 'root' for the user, yet would think you'd have a less privileged account setup for your game? system root != MySQL root
13 Feb, 2009, Zeno wrote in the 6th comment:
Votes: 0
Which part? The latest code was from the MySQL website.
13 Feb, 2009, Kline wrote in the 7th comment:
Votes: 0
The code in your first post – almost a carbon copy of what I have in my game down to spacing and everything :P Just some minor log/etc edits.
13 Feb, 2009, Zeno wrote in the 8th comment:
Votes: 0
I think I ripped it from AFKMUD or the sort.
13 Feb, 2009, Guest wrote in the 9th comment:
Votes: 0
If it helps, this is what the AFKMud code uses:
void init_mysql(  )
{
if( !mysql_init( &myconn ) )
{
mysql_close( &myconn );
bug( "%s: mysql_init() failed.", __FUNCTION__ );
log_printf( "Error: %s.", mysql_error( &myconn ) );
return;
}

if( !mysql_real_connect( &myconn, sysdata->dbserver.c_str( ), sysdata->dbuser.c_str( ), sysdata->dbpass.c_str( ), sysdata->dbname.c_str( ), 0, NULL, 0 ) )
{
mysql_close( &myconn );
bug( "%s: mysql_real_connect() failed.", __FUNCTION__ );
log_printf( "Error: %s.", mysql_error( &myconn ) );
return;
}
mysql_options( &myconn, MYSQL_OPT_RECONNECT, "1" );
log_string( "Connection to mysql database established." );
}


The database connection works and remains active for the whole time the MUD is up.
13 Feb, 2009, Kline wrote in the 10th comment:
Votes: 0
Thanks! I actually never knew about mysql_options(), I'm thinking that will fix my MUD disconnecting from the DB randomly (and then segfaulting when trying to access db info :).

edit: http://dev.mysql.com/doc/refman/5.1/en/m...
mysql_options() should be called after mysql_init() and before mysql_connect() or mysql_real_connect().

Not sure how big a difference it makes, if yours works 100%? I'll move mine prior to stick with their API :)
13 Feb, 2009, Guest wrote in the 11th comment:
Votes: 0
It works, therefore I shall not fix it :)
13 Feb, 2009, Zeno wrote in the 12th comment:
Votes: 0
Argh! I copied the two code files from AFKMUD just now for SQL.

Doesn't work.
Quote
Log: [*****] BUG: init_mysql: mysql_real_connect() failed.
Log: Error: Access denied for user 'user'@'localhost' (using password: YES).


100% sure I'm using the right user/pass.

Root doesn't work either.
13 Feb, 2009, David Haley wrote in the 13th comment:
Votes: 0
Are you absolutely sure that the MySQL permissions identify "localhost" as a valid host? Note that 127.0.0.1 might not be the same thing…
13 Feb, 2009, Zeno wrote in the 14th comment:
Votes: 0
I've tried 127.0.0.1 as well, same error.
13 Feb, 2009, David Haley wrote in the 15th comment:
Votes: 0
No, not the host you connect to, but the host MySQL thinks you're coming from. Does the MySQL permission table have entries for both localhost and 127.0.0.1?
13 Feb, 2009, Zeno wrote in the 16th comment:
Votes: 0
It does for root, which fails as well.

I think it has to do with my Makefile, the only difference between the raw code (connects fine) and my MUD code (doesn't connect) is the Makefile & compiling.
13 Feb, 2009, Zeno wrote in the 17th comment:
Votes: 0
Got it, lawl.

See my Makefile thread. Once I changed the lib64 and fixed the errors, it connects.
13 Feb, 2009, David Haley wrote in the 18th comment:
Votes: 0
Wait, we were talking about a codebase that hadn't compiled correctly? How were you running this in the first place? It might have been helpful for us to know there were compiling issues here… :rolleyes:
13 Feb, 2009, Zeno wrote in the 19th comment:
Votes: 0
It was compiling without errors or warnings, even though that lib64 dir never existed.

The MySQL functions ran just fine (mysql_init etc)… up to the point of trying to connect.

It's one big wtf.
14 Feb, 2009, Rojan QDel wrote in the 20th comment:
Votes: 0
Clearly, this is not a problem with your MySQL C code. Take a look at this tutorial: http://www.devarticles.com/c/a/MySQL/Cre...

As far as I can tell, your user does not have proper permissions for the given host, or table, or action on that table.
0.0/21