我时常遇到以下错误。
Authentication to host 'localhost' for user 'root' using method 'mysql_native_password' failed with message: Reading from the stream has failed.
我知道连接字符串是正确的 - 首先在应用程序启动后对其进行测试,如果失败,应用程序会给出一条消息并退出。
其次,大多数时候报告它的函数也在使用它来正确连接,它时不时地发生。
我试过更改为 127.0.0.1,但似乎没有什么不同。
它在 Windows Server 2012 64 位上。
我已将 127.0.0.1 localhost 添加到主机文件。
应用程序在 99% 的时间里都在正确运行并连接到数据库,只是偶尔会出现此错误。
最重要的是,我检查了 show full processlist 命令,但我始终只有几个连接。
我无法解释这个问题,也不知道如何解决。
谢谢, 吉姆
最佳答案
这是很老的了,但是因为我刚刚找到了这个问题的解决方案,所以我会分享它以防万一有人还在寻找它(据我所知,这也是当前 Mysql 版本的问题) .
此类问题记录在 this bug report 中.
我在这里引用那个错误报告中最重要的答案:
[21 May 2014 15:40] Francisco Alberto Tirado Zavala
Hello Jorge.
When you are working with a Client-Server application all the connections are performed by Sockets, when you create a connection you open a Socket between your client and your server, and an Id/ProccesId is assigned/reserved in the server for the connection requested by the client. If the server is restarted the Id given to the client is lost, so you need to create a new valid connection opening a new socket to get a valid Id from the server.
Also you must consider that the server can terminate or close a connection if the connection is inactive after some time. If you have your connection open also could affect your connections pool, because a connection open means less connections for your pool: having 10 clients connected at the same time are 10 connection less from the Server available connections.
If you can't change the design in your application (having a global connection), as a workaround you can have a method to validate that the connection is valid, for example the following method ping the server and if the ping fails it open the connection again:
....
MySqlConnection _conn = new MySqlConnection("MyConnectionString");
private void CheckConnection()
{
if(!_conn.Ping())
{
_conn.Open();
}
}
....
As you can see the code is very simple and it works in the following way: when the connection try to ping the server if the connection is invalid, internally the stream that has the socket information will be terminated and the connection will be set on status closed as well as the invalid connection will be removed from the connections pool if exists, then opening the connection again will create a new connection which means a new valid socket. If the ping succeed so you will continue using the same connection.
To use it you can call it before trying to perform an action to the database, and the connection will be re-opened just when is invalid:
....
private void button1_Click(object sender, EventArgs e)
{
//assuming that the connection is already open
CheckConnectionState();
var cmd = new MySqlCommand("select 1", _conn);
cmd.ExecuteNonQuery();
}
....
This is just a workaround, and as far I know all the connections to any database server will have the same behavior: if the server is restarted any connection will be lost, as well as the server will clean every unused connection.
Thanks for your time.
关于MySQL - 身份验证(但仅在某些时候),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25743581/
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
我有一些非常大的模型,我必须将它们迁移到最新版本的Rails。这些模型有相当多的验证(User有大约50个验证)。是否可以将所有这些验证移动到另一个文件中?说app/models/validations/user_validations.rb。如果可以,有人可以提供示例吗? 最佳答案 您可以为此使用关注点:#app/models/validations/user_validations.rbrequire'active_support/concern'moduleUserValidationsextendActiveSupport:
当我的预订模型通过rake任务在状态机上转换时,我试图找出如何跳过对ActiveRecord对象的特定实例的验证。我想在reservation.close时跳过所有验证!叫做。希望调用reservation.close!(:validate=>false)之类的东西。仅供引用,我们正在使用https://github.com/pluginaweek/state_machine用于状态机。这是我的预订模型的示例。classReservation["requested","negotiating","approved"])}state_machine:initial=>'requested
我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser
我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
文章目录一、概述简介原理模块二、配置Mysql使用版本环境要求1.操作系统2.mysql要求三、配置canal-server离线下载在线下载上传解压修改配置单机配置集群配置分库分表配置1.修改全局配置2.实例配置垂直分库水平分库3.修改group-instance.xml4.启动监听四、配置canal-adapter1修改启动配置2配置映射文件3启动ES数据同步查询所有订阅同步数据同步开关启动4.验证五、配置canal-admin一、概述简介canal是Alibaba旗下的一款开源项目,Java开发。基于数据库增量日志解析,提供增量数据订阅&消费。Git地址:https://github.co