window 平台下mysql主从的监控(window执行计划配合vb脚本)
(1)在window上安装ODBC(vb链接MySQL的驱动程序)
下载地址为:http://dev.mysql.com/downloads/connector/odbc/
根据window系统选择相应版本即可。
我安装的是mysql-connector-odbc-5.3.4
如果在安装mysql-connector-odbc-5.3.4报错了,
Error 1918.Error installing ODBC driver Mysql ODBC 5.3 ANSI Driver,ODBC error 13: 不能加载安装或转换器。
Verify that the file MySQL ODBC 5.3 ANSI Driver exists and that you can accessit
那么就是缺少组件(vcredist_x64.exe),下载安装就行了。
下载地址为 http://pan.baidu.com/s/1bnozwsB
(2) 安装完mysql-connector-odbc-5.3.4 驱动程序之后就可以利用vb操作数据库了
以下是一段vb脚本,主要是用来监控MySQL主从状态,并发送邮件通知
Dim Cnn
Dim Rst
Dim strCnn
Dim status,TextBody
StrCnn="Provider=MSDASQL.1;Persist Security Info=True;Extended Properties=‘Driver=MySQL ODBC 5.3 ANSI Driver;SERVER=10.0.1.20;UID=root;PWD=root;DATABASE=mysql;PORT=3306‘"
Set Cnn = CreateObject("ADODB.Connection")
Cnn.Open strCnn
‘查看是否连接成功,成功状态值为1
‘msgbox Conn.state
If Cnn.State = 0 Then
‘msgbox "连接数据库失败"
else
‘ msgbox "连接数据库成功"
End If
Set Rst =CreateObject("ADODB.Recordset")
Rst.open "select VARIABLE_VALUE from information_schema.GLOBAL_STATUS where VARIABLE_NAME =‘SLAVE_RUNNING‘",Cnn
status=Rst("VARIABLE_VALUE").value
if status <> "ON" Then
TextBody = "master-slave异常"
call CheckFile(TextBody)
end if
‘定义一个函数,检查每天的备份是否生成
function CheckFile(byval TextBody )
Const Email_From = "Test@163.com" ‘发件人邮箱
Const Password = "shufu@2014" ‘发件人邮箱密码
Const Email_To = "Test@123.com" ‘收件人邮箱
Set CDO = CreateObject("CDO.Message") ‘创建CDO.Message对象
CDO.Subject = "警告:服务器10.0.1.20 异常" ‘邮件主题
CDO.From = Email_From ‘发件人地址
CDO.To = Email_To ‘收件人地址
CDO.TextBody = TextBody ‘邮件正文
‘cdo.AddAttachment = "C:\hello.txt" ‘邮件附件文件路径
Const schema = "http://schemas.microsoft.com/cdo/configuration/" ‘规定必须是这个,我也不知道为什么
With CDO.Configuration.Fields ‘用with关键字减少代码输入
.Item(schema & "sendusing") = 2 ‘使用网络上的SMTP服务器而不是本地的SMTP服务器
.Item(schema & "smtpserver") = "smtp.163.com" ‘SMTP服务器地址
.Item(schema & "smtpauthenticate") = 1 ‘服务器认证方式
.Item(schema & "sendusername") = Email_From ‘发件人邮箱
.Item(schema & "sendpassword") = Password ‘发件人邮箱密码
.Item(schema & "smtpserverport") = 25 ‘SMTP服务器端口
.Item(schema & "smtpusessl") = True ‘是否使用SSL
.Item(schema & "smtpconnectiontimeout") = 60 ‘连接服务器的超时时间
.Update ‘更新设置
End With
CDO.Send ‘发送邮件
End function本文出自 “SQLServer MySQL” 博客,请务必保留此出处http://dwchaoyue.blog.51cto.com/2826417/1583978
window 平台下mysql主从的监控(window执行计划配合vb脚本)
原文:http://dwchaoyue.blog.51cto.com/2826417/1583978