2013-04-21 3 views
1

MySQL 서버에 대한 연결을 테스트하는 최선의 방법은 무엇입니까? 예를 들어 핑 (ping) 할 수 있습니까? MySQLdb와 파이썬을 사용하고 있습니다.MySQL 서버에 대한 연결 테스트

나는이 내가 사용했던 것입니다 내 프로그램은 다음과 같은 방법

....connect to MySQL server 
database = MySQLdb.connect(host="127.0.0.1 etc... 

While true: 
    **... Check to see if connection is still alive if not reconnect** 

    ... send data to MySQL... 
    time.sleep(30) 

답변

0

으로 구성되어야합니다.

import MySQLdb 

try: 
    import MySQLdb.converters 
except ImportError: 
    _connarg('conv') 

def connect(host='ronak.local', user='my_dev_1', passwd='my_dev_1', db='my_dev1', port=3306): 

    try: 
     orig_conv = MySQLdb.converters.conversions 
     conv_iter = iter(orig_conv) 
     convert = dict(zip(conv_iter, [str,] * len(orig_conv.keys()))) 
     print "Connecting host=%s user=%s db=%s port=%d" % (host, user, db, port) 
     conn = MySQLdb.connect(host, user, passwd, db, port, conv=convert) 
    except MySQLdb.Error, e: 
     print "Error connecting %d: %s" % (e.args[0], e.args[1]) 
    return conn 


def parse_data_and_description(cursor, data, rs_id): 

    res = [] 
    cols = [d[0] for d in cursor.description] 

    for i in data: 
     res.append(OrderedDict(zip(cols, i))) 
    return res 
    rs_id=0; 

def get_multiple_result_sets(): 
    conn = connect() 
    cursor = conn.cursor() 
    final_list = [] 
    try: 
     conn.autocommit(True) 
     cursor.execute ("CALL %s%s" % (sp, args)) 
     while True: 
      rs_id+=1 

      data = cursor.fetchall() 
      listout = parse_data_and_description(cursor, data, rs_id) 
      print listout 
      if cursor.nextset()==None: 
       # This means no more recordsets available 
       break 
      print "\n" 
      # Consolidate all the cursors in a single list 
      final_list.append(listout) 
     print final_list 
    except MySQLdb.Error, e: 
     # Lets rollback the transaction in case of an exception 
     conn.rollback() 
     print "Transaction aborted: %d: %s" % (e.args[0], e.args[1]) 
     cursor.close() 
     conn.close() 
    else: 
     # Commit the transaction in case of no failures/exceptions 
     conn.commit() 
     print "Transaction succeeded" 
     cursor.close() 
     conn.close() 
관련 문제