MySQL Ping

Just starting out? Need help? Post your questions and find answers here.
Warmonger
Enthusiast
Enthusiast
Posts: 156
Joined: Wed Apr 20, 2011 4:24 pm

MySQL Ping

Post by Warmonger »

I looked through the database functions, and it seems there isn't anything to ping MySQL to keep the connection active. I wrote a loginserver in PB and its completely stable. The only downside is overnight, it looses its connection with MySQL (the connection with MySQL has gone away). Is there a way in PureBasic to ping MySQL so the connection isn't lost?

SOLVED!

Code: Select all

; ------------------------------------------------------------
;   MySQL_Ping - Pings MySQL To Keep An Active Connection
; ------------------------------------------------------------
Procedure MySQL_Ping(Parameter)
  Timer = ElapsedMilliseconds()
  Repeat
    Timed = ElapsedMilliseconds()-Timer
    If Timed > 10000
      Query = DatabaseQuery(0, "SELECT 1")
      If Query
        While NextDatabaseRow(0)
          PingResult.s = GetDatabaseString(0, 0)
        Wend
        If PingResult = "1"
          Timer = ElapsedMilliseconds()
        Else
          ConsoleColor(12, 0)
          Print("[ERROR] ") 
          ConsoleColor(7, 0)
          PrintN("Lost Connection With MySQL")
        EndIf
      EndIf
    Else
      Delay(1)
    EndIf
  ForEver
EndProcedure
Last edited by Warmonger on Sat Jul 07, 2012 5:19 am, edited 2 times in total.
Its Not A Bug, Its An Undocumented Feature!
Relax Its All Just Ones And Zeros
There Is No Place Like 127.0.0.1 Except ::1
I do things TO my computer, not WITH my computer... I am a nerd.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: MySQL Ping

Post by IdeasVacuum »

Never done it but can you simply send a query per time interval?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Warmonger
Enthusiast
Enthusiast
Posts: 156
Joined: Wed Apr 20, 2011 4:24 pm

Re: MySQL Ping

Post by Warmonger »

IdeasVacuum wrote:Never done it but can you simply send a query per time interval?
I thought of that possibility, and is using "Select 1" query to see if MySQL is still responding. But I rather have a native PureBasic function that acts like MySQL_Ping.
Its Not A Bug, Its An Undocumented Feature!
Relax Its All Just Ones And Zeros
There Is No Place Like 127.0.0.1 Except ::1
I do things TO my computer, not WITH my computer... I am a nerd.
Num3
PureBasic Expert
PureBasic Expert
Posts: 2812
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Re: MySQL Ping

Post by Num3 »

COF :wink:

Code: Select all

mysql_ping(hDB)
Warmonger
Enthusiast
Enthusiast
Posts: 156
Joined: Wed Apr 20, 2011 4:24 pm

Re: MySQL Ping

Post by Warmonger »

Num3 wrote:COF :wink:

Code: Select all

mysql_ping(hDB)
mysql_ping() doesn't exist in the PureBasic help file. I don't think its an internal function of the PureBasic library. I wrote a MySQL library that I could simply call it from, but I don't want to use a wrapper at all. I am trying to do it through PureBasic natively. Using "SELECT 1" query seems to work for now, as all I have to do is check for return value 1. Tho executing a query every 10 seconds doesn't seem too efficient even tho the query takes less then 0.00ms to execute.
Its Not A Bug, Its An Undocumented Feature!
Relax Its All Just Ones And Zeros
There Is No Place Like 127.0.0.1 Except ::1
I do things TO my computer, not WITH my computer... I am a nerd.
kinglestat
Enthusiast
Enthusiast
Posts: 746
Joined: Fri Jul 14, 2006 8:53 pm
Location: Malta
Contact:

Re: MySQL Ping

Post by kinglestat »

ideally you query the time like select now()
I may not help with your coding
Just ask about mental issues!

http://www.lulu.com/spotlight/kingwolf
http://www.sen3.net
Warmonger
Enthusiast
Enthusiast
Posts: 156
Joined: Wed Apr 20, 2011 4:24 pm

Re: MySQL Ping

Post by Warmonger »

kinglestat wrote:ideally you query the time like select now()
That would only give me a more complex variable that is harder to use. I rather just do "SELECT 1" and see if MySQL will return that value.
Last edited by Warmonger on Sat Jul 07, 2012 5:19 am, edited 1 time in total.
Its Not A Bug, Its An Undocumented Feature!
Relax Its All Just Ones And Zeros
There Is No Place Like 127.0.0.1 Except ::1
I do things TO my computer, not WITH my computer... I am a nerd.
Zach
Addict
Addict
Posts: 1677
Joined: Sun Dec 12, 2010 12:36 am
Location: Somewhere in the midwest
Contact:

Re: MySQL Ping

Post by Zach »

Warmonger wrote:
Num3 wrote:COF :wink:

Code: Select all

mysql_ping(hDB)
mysql_ping() doesn't exist in the PureBasic help file. I don't think its an internal function of the PureBasic library. I wrote a MySQL library that I could simply call it from, but I don't want to use a wrapper at all. I am trying to do it through PureBasic natively. Using "SELECT 1" query seems to work for now, as all I have to do is check for return value 1. Tho executing a query every 10 seconds doesn't seem too efficient even tho the query takes less then 0.00ms to execute.

