none
MySQL - PowerShell RRS feed

  • Question

  • Hi, I need a script that tells me if the MySQL is ONLINE or not. If this down  or not returns values from a table, that I send an email
    How I can do it with PS?


    # Connection Variables 
    $user = 'myuser' 
    $pass = 'mypass' 
    $database = 'mydatabase' 
    $MySQLHost = 'database.server.com' 
     
    # Connect to MySQL Database 
    $conn = Connect-MySQL $user $pass $MySQLHost $database


    function ConnectMySQL([string]$user, [string]$pass, [string]$MySQLHost, [string]$database) { 
        # Load MySQL .NET Connector Objects 
        [void][system.reflection.Assembly]::LoadWithPartialName("MySql.Data") 
     
        # Open Connection 
        $connStr = "server=" + $MySQLHost + ";port=3306;uid=" + $user + ";pwd=" + $pass + ";database="+$database+";Pooling=FALSE" 
        try {
            $conn = New-Object MySql.Data.MySqlClient.MySqlConnection($connStr) 
            $conn.Open()
        } catch [System.Management.Automation.PSArgumentException] {
            Log "Unable to connect to MySQL server, do you have the MySQL connector installed..?"
            Log $_
            Exit
        } catch {
            Log "Unable to connect to MySQL server..."
            Log $_.Exception.GetType().FullName
            Log $_.Exception.Message
            exit
        }
        Log "Connected to MySQL database $MySQLHost\$database"
     
        return $conn 
    }


    Sunday, November 8, 2015 5:11 PM

Answers