uint32 MySQLConnection::Open() { MYSQL *mysqlInit; mysqlInit = mysql_init(NULL); if (!mysqlInit) { TC_LOG_ERROR("sql.sql", "Could not initialize Mysql connection to database `%s`", m_connectionInfo.database.c_str()); return CR_UNKNOWN_ERROR; } int port; char const* unix_socket; //unsigned int timeout = 10; mysql_options(mysqlInit, MYSQL_SET_CHARSET_NAME, "utf8"); //mysql_options(mysqlInit, MYSQL_OPT_READ_TIMEOUT, (char const*)&timeout); #ifdef _WIN32 if (m_connectionInfo.host == ".") // named pipe use option (Windows) { unsigned int opt = MYSQL_PROTOCOL_PIPE; mysql_options(mysqlInit, MYSQL_OPT_PROTOCOL, (char const*)&opt); port = 0; unix_socket = 0; } else // generic case { port = atoi(m_connectionInfo.port_or_socket.c_str()); unix_socket = 0; } #else if (m_connectionInfo.host == ".") // socket use option (Unix/Linux) { unsigned int opt = MYSQL_PROTOCOL_SOCKET; mysql_options(mysqlInit, MYSQL_OPT_PROTOCOL, (char const*)&opt); m_connectionInfo.host = "localhost"; port = 0; unix_socket = m_connectionInfo.port_or_socket.c_str(); } else // generic case { port = atoi(m_connectionInfo.port_or_socket.c_str()); unix_socket = nullptr; } #endif m_Mysql = mysql_real_connect(mysqlInit, m_connectionInfo.host.c_str(), m_connectionInfo.user.c_str(), m_connectionInfo.password.c_str(), m_connectionInfo.database.c_str(), port, unix_socket, 0); if (m_Mysql) { if (!m_reconnecting) { TC_LOG_INFO("sql.sql", "MySQL client library: %s", mysql_get_client_info()); TC_LOG_INFO("sql.sql", "MySQL server ver: %s ", mysql_get_server_info(m_Mysql)); // MySQL version above 5.1 IS required in both client and server and there is no known issue with different versions above 5.1 // if (mysql_get_server_version(m_Mysql) != mysql_get_client_version()) // TC_LOG_INFO("sql.sql", "[WARNING] MySQL client/server version mismatch; may conflict with behaviour of prepared statements."); } TC_LOG_INFO("sql.sql", "Connected to MySQL database at %s", m_connectionInfo.host.c_str()); mysql_autocommit(m_Mysql, 1); // set connection properties to UTF8 to properly handle locales for different // server configs - core sends data in UTF8, so MySQL must expect UTF8 too mysql_set_character_set(m_Mysql, "utf8"); return 0; } else { TC_LOG_ERROR("sql.sql", "Could not connect to MySQL database at %s: %s", m_connectionInfo.host.c_str(), mysql_error(mysqlInit)); mysql_close(mysqlInit); return mysql_errno(mysqlInit); } }
int main (int argc, char **argv) { MYSQL mysql; MYSQL_RES *res; MYSQL_ROW row; /* should be status */ char *result = NULL; char *error = NULL; char slaveresult[SLAVERESULTSIZE]; char* perf; perf = strdup (""); setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); /* Parse extra opts if any */ argv=np_extra_opts (&argc, argv, progname); if (process_arguments (argc, argv) == ERROR) usage4 (_("Could not parse arguments")); /* initialize mysql */ mysql_init (&mysql); if (opt_file != NULL) mysql_options(&mysql,MYSQL_READ_DEFAULT_FILE,opt_file); if (opt_group != NULL) mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,opt_group); else mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"client"); if (ssl) mysql_ssl_set(&mysql,key,cert,ca_cert,ca_dir,ciphers); /* establish a connection to the server and error checking */ if (!mysql_real_connect(&mysql,db_host,db_user,db_pass,db,db_port,db_socket,0)) { if (ignore_auth && mysql_errno (&mysql) == ER_ACCESS_DENIED_ERROR) { printf("MySQL OK - Version: %s (protocol %d)\n", mysql_get_server_info(&mysql), mysql_get_proto_info(&mysql) ); mysql_close (&mysql); return STATE_OK; } else if (mysql_errno (&mysql) == CR_UNKNOWN_HOST) die (STATE_WARNING, "%s\n", mysql_error (&mysql)); else if (mysql_errno (&mysql) == CR_VERSION_ERROR) die (STATE_WARNING, "%s\n", mysql_error (&mysql)); else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY) die (STATE_WARNING, "%s\n", mysql_error (&mysql)); else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR) die (STATE_WARNING, "%s\n", mysql_error (&mysql)); else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR) die (STATE_WARNING, "%s\n", mysql_error (&mysql)); else die (STATE_CRITICAL, "%s\n", mysql_error (&mysql)); } /* get the server stats */ result = strdup (mysql_stat (&mysql)); /* error checking once more */ if (mysql_error (&mysql)) { if (mysql_errno (&mysql) == CR_SERVER_GONE_ERROR) die (STATE_CRITICAL, "%s\n", mysql_error (&mysql)); else if (mysql_errno (&mysql) == CR_SERVER_LOST) die (STATE_CRITICAL, "%s\n", mysql_error (&mysql)); else if (mysql_errno (&mysql) == CR_UNKNOWN_ERROR) die (STATE_CRITICAL, "%s\n", mysql_error (&mysql)); } /* try to fetch some perf data */ if (mysql_query (&mysql, "show global status") == 0) { if ( (res = mysql_store_result (&mysql)) == NULL) { error = strdup(mysql_error(&mysql)); mysql_close (&mysql); die (STATE_CRITICAL, _("status store_result error: %s\n"), error); } while ( (row = mysql_fetch_row (res)) != NULL) { int i; for(i = 0; i < LENGTH_METRIC_UNIT; i++) { if (strcmp(row[0], metric_unit[i]) == 0) { xasprintf(&perf, "%s%s ", perf, perfdata(metric_unit[i], atol(row[1]), "", FALSE, 0, FALSE, 0, FALSE, 0, FALSE, 0)); continue; } } for(i = 0; i < LENGTH_METRIC_COUNTER; i++) { if (strcmp(row[0], metric_counter[i]) == 0) { xasprintf(&perf, "%s%s ", perf, perfdata(metric_counter[i], atol(row[1]), "c", FALSE, 0, FALSE, 0, FALSE, 0, FALSE, 0)); continue; } } } /* remove trailing space */ if (strlen(perf) > 0) perf[strlen(perf) - 1] = '\0'; } if(check_slave) { /* check the slave status */ if (mysql_query (&mysql, "show slave status") != 0) { error = strdup(mysql_error(&mysql)); mysql_close (&mysql); die (STATE_CRITICAL, _("slave query error: %s\n"), error); } /* store the result */ if ( (res = mysql_store_result (&mysql)) == NULL) { error = strdup(mysql_error(&mysql)); mysql_close (&mysql); die (STATE_CRITICAL, _("slave store_result error: %s\n"), error); } /* Check there is some data */ if (mysql_num_rows(res) == 0) { mysql_close(&mysql); die (STATE_WARNING, "%s\n", _("No slaves defined")); } /* fetch the first row */ if ( (row = mysql_fetch_row (res)) == NULL) { error = strdup(mysql_error(&mysql)); mysql_free_result (res); mysql_close (&mysql); die (STATE_CRITICAL, _("slave fetch row error: %s\n"), error); } if (mysql_field_count (&mysql) == 12) { /* mysql 3.23.x */ snprintf (slaveresult, SLAVERESULTSIZE, _("Slave running: %s"), row[6]); if (strcmp (row[6], "Yes") != 0) { mysql_free_result (res); mysql_close (&mysql); die (STATE_CRITICAL, "%s\n", slaveresult); } } else { /* mysql 4.x.x and mysql 5.x.x */ int slave_io_field = -1 , slave_sql_field = -1, seconds_behind_field = -1, i, num_fields; MYSQL_FIELD* fields; num_fields = mysql_num_fields(res); fields = mysql_fetch_fields(res); for(i = 0; i < num_fields; i++) { if (strcmp(fields[i].name, "Slave_IO_Running") == 0) { slave_io_field = i; continue; } if (strcmp(fields[i].name, "Slave_SQL_Running") == 0) { slave_sql_field = i; continue; } if (strcmp(fields[i].name, "Seconds_Behind_Master") == 0) { seconds_behind_field = i; continue; } } /* Check if slave status is available */ if ((slave_io_field < 0) || (slave_sql_field < 0) || (num_fields == 0)) { mysql_free_result (res); mysql_close (&mysql); die (STATE_CRITICAL, "Slave status unavailable\n"); } /* Save slave status in slaveresult */ snprintf (slaveresult, SLAVERESULTSIZE, "Slave IO: %s Slave SQL: %s Seconds Behind Master: %s", row[slave_io_field], row[slave_sql_field], seconds_behind_field!=-1?row[seconds_behind_field]:"Unknown"); /* Raise critical error if SQL THREAD or IO THREAD are stopped */ if (strcmp (row[slave_io_field], "Yes") != 0 || strcmp (row[slave_sql_field], "Yes") != 0) { mysql_free_result (res); mysql_close (&mysql); die (STATE_CRITICAL, "%s\n", slaveresult); } if (verbose >=3) { if (seconds_behind_field == -1) { printf("seconds_behind_field not found\n"); } else { printf ("seconds_behind_field(index %d)=%s\n", seconds_behind_field, row[seconds_behind_field]); } } /* Check Seconds Behind against threshold */ if ((seconds_behind_field != -1) && (strcmp (row[seconds_behind_field], "NULL") != 0)) { double value = atof(row[seconds_behind_field]); int status; status = get_status(value, my_threshold); xasprintf (&perf, "%s %s", perf, fperfdata ("seconds behind master", value, "s", TRUE, (double) warning_time, TRUE, (double) critical_time, FALSE, 0, FALSE, 0)); if (status == STATE_WARNING) { printf("SLOW_SLAVE %s: %s|%s\n", _("WARNING"), slaveresult, perf); exit(STATE_WARNING); } else if (status == STATE_CRITICAL) { printf("SLOW_SLAVE %s: %s|%s\n", _("CRITICAL"), slaveresult, perf); exit(STATE_CRITICAL); } } } /* free the result */ mysql_free_result (res); } /* close the connection */ mysql_close (&mysql); /* print out the result of stats */ if (check_slave) { printf ("%s %s|%s\n", result, slaveresult, perf); } else { printf ("%s|%s\n", result, perf); } return STATE_OK; }
int main( int argc, char * argv[] ) { char szSQL[ 200 ], aszFlds[ 25 ][ 25 ], szDB[ 50 ] ; const char * pszT; int i, j, k, l, x ; MYSQL * myData ; MYSQL_RES * res ; MYSQL_FIELD * fd ; MYSQL_ROW row ; //....just curious.... printf( "sizeof( MYSQL ) == %d\n", sizeof( MYSQL ) ) ; if ( argc == 2 ) { strcpy( szDB, argv[ 1 ] ) ; strcpy( szSQL, DEFALT_SQL_STMT ) ; if (!strcmp(szDB,"--debug")) { strcpy( szDB, "mysql" ) ; printf("Some mysql struct information (size and offset):\n"); printf("net:\t%3d %3d\n",sizeof(myData->net),offsetof(MYSQL,net)); printf("host:\t%3d %3d\n",sizeof(myData->host),offsetof(MYSQL,host)); printf("port:\t%3d %3d\n",sizeof(myData->port),offsetof(MYSQL,port)); printf("protocol_version:\t%3d %3d\n",sizeof(myData->protocol_version), offsetof(MYSQL,protocol_version)); printf("thread_id:\t%3d %3d\n",sizeof(myData->thread_id), offsetof(MYSQL,thread_id)); printf("affected_rows:\t%3d %3d\n",sizeof(myData->affected_rows), offsetof(MYSQL,affected_rows)); printf("packet_length:\t%3d %3d\n",sizeof(myData->packet_length), offsetof(MYSQL,packet_length)); printf("status:\t%3d %3d\n",sizeof(myData->status), offsetof(MYSQL,status)); printf("fields:\t%3d %3d\n",sizeof(myData->fields), offsetof(MYSQL,fields)); printf("field_alloc:\t%3d %3d\n",sizeof(myData->field_alloc), offsetof(MYSQL,field_alloc)); printf("free_me:\t%3d %3d\n",sizeof(myData->free_me), offsetof(MYSQL,free_me)); printf("options:\t%3d %3d\n",sizeof(myData->options), offsetof(MYSQL,options)); puts(""); } } else if ( argc > 2 ) { strcpy( szDB, argv[ 1 ] ) ; strcpy( szSQL, argv[ 2 ] ) ; } else { strcpy( szDB, "mysql" ) ; strcpy( szSQL, DEFALT_SQL_STMT ) ; } //.... if ( (myData = mysql_init((MYSQL*) 0)) && mysql_real_connect( myData, NULL, NULL, NULL, NULL, MYSQL_PORT, NULL, 0 ) ) { if ( mysql_select_db( myData, szDB ) < 0 ) { printf( "Can't select the %s database !\n", szDB ) ; mysql_close( myData ) ; return 2 ; } } else { printf( "Can't connect to the mysql server on port %d !\n", MYSQL_PORT ) ; mysql_close( myData ) ; return 1 ; } //.... if ( ! mysql_query( myData, szSQL ) ) { res = mysql_store_result( myData ) ; i = (int) mysql_num_rows( res ) ; l = 1 ; printf( "Query: %s\nNumber of records found: %ld\n", szSQL, i ) ; //....we can get the field-specific characteristics here.... for ( x = 0 ; fd = mysql_fetch_field( res ) ; x++ ) strcpy( aszFlds[ x ], fd->name ) ; //.... while ( row = mysql_fetch_row( res ) ) { j = mysql_num_fields( res ) ; printf( "Record #%ld:-\n", l++ ) ; for ( k = 0 ; k < j ; k++ ) printf( " Fld #%d (%s): %s\n", k + 1, aszFlds[ k ], (((row[k]==NULL)||(!strlen(row[k])))?"NULL":row[k])) ; puts( "==============================\n" ) ; } mysql_free_result( res ) ; } else printf( "Couldn't execute %s on the server !\n", szSQL ) ; //.... puts( "==== Diagnostic info ====" ) ; pszT = mysql_get_client_info() ; printf( "Client info: %s\n", pszT ) ; //.... pszT = mysql_get_host_info( myData ) ; printf( "Host info: %s\n", pszT ) ; //.... pszT = mysql_get_server_info( myData ) ; printf( "Server info: %s\n", pszT ) ; //.... res = mysql_list_processes( myData ) ; l = 1 ; if (res) { for ( x = 0 ; fd = mysql_fetch_field( res ) ; x++ ) strcpy( aszFlds[ x ], fd->name ) ; while ( row = mysql_fetch_row( res ) ) { j = mysql_num_fields( res ) ; printf( "Process #%ld:-\n", l++ ) ; for ( k = 0 ; k < j ; k++ ) printf( " Fld #%d (%s): %s\n", k + 1, aszFlds[ k ], (((row[k]==NULL)||(!strlen(row[k])))?"NULL":row[k])) ; puts( "==============================\n" ) ; } } else { printf("Got error %s when retreiving processlist\n",mysql_error(myData)); } //.... res = mysql_list_tables( myData, "%" ) ; l = 1 ; for ( x = 0 ; fd = mysql_fetch_field( res ) ; x++ ) strcpy( aszFlds[ x ], fd->name ) ; while ( row = mysql_fetch_row( res ) ) { j = mysql_num_fields( res ) ; printf( "Table #%ld:-\n", l++ ) ; for ( k = 0 ; k < j ; k++ ) printf( " Fld #%d (%s): %s\n", k + 1, aszFlds[ k ], (((row[k]==NULL)||(!strlen(row[k])))?"NULL":row[k])) ; puts( "==============================\n" ) ; } //.... pszT = mysql_stat( myData ) ; puts( pszT ) ; //.... mysql_close( myData ) ; return 0 ; }
bool MySQLConnection::Open() { MYSQL *mysqlInit; mysqlInit = mysql_init(NULL); if (!mysqlInit) { sLog->outError("Could not initialize Mysql connection to database `%s`", m_connectionInfo.database.c_str()); return false; } int port; char const* unix_socket; mysql_options(mysqlInit, MYSQL_SET_CHARSET_NAME, "utf8"); #ifdef _WIN32 if (m_connectionInfo.host == ".") // named pipe use option (Windows) { unsigned int opt = MYSQL_PROTOCOL_PIPE; mysql_options(mysqlInit, MYSQL_OPT_PROTOCOL, (char const*)&opt); port = 0; unix_socket = 0; } else // generic case { port = atoi(m_connectionInfo.port_or_socket.c_str()); unix_socket = 0; } #else if (m_connectionInfo.host == ".") // socket use option (Unix/Linux) { unsigned int opt = MYSQL_PROTOCOL_SOCKET; mysql_options(mysqlInit, MYSQL_OPT_PROTOCOL, (char const*)&opt); m_connectionInfo.host = "localhost"; port = 0; unix_socket = m_connectionInfo.port_or_socket.c_str(); } else // generic case { port = atoi(m_connectionInfo.port_or_socket.c_str()); unix_socket = 0; } #endif m_Mysql = mysql_real_connect(mysqlInit, m_connectionInfo.host.c_str(), m_connectionInfo.user.c_str(), m_connectionInfo.password.c_str(), m_connectionInfo.database.c_str(), port, unix_socket, 0); if (m_Mysql) { if (!m_reconnecting) { sLog->outSQLDriver("MySQL client library: %s", mysql_get_client_info()); sLog->outSQLDriver("MySQL server ver: %s ", mysql_get_server_info(m_Mysql)); if (mysql_get_server_version(m_Mysql) != mysql_get_client_version()) sLog->outSQLDriver("[WARNING] MySQL client/server version mismatch; may conflict with behaviour of prepared statements."); } sLog->outDetail("Connected to MySQL database at %s", m_connectionInfo.host.c_str()); mysql_autocommit(m_Mysql, 1); // set connection properties to UTF8 to properly handle locales for different // server configs - core sends data in UTF8, so MySQL must expect UTF8 too mysql_set_character_set(m_Mysql, "utf8"); return true; } else { sLog->outError("Could not connect to MySQL database at %s: %s\n", m_connectionInfo.host.c_str(), mysql_error(mysqlInit)); mysql_close(mysqlInit); return false; } }
bool DatabaseMysql::Initialize(const char *infoString) { if(!Database::Initialize(infoString)) return false; tranThread = NULL; MYSQL *mysqlInit; mysqlInit = mysql_init(NULL); if (!mysqlInit) { sLog.outError( "Could not initialize Mysql connection" ); return false; } InitDelayThread(); Tokens tokens = StrSplit(infoString, ";"); Tokens::iterator iter; std::string host, port_or_socket, user, password, database; int port; char const* unix_socket; iter = tokens.begin(); if(iter != tokens.end()) host = *iter++; if(iter != tokens.end()) port_or_socket = *iter++; if(iter != tokens.end()) user = *iter++; if(iter != tokens.end()) password = *iter++; if(iter != tokens.end()) database = *iter++; mysql_options(mysqlInit,MYSQL_SET_CHARSET_NAME,"utf8"); #ifdef WIN32 if(host==".") // named pipe use option (Windows) { unsigned int opt = MYSQL_PROTOCOL_PIPE; mysql_options(mysqlInit,MYSQL_OPT_PROTOCOL,(char const*)&opt); port = 0; unix_socket = 0; } else // generic case { port = atoi(port_or_socket.c_str()); unix_socket = 0; } #else if(host==".") // socket use option (Unix/Linux) { unsigned int opt = MYSQL_PROTOCOL_SOCKET; mysql_options(mysqlInit,MYSQL_OPT_PROTOCOL,(char const*)&opt); host = "localhost"; port = 0; unix_socket = port_or_socket.c_str(); } else // generic case { port = atoi(port_or_socket.c_str()); unix_socket = 0; } #endif mMysql = mysql_real_connect(mysqlInit, host.c_str(), user.c_str(), password.c_str(), database.c_str(), port, unix_socket, 0); if (mMysql) { sLog.outDetail( "Connected to MySQL database at %s", host.c_str()); sLog.outString( "MySQL client library: %s", mysql_get_client_info()); sLog.outString( "MySQL server ver: %s ", mysql_get_server_info( mMysql)); /*----------SET AUTOCOMMIT ON---------*/ // It seems mysql 5.0.x have enabled this feature // by default. In crash case you can lose data!!! // So better to turn this off // --- // This is wrong since mangos use transactions, // autocommit is turned of during it. // Setting it to on makes atomic updates work if (!mysql_autocommit(mMysql, 1)) sLog.outDetail("AUTOCOMMIT SUCCESSFULLY SET TO 1"); else sLog.outDetail("AUTOCOMMIT NOT SET TO 1"); /*-------------------------------------*/ // set connection properties to UTF8 to properly handle locales for different // server configs - core sends data in UTF8, so MySQL must expect UTF8 too PExecute("SET NAMES `utf8`"); PExecute("SET CHARACTER SET `utf8`"); return true; } else { sLog.outError( "Could not connect to MySQL database at %s: %s\n", host.c_str(),mysql_error(mysqlInit)); mysql_close(mysqlInit); return false; } }
bool DatabaseMysql::Initialize(const char *infoString) { m_strInfoString = infoString; MYSQL *mysqlInit; mysqlInit = mysql_init(NULL); if (!mysqlInit) { IME_SQLERROR("Could not initialize Mysql connection"); return false; } std::vector<std::string> vecData; CUtil::StrSplit(infoString, "'", vecData); std::string host, port_or_socket, user, password, database; int port; char const* unix_socket; if (vecData.size() != 5 && vecData.size() != 4) { IME_SQLERROR("VEC DATA SIZE ERROR[%d]", (int)vecData.size()); return false; } host = vecData[0]; port_or_socket = vecData[1]; user = vecData[2]; password = vecData[3]; if (vecData.size() == 5) { database = vecData[4]; } else { database = ""; } m_strDbName = database; port = atoi(port_or_socket.c_str()); mysql_options(mysqlInit,MYSQL_SET_CHARSET_NAME,"utf8"); char value = 1; mysql_options(mysqlInit, MYSQL_OPT_RECONNECT, (char *)&value); #ifdef WIN32 if(host==".") // named pipe use option (Windows) { unsigned int opt = MYSQL_PROTOCOL_PIPE; mysql_options(mysqlInit,MYSQL_OPT_PROTOCOL,(char const*)&opt); port = 0; unix_socket = 0; } else // generic case { port = atoi(port_or_socket.c_str()); unix_socket = 0; } #else if(host==".") // socket use option (Unix/Linux) { unsigned int opt = MYSQL_PROTOCOL_SOCKET; mysql_options(mysqlInit,MYSQL_OPT_PROTOCOL,(char const*)&opt); host = "localhost"; port = 0; unix_socket = port_or_socket.c_str(); } else // generic case { port = atoi(port_or_socket.c_str()); unix_socket = 0; } #endif mMysql = mysql_real_connect(mysqlInit, host.c_str(), user.c_str(), password.c_str(), database.c_str(), port, unix_socket, CLIENT_FOUND_ROWS); if (mMysql) { IME_SQLDRIVER("Connected to MySQL database at %s. Database : %s", host.c_str(),database.c_str()); IME_SQLDRIVER("MySQL client library: %s", mysql_get_client_info()); IME_SQLDRIVER("MySQL server ver: %s", mysql_get_server_info( mMysql)); //todo ??? /*----------SET AUTOCOMMIT ON---------*/ // It seems mysql 5.0.x have enabled this feature // by default. In crash case you can lose data!!! // So better to turn this off // --- // This is wrong since mangos use transactions, // autocommit is turned of during it. // Setting it to on makes atomic updates work if (!mysql_autocommit(mMysql, 1)) IME_SQLDRIVER("AUTOCOMMIT SUCCESSFULLY SET TO 1"); else IME_SQLDRIVER("AUTOCOMMIT NOT SET TO 1"); /*-------------------------------------*/ return true; } else { IME_SQLERROR( "Could not connect to MySQL database at %s: %s\n", host.c_str(),mysql_error(mysqlInit)); mysql_close(mysqlInit); return false; } }