...as opposed to pinging the server every 10 seconds??

I don't think it should really matter whether you use a Native PB command, or a workaround, as long as it is doing the job and it is reliable and predictable behavior. Perhaps you should ping only once per minute, or try other higher values to see what the actual timeout of the server is. Then you only need to ping once every so often within that time frame.

Is there not a query itself that you could send to the server, to return the value for how long it waits before timing out a connection? You said the problem only exists overnight, so I am assuming it is not a "every 10 seconds" issue (if it is that seems stupid, but I'm not a Database person either, so...).

Sending a keep-alive query every 60 seconds or however long the window is, should work fine I would think. All a ping does is send a packet of data, and all you are doing now is sending a packet of data. The difference is only in the external method.
Warmonger
Enthusiast
Enthusiast
Posts: 156
Joined: Wed Apr 20, 2011 4:24 pm

Re: MySQL Ping

Post by Warmonger »

Zach wrote:
Warmonger wrote:
Num3 wrote:COF :wink:

Code: Select all

mysql_ping(hDB)
mysql_ping() doesn't exist in the PureBasic help file. I don't think its an internal function of the PureBasic library. I wrote a MySQL library that I could simply call it from, but I don't want to use a wrapper at all. I am trying to do it through PureBasic natively. Using "SELECT 1" query seems to work for now, as all I have to do is check for return value 1. Tho executing a query every 10 seconds doesn't seem too efficient even tho the query takes less then 0.00ms to execute.

...as opposed to pinging the server every 10 seconds??

I don't think it should really matter whether you use a Native PB command, or a workaround, as long as it is doing the job and it is reliable and predictable behavior. Perhaps you should ping only once per minute, or try other higher values to see what the actual timeout of the server is. Then you only need to ping once every so often within that time frame.

Is there not a query itself that you could send to the server, to return the value for how long it waits before timing out a connection? You said the problem only exists overnight, so I am assuming it is not a "every 10 seconds" issue (if it is that seems stupid, but I'm not a Database person either, so...).

Sending a keep-alive query every 60 seconds or however long the window is, should work fine I would think. All a ping does is send a packet of data, and all you are doing now is sending a packet of data. The difference is only in the external method.
The function I wrote above works perfect. When executed you can return the value of 1 from the first row. You can use this to check if MySQL executed the query or not. If it did then it will return 1, if not then it will return an error. I have it on a tight loop because the original server file im emulating pings MySQL every 10 seconds as well. The query is light and executes near instantly, so. I guess my problem is solved.

Edit: MySQL's default connection timeout is 15 seconds. So you have to ping it one way or another every 10 seconds at least to keep it alive.
Last edited by Warmonger on Sat Jul 07, 2012 5:19 am, edited 2 times in total.
Its Not A Bug, Its An Undocumented Feature!
Relax Its All Just Ones And Zeros
There Is No Place Like 127.0.0.1 Except ::1
I do things TO my computer, not WITH my computer... I am a nerd.
culita
User
User
Posts: 29
Joined: Thu May 03, 2012 1:24 pm

Re: MySQL Ping

Post by culita »

TOVARISH you must use this bomb:

Code: Select all

InitNetwork()

Procedure TestInternetConnection()
;copyright: netmaestro the ugly frog
  connection = OpenNetworkConnection("www.freewebmasterhelp.com", 3306)
  If connection
    CloseNetworkConnection(connection)
    ProcedureReturn 1
  Else
    ProcedureReturn 0
  EndIf
EndProcedure

Debug TestInternetConnection()


replace http://www.freewebmasterhelp.com with your domain,
and make sure you have permiss to access it out of localhost,
in some cases you will need special permisions.

this example if you run it must work because http://www.freewebmasterhelp.com have mysql


GOOD LUCK!
culita
User
User
Posts: 29
Joined: Thu May 03, 2012 1:24 pm

Re: MySQL Ping

Post by culita »

maybe this will help you

Code: Select all

InitNetwork()

Procedure TestInternetConnection(domain$,port)
;copyright: netmaestro the ugly frog
  connection = OpenNetworkConnection(domain$,port)
  If connection
    CloseNetworkConnection(connection)
    ProcedureReturn 1
  Else
    ProcedureReturn 0
  EndIf
EndProcedure


;###############################################################
; !!!!!!!!!!!!!!!!!!! SEE this list !!!!!!!!!!!!!!!!!!!!!!!!!!!!
;http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers
;###############################################################


If  TestInternetConnection("www.freewebmasterhelp.com", 3306)
  Debug "mysql service is up"
Else
  Debug "mysql service is down / the mysql server doesn't accept external connections for security reasons"
EndIf



If  TestInternetConnection("www.freewebmasterhelp.com", 80)
  Debug "http service is up"
Else
  Debug "http service is down"
EndIf



If  TestInternetConnection("www.freewebmasterhelp.com", 21)
  Debug "FTP—control (command) service is up"
Else
  Debug "FTP—control (command) service is down / the ftp server doesn't accept external connections for security reasons"
EndIf


If  TestInternetConnection("www.freewebmasterhelp.com", 25)
  Debug "smtp service is up"
Else
  Debug "smtp service is down"
EndIf
Post Reply