wayne
wayne

Reputation: 201

Error connecting to Amazon EC2 MySQL from C#

I have an AWS EC2 instance running. I've tried to make a connection to my MySQL like this in C#

MySqlConnection l_DBConn = new MySqlConnection();
l_DBConn.ConnectionString = "Server=my-ec2.compute-1.amazonaws.com;Port=3306;Database=mydatabase;uid=root;password=mypassword;port=3306;charset=utf8"
MySqlCommand command = l_DBConn.CreateCommand();
command.CommandText = "select * from users";
MySqlDataReader Reader = command.ExecuteReader();

The error received when I run the last line is "Connection must be valid and open.". I still can connect to my instance using Filezilla,Putty and MySQL WorkBench also(with SSH). And I'm using MySql.Data.MySqlClient in my code. Anyone has any idea about this? Is this because my instance does not allow remote access or a problem with my connection string?

Thank you in advance, Wayne.

Edit: The problem is solved by adding l_DBConn.Open() and add my IP to the server using RANT ALL PRIVILEGES ON . TO root@'hostname' IDENTIFIED BY 'root-password'. Thanks!

Upvotes: 1

Views: 1666

Answers (2)

Geoff Appleford
Geoff Appleford

Reputation: 18832

Some ideas:

  • Make sure you can connect to your database locally first.
  • Is Sql Server setup to allow mixed authentication
  • Have you open port 3306 in the EC2 firewall and the windows firewall if you're running it.

Upvotes: 1

flipchart
flipchart

Reputation: 6578

Try calling l_DBConn.open() before executing the reader. The error implies that the connection is not open since you haven't explicitely opened it

Upvotes: 1

Related Questions