locked
Connection to MySQL 5.0 within ASP.NET MVC Project fail RRS feed

  • Question

  • User1547786191 posted

    I am using mysql-connector-net-6.4.4 with Visual Studio 10 Prof.

    I get the error:
    "Unable to connect to any of the specified MySQL hosts"
    "error-no: 1042 = ER_BAD_HOST_ERROR."

    And the InnerException:
    "System.Net.Sockets.SocketException"
    "error-no 10022: An invalid argument was supplied"
    "Socket Error Code: invalid argument"


    With "MySQL Administrator or MySQL Workbench, I am able to get a connection to the database (also with MySQL Query Browser).
    But in my ASP.NET Project it does not work?

    That is the code I use within the C# Project:

    String myConnectionString = "server=127.0.0.1;" 
    + "uid=root;" 
    + "Persist Security Info=True;" 
    + "pwd=secret;" 
    + "port=3333;" 
    + "database=test;" 
    + "charset=latin1;" 
    ; 
    
    try 
    { 
    MySqlConnection conMySQL = new MySqlConnection(); 
    conMySQL.ConnectionString = myConnectionString; 
    conMySQL.Open(); 
    //later execute queries, etc 
    conMySQL.Close(); 
    } 
    catch (MySqlException ex) 
    { 
    string sMessage = ex.Message; 
    } 
    

    I have referenced in my ASP.NET MVC project:
    C:\Programme\MySQL\MySQL Connector Net 6.5.0\Assemblies\v4.0\MySql.Data.dll
    C:\Programme\MySQL\MySQL Connector Net 6.5.0\Assemblies\v4.0\MySql.Data.Entity.dll
    C:\Programme\MySQL\MySQL Connector Net 6.5.0\Assemblies\v4.0\MySql.Web.dll

    After a long try and no goal, I have done program a connection with a simple WindowsForms Project and VS2010 and the same connection-code above. This has worked! I could fetch some data with the Windowsform Project. 

    But withnin the ASP.NET MVC3 project not. I have try to put the code in Global.asax.cs in Application_Start(){...} at the very begining. But I can get figured out what is the diffence between the Windowsform-App (which works) and the ASP.NET-App (which works not).

    The exact error message is:
    "A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll"
    "A first chance exception of type 'System.Net.Sockets.SocketException' occurred in MySql.Data.dll"
    "A first chance exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll"
    "WebDev.WebServer40.exe Error: 0 : Unable to connect to any of the specified MySQL hosts."

    Can anyone please give me an advice, how can get I fixed this?

    Maybe it is a premission problem with "WebDev.WebServer40.exe" (the internal webserver which comes with the Visual Studio)?


    Thanks a lot for help. I have not found anything useful with Google.
    Harald

    Tuesday, January 17, 2012 2:28 PM

All replies

  • User197322208 posted

    asp.net user does not have rights to connect to

    3333
    Tuesday, January 17, 2012 5:21 PM
  • User1547786191 posted

    thanks! but,

    (1) why? With a normal Windows Apllication, which use the port=3333 within the connection string, there is no problem. Sure, I have changed the standard connection port for MySQL-DB in my.ini from 3306 to 3333, because to have a bit more security against outside.

    (2) how can i solve this?
    Looking for "how to set the right premissions"? The program "WebDev.WebServer40.EXE" have Administrator Group rights and is startet as Webserver for the Visual Studio, so I had belive ther should no permissions problems.

    (3) How can I give the asp.net user the right to connect, via port 3333, to the MySQL-DB?

    I have done, until now, more Windows programming, not so much ASP.NET Programs, so I am a newbee in to do ASP.NET configuration settings. With this myself teaching project I like to learn more about to program ASP.NET in conjunction with MVC.

    Harald

     

     

    Wednesday, January 18, 2012 11:11 AM
  • User197322208 posted

    asp.net runs under application pool account. Put the application pool run under your windows account.

    Wednesday, January 18, 2012 4:39 PM
  • User1547786191 posted

    application pool? - I am running ASP.NET with Visual Studio and the WebDev.WebServer40.exe. Not with the IIS.

    For the IIS 6.0 I have 4 AppPool defined (for some small test webs) and there is one DefaultAppPool and one MSSharePointAppPool. For my 4 AppPool's I have creates some new ISR_USER_... Accounts. I use the Accounts in the AppPool and for the security settings for the application directory witch are used in the webs who are collect together in the web pools.

    The IIS is "name based" configured not "IP based",so one IP differnd (self set) DNS-Names, which are manged in the system .../etc/host file. These web are developer webs for testing not to be reached from outsite.

    In between, I found an article on the MySQL site: http://blogs.oracle.com/MySqlOnWindows/entry/running_connector_net_6_5
    "Running Connector/Net 6.5 inside Medium-Trust Level"

    There is an example for this problem, but with pipes. I try that example and it works with pipes. But I need to run TCP/IP connection to the MySQL-DB connected about a port.

    Also I have try Your suggestion, and give the Standard Application Pool the settings to run under my Admin User Account. I put back the code form pipes to TCP/IP Connection, but the same error like I had asked for exist: "WebDev.WebServer40.exe Error: 0 : Unable to connect to any of the specified MySQL hosts."

    In the other example it was written:

    "The permissions that we needed were:
    - System.Net.SocketPermission
    - System.Security.Permissions.ReflectionPermission
    - System.Net.DnsPermission 
    - System.Security.Permissions.SecurityPermission "

    So make it sence, to try, to set that procammically? Does this work with ASP.NET
    Or can You please more explane about: ASP.NET running with WebDev.WebServer40.exe and their security settings?
    How to find? Where to configure? Or where to read about?

    Thanks again;

      @in addition:

    I found in the MSDN help:
    "The Visual Studio Development Server runs in a different security context than full IIS.

    When you run a page using the Visual Studio Development Server, the page runs in the context of your current user account.

    For example, if you are running as an administrator-level user, a page running in the Visual Studio Development Server
    will have administrator-level privileges.
    "
    So I think, with adminstrator-level privelege, it should not be a problem to connect to the MySQL DB which also runs under adminstrator-level privelege?

     

    Thursday, January 19, 2012 11:25 AM
  • User1547786191 posted

    No help?

    I am running ASP.NET with Visual Studio and its own Webserver the ...\WebDev.WebServer40.exe

    I can not connect to the database within the ASP.NET Project. But with a normal Visual Studio Windows-Project there are no Problems (same code).

    What could there be specific for the ASP.NET MVC2 Visual Studio Project?

    Under which permissions runs WebDev.WebServer40.exe?

    Obviously it does run with the VisualStudio and under my Admin-UserAccount, could there are in addition CAS ( Code Access Security ) restrictions which are not set, when DB-open code run in a normall Windows program?

    The problem is primary not how to connect to the mysql db, but more: why does this not work when run as a WebProject?

     

    Tuesday, January 24, 2012 9:22 AM