MySQL数据库托管在AWS EC2实例上的连接字符串?

huangapple go评论61阅读模式
英文:

Connection string of MySQL database hosted on AWS EC2 instance?

问题

我已在AWS的免费实例t2微型上设置了一个MySQL数据库,按照以下链接操作:https://towardsdatascience.com/running-mysql-databases-on-aws-ec2-a-tutorial-for-beginners-4301faa0c247

现在我可以使用MySQL Workbench和pem文件连接到数据库。

但是我的代码中应该使用什么连接字符串来连接到基于EC2实例的MySQL数据库?

我尝试过以下格式:
"Server=ec2-xyz.compute-1.amazonaws.com;Port=3306;Database=test;User ID=root;Password=test;"

当我尝试从.NET Core API访问时,我收到以下错误:MySql.Data.MySqlClient.MySqlException (0x80004005): 无法连接到指定的任何MySQL主机。

英文:

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

答案1

得分: 1

尝试过这种格式吗,其中指定了 UID 和 PWD 而不是 UserId 和 Password?连接字符串语法可能对这些名称要求严格。

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

英文:

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;

答案2

得分: 1

你需要通过SSH连接它。我找到了一篇解释如何通过SSH连接到EC2实例的文章。你需要使用"Renci SSH.NET"和"MySQLConnector" NuGet包。https://mysqlconnector.net/tutorials/connect-ssh/

var server = "你的数据库和SSH服务器";
var sshUserName = "你的SSH用户名";
var sshPassword = "你的SSH密码";
var databaseUserName = "你的数据库用户名";
var databasePassword = "你的数据库密码";

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();
}

希望这有所帮助!

英文:

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!

huangapple
  • 本文由 发表于 2023年2月6日 09:50:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356722.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定