You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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).
19
19
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
+
20
28
## Prerequisites
21
29
22
30
- 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
32
40
33
41
Set up those environment variables by using the following commands:
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.
Replace the placeholders with the following values, which are used throughout this article:
45
74
46
75
-`<YOUR_DATABASE_NAME>`: The name of your MySQL server. It should be unique across Azure.
47
76
-`<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
+
---
50
80
51
81
Next, create a resource group:
52
82
53
83
```azurecli
54
84
az group create \
55
85
--name $AZ_RESOURCE_GROUP \
56
86
--location $AZ_LOCATION \
57
-
| jq
87
+
--output tsv
58
88
```
59
89
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
-
64
90
## Create an Azure Database for MySQL instance
65
91
92
+
### Create a MySQL server and set up admin user
93
+
66
94
The first thing we'll create is a managed MySQL server.
67
95
68
96
> [!NOTE]
69
97
> 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).
70
98
71
-
In [Azure Cloud Shell](https://shell.azure.com/), run the following script:
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:
72
108
73
109
```azurecli
74
110
az mysql flexible-server create \
75
111
--resource-group $AZ_RESOURCE_GROUP \
76
112
--name $AZ_DATABASE_NAME \
77
113
--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
84
124
```
85
125
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
+
---
87
167
88
168
The MySQL server that you created has a empty database called **flexibleserverdb**. We will use this database for this article.
89
169
90
170
[Having any issues? Let us know.](https://github.com/MicrosoftDocs/azure-docs/issues)
91
171
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).
> 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.
0 commit comments