(五)PowerShell 操作mysql数据库(弃用)

  1. 安装MySQLCmdlets模块
PS /Users/sixdog/Documents/PowerShell> Install-Module MySQLCmdlets                                                                   

Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change its InstallationPolicy value by running the 
Set-PSRepository cmdlet. Are you sure you want to install the modules from 'PSGallery'?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): y
PS /Users/sixdog/Documents/PowerShell>      
  1. 确认已安装的MySQLCmdlets
PS /Users/sixdog/Documents/PowerShell> Get-InstalledModule                                                              

Version              Name                                Repository           Description
-------              ----                                ----------           -----------
23.0.8565.1          MySQLCmdlets                        PSGallery            CData Cmdlets for MySQL

PS /Users/sixdog/Documents/PowerShell> 
  1. 连接MySQL,并做查询
$username = 'root'
$password = '123456'
$schema = 'test'
$ip = '192.168.0.8'
$port = '3307'
$mysql = Connect-MySQL  -User "$username" -Password "$password" -Database "$schema" -Server "$ip" -Port "$port"

$id = "2"
$user = Select-MySQL -Connection $mysql -Table "User" -Where "id = `'$id`'"
$user
  1. 报错
PS /Users/sixdog/Documents/PowerShell> ./test.ps1
Connect-MySQL: /Users/sixdog/Documents/PowerShell/test.ps1:6:10
Line |
   6 |  $mysql = Connect-MySQL  -User "$username" -Password "$password" -Data|           ~~~~~~~~~~~~~
     | The term 'Connect-MySQL' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name,
     | or if a path was included, verify that the path is correct and try again.
Select-MySQL: /Users/sixdog/Documents/PowerShell/test.ps1:9:11
Line |
   9 |  $orders = Select-MySQL -Connection $mysql -Table "User" -Where "id =|            ~~~~~~~~~~~~
     | The term 'Select-MySQL' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name,
     | or if a path was included, verify that the path is correct and try again.
  1. 发现Get-Module和Get-InstalledModule的不同
    51 Get-Module文档里解释:列出当前会话中导入或可从 PSModulePath 导入的模块。
    5.2Get-InstalledModule文档里解释:获取 PowerShellGet 安装的计算机上的模块列表
    5.3执行命令发现MySQLCmdlets不在当前会话中
PS /Users/sixdog/Documents/PowerShell> Get-Module            

ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Manifest   1.2.5                 Microsoft.PowerShell.Archive        {Compress-Archive, Expand-Archive}
Manifest   7.0.0.0               Microsoft.PowerShell.Management     {Add-Content, Clear-Content, Clear-Item, Clear-ItemProperty…}
Manifest   7.0.0.0               Microsoft.PowerShell.Utility        {Add-Member, Add-Type, Clear-Variable, Compare-Object…}
Script     1.4.8.1               PackageManagement                   {Find-Package, Find-PackageProvider, Get-Package, Get-PackageProvider…}
Script     0.2.0                 PowerShellEditorServices.Commands   {Clear-Host, ConvertFrom-ScriptExtent, ConvertTo-ScriptExtent, Find-Ast…}
Binary     0.2.0                 PowerShellEditorServices.VSCode     {Close-VSCodeHtmlContentView, New-VSCodeHtmlContentView, Set-VSCodeHtmlContentVi…
Script     2.2.5                 PowerShellGet                       {Find-Command, Find-DscResource, Find-Module, Find-RoleCapability…}
Script     2.2.6                 PSReadLine                          {Get-PSReadLineKeyHandler, Get-PSReadLineOption, Remove-PSReadLineKeyHandler, Se…

PS /Users/sixdog/Documents/PowerShell> Get-InstalledModule

Version              Name                                Repository           Description
-------              ----                                ----------           -----------
23.0.8565.1          MySQLCmdlets                        PSGallery            CData Cmdlets for MySQL

6.将MySQLCmdlets引入会话中

PS /Users/sixdog/Documents/PowerShell> Import-Module MySQLCmdlets                                                                    
PS /Users/sixdog/Documents/PowerShell> Get-Module                

ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Manifest   1.2.5                 Microsoft.PowerShell.Archive        {Compress-Archive, Expand-Archive}
Manifest   7.0.0.0               Microsoft.PowerShell.Management     {Add-Content, Clear-Content, Clear-Item, Clear-ItemProperty…}
Manifest   7.0.0.0               Microsoft.PowerShell.Utility        {Add-Member, Add-Type, Clear-Variable, Compare-Object…}
Script     23.0.8565…            MySQLCmdlets                        {Add-MySQL, Connect-MySQL, Disconnect-MySQL, Get-License…}
Script     1.4.8.1               PackageManagement                   {Find-Package, Find-PackageProvider, Get-Package, Get-PackageProvider…}
Script     0.2.0                 PowerShellEditorServices.Commands   {Clear-Host, ConvertFrom-ScriptExtent, ConvertTo-ScriptExtent, Find-Ast…}
Binary     0.2.0                 PowerShellEditorServices.VSCode     {Close-VSCodeHtmlContentView, New-VSCodeHtmlContentView, Set-VSCodeHtmlContentVi…
Script     2.2.5                 PowerShellGet                       {Find-Command, Find-DscResource, Find-Module, Find-RoleCapability…}
Script     2.2.6                 PSReadLine                          {Get-PSReadLineKeyHandler, Get-PSReadLineOption, Remove-PSReadLineKeyHandler, Se…

PS /Users/sixdog/Documents/PowerShell> 
  1. 再次执行查询
PS /Users/sixdog/Documents/PowerShell> ./test.ps1

A valid license is required to use MySQLCmdlets.
Would you like to install a license now? (You may use "TRIAL" as the Product Key to activate a trial license.)
[Y] Yes  [N] No  [?] Help (default is "Y"): y

cmdlet Get-License at command pipeline position 1
Supply values for the following parameters:
(Type !? for Help.)
Email: ssssssss@gmail.com
Name: sixdog
ProductKey: TRIAL

Downloading license ...
Verifying license data...
License installation succeeded.
  1. 上一步操作查了下是试用版,感觉没啥意思,不想用,再找找别的连接数据库的模块
  2. 远程访问需要安装「 Connector/NET x.x.xx 」,不符合不需要安装就能用原则
  3. 还有的需要导入[Reflection.Assembly]::LoadFile(“C:\Program Files\CData\CData ADO.NET Provider for MySQL\lib\System.Data.CData.MySQL.dll”)
  4. 宗于以上原因,使用mysql需要安装东西,放弃。。。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值