Powershell中的MySQL超时 [英] MySQL timeout in powershell

查看:58
本文介绍了Powershell中的MySQL超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows笔记本电脑上安装了MySQL DB.我正在使用Powershell脚本来连接/查询/输入到数据库.我有一个要运行的查询,我可以在MySQL Workbench中运行它,目前需要31.032秒才能运行,并返回3行.

I have a MySQL DB setup on my Windows laptop. I'm using a powershell script to connect/query/input to the DB. I have a query I'm trying to run, I can run it in the MySQL Workbench currently it takes 31.032 sec to run and returns 3 rows.

SELECT puz1.sudoku9x9_id, puz1.difficulty, puz2.sudoku9x9_id, puz2.difficulty 
    FROM sudoku9x9 as puz1 
    INNER JOIN sudoku9x9 as puz2 
        WHERE  
            puz1.clue_9 = puz2.clue_1 AND puz1.region_9 = puz2.region_1 AND 
            puz1.difficulty/puz2.difficulty BETWEEN .84 AND 1.19 AND 
            NOT EXISTS (SELECT 1 FROM samurai2x AS a 
            WHERE 
                a.puz1_id = puz1.sudoku9x9_id AND a.puz2_id = puz2.sudoku9x9_id)

Powershell脚本

Powershell Script

$samurai2x = "SELECT puz1.sudoku9x9_id, puz1.difficulty, puz2.sudoku9x9_id, puz2.difficulty FROM sudoku9x9 as puz1 INNER JOIN sudoku9x9 as puz2 WHERE puz1.clue_9 = puz2.clue_1 AND puz1.region_9 = puz2.region_1 AND puz1.difficulty/puz2.difficulty BETWEEN .84 AND 1.19 AND NOT EXISTS (SELECT 1 FROM samurai2x AS a WHERE a.puz1_id = puz1.sudoku9x9_id AND a.puz2_id = puz2.sudoku9x9_id) LIMIT 1"

Invoke-MySqlQuery -Query $samurai2x | ForEach {
    $diff = ([INT]$_.'difficulty' + [INT]$_.'difficulty1') / 2
    Invoke-MySqlQuery -Query "INSERT INTO samurai2x(difficulty, puz1_id, puz2_id) VALUES ('$diff', '$($_.'sudoku9x9_id')', '$($_.'sudoku9x9_id1')')"
}

当我运行powershell脚本时,它超时.因此,我研究了更改超时选项.我先跑

When I run the powershell script, it times out. So I looked into changing the timeout options. I first ran

SET GLOBAL connect_timeout=31536000;
SET GLOBAL wait_timeout=2147483;
SET GLOBAL interactive_timeout=31536000;

数字是MySQL文档中允许的最大值.那没切!因此,我编辑了my.ini文件并添加了

The numbers are the max allowed from the MySQL documentation. That did not cut it! So I edited the my.ini file and added

[mysqld]

connect_timeout=31536000
wait_timeout=2147483
interactive_timeout=31536000

我重新启动了MySQL服务.还是同样的问题!

I restarted the MySQL service. Still the same issue!

当脚本连接到数据库时,它将显示

When the script connects to the DB it displays

ServerThread      : 26
DataSource        : localhost
ConnectionTimeout : 15
Database          : sudoku
UseCompression    : False
State             : Open
ServerVersion     : 5.7.17-log
ConnectionString  : server=localhost;port=3306;user id=root;database=sudoku
IsPasswordExpired : False
Site              : 
Container         :

在编辑超时之前和每次尝试之后,ConnectionTimeout始终显示15.

The ConnectionTimeout has always displayed 15 prior to editing the timeout and after every attempt.

我在这里想念什么?预先感谢您的帮助.

What am I missing here guys? Thanks in advance for the help.

# Set MySQL connection info
$username = "root"
$password = cat D:\Sudoku\mysecurestring.txt | convertto-securestring
$dbcred = new-object -typename System.Management.Automation.PSCredential `
         -argumentlist $username, $password

# Connect to MySQL server
Connect-MySqlServer -Credential $dbcred -ComputerName localhost -Database sudoku

我正在根据此站点上的步骤连接到数据库:

I'm connecting to the DB based on the steps at this site: Querying MySQL Databases with PowerShell

编辑

底部是修改后的功能.我在每个新行上方都添加了一个已添加"注释.

At the bottom is the modified function. I put a comment "Added" above each new line.

新连接线

Connect-MySqlServer -Credential $dbcred -ComputerName localhost -Database sudoku -CommandTimeOut 600 -ConnectionTimeOut 25

新连接输出

ServerThread      : 23
DataSource        : localhost
ConnectionTimeout : 25
Database          : sudoku
UseCompression    : False
State             : Open
ServerVersion     : 5.7.17-log
ConnectionString  : server=localhost;port=3306;user id=root;database=sudoku;defaultcommandtimeout=600;connectiontimeout=25
IsPasswordExpired : False
Site              : 
Container         : 

修改功能

function Connect-MySqlServer
{
    <#
    .SYNOPSIS
        Connect to a MySQL Server
    .DESCRIPTION
        This function will establish a connection to a local or remote instance of 
        a MySQL Server. By default it will connect to the local instance on the 
        default port.
    .PARAMETER ComputerName
        The name of the remote computer to connect to, otherwise default to localhost
    .PARAMETER Port
        By default this is 3306, otherwise specify the correct value
    .PARAMETER Credential
        Typically this may be your root credentials, or to work in a specific 
        database the credentials with appropriate rights to do work in that database.
    .PARAMETER Database
        An optional parameter that will connect you to a specific database
    .PARAMETER TimeOut
        By default timeout is set to 15 seconds
    .EXAMPLE
        Connect-MySqlServer -Credential (Get-Credential)

        cmdlet Get-Credential at command pipeline position 1
        Supply values for the following parameters:
        Credential


        ServerThread      : 2
        DataSource        : localhost
        ConnectionTimeout : 15
        Database          :
        UseCompression    : False
        State             : Open
        ServerVersion     : 5.6.22-log
        ConnectionString  : server=localhost;port=3306;User Id=root
        IsPasswordExpired : False
        Site              :
        Container         :

        Description
        -----------
        Connect to the local mysql instance as root. This example uses the 
        Get-Credential cmdlet to prompt for username and password.
    .EXAMPLE
        Connect-MySqlServer -ComputerName db.company.com -Credential (Get-Credential)

        cmdlet Get-Credential at command pipeline position 1
        Supply values for the following parameters:
        Credential


        ServerThread      : 2
        DataSource        : db.company.com
        ConnectionTimeout : 15
        Database          :
        UseCompression    : False
        State             : Open
        ServerVersion     : 5.6.22-log
        ConnectionString  : server=db.company.com;port=3306;User Id=root
        IsPasswordExpired : False
        Site              :
        Container         :

        Description
        -----------
        Connect to a remote mysql instance as root. This example uses the 
        Get-Credential cmdlet to prompt for username and password.
    .NOTES
        FunctionName : Connect-MySqlServer
        Created by   : jspatton
        Date Coded   : 02/11/2015 09:19:10
    .LINK
        https://github.com/jeffpatton1971/mod-posh/wiki/MySQL#Connect-MySqlServer
    #>
    [OutputType('MySql.Data.MySqlClient.MySqlConnection')]
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [pscredential]$Credential,

        [Parameter()]
        [ValidateScript({ Test-Connection -ComputerName $_ -Quiet -Count 1 })]
        [ValidateNotNullOrEmpty()]
        [string]$ComputerName = $env:COMPUTERNAME,

        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [int]$Port = 3306,

        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string]$Database,

        # Added
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [int]$CommandTimeOut = 15,

        # Added
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [int]$ConnectionTimeOut = 20
    )
    begin
    {
        $ErrorActionPreference = 'Stop'

        if ($PSBoundParameters.ContainsKey('Database')) {
            $connectionString = 'server={0};port={1};uid={2};pwd={3};database={4};' -f $ComputerName,$Port,$Credential.UserName, $Credential.GetNetworkCredential().Password,$Database
        }
        else
        {
            $connectionString = 'server={0};port={1};uid={2};pwd={3};' -f $ComputerName, $Port, $Credential.UserName, $Credential.GetNetworkCredential().Password
        }
        # Added
        $connectionString = $connectionString + "default command timeout=$CommandTimeOut; Connection Timeout=$ConnectionTimeOut;"
    }
    process
    {
        try
        {
            [MySql.Data.MySqlClient.MySqlConnection]$conn = New-Object MySql.Data.MySqlClient.MySqlConnection($connectionString)
            $conn.Open()
            $Global:MySQLConnection = $conn
            if ($PSBoundParameters.ContainsKey('Database')) {
                $null =  New-Object MySql.Data.MySqlClient.MySqlCommand("USE $Database", $conn)
            }
            $conn
        }
        catch
        {
            Write-Error -Message $_.Exception.Message
        }
    }
}

推荐答案

在Powershell脚本中的连接字符串中添加default command timeout=60;.

Add default command timeout=60; to the connection string in your Powershell script.

您可能还想设置

You may also want to set the CommandTimeout property of the MySqlCommand object.

这篇关于Powershell中的MySQL超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