Skip to content

Commit 4046519

Browse files
committed
upgrade mysql flexible-server for passwordless
1 parent 2198750 commit 4046519

File tree

2 files changed

+275
-30
lines changed

2 files changed

+275
-30
lines changed

articles/mysql/flexible-server/connect-java.md

Lines changed: 274 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ ms.date: 01/16/2021
1717

1818
This topic demonstrates creating a sample application that uses Java and [JDBC](https://en.wikipedia.org/wiki/Java_Database_Connectivity) to store and retrieve information in [Azure Database for MySQL Flexible Server](./index.yml).
1919

20+
JDBC is the standard Java API to connect to traditional relational databases.
21+
22+
In this article, we'll include two authentication methods: Azure Active Directory (Azure AD) authentication and MySQL authentication. The **Passwordless** tab shows the Azure AD authentication and the **Password** tab shows the MySQL authentication.
23+
24+
Azure AD authentication is a mechanism for connecting to Azure Database for MySQL using identities defined in Azure AD. With Azure AD authentication, you can manage database user identities and other Microsoft services in a central location, which simplifies permission management.
25+
26+
MySQL authentication uses accounts stored in MySQL. If you choose to use passwords as credentials for the accounts, these credentials will be stored in the `user` table. Because these passwords are stored in MySQL, you'll need to manage the rotation of the passwords by yourself.
27+
2028
## Prerequisites
2129

2230
- An Azure account with an active subscription.
@@ -32,67 +40,286 @@ We are going to use environment variables to limit typing mistakes, and to make
3240

3341
Set up those environment variables by using the following commands:
3442

43+
### [Passwordless (Recommended)](#tab/passwordless)
44+
45+
```bash
46+
export AZ_RESOURCE_GROUP=database-workshop
47+
export AZ_DATABASE_NAME=<YOUR_DATABASE_NAME>
48+
export AZ_LOCATION=<YOUR_AZURE_REGION>
49+
export AZ_MYSQL_AD_NON_ADMIN_USERNAME=demo-non-admin
50+
export AZ_USER_IDENTITY_NAME=<YOUR_USER_ASSIGNED_MANAGEMED_IDENTITY_NAME>
51+
export CURRENT_USERNAME=$(az ad signed-in-user show --query userPrincipalName -o tsv)
52+
export CURRENT_USER_OBJECTID=$(az ad signed-in-user show --query id -o tsv)
53+
```
54+
55+
Replace the placeholders with the following values, which are used throughout this article:
56+
57+
- `<YOUR_DATABASE_NAME>`: The name of your MySQL server. It should be unique across Azure.
58+
- `<YOUR_AZURE_REGION>`: The Azure region you'll use. You can use `eastus` by default, but we recommend that you configure a region closer to where you live. You can see the full list of available regions by entering `az account list-locations`.
59+
- `<YOUR_USER_ASSIGNED_MANAGEMED_IDENTITY_NAME>`: The name of your user assigned managed identity server. It should be unique across Azure.
60+
61+
### [Password](#tab/password)
62+
3563
```bash
36-
AZ_RESOURCE_GROUP=database-workshop
37-
AZ_DATABASE_NAME= flexibleserverdb
38-
AZ_LOCATION=<YOUR_AZURE_REGION>
39-
AZ_MYSQL_USERNAME=demo
40-
AZ_MYSQL_PASSWORD=<YOUR_MYSQL_PASSWORD>
41-
AZ_LOCAL_IP_ADDRESS=<YOUR_LOCAL_IP_ADDRESS>
64+
export AZ_RESOURCE_GROUP=database-workshop
65+
export AZ_DATABASE_NAME=<YOUR_DATABASE_NAME>
66+
export AZ_LOCATION=<YOUR_AZURE_REGION>
67+
export AZ_MYSQL_ADMIN_USERNAME=demo
68+
export AZ_MYSQL_ADMIN_PASSWORD=<YOUR_MYSQL_ADMIN_PASSWORD>
69+
export AZ_MYSQL_NON_ADMIN_USERNAME=demo-non-admin
70+
export AZ_MYSQL_NON_ADMIN_PASSWORD=<YOUR_MYSQL_NON_ADMIN_PASSWORD>
4271
```
4372

4473
Replace the placeholders with the following values, which are used throughout this article:
4574

4675
- `<YOUR_DATABASE_NAME>`: The name of your MySQL server. It should be unique across Azure.
4776
- `<YOUR_AZURE_REGION>`: The Azure region you'll use. You can use `eastus` by default, but we recommend that you configure a region closer to where you live. You can have the full list of available regions by entering `az account list-locations`.
48-
- `<YOUR_MYSQL_PASSWORD>`: The password of your MySQL database server. That password should have a minimum of eight characters. The characters should be from three of the following categories: English uppercase letters, English lowercase letters, numbers (0-9), and non-alphanumeric characters (!, $, #, %, and so on).
49-
- `<YOUR_LOCAL_IP_ADDRESS>`: The IP address of your local computer, from which you'll run your Java application. One convenient way to find it is to point your browser to [whatismyip.akamai.com](http://whatismyip.akamai.com/).
77+
- `<YOUR_MYSQL_ADMIN_PASSWORD>` and `<YOUR_MYSQL_NON_ADMIN_PASSWORD>`: The password of your MySQL database server. That password should have a minimum of eight characters. The characters should be from three of the following categories: English uppercase letters, English lowercase letters, numbers (0-9), and non-alphanumeric characters (!, $, #, %, and so on).
78+
79+
---
5080

5181
Next, create a resource group:
5282

5383
```azurecli
5484
az group create \
5585
--name $AZ_RESOURCE_GROUP \
5686
--location $AZ_LOCATION \
57-
| jq
87+
--output tsv
5888
```
5989

60-
> [!NOTE]
61-
> We use the `jq` utility, which is installed by default on [Azure Cloud Shell](https://shell.azure.com/) to display JSON data and make it more readable.
62-
> If you don't like that utility, you can safely remove the `| jq` part of all the commands we'll use.
63-
6490
## Create an Azure Database for MySQL instance
6591

92+
### Create a MySQL server and set up admin user
93+
6694
The first thing we'll create is a managed MySQL server.
6795

6896
> [!NOTE]
6997
> You can read more detailed information about creating MySQL servers in [Create an Azure Database for MySQL server by using the Azure portal](./quickstart-create-server-portal.md).
7098
71-
In [Azure Cloud Shell](https://shell.azure.com/), run the following script:
99+
#### [Passwordless connection (Recommended)](#tab/passwordless)
100+
101+
If you're using Azure CLI, run the following command to make sure it has sufficient permission:
102+
103+
```bash
104+
az login --scope https://graph.microsoft.com/.default
105+
```
106+
107+
Run the following command to create the server:
72108

73109
```azurecli
74110
az mysql flexible-server create \
75111
--resource-group $AZ_RESOURCE_GROUP \
76112
--name $AZ_DATABASE_NAME \
77113
--location $AZ_LOCATION \
78-
--sku-name Standard_B1ms \
79-
--storage-size 5120 \
80-
--admin-user $AZ_MYSQL_USERNAME \
81-
--admin-password $AZ_MYSQL_PASSWORD \
82-
--public-access $AZ_LOCAL_IP_ADDRESS
83-
| jq
114+
--yes \
115+
--output tsv
116+
```
117+
118+
Run the following command to create user identity for assigning:
119+
120+
```azurecli
121+
az identity create \
122+
--resource-group $AZ_RESOURCE_GROUP \
123+
--name $AZ_USER_IDENTITY_NAME
84124
```
85125

86-
Make sure your enter \<YOUR-IP-ADDRESS\> in order to access the server from your local machine. This command creates a Burstable Tier MySQL flexible server suitable for development.
126+
Run the following command to assign the identity to MySQL server for creating Azure AD admin:
127+
128+
```azurecli
129+
az mysql flexible-server identity assign \
130+
--resource-group $AZ_RESOURCE_GROUP \
131+
--server-name $AZ_DATABASE_NAME \
132+
--identity $AZ_USER_IDENTITY_NAME
133+
```
134+
135+
Run the following command to set the Azure AD admin user:
136+
137+
```azurecli
138+
az mysql flexible-server ad-admin create \
139+
--resource-group $AZ_RESOURCE_GROUP \
140+
--server-name $AZ_DATABASE_NAME \
141+
--display-name $CURRENT_USERNAME \
142+
--object-id $CURRENT_USER_OBJECTID \
143+
--identity $AZ_USER_IDENTITY_NAME
144+
```
145+
146+
> [!IMPORTANT]
147+
> When setting the administrator, a new user is added to the Azure Database for MySQL server with full administrator permissions. Only one Azure AD admin can be created per MySQL server and selection of another one will overwrite the existing Azure AD admin configured for the server.
148+
149+
This command creates a small MySQL server and sets the Active Directory admin to the signed-in user.
150+
151+
#### [Password](#tab/password)
152+
153+
```azurecli
154+
az mysql flexible-server create \
155+
--resource-group $AZ_RESOURCE_GROUP \
156+
--name $AZ_DATABASE_NAME \
157+
--location $AZ_LOCATION \
158+
--admin-user $AZ_MYSQL_ADMIN_USERNAME \
159+
--admin-password $AZ_MYSQL_ADMIN_PASSWORD \
160+
--yes \
161+
--output tsv
162+
```
163+
164+
This command creates a small MySQL server.
165+
166+
---
87167

88168
The MySQL server that you created has a empty database called **flexibleserverdb**. We will use this database for this article.
89169

90170
[Having any issues? Let us know.](https://github.com/MicrosoftDocs/azure-docs/issues)
91171

172+
### Configure a firewall rule for your MySQL server
173+
174+
Azure Database for MySQL instances are secured by default. They have a firewall that doesn't allow any incoming connection.
175+
176+
You can skip this step if you are using bash, because the `flexible-server create` command already detected your local IP address and set it on MySQL server.
177+
178+
But if you're connecting to your MySQL server from WSL on a Windows computer, you'll need to add the WSL host ID to your firewall.
179+
180+
Obtain the IP address of your host machine by running the following command in WSL:
181+
182+
```bash
183+
cat /etc/resolv.conf
184+
```
185+
186+
Copy the IP address following the term `nameserver`, then use the following command to set an environment variable for the WSL IP Address:
187+
188+
```bash
189+
AZ_WSL_IP_ADDRESS=<the-copied-IP-address>
190+
```
191+
192+
Then, use the following command to open the server's firewall to your WSL-based app:
193+
194+
```azurecli
195+
az mysql flexible-server firewall-rule create \
196+
--resource-group $AZ_RESOURCE_GROUP \
197+
--name $AZ_DATABASE_NAME \
198+
--start-ip-address $AZ_WSL_IP_ADDRESS \
199+
--end-ip-address $AZ_WSL_IP_ADDRESS \
200+
--rule-name allowiprange \
201+
--output tsv
202+
```
203+
204+
### Configure a MySQL database
205+
206+
Create a new database called `demo` by using the following command:
207+
208+
```azurecli
209+
az mysql flexible-server db create \
210+
--resource-group $AZ_RESOURCE_GROUP \
211+
--database-name demo \
212+
--server-name $AZ_DATABASE_NAME \
213+
--output tsv
214+
```
215+
216+
### Create a MySQL non-admin user and grant permission
217+
218+
Next, create a non-admin user and grant all permissions on the `demo` database to it.
219+
220+
> [!NOTE]
221+
> You can read more detailed information about creating MySQL users in [Create users in Azure Database for MySQL](/azure/mysql/single-server/how-to-create-users).
222+
223+
#### [Passwordless connection (Recommended)](#tab/passwordless)
224+
225+
Create a SQL script called *create_ad_user.sql* for creating a non-admin user. Add the following contents and save it locally:
226+
227+
```bash
228+
export AZ_MYSQL_AD_NON_ADMIN_USERID=$CURRENT_USER_OBJECTID
229+
230+
cat << EOF > create_ad_user.sql
231+
SET aad_auth_validate_oids_in_tenant = OFF;
232+
233+
CREATE AADUSER '$AZ_MYSQL_AD_NON_ADMIN_USERNAME' IDENTIFIED BY '$AZ_MYSQL_AD_NON_ADMIN_USERID';
234+
235+
GRANT ALL PRIVILEGES ON demo.* TO '$AZ_MYSQL_AD_NON_ADMIN_USERNAME'@'%';
236+
237+
FLUSH privileges;
238+
239+
EOF
240+
```
241+
242+
Then, use the following command to run the SQL script to create the Azure AD non-admin user:
243+
244+
```bash
245+
mysql -h $AZ_DATABASE_NAME.mysql.database.azure.com --user $CURRENT_USERNAME --enable-cleartext-plugin --password=$(az account get-access-token --resource-type oss-rdbms --output tsv --query accessToken) < create_ad_user.sql
246+
```
247+
248+
Now use the following command to remove the temporary SQL script file:
249+
250+
```bash
251+
rm create_ad_user.sql
252+
```
253+
254+
#### [Password](#tab/password)
255+
256+
Create a SQL script called *create_user.sql* for creating a non-admin user. Add the following contents and save it locally:
257+
258+
```bash
259+
cat << EOF > create_user.sql
260+
261+
CREATE USER '$AZ_MYSQL_NON_ADMIN_USERNAME'@'%' IDENTIFIED BY '$AZ_MYSQL_NON_ADMIN_PASSWORD';
262+
263+
GRANT ALL PRIVILEGES ON demo.* TO '$AZ_MYSQL_NON_ADMIN_USERNAME'@'%';
264+
265+
FLUSH PRIVILEGES;
266+
267+
EOF
268+
```
269+
270+
Then, use the following command to run the SQL script to create the Azure AD non-admin user:
271+
272+
```bash
273+
mysql -h $AZ_DATABASE_NAME.mysql.database.azure.com --user $AZ_MYSQL_ADMIN_USERNAME --enable-cleartext-plugin --password=$AZ_MYSQL_ADMIN_PASSWORD < create_user.sql
274+
```
275+
276+
Now use the following command to remove the temporary SQL script file:
277+
278+
```bash
279+
rm create_user.sql
280+
```
281+
282+
---
283+
92284
### Create a new Java project
93285

94286
Using your favorite IDE, create a new Java project, and add a `pom.xml` file in its root directory:
95287

288+
#### [Passwordless connection (Recommended)](#tab/passwordless)
289+
290+
```xml
291+
<?xml version="1.0" encoding="UTF-8"?>
292+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
293+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
294+
<modelVersion>4.0.0</modelVersion>
295+
<groupId>com.example</groupId>
296+
<artifactId>demo</artifactId>
297+
<version>0.0.1-SNAPSHOT</version>
298+
<name>demo</name>
299+
300+
<properties>
301+
<java.version>1.8</java.version>
302+
<maven.compiler.source>1.8</maven.compiler.source>
303+
<maven.compiler.target>1.8</maven.compiler.target>
304+
</properties>
305+
306+
<dependencies>
307+
<dependency>
308+
<groupId>mysql</groupId>
309+
<artifactId>mysql-connector-java</artifactId>
310+
<version>8.0.30</version>
311+
</dependency>
312+
<dependency>
313+
<groupId>com.azure</groupId>
314+
<artifactId>azure-identity-providers-jdbc-mysql</artifactId>
315+
<version>1.0.0-beta.1</version>
316+
</dependency>
317+
</dependencies>
318+
</project>
319+
```
320+
321+
#### [Password](#tab/password)
322+
96323
```xml
97324
<?xml version="1.0" encoding="UTF-8"?>
98325
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -113,29 +340,47 @@ Using your favorite IDE, create a new Java project, and add a `pom.xml` file in
113340
<dependency>
114341
<groupId>mysql</groupId>
115342
<artifactId>mysql-connector-java</artifactId>
116-
<version>8.0.20</version>
343+
<version>8.0.30</version>
117344
</dependency>
118345
</dependencies>
119346
</project>
120347
```
121348

349+
---
350+
122351
This file is an [Apache Maven](https://maven.apache.org/) that configures our project to use:
123352

124353
- Java 8
125354
- A recent MySQL driver for Java
126355

127356
### Prepare a configuration file to connect to Azure Database for MySQL
128357

129-
Create a *src/main/resources/application.properties* file, and add:
358+
Run the following script in the project root directory to create a *src/main/resources/application.properties* file and add configuration details:
130359

131-
```properties
132-
url=jdbc:mysql://$AZ_DATABASE_NAME.mysql.database.azure.com:3306/demo?serverTimezone=UTC
133-
user=demo
134-
password=$AZ_MYSQL_PASSWORD
360+
#### [Passwordless connection (Recommended)](#tab/passwordless)
361+
362+
```bash
363+
mkdir -p src/main/resources && touch src/main/resources/application.properties
364+
365+
cat << EOF > src/main/resources/application.properties
366+
url=jdbc:mysql://${AZ_DATABASE_NAME}.mysql.database.azure.com:3306/demo?sslMode=REQUIRED&serverTimezone=UTC&defaultAuthenticationPlugin=com.azure.identity.providers.mysql.AzureIdentityMysqlAuthenticationPlugin&authenticationPlugins=com.azure.identity.providers.mysql.AzureIdentityMysqlAuthenticationPlugin
367+
user=${AZ_MYSQL_AD_NON_ADMIN_USERNAME}
368+
EOF
135369
```
136370

137-
- Replace the two `$AZ_DATABASE_NAME` variables with the value that you configured at the beginning of this article.
138-
- Replace the `$AZ_MYSQL_PASSWORD` variable with the value that you configured at the beginning of this article.
371+
#### [Password](#tab/password)
372+
373+
```bash
374+
mkdir -p src/main/resources && touch src/main/resources/application.properties
375+
376+
cat << EOF > src/main/resources/application.properties
377+
url=jdbc:mysql://${AZ_DATABASE_NAME}.mysql.database.azure.com:3306/demo?useSSL=true&sslMode=REQUIRED&serverTimezone=UTC
378+
user=${AZ_MYSQL_NON_ADMIN_USERNAME}
379+
password=${AZ_MYSQL_NON_ADMIN_PASSWORD}
380+
EOF
381+
```
382+
383+
---
139384

140385
> [!NOTE]
141386
> We append `?serverTimezone=UTC` to the configuration property `url`, to tell the JDBC driver to use the UTC date format (or Coordinated Universal Time) when connecting to the database. Otherwise, our Java server would not use the same date format as the database, which would result in an error.

articles/mysql/single-server/connect-java.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ EOF
233233
Then, use the following command to run the SQL script to create the Azure AD non-admin user:
234234

235235
```bash
236-
mysql -h $AZ_DATABASE_NAME.mysql.database.azure.com --user $CURRENT_USERNAME@$AZ_DATABASE_NAME --enable-cleartext-plugin --password=`az account get-access-token --resource-type oss-rdbms --output tsv --query accessToken` < create_ad_user.sql
236+
mysql -h $AZ_DATABASE_NAME.mysql.database.azure.com --user $CURRENT_USERNAME@$AZ_DATABASE_NAME --enable-cleartext-plugin --password=$(az account get-access-token --resource-type oss-rdbms --output tsv --query accessToken) < create_ad_user.sql
237237
```
238238

239239
Now use the following command to remove the temporary SQL script file:

0 commit comments

Comments
 (0)