如何在 Amazon EMR 上的 Hive 與 Amazon RDS for MySQL 上的中繼存放區之間建立 SSL 連線?
我想要在 Amazon EMR 上的 Hive 與 Amazon Relational Database Service (Amazon RDS) for MySQL 上的中繼存放區之間建立 SSL 連線。
簡短說明
若要在 Hive 和外部中繼存放區之間建立加密連線,請使用 SSL 憑證。您可以在啟動新的 Amazon EMR 叢集時或叢集運作後設定 SSL 連線。
注意: 以下步驟在 Amazon EMR 版本 7.3.0 和 Amazon RDS for MySQL 版本 8.0.39 上進行了測試。
解決方法
注意: 如果您在執行 AWS Command Line Interface (AWS CLI) 命令時收到錯誤訊息,則請參閱對 AWS CLI 進行錯誤疑難排解。此外,請確定您使用的是最新的 AWS CLI 版本。
在新的 Amazon EMR 叢集上設定 SSL 連接
請完成下列步驟:
-
若要建立 Amazon RDS for MySQL 資料庫執行個體,請執行下列 create-db-instance 命令:
aws rds create-db-instance \ --db-name hive \ --db-instance-identifier example-db-instance-identifier \ --db-instance-class db.c6gd.large \ --engine mysql --engine-version 8.0.39 \ --db-subnet-group-name example-subnet-group \ --master-username example-rds-primary-user \ --master-user-password example-rds-primary-password \ --allocated-storage 200 \ --storage-type gp3 \ --vpc-security-group-ids example-rds-vpc-security-group注意: 將 example-db-instance-identifier 替換為您的資料庫執行個體識別碼,將 example-subnet-group 替換為您的子網路群組名稱,將 example-rds-primary-user 替換為您的Amazon RDS 主使用者名稱,將 example-rds-primary-password 替換為您的Amazon RDS 主密碼。另外,將 example-rds-vpc-security-group 替換為您的 Amazon RDS Amazon Virtual Private Cloud (Amazon VPC) 安全群組名稱。
-
以主使用者身分連線到 Amazon RDS for MySQL 資料庫執行個體。然後,為 Hive 中繼存放區建立使用者:
注意: 請確認您已限制主要使用者對您先前建立的資料庫執行個體的存取權。mysql -h example-rds-db-instance-endpoint -P 3306 -u example-rds-primary-user -p Enter password: example-rds-primary-passwordCREATE USER 'example-hive-username' IDENTIFIED BY 'example-hive-password' REQUIRE SSL; REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'example-hive-username'; GRANT ALL PRIVILEGES ON hive.* TO 'example-hive-username'; FLUSH PRIVILEGES;注意: 將 example-rds-db-instance-endpoint 替換為您的 Amazon RDS 資料庫執行個體端點,將 example-rds-primary-user 替換為您的Amazon RDS 主使用者名稱,並將 example-rds-primary-password 替換為您的 Amazon RDS 主密碼。另外,將 example-hive-username 替換為您的 Hive 使用者名稱,將 example-hive-password 替換為您的 example-hive-username 的密碼。
-
建立類似於以下內容的 JSON 組態檔案:
注意: 在下一個步驟中,使用以下 JSON 檔案啟動 Amazon EMR 叢集。此檔案允許 Hive 建立與 Amazon RDS 資料庫執行個體的 SSL 連線。
[ { "Classification": "hive-site", "Properties": { "javax.jdo.option.ConnectionURL": "jdbc:mysql://example-rds-db-instance-endpoint:3306/hive?createDatabaseIfNotExist=true&useSSL=true&serverSslCert=/home/hadoop/global-bundle.pem", "javax.jdo.option.ConnectionDriverName": "org.mariadb.jdbc.Driver", "javax.jdo.option.ConnectionUserName": "example-hive-username", "javax.jdo.option.ConnectionPassword": "example-hive-password" } } ]注意: 將 example-rds-db-instance-endpoint 替換為您的 Amazon RDS 資料庫執行個體端點,將 example-hive-username 替換為 Hive 要用來連線 Amazon RDS DB 執行個體的使用者名稱,並將 example-hive- password 替換為 example-hive-username 的密碼。
-
在與您的 Amazon RDS for MySQL 執行個體關聯的安全群組中建立傳入規則,如下所示:
在 Type (類型) 中,選擇 MYSQL/Aurora (3306)。
在 Protocol (通訊協定) 中,預設會選取 TCP (6)。
在 Port Range (連接埠範圍),預設會選取 3306。
在 Source (來源),輸入與主節點關聯的 Amazon EMR 受管安全群組的群組 ID。
注意: 此規則允許 Amazon EMR 叢集的主節點存取 Amazon RDS 執行個體。如需詳細資訊,請參閱 VPC 安全群組概觀。 -
建立以下引導操作指令碼檔案。然後,將其上傳到 Amazon S3 儲存貯體。引導操作會將 SSL 憑證下載到主節點上的 /home/hadoop/。
#!/bin/bash if grep isMaster /mnt/var/lib/info/instance.json | grep false; then echo "This is not primary node, do nothing, exiting" exit 0 fi echo "This is primary, continuing to execute script" cd /home/hadoop wget -S -T 10 -t 5 https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem -
若要啟動 Amazon EMR 叢集,請使用先前的 JSON 檔案和引導操作執行以下 create-cluster 命令:
範例:
aws emr create-cluster \ --name "EMR Hive Metastore SSL" \ --log-uri $LOG_URI \ --release-label "emr-7.3.0" \ --service-role $EMRServiceRole \ --ec2-attributes KeyName=$EC2_KEY_PAIR,InstanceProfile=$EMREC2Role,SubnetId=$EMR_SUBNET,EmrManagedSlaveSecurityGroup=$EMR_CORE_AND_TASK_VPC_SG,EmrManagedMasterSecurityGroup=$EMR_PRIMARY_VPC_SG \ --applications Name=Hadoop Name=Hive \ --bootstrap-actions Path=$BOOTSTRAP_ACTION_SCRIPT_PATH \ --configurations file:///<Full-Path-To>/hive-ext-meta-mysql-ssl.json \ --instance-groups '[{"InstanceCount":1,"InstanceGroupType":"CORE","Name":"Core","InstanceType":"m5.xlarge","EbsConfiguration":{"EbsBlockDeviceConfigs":[{"VolumeSpecification":{"VolumeType":"gp2","SizeInGB":32},"VolumesPerInstance":2}]}},{"InstanceCount":1,"InstanceGroupType":"MASTER","Name":"Primary","InstanceType":"m5.xlarge","EbsConfiguration":{"EbsBlockDeviceConfigs":[{"VolumeSpecification":{"VolumeType":"gp2","SizeInGB":32},"VolumesPerInstance":2}]}},{"InstanceCount":1,"InstanceGroupType":"TASK","Name":"Task - 1","InstanceType":"m5.xlarge","EbsConfiguration":{"EbsBlockDeviceConfigs":[{"VolumeSpecification":{"VolumeType":"gp2","SizeInGB":32},"VolumesPerInstance":2}]}}]' -
在主節點上開啟 Hive 工作階段。然後,建立一個用於測試的資料表。
範例:
hive> create table tb_test (col1 STRING, col2 BIGINT); OK Time taken: 2.371 secondshive> describe tb_test; OK col1 string col2 bigint Time taken: 0.254 seconds, Fetched: 2 row(s) -
若要連線到 Amazon RDS for MySQL 中繼存放區,請使用主節點上的 mysql 用戶端。然後,檢查中繼存放區中的資料表中繼資料。如果中繼資料與您在上一個步驟中建立的資料表相對應,則 SSL 連線有效。
範例:
mysql -h example-rds-db-instance-endpoint -P 3306 -u example-rds-primary-user -pEnter password: example-rds-primary-password mysql> use hive; Database changed mysql> select t1.OWNER, t1.TBL_NAME, t1.TBL_TYPE, s1.INPUT_FORMAT, s1.OUTPUT_FORMAT, s1.LOCATION from TBLS t1 inner join SDS s1 on s1.SD_ID = t1.SD_ID where t1.TBL_NAME = 'tb_test'\G *************************** 1. row *************************** OWNER: hadoop TBL_NAME: tb_test TBL_TYPE: MANAGED_TABLE INPUT_FORMAT: org.apache.hadoop.mapred.TextInputFormat OUTPUT_FORMAT: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat LOCATION: hdfs://ip-xxx-xx-xx-xxx.ec2.internal:8020/user/hive/warehouse/tb_test 1 row in set (0.23 sec) mysql> select t1.OWNER, t1.TBL_NAME, c1.COLUMN_NAME, c1.TYPE_NAME from TBLS t1 inner join SDS s1 on s1.SD_ID = t1.SD_ID inner join COLUMNS_V2 c1 on c1.CD_ID = s1.CD_ID where t1.TBL_NAME = 'tb_test'; +--------+----------+-------------+-----------+ | OWNER | TBL_NAME | COLUMN_NAME | TYPE_NAME | +--------+----------+-------------+-----------+ | hadoop | tb_test | col1 | string | | hadoop | tb_test | col2 | bigint | +--------+----------+-------------+-----------+ 2 rows in set (0.22 sec)注意: 將 example-rds-db-instance-endpoint 替換為您的 Amazon RDS 資料庫執行個體端點,將 example-rds-primary-user 替換為您的 Amazon RDS 主使用者名稱,並將 example-rds-primary-password 替換為您的 Amazon RDS 主密碼。
在正在執行的 Amazon EMR 叢集上設定 SSL 連線
注意: 在開始之前,請確保您有 Amazon RDS for MySQL 資料庫執行個體。
-
將 SSL 憑證下載到主節點上的 /home/hadoop/:
cd /home/hadoop && wget -S -T 10 -t 5 https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem -
在 /etc/hive/conf.dist 目錄中,在 hive-site.xml 檔案中新增或編輯以下行:
<property> <name>javax.jdo.option.ConnectionURL</name> <value>jdbc:mysql://example-rds-db-instance-endpoint:3306/hive?createDatabaseIfNotExist=true&useSSL=true&serverSslCert=/home/hadoop/global-bundle.pem</value> <description>example-rds-db-instance-endpoint</description> </property> <property> <name>javax.jdo.option.ConnectionUserName</name> <value>example-hive-username</value> <description>example-metastore-db-user</description> </property> <property> <name>javax.jdo.option.ConnectionPassword</name> <value>example-hive-password</value> <description>example-metastore-db-password</description> </property>注意: 將 example-rds-db-instance-endpoint 替換為您的 Amazon RDS 資料庫執行個體端點,將 example-hive-username 替換為 Hive 要用來連線 Amazon RDS DB 執行個體的使用者名稱,並將 example-hive- password 替換為 example-hive-username 的密碼。此外,符號 (&) 在 XML 中是一個特殊字元。若要在 hive-site.xml 中使用符號 (例如在 JDBC 字串中) 時,請使用 & 而不是 &。如果您未使用 &,那麼當您重新啟動 hive-hcatalog-server 時,您將收到錯誤。
-
測試 SSL 連線:
mysql -h example-rds-db-instance-endpoint -P 3306 -u example-hive-username -p --ssl-ca /home/hadoop/global-bundle.pem Enter password: example-hive-password注意: 將 example-rds-db-instance-endpoint 替換為您的 Amazon RDS 資料庫執行個體端點,將 example-hive-username 替換為 Hive 要用來連線 Amazon RDS DB 執行個體的使用者名稱,並將 example-hive- password 替換為 example-hive-username 的密碼。
-
在主節點上重新啟動 hive-hcatalog-server。
-
確認服務已成功重新啟動:
sudo systemctl status hive-hcatalog-server.service -
在主節點上開啟 Hive 工作階段。然後,建立一個用於測試的資料表。
範例:
hive> create table tb_test (col1 STRING, col2 BIGINT);OK Time taken: 2.371 seconds hive> describe tb_test; OK col1 string col2 bigint Time taken: 0.254 seconds, Fetched: 2 row(s) -
若要連線到 Amazon RDS for MySQL 中繼存放區,請使用主節點上的 mysql 用戶端。然後,檢查中繼存放區中的資料表中繼資料。如果中繼資料與您在上一個步驟中建立的資料表相對應,則 SSL 連線有效。
範例:
$ mysql -h example-rds-db-instance-endpoint -P 3306 -u example-rds-primary-user -p Enter password: example-rds-primary-password mysql> use hive; Database changed mysql> select t1.OWNER, t1.TBL_NAME, t1.TBL_TYPE, s1.INPUT_FORMAT, s1.OUTPUT_FORMAT, s1.LOCATION from TBLS t1 inner join SDS s1 on s1.SD_ID = t1.SD_ID where t1.TBL_NAME = 'tb_test'\G *************************** 1. row *************************** OWNER: hadoop TBL_NAME: tb_test TBL_TYPE: MANAGED_TABLE INPUT_FORMAT: org.apache.hadoop.mapred.TextInputFormat OUTPUT_FORMAT: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat LOCATION: hdfs://ip-xxx-xx-xx-xxx.ec2.internal:8020/user/hive/warehouse/tb_test 1 row in set (0.23 sec) mysql> select t1.OWNER, t1.TBL_NAME, c1.COLUMN_NAME, c1.TYPE_NAME from TBLS t1 inner join SDS s1 on s1.SD_ID = t1.SD_ID inner join COLUMNS_V2 c1 on c1.CD_ID = s1.CD_ID where t1.TBL_NAME = 'tb_test'; +--------+----------+-------------+-----------+ | OWNER | TBL_NAME | COLUMN_NAME | TYPE_NAME | +--------+----------+-------------+-----------+ | hadoop | tb_test | col1 | string | | hadoop | tb_test | col2 | bigint | +--------+----------+-------------+-----------+ 2 rows in set (0.22 sec)注意: 將 example-rds-db-instance-endpoint 替換為您的 Amazon RDS 資料庫執行個體端點,將 example-rds-primary-user 替換為您的 Amazon RDS 主使用者名稱,並將 example-rds-primary-password 替換為您的 Amazon RDS 主密碼。
針對 hive-hcatalog 伺服器重新啟動錯誤進行疑難排解
當您嘗試重新啟動 hive-hcatalog-server 時,您可能會收到以下錯誤或類似錯誤:
「2020-08-20T14:18:50,750 WARN [main] org.apache.hadoop.hive.metastore.HiveMetaStore - 發生錯誤後重新建立預設資料庫: 無法開啟到指定資料庫的測試連線。JDBC 網址 = jdbc:mysql://mysql-hive-meta.########.us-east-1.rds.amazonaws.com:3306/hive?createDatabaseIfNotExist=true&useSSL=true&serverSSlCert=/home/hadoop /global-bundle.pem,使用者名稱= masteruser。終止連線集區 (如果您希望在應用程式之後才啟動資料庫,請將 lazyInit 設為 true)。原始例外狀況:------java.sql.SQLException: 主機 '172.31.41.187' 因大量連線錯誤而遭到封鎖;請使用 'mysqladmin flush-hosts' 解除封鎖」
當 Amazon RDS for MySQL 資料庫執行個體出於安全考量封鎖 Amazon EMR 叢集的主節點時,就會發生此錯誤。若要解決此錯誤,請完成下列步驟:
- 連線到其他本機電腦或已安裝 mysqladmin 工具的 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體。
- 從資料庫執行個體排清主節點:
注意: 將 example-rds-primary-username 替換為您的 Amazon RDS 主使用者名稱,將 example-rds-primary-password 替換為您的 Amazon RDS 主密碼。mysqladmin -h example-rds-db-instance-endpoint -P 3306 -u example-rds-primary-username -p flush-hosts Enter password: example-rds-primary-password - 重新啟動 hive-hcatalog-server。
相關資訊
相關內容
- 已提問 1 年前

