简体   繁体   中英

Authentication to host 'localhost' for user 'root' using method 'mysql_native_password' failed with message: Unknown database 'xxx'

I am using code first approach in my project. But when i am trying to run db-migrations by using 'update-database' command i am getting null reference exception and when I am running the application I am getting the following configuration error: Authentication to host 'localhost' for user 'root' using method 'mysql_native_password' failed with message: Unknown database 'xxx'.

Here is the connection string:

 <add name="DefaultConnectionString" connectionString="server=localhost;port=3306;Database=cps;uid=root;pwd=root;" providerName="MySql.Data.MySqlClient" />

PS: It's running fine on the other system.

Your connection string is demanding a MySQL database called cps . It seems likely you have not yet created that database on your local MySQL server.

Pro tip: Always read error message text carefully, asking yourself "what could this mean?"

The solution I got for below error.

Authentication to host 'localhost' for user 'root' using method 'mysql_native_password' failed with message: Access denied for user 'root'@'localhost' (using password: YES)

<add name="constring" connectionString="Server=localhost;Uid=root;Database=databasename;Port=3306;" providerName="MySql.Data.MySqlClient"/>

=> Just remove the password

=> If you have 2 local servers with a different port number, just add it or else by default it will take 3306

Authentication to host 'localhost' for user 'root' using method 'mysql_native_password' failed with message: Access denied for user 'root'@'localhost' (using password: YES)

from

public string strconn = "SERVER = localhost ;DATABASE=restaurant;UID = root;pwd=12345678;convert zero datetime=True";

become

public string strconn = "SERVER =127.0.0.1 ;DATABASE=restaurant;UID = root;pwd=12345678;convert zero datetime=True"; fix localhost to 127.0.0.1 in connectionstring

Thanks for all the answers, But in my case as @GirishBabuC said its port and need to append the port in connection string in case you are using other then default port.

<add name="constring" connectionString="Server=localhost;Uid=root;Database=databasename;Port=3307;" providerName="MySql.Data.MySqlClient"/>

in my case its 3307.

while installing its asks for port.

This issue could come up depending on which version of MySQL is being targeted. You are welcome to try this solution to see if it works for you. Instead of using the Uid=*;Pwd=*; format, make use of the user=*;password=*; format.

Original Connection String:

Server=localhost;Port=3306;Database=databasename;Uid=sqltracking;Pwd=sqltracking;

One that works

server=localhost;port=3306;database=databasename;user=sqltracking;password=sqltracking;

The first method works on MySql 8, but not on MySql 5.

I haven't dug into the documentation behind these connection string configurations, however, I have noticed a difference between MySQL 5.7 and 8.0.

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