3

I have an ASP.NET MVC app connected to a MySQL database, which works fine with my local MySQL server. I'm trying to push the app to azure using their new feature: MySQL in-app for Web Apps

Previously, I tested the same app in azure with a SQL server database and I got it working as follows:

  1. I created a SQL database in my azure portal, and I got its ADO.NET connection string as explained here: https://azure.microsoft.com/en-us/documentation/articles/sql-database-develop-dotnet-simple/
  2. I published the application from Visual Studio, with the Publish command. In its wizard I was able to set the DefaultConnection string which should be used at runtime.

Can I proceed similarly with the MySQL-in-app database? If so, how can a get the connection string to be set in the publish step? I found an explanation with a script here but I don’t understand where should I run it to get the connection string!

2
  • You seemingly figured this out, so I hope you can help. I obtained the connection string and now pass Environment.GetEnvironmentVariable( "MYSQLCONNSTR_localdb" ) to options.UseSqlServer(). However, I still get 'access denied'. Is there anything more that needs to be done? How do I add the database (migrations) to MySQL in App? Commented Feb 20, 2017 at 3:32
  • Never mind, I just realized I am not using MySQL. To use this of course MySQL providers should be set up. Commented Feb 20, 2017 at 18:08

3 Answers 3

5

The “Get the database connection string” section explained with code how to get the database connection string via MYSQLCONNSTR_localdb environment variable in PHP, if you’d like to get the connection string of local MySQL database that is used in MySQL In App (Preview) in C#, you could refer to the following code snippet.

protected void Page_Load(object sender, EventArgs e)
{
    Response.Write(Environment.GetEnvironmentVariable("MYSQLCONNSTR_localdb").ToString());
}

Output

Database=localdb;Data Source=127.0.0.1:49497;User Id=azure;Password=6#vWHD_$

Besides, as far as I know, MySQL In App (Preview) runs a local MySQL instance with our app and shares resources from App service plan, we use MySQL In App (Preview) for development and testing. If you’d like to migrate the application to production to use non-local MySQL instance, you could create a MySQL database in the Azure portal (provider is ClearDB) and connect to it from your Azure App Service.

Sign up to request clarification or add additional context in comments.

1 Comment

I got the connection string through the environment variable and pass it to options.UseSqlServer(). However, I still get 'access denied'. I can access PhpMyAdmin through the portal, and no tables exist. What is the next step to import the database?
1

I wrote a little helper to make this easier:

https://github.com/kedzior-io/kedzior.io.ConnectionStringConverter

In Startup.cs

string connectionString = Environment.GetEnvironmentVariable("MYSQLCONNSTR_localdb");
        services.AddDbContext<ApplicationDbContext>(options =>  
            options.UseMySql(AzureMySQL.ToMySQLStandard(connectionString))
        );

You can setup environment variable on your machine as well:

Name: MYSQLCONNSTR_localdb
Value: Database=dotnetpwa;Data Source=127.0.0.1:3307;User Id=kedzior;Password=kedzior

This way you can run your application locally and publish it to Azure without even needing to know the connection string of MySQL In-App.

Comments

0

In my case, i get it works changing the connection's string format.

ConStr = System.Environment.GetEnvironmentVariable("MYSQLCONNSTR_localdb");
ConStr = ConStr.Replace("Database", "database");
ConStr = ConStr.Replace("Data Source", "server");
ConStr = ConStr.Replace("User Id", "user");
ConStr = ConStr.Replace("Password", "password");
ConStr = ConStr.Replace("127.0.0.1", "localhost");

// This line split "server=localhost:[port]" in "server=localhost;port=[port]
ConStr = ConStr.Replace(":", ";port="); 

Only the last line appears fix the problem for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.