一、安装xinetd服务
yum -y install xinetd
二、编写新增checkmysql 服务
[root@dsk_mooc86 ~]# vim /etc/xinetd.d/checkmysql# default: on
# description: The telnet server serves telnet sessions; it uses \
# unencrypted username/password pairs for authentication.
service checkmysql
{
flags = REUSE
socket_type = stream
wait = no
user = root
server = /root/mysqlchk_status.sh
log_on_failure += USERID
disable = no
port =9888
bind = 192.168.0.1
only_from = 192.168.0..0/24
}
三、编辑/etc/services ,确保端口对应
echo "checkmysql 9888/tcp" >>/etc/services
四、重启服务/etc/init.d/xinetd restart
这个功能配合haproxy可以对从库数据库进行健康检查,比如从库滞后,查询缓慢,从库同步故障等问题,自动摘除有问题的从库
[root@mysql_test1 ~]# telnet 10.0.20.3 9888
Trying 10.0.20.3...
Connected to 10.0.20.3.
Escape character is '^]'.
mysql: [Warning] Using a password on the command line interface can be insecure.
HTTP/1.1 200 OK
Connection closed by foreign host.
标红的影响了HTTP/1.1 200 OK的完全匹配,导致这个从库摘除掉,为了避免mysql: [Warning] Using a password on the command line interface can be insecure.的出现
采用python处理,写脚本
yum install MySQL-python
import MySQLdb
Python中MySQLdb的connect的用法
MySQLdb模块是python连接mysql数据库的一个模块,在操作mysql数据库是经常使用,在连接数据库时connect是最常用的一种方法,这个方法有好多参数,总结了一下,主要有一下几种:
connect()方法用于连接数据库,返回一个数据库连接对象。如果要连接一个位于www.gyyx.com服务器上名为mysql的MySQL数据库,连接串可以这样写:
db = MySQLdb.connect(host="www.gyyx.com",user="user",passwd="xxx",db="mysql" )
connect()的参数列表如下:
host,连接的数据库服务器主机名,默认为本地主机(localhost)。
user,连接数据库的用户名,默认为当前用户。
passwd,连接密码,没有默认值。
db,连接的数据库名,没有默认值。
conv,将文字映射到Python类型的字典。默认为MySQLdb.converters.conversions
cursorclass,cursor()使用的种类,默认值为MySQLdb.cursors.Cursor。
compress,启用协议压缩功能。
named_pipe,在windows中,与一个命名管道相连接。
init_command,一旦连接建立,就为数据库服务器指定一条语句来运行。
read_default_file,使用指定的MySQL配置文件。
read_default_group,读取的默认组。
unix_socket,在unix中,连接使用的套接字,默认使用TCP。
port,指定数据库服务器的连接端口,默认是3306。
连接对象的db.close()方法可关闭数据库连接,并释放相关资源。
连接对象的db.cursor([cursorClass])方法返回一个指针对象,用于访问和操作数据库中的数据。
连接对象的db.begin()方法用于开始一个事务,如果数据库的AUTOCOMMIT已经开启就关闭它,直到事务调用commit()和rollback()结束。
连接对象的db.commit()和db.rollback()方法分别表示事务提交和回退。
指针对象的cursor.close()方法关闭指针并释放相关资源。
指针对象的cursor.execute(query[,parameters])方法执行数据库查询。
指针对象的cursor.fetchall()可取出指针结果集中的所有行,返回的结果集一个元组(tuples)。
指针对象的cursor.fetchmany([size=cursor.arraysize])从查询结果集中取出多行,我们可利用可选的参数指定取出的行数。
指针对象的cursor.fetchone()从查询结果集中返回下一行。
指针对象的cursor.arraysize属性指定由cursor.fetchmany()方法返回行的数目,影响fetchall()的性能,默认值为1。
指针对象的cursor.rowcount属性指出上次查询或更新所发生行数。-1表示还没开始查询或没有查询到数据。
下面是一个连接mysql的例子:
def mysql_conn():
try:
conn = MySQLdb.connect(host = '192.168.8.100',user = 'mysql',passwd = '123456',connect_timeout=10)
cursor = conn.cursor()
sql = "SELECT COUNT(1) FROM mysql.user"
cursor.execute(sql)
alldata = cursor.fetchall()
count = alldata[0][0]
cursor.close()
conn.close()
print count
except Exception,e:
print "Can not Connect to mysql server"
本文出自 “王伟” 博客,请务必保留此出处http://wangwei007.blog.51cto.com/68019/1130148
-----------------------------------------------------------------------------------------------------------python脚本如下:
[root@mysql_test3 ~]# cat /root/mysqlpython.py
#!/usr/bin/env python
#encoding:utf-8
import MySQLdb
import sys,os
import random
import time
db1_ip="10.0.20.1"
db2_ip="10.0.20.3"
randnum01= random.randint(1, 20000)
randid01= random.randint(1, 30)
slaptime=4
#print randnum01
#print randid01
conn = MySQLdb.connect(host=db1_ip,user='root',passwd='123456',port=38141,charset='utf8')
conn.select_db('t11')
cur = conn.cursor()
sql="update sync3 set value="+str(randnum01)+" where id="+str(randid01)
#print sql
cur.execute(sql)
conn.commit()
cur.close()
conn.close()
#sleep 3s
time.sleep(slaptime)
conn_read = MySQLdb.connect(host=db2_ip,user='root',passwd='123456',port=38141,charset='utf8')
conn_read.select_db('t11')
cur_read = conn_read.cursor()
sql_read="select value from sync3 where id="+str(randid01)
#print sql_read
cur_read.execute(sql_read)
conn_read.commit()
for row in cur_read.fetchall():
fname = row[0]
if fname==randnum01:
print "HTTP/1.1 200 OK\r\n"
else:
print "HTTP/1.1 503 Service Unavailable\r\n"
cur_read.close()
conn_read.close()