问题
After a whole day trying dozen of solutions I'm really stuck here. I get the infamous Unable to connect to any of the specified MySQL hosts error. It's a very generic error so I already tried a few things. First some details that hopefully will help:
I'm using:
- Entity Framework 6
- ASP.NET MVC 5
- MySQL with MySQL Connector 6.8.3
Everything works when I run the application in Visual Studio 2013. It connects to the MySQL server with no problems, so i suppose there's nothing wrong with my connectionstring. However when I deploy my application I get this error. I thought it was maybe because my shared hosting trust level was set on Medium, however I contacted my hosting provider and they said it was set on Full. Pages that don't use the database are rendered properly.
Here's the stack trace of the error:
[MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts.]
MySql.Data.MySqlClient.NativeDriver.Open() +1285
MySql.Data.MySqlClient.Driver.Open() +260
MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings) +269
MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection() +18
MySql.Data.MySqlClient.MySqlPool.GetPooledConnection() +403
MySql.Data.MySqlClient.MySqlPool.TryToGetDriver() +228
MySql.Data.MySqlClient.MySqlPool.GetConnection() +106
MySql.Data.MySqlClient.MySqlConnection.Open() +1579
MySql.Data.MySqlClient.MySqlProviderServices.GetDbProviderManifestToken(DbConnection connection) +186
System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection) +118
[ProviderIncompatibleException: The provider did not return a ProviderManifestToken string.]
System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection) +459
System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection) +118
System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) +94
System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input) +248
System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +618
System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +26
System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +72
System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() +21
System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider() +64
System.Linq.Queryable.Where(IQueryable`1 source, Expression`1 predicate) +85
DailyMusic.Models.DAL.ReviewRepository.GetMostRecentReviews(Int32 number) +487
DailyMusic.Controllers.HomeController.Index() +21
lambda_method(Closure , ControllerBase , Object[] ) +79
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +242
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +39
System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__36(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +12
System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +139
System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3c() +112
System.Web.Mvc.Async.<>c__DisplayClass45.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3e() +452
System.Web.Mvc.Async.<>c__DisplayClass30.<BeginInvokeActionMethodWithFilters>b__2f(IAsyncResult asyncResult) +15
System.Web.Mvc.Async.<>c__DisplayClass28.<BeginInvokeAction>b__19() +37
System.Web.Mvc.Async.<>c__DisplayClass1e.<BeginInvokeAction>b__1b(IAsyncResult asyncResult) +241
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +29
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +111
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +19
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__4(IAsyncResult asyncResult, ProcessRequestState innerState) +51
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +111
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288
回答1:
After another few hours, I found the solution. It's a rather specific one but maybe there are others in the same situation.
I'm on a shared hosting environment and had two different credentials to access the database, one for accessing remotely and one for accessing from the server. Seems that the credentials for the remote connectioning didn't work on the server, therefore everything worked in Visual Studio but once deployed I had to use the other credentials. I assumed the remote credentials would also work on the server but they didn't.
回答2:
Thanks to @Jenthe, that give me an idea. For who is trying to acess the RDS database on Amazon AWS and is getting trouble, most likely you need to create a security group to the database, what I have did and worked was:
1) Go to https://console.aws.amazon.com/ec2/v2/
2) On left click on Security Groups
3) Create Security Group
4) Configure security group with the port of your database (in my case 3306, mysql)
5) Go to your database instance > Instance Actions > Modify
6) On "Security group", add the security group that you just created
7) Go to Continue and Apply immediately
来源:https://stackoverflow.com/questions/24830418/unable-to-connect-to-any-of-the-specified-mysql-hosts-mysql-connector-6-8-3-an