skip to Main Content

I did setup of a MySQL database on AWS free instance t2 micro by following the link: https://towardsdatascience.com/running-mysql-databases-on-aws-ec2-a-tutorial-for-beginners-4301faa0c247

Now I am able to connect to database using MySql Workbench and SSH with pem file.

But What should be the connection string in my code to connect to ec2 instace based my sql database?

I’ve trid this format:
"Server=ec2-xyz.compute-1.amazonaws.com;Port=3306;Database=test;User ID=root;Password=test;"

I am getting this error when I try to access it from .NET Core API: MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts

2

Answers


  1. Have you tried a format like this, where it specifies UID and PWD instead of UserId and Password? The connection string syntax may be picky about those names.

    Database=database_name;UID=user_name;PWD=password;Port=port_number;

    Login or Signup to reply.
  2. You need to connect it via ssh. I found this article that explains how to connect to an ec2 instance through ssh. You will need "Renci SSH.NET" and "MySQLConnector" NuGet packages. https://mysqlconnector.net/tutorials/connect-ssh/

    var server = "your db & ssh server";
    var sshUserName = "your SSH user name";
    var sshPassword = "your SSH password";
    var databaseUserName = "your database user name";
    var databasePassword = "your database password";
    
    var (sshClient, localPort) = ConnectSsh(server, sshUserName, sshPassword);
    using (sshClient)
    {
        MySqlConnectionStringBuilder csb = new MySqlConnectionStringBuilder
        {
            Server = "127.0.0.1",
            Port = localPort,
            UserID = databaseUserName,
            Password = databasePassword,
        };
    
        using var connection = new MySqlConnection(csb.ConnectionString);
        connection.Open();
    

    Hope this helps!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search