练习 - 部署 Azure Database for MySQL
在此练习中,你将创建 Azure for MySQL Database 实例,并为其加载示例数据。
获取示例应用程序和脚本
首先,从 GitHub 存储库中克隆示例应用程序和 shell 脚本:
git clone https://github.com/MicrosoftDocs/mslearn-jakarta-ee-azure.git
克隆项目后,你会看到以下目录和文件:
├── Azure-MySQL-Setup-For-Sample-App.md
├── README.md
├── pom.xml
├── setup_mysql.sh
├── src
│ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ └── microsoft
│ │ │ └── azure
│ │ │ └── samples
│ │ │ ├── JAXRSConfiguration.java
│ │ │ ├── controllers
│ │ │ │ ├── CityService.java
│ │ │ │ └── CountryService.java
│ │ │ ├── entities
│ │ │ │ ├── City.java
│ │ │ │ └── Country.java
│ │ │ └── rest
│ │ │ └── WorldServiceEndpoint.java
│ │ ├── resources
│ │ │ └── META-INF
│ │ │ └── persistence.xml
│ │ └── webapp
│ │ └── WEB-INF
│ │ ├── beans.xml
│ │ ├── createMySQLDataSource.sh
│ │ └── web.xml
│ └── test
│ └── java
│ └── com
│ └── microsoft
│ └── azure
│ └── samples
│ └── SampleTest.java
└── world.sql
登录 Azure
如果你尚未登录 Azure,请先登录:
az login
设置默认安装位置
此模块中使用的脚本执行的命令需要 --___location
选项。 可以使用以下命令为此选项指定默认值。
az configure --defaults ___location=<desired ___location>
备注
建议你更改到用于部署你的 Java EE 应用程序的区域。
创建 Azure Database for MySQL 实例
在登录后,使用项目脚本 setup_mysql.sh
创建你的 Azure Database for MySQL 实例。 请确保位于 mslearn-jakarta-ee-azure
目录中。
重要
在 IPv4 环境中运行以下命令。 如果你的环境具有 IPv6 地址,则此命令会失败,因为它的防火墙配置尚不支持 IPv6 地址。
./setup_mysql.sh flexible
记下命令输出中显示的密钥值。 你在后续步骤中将使用这些值。
[INFO] -------------------------------------------------------
[INFO] Azure Database for MySQL Setup Completed SUCCESS
[INFO] -------------------------------------------------------
[INFO] 1. Please copy the following value into your temporal file
[INFO]
[INFO] RESOURCE GROUP is MySQL-RG-20201208152233
[INFO] MySQL HOSTNAME is mysqlserver-wqcnzwhqvw.mysql.database.azure.com
[INFO] MySQL USERNAME is azureuser
[INFO] MySQL PASSWORD is **********
[INFO]
[INFO]
[INFO] 2. Please execute the following command.
[INFO]
[INFO] mysql -u azureuser -h mysqlserver-wqcnzwhqvw.mysql.database.azure.com -p [Enter Key]
[INFO] Enter password: ********** [COPY&PASTE]
[INFO]
[INFO]
[INFO] 3. Clean up Resource (Delete MySQL DB)
[INFO] az group delete -n MySQL-RG-20201208152233
[INFO] -------------------------------------------------------
从示例数据库中获取数据
在此模块中,你将使用来自官方 MySQL 网站的名为 world
的示例数据库。 若要获取数据,请执行以下操作:
下载数据库文件:
curl -o world-db.zip https://downloads.mysql.com/docs/world-db.zip
解压缩数据库文件:
unzip world-db.zip
访问 SQL 文件:
cd world-db ls -l world.sql
-rw-r--r-- 1 ****** wheel 398635 1 7 12:25 world.sql
登录到 MySQL 数据库
获得 MySQL 数据库后,可以使用 mysql
命令和你创建灵活服务器实例时记录的密码来访问数据库:
mysql -u azureuser -h mysqlserver-<your instance>.mysql.database.azure.com -p [Enter]
Enter password: [**********]
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.7.29-log MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
为应用程序创建数据库和表
运行以下 mysql
命令:
mysql> source world.sql
Query OK, 0 rows affected (0.01 sec)
....
....
Query OK, 0 rows affected (0.01 sec)
mysql>
world
数据库及其表将在 MySQL 数据库中自动创建。 此操作需要几分钟。
确认数据库和表
确认数据库在你的服务器中:
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | world | +--------------------+ 5 rows in set (0.02 sec)
指向
world
数据库中的数据:mysql> use world; Database changed
确认
world
数据库中的表:mysql> show tables; +-----------------+ | Tables_in_world | +-----------------+ | city | | country | | countrylanguage | +-----------------+ 3 rows in set (0.04 sec)
查询示例数据库
现在,你可以查看 world
数据库的内容。
获取所有洲信息:
mysql> select distinct Continent from country ; +---------------+ | Continent | +---------------+ | North America | | Asia | | Africa | | Europe | | South America | | Oceania | | Antarctica | +---------------+
按州获取国家/地区名称和国家/地区代码:
mysql> select code,name from country where Continent='Asia'; +------+----------------------+ | code | Name | +------+----------------------+ | AFG | Afghanistan | | ARE | United Arab Emirates | | ARM | Armenia | | AZE | Azerbaijan | | BGD | Bangladesh | | BHR | Bahrain | | BRN | Brunei | | BTN | Bhutan | | CHN | China | | CYP | Cyprus | | GEO | Georgia | | HKG | Hong Kong SAR | | IDN | Indonesia | | IND | India | | IRN | Iran | | IRQ | Iraq | | ISR | Israel | | JOR | Jordan | | JPN | Japan | ..... | VNM | Vietnam | | YEM | Yemen | +------+----------------------+ 51 rows in set (0.02 sec)
获取人口超过 1 百万的所有城市:
mysql> select * from city where CountryCode='JPN' AND Population > 1000000 ORDER BY Population DESC; +------+---------------------+-------------+-----------+------------+ | ID | Name | CountryCode | District | Population | +------+---------------------+-------------+-----------+------------+ | 1532 | Tokyo | JPN | Tokyo-to | 7980230 | | 1533 | Jokohama [Yokohama] | JPN | Kanagawa | 3339594 | | 1534 | Osaka | JPN | Osaka | 2595674 | | 1535 | Nagoya | JPN | Aichi | 2154376 | | 1536 | Sapporo | JPN | Hokkaido | 1790886 | | 1537 | Kioto | JPN | Kyoto | 1461974 | | 1538 | Kobe | JPN | Hyogo | 1425139 | | 1539 | Fukuoka | JPN | Fukuoka | 1308379 | | 1540 | Kawasaki | JPN | Kanagawa | 1217359 | | 1541 | Hiroshima | JPN | Hiroshima | 1119117 | | 1542 | Kitakyushu | JPN | Fukuoka | 1016264 | +------+---------------------+-------------+-----------+------------+ 11 rows in set (0.33 sec)
单元总结
现在,你已完成 MySQL 服务器的设置和准备工作。 下一单元将介绍将 Java EE (Jakarta EE) 应用程序部署到 Azure 应用服务上的 JBoss EAP 并对其进行配置的步骤。