简体   繁体   中英

Can't connect to MySQL database using MySQLConnection

I'm using the MySqlConnection class to connect to a local MySQL database:

SourceHost = ConfigurationManager.AppSettings["sourceHost"];
SourceDatabase = ConfigurationManager.AppSettings["sourceDatabase"];
SourceUsername = ConfigurationManager.AppSettings["sourceUsername"];
SourcePassword = ConfigurationManager.AppSettings["sourcePassword"];

SourceString = String.Format("Server={0};Database={1};UID={2};Password={3};", SourceHost, SourceDatabase, SourceUsername, SourcePassword);
var sqlSourceConnection = new MySqlConnection(SourceString);

sqlSourceConnection.Open();

These are the values in the App.config file

<add key="sourceHost" value="myHost" />
<add key="sourceDatabase" value="myDB" />
<add key="sourceUsername" value="myUSer" />
<add key="sourcePassword" value="myPSW" />
<add key="sourceDbType" value="mysql" />

The error I get is saying: Access denied for user myUser@myHost.myDomain using password:YES, but by using the myUser and myPSW credentials I can login from the command prompt.

What's wrong?

EDIT: It worked when I changed the sourceHost to

<add key="sourceHost" value="localhost" />

User myUser is only authorized from localhost.
You need to authorize myUser for your hostname.

GRANT ALL PRIVILEGES ON *.* to 'myUser'@'%' WITH GRANT OPTION;

or

GRANT ON *.* TO 'myUser'@'myHost.myDomain' IDENTIFIED BY 'password' ;

or you can also do

GRANT ON *.* TO 'myUser'@'%.myDomain' IDENTIFIED BY 'password' ;

Standard

Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;

Specifying TCP port

Server=myServerAddress;Port=1234;Database=myDataBase;Uid=myUsername;Pwd=myPassword;

The port 3306 is the default MySql port.The value is ignored if Unix socket is used.

When you try SHOW GRANTS you should see line like this:

mysql> SHOW GRANTS;
+-----------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'myUSer'@'localhost' WITH GRANT OPTION |
+-----------------------------------------------------------------------+

Note the localhost , so this happens with the connection:

  • When you try to connect to localhost system automatically uses 127.0.0.1 as connection source address.
  • But when you connect to 10.0.0.1 it uses 10.0.0.1 as your source address.

Just try ping with -S :

C:\>ping localhost -S 127.0.0.1

Pinging localhost [127.0.0.1] from 127.0.0.1 with 32 bytes of data:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

C:\>ping localhost -S 10.0.0.1

Pinging localhost [127.0.0.1] from 10.0.0.1 with 32 bytes of data:
PING: transmit failed. General failure.

C:\>ping 10.0.0.1 -S 10.0.0.1

Pinging 10.0.0.1 from 10.0.0.1 with 32 bytes of data:
Reply from 10.0.0.1: bytes=32 time<1ms TTL=128

C:\>ping 10.0.0.1 -S 127.0.0.1

Pinging 10.0.0.1 from 127.0.0.1 with 32 bytes of data:
PING: transmit failed. General failure.

So it works with localhost , because localhost is an allowed source host , and it doesn't with your_host because 10.0.0.1 is not an allowed host.

Just grant privileges for your user to 10.0.0.1 and it should work (maybe you'll have to change firewall settings), or allow all connection from the outside (using % as host).

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