
# Hive对接外置自建关系型数据库
#### 应用场景
在已有Hive数据的集群上外置元数据库后，之前的元数据表不会自动同步。因此在安装Hive之初就要确认好元数据是外置数据库还是内置到DBService，如果是外置自建数据库，则需在安装Hive时或者暂无Hive数据时将元数据外置，安装后不允许修改，否则将会造成原有元数据丢失。
Hive支持开源MySQL和Postgres元数据库，本章节以对接开源MySQL和Postgres数据库进行说明。
#### 约束与限制
- 当外置元数据到MySQL后，Hive仅表名、字段名、表描述支持中文，其余暂不支持。
- 适用于MRS 3.x及以后版本。
 
#### 步骤一：安装数据库并上传驱动包
1. 安装开源MySQL或Postgres数据库。
   ![](https://support.huaweicloud.com/bestpractice-mrs/public_sys-resources/note_3.0-zh-cn.png)
   数据库安装节点需与集群处于同一网段，能互相访问。
   
2. 上传驱动包。
   - Postgres： 使用开源驱动包替换集群已有的驱动包。将Postgres驱动包"postgresql-42.2.5.jar"上传至所有MetaStore实例节点"${BIGDATA_HOME}/third_lib/Hive"目录下（开源驱动包下载地址：<https://repo1.maven.org/maven2/org/postgresql/postgresql/42.2.5/>）。
     在上传驱动包的所有MetaStore实例节点上执行以下命令修改驱动包权限：
     **cd ${BIGDATA_HOME}/third_lib/Hive**
     **chown omm:wheel** **postgresql-42.2.5.jar**
     **chmod 600** **postgresql-42.2.5.jar**
     
   
   - MySQL： 进入MySQL官网（<https://www.mysql.com/>），选择"DOWNLOADS \> MySQL Community(GPL) Downloads \> Connector/J"下载对应版本的驱动包。
     - MRS 8.2.0之前版本，将MySQL对应版本的驱动包上传至所有Metastore实例节点"/opt/Bigdata/FusionInsight_HD_\*/install/FusionInsight-Hive-\*/hive-\*/lib/"目录下。
     
     - MRS 8.2.0及之后版本，将MySQL对应版本的驱动包上传至所有Metastore实例节点"${BIGDATA_HOME}/third_lib/Hive"目录下。
     
     
     在上传驱动包的所有MetaStore实例节点上执行以下命令修改驱动包权限：
     **cd /opt/Bigdata/FusionInsight_HD_\*/install/FusionInsight-Hive-\*/hive-\*/lib/**
     **chown omm:wheel** **mysql-connector-java-\*.jar**
     **chmod 600** **mysql-connector-java-\*.jar**
     
    
 
#### 步骤二：在自建数据库中建表及赋权
1. 在自建数据库中创建用户、元数据库，并为用户赋予该库的所有权限。例如：
   - 以数据库管理员用户在Postgres中执行以下命令创建数据库"test"和用户"testuser"，并授予"test"数据库的所有权限给"testuser"用户。 **create user testuser with password '** *password* **';**
     **create database test owner testuser;**
     **grant all privileges on database test to testuser;**
     
   
   - 以数据库管理员用户在MySQL中执行以下命令创建数据库"test"和用户"testuser"，并授予"test"数据库的所有权限给"testuser"用户。 **create database test;**
     **create user 'testuser'@'%' identified by '** *password* **';**
     **grant all privileges on test.\* to 'testuser';**
     **flush privileges;**
     
    
2. 导入元数据建表SQL。
   - Postgres的SQL文件路径：${BIGDATA_HOME}/FusionInsight_HD_\*/install/FusionInsight-Hive-\*/hive-\*/scripts/metastore/upgrade/postgres/hive-schema-3.1.0.postgres.sql Postgres导入sql文件的命令：
     **./bin/psql -U** *username* **-d** *databasename* **-f hive-schema-3.1.0.postgres.sql**
     其中：
     ./bin/psql：在Postgres安装目录下。
     *username*：登录Postgres的用户名。
     *databasename*：数据库库名。
     
   
   - MySQL的SQL文件路径：${BIGDATA_HOME}/FusionInsight_HD_\*/install/FusionInsight-Hive-\*/hive-\*/scripts/metastore/upgrade/mysql/hive-schema-3.1.0.mysql.sql MySQL导入sql文件的命令：
     **./bin/mysql -u** *username* **-p** **-D** *databasename* **\<hive-schema-3.1.0.mysql.sql**
     其中：
     ./bin/mysql：在MySQL安装目录下*。*
     *username*：登录MySQL的用户名。
     *databasename*：数据库库名。
     
    
 
#### 步骤三：在MRS集群配置参数对接数据库
1. 登录FusionInsight Manager，选择"集群 \> 服务 \> Hive \> 配置 \> 全部配置 \> Hive（服务） \> MetaDB"，修改以下参数并保存，使Hive的配置对接到开源数据库。
   表1参数说明 
   | 参数名                                   | 默认值                                                                                              | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
   |:---|:---|:---|
   | javax.jdo.option.ConnectionDriverName | org.postgresql.Driver                                                                            | Metastore上连接元数据的驱动类。 - 外置MySQL，则值为： com.mysql.jdbc.Driver   - 外置Postgres，则值为： org.postgresql.Driver                                                                                                                                                                                                                          |
   | javax.jdo.option.ConnectionURL        | jdbc:postgresql://%{DBSERVICE_FLOAT_IP}%{DBServer}:%{DBSERVICE_CPORT}/hivemeta?socketTimeout=600 | Metastore元数据JDBC链接的URL。 - 外置MySQL，则值为： jdbc:mysql://*MySQL的IP* :*MySQL的端口* /*test*?characterEncoding=utf-8\&socketTimeout=300000   - 外置Postgres，则值为： jdbc:postgresql://*Postgres的IP* :*Postgres的端口号* /*test*?socketTimeout=600    说明： "test"为[1]中在MySQL或PgSQL中创建的数据库名称。 |
   | javax.jdo.option.ConnectionUserName   | hive${SERVICE_INDEX}${SERVICE_INDEX}                                                             | Metastore上连接外置元数据的数据库用户名。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
      
   
2. 在MetaStore中修改Postgres数据库密码，选择"集群 \> 服务 \> Hive \> 配置 \> 全部配置 \> MetaStore（角色） \> MetaDB"，修改以下参数并保存。
   表2参数说明 
   | 参数名                                        | 默认值          | 描述                                 |
   |:---|:---|:---|
   | javax.jdo.option.extend.ConnectionPassword | \*\*\*\*\*\* | Metastore上连接外置元数据的数据库用户密码。密码后台会加密。 |
      
   
3. MRS 3.2.0-LTS及之后版本，如果是多套MRS集群对接同一个关系型数据库的场景，还需在左侧导航栏选择"MetaStore（角色） \> 自定义"，在自定义参数"hive.metastore.customized.configs"中新增"metastore.whether.query.all.tokens"配置项，值为"false"，单击"保存"保存配置。
4. 登录所有MetaStore服务的后台节点，检查本地目录"/opt/Bigdata/tmp"是否存在。
   - 存在，直接执行[5]。
   
   - 不存在，则先执行以下命令，创建目录。 **mkdir -p /opt/** **Bigdata/tmp**
     **chmod 755 /opt/** **Bigdata/tmp**
     
    
5. 选择"概览 \> 更多 \> 重启服务"，输入密码开始重启Hive服务。
6. Hive重启完成后，登录MySQL或Postgres数据库，可以查看到[1]创建的元数据库中有元数据表生成：
   ![](https://support.huaweicloud.com/bestpractice-mrs/zh-cn_image_0000002125012737.png)
   
 
#### 步骤四：验证Hive元数据库是否外置成功
1. 以客户端安装用户登录安装Hive客户端的节点： **cd** *客户端安装目录*
   **source bigdata_env**
   **kinit** *组件业务用户*（未开启Kerberos认证的集群请跳过该操作）
   
2. 执行以下命令登录Hive客户端命令行： **beeline**
   
3. 执行以下命令创建表**test** ：
   **create table** *test* **(id int,str1 string,str2 string);**
   
4. 在MySQL或Postgres的**test** 数据库中执行以下命令查看是否有表**test** 相关信息：
   **select \* from TBLS;**
   能查看到表**test**相关信息则说明外置Hive数据库成功，例如：
   - 在MySQL中查看的结果为： ![](https://support.huaweicloud.com/bestpractice-mrs/zh-cn_image_0000002089613072.png "点击放大")
     
   
   - 在Postgres中查看的结果为： ![](https://support.huaweicloud.com/bestpractice-mrs/zh-cn_image_0000002089453200.png "点击放大")
     
    
 
