Skip to content

Latest commit

 

History

History
207 lines (172 loc) · 8.02 KB

code-mysql-secret.md

File metadata and controls

207 lines (172 loc) · 8.02 KB
author ms.service ms.topic ms.date ms.author
yungez
service-connector
include
08/01/2023
yungez
  1. Install dependencies. Follow the guidance to install connector/NET MySQL
  2. In code, get MySQL connection string from environment variables added by Service Connector service. To establish encrypted connection to MySQL server over SSL, refer to these steps.
    using System;
    using System.Data;
    using MySql.Data.MySqlClient;
    
    string connectionString = Environment.GetEnvironmentVariable("AZURE_MYSQL_CONNECTIONSTRING");
    using (MySqlConnection connection = new MySqlConnection(connectionString))
    {
        connection.Open();
    }
  1. Install dependencies. Follow the guidance to install Connector/J.
  2. In code, get MySQL connection string from environment variables added by Service Connector service. To establish encrypted connection to MySQL server over SSL, refer to these steps.
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    
    String connectionString = System.getenv("AZURE_MYSQL_CONNECTIONSTRING");
    try (Connection connection = DriverManager.getConnection(connectionString)) {
        System.out.println("Connection successful!");
    } catch (SQLException e) {
        e.printStackTrace();
    }
  1. Install dependencies. Add following dependencies to your pom.xml file.
    <dependencyManagement>
      <dependencies>
        <dependency>
          <groupId>com.azure.spring</groupId>
          <artifactId>spring-cloud-azure-dependencies</artifactId>
          <version>4.10.0</version>
          <type>pom</type>
          <scope>import</scope>
        </dependency>
        <dependency>
          <groupId>com.azure.spring</groupId>
          <artifactId>spring-cloud-azure-starter-jdbc-mysql</artifactId>
        </dependency>
      </dependencies>
    </dependencyManagement>
  2. Setup normal Spring App application, more detail in this section. To establish encrypted connection to MySQL server over SSL, refer to these steps.
  1. Install dependencies. Follow the guidance to install Connector/Python.
  2. In code, get MySQL connection information from environment variables added by Service Connector service. To establish encrypted connection to MySQL server over SSL, refer to these steps.
    import os
    import mysql.connector
    
    host = os.getenv('AZURE_MYSQL_HOST')
    user = os.getenv('AZURE_MYSQL_USER')
    password = os.getenv('AZURE_MYSQL_PASSWORD')
    database = os.getenv('Azure_MYSQL_NAME')
    
    cnx = mysql.connector.connect(user=user, password=password,
                                  host=host,
                                  database=database)
    
    cnx.close()
  1. Install dependencies.
    pip install django
  2. In setting file, get MySQL database information from environment variables added by Service Connector service. To establish encrypted connection to MySQL server over SSL, refer to these steps.
    # in your setting file, eg. settings.py
    host = os.getenv('AZURE_MYSQL_HOST')
    user = os.getenv('AZURE_MYSQL_USER')
    password = os.getenv('AZURE_MYSQL_PASSWORD')
    database = os.getenv('AZURE_MYSQL_NAME')
    
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': database,
            'USER': user,
            'PASSWORD': password,
            'HOST': host
        }
    }
  1. Install dependencies.
    go get -u github.com/go-sql-driver/mysql
  2. In code, get MySQL connection string from environment variables added by Service Connector service. To establish encrypted connection to MySQL server over SSL, refer to these steps.
    import (
    "database/sql"
    "fmt"
    "os"
    
    _ "github.com/go-sql-driver/mysql"
    )
    
    connectionString := os.Getenv("AZURE_MYSQL_CONNECTIONSTRING")
    db, err := sql.Open("mysql", connectionString)
  1. Install dependencies.
    npm install mysql2
  2. In code, get MySQL connection information from environment variables added by Service Connector service. To establish encrypted connection to MySQL server over SSL, refer to these steps.
    const mysql = require('mysql2')
    
    const connection = mysql.createConnection({
      host: process.env.AZURE_MYSQL_HOST,
      user: process.env.AZURE_MYSQL_USER,
      password: process.env.AZURE_MYSQL_PASSWORD,
      database: process.env.AZURE_MYSQL_DATABASE,
      port: Number(process.env.AZURE_MYSQL_PORT) ,
      // ssl: process.env.AZURE_MYSQL_SSL
    });
    
    connection.connect((err) => {
      if (err) {
        console.error('Error connecting to MySQL database: ' + err.stack);
        return;
      }
      console.log('Connected to MySQL database.');
    });
  1. Install dependencies. Follow the guide to install MySQLi.
  2. In code, get MySQL connection information from environment variables added by Service Connector service. To establish encrypted connection to MySQL server over SSL, refer to these steps.
    <?php
    $host = getenv('AZURE_MYSQL_HOST');
    $username = getenv('AZURE_MYSQL_USER');
    $password = getenv('AZURE_MYSQL_PASSWORD');
    $database = getenv('Azure_MYSQL_DBNAME');
    $port = int(getenv('AZURE_MYSQL_PORT'));
    # $flag = getenv('AZURE_MYSQL_FLAG');
    
    $conn = mysqli_init();
    # mysqli_ssl_set($conn,NULL,NULL,NULL,NULL,NULL);
    mysqli_real_connect($conn, $host, $username, $password, $database, $port, NULL);
    
    if (mysqli_connect_errno($conn)) {
        die('Failed to connect to MySQL: ' . mysqli_connect_error());
    }
    
    echo 'Connected successfully to MySQL database!';
    mysqli_close($conn);
    ?>
  1. Install dependencies.
    gem install mysql2
  2. In code, get MySQL connection information from environment variables added by Service Connector service. To establish encrypted connection to MySQL server over SSL, refer to these steps.
    require 'mysql2'
    require 'dotenv/load'
    
    client = Mysql2::Client.new(
      host: ENV['AZURE_MYSQL_HOST'],
      username: ENV['AZURE_MYSQL_USERNAME'],
      password: ENV['AZURE_MYSQL_PASSWORD'],
      database: ENV['AZURE_MYSQL_DATABASE'],
      # sslca: ca_path
    )
    
    client.close

For other languages, use the connection properties that Service Connector sets to the environment variables to connect the database. For environment variable details, see Integrate Azure Database for MySQL with Service Connector.