简体   繁体   中英

Cannot connect to database using static IP address + port in C#

I have an existing application that connects to the MySQL database (using WampServer). I can use localhost as the name of my server.

I tried using the static IP address instead of localhost . The database can be accessed from http://192.168.1.1:1234/phpmyadmin .

I already set up my app.config this way:

<appSettings>
    <add key="MyConnection" value="Data Source=192.168.1.1;Port=1234;Database=mydb;UID=root;" />
  </appSettings>

but receiving an error when I tried to run the application:

Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

Any suggestions what to do?

By default, phpmyadmin listen on localhost (127.0.0.1). You have to modify the config file to listen on 0.0.0.0

Or just comment this line in your /etc/my.cnf :

#bind-address            = 127.0.0.1

And restart your mysql server

The database can be accessed from http://192.168.1.1:1234/phpmyadmin

This just means that your webserver and PHP is running at 192.168.1.1 on port 1234 . This doesn't tell us anything about where MySQL is running.

If PHPMyAdmin is working correctly, and you can see your databases, then you could look in the PHPMyAdmin config file for the MySQL connection info.

Typically, if configured for TCP, MySQL listens on port 3306. I would try changing your connection string to

Data Source=192.168.1.1;Port=3306;...

If that doesn't work, it's probably as user2196728 said , that MySQL is only listening on the local loopback adapter (127.0.0.1), which is more secure. In that case, you should use localhost in your connection string, or change the config file to listen on 0.0.0.0, which means all adapters with an IP address.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM