I'm making a web api chat application in ASP.NET Razor Pages in VS Code with SignalR and with MySQL database. (Microsoft SQL Server Management Studio)
I wanted to store the messages and the users in a table, but when I made the MySQL connection I got an error message:
MySql.Data.MySqlClient.MySqlException (0x80004005): Host '' is not allowed to connect to this MySQL server
It can cause, that I connected to the database before in appsettings.json file with ConnectionString?
Here is the ChatHub.cs code:
using Microsoft.AspNetCore.SignalR;
using System.Threading.Tasks;
using System;
using MySql.Data;
using MySql.Data.MySqlClient;
using probagetrequest.Models;
namespace SignalRChat.Hubs
{
public class ChatHub : Hub
{
public async Task SendMessage(string user, string message)
{
string connn = "Server=localhost;Database=DBChatApp;user id=sa;password=xxx";
MySqlConnection con = new MySqlConnection(connn);
try {
Console.WriteLine("Connecting to MySql...");
con.Open();
string cmddd = "INSERT INTO Messages (Username,Message) VALUES (@Usernamee,@Messagee)";
MySqlCommand cmd = new MySqlCommand(cmddd, con);
cmd.Parameters.Add("@Usernamee", MySqlDbType.VarString).Value = user;
cmd.Parameters.Add("@Messagee", MySqlDbType.VarString).Value = message;
}
catch (Exception exc)
{
Console.WriteLine(exc.ToString());
}
con.Close();
Console.WriteLine("Done!!!!!");
Console.WriteLine($"user={user}, message={message}");
await Clients.All.SendAsync("ReceiveMessage", user, message);
}
}
}
Did you try logging into MySQL DB through mysql client CLI. I think it's a problem with "mysql user" privileges mentioned in your project to connect to the MySQL DB.
In general, the error you mentioned comes when a new database is setup but left un-configured.
Try the following commands logging into MySQL as super user
mysql> CREATE USER 'root'@'%' IDENTIFIED BY 'root';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
User contributions licensed under CC BY-SA 3.0