Mysql, Maria DB

[Python] maria DB 접속 로그 (1) - 설정 및 모니터링

Pydole 2018. 5. 12. 17:33

1. maria DB 로깅설정

set global general_log = 1;                 
set global log_output = 'TABLE';
show variables where Variable_name in ('version', 'log' , 'general_log')

 

 

 

 

2. 접속로그 확인 쿼리

  select * from general_log where  command_type = 'Connect'

 

 

 

 

 

 

3. 파이썬(python)을 이용한 접속로그 출력

 

import pymysql

conn = pymysql.connect(host='ip', user='root', password='password',db='mysql', charset='utf-8')
c = conn.cursor()
c.execute('select * from general_log where command_type = "Connect"')
rows = c.fetchall()

strOut = ''
strFormat = '%-20s\t%-30s\t%-20s\t%-20s\t%-20s\t%-20s\n'
strOut = strFormat % ('event_time', 'user_host', 'thread_id', 'server_id', 'command_type', 'agument')
strOut += '--------------------\t------------------------------\t--------------------\t--------------------\t--------------------\t--------------------'
print(strOut)

for i in rows:
      i = list(map(str,i))
      strOut = strFormat % (i[0][:19],i[1],i[2],i[3],i[4],i[5])
      print(strOut, end='')

 

 

 

 

4. 활용

  - 일 단위로 로그를 받아서 접속로그를 파일 또는 다른 DB로 저장. (보안증적)

  - maria db 날짜쿼리의 활용하여 모니터링