mysql - Hive:无法连接到 Docker 内的 SQL

标签 mysql docker ubuntu hadoop hive

我正在尝试使用 hadoop 和 hive 创建一个 docker 容器。这是我的 Dockerfile

FROM ubuntu:latest

USER root
RUN apt-get update
#RUN apt-get -y install default-jre
RUN apt-get install -y python-pip python-dev build-essential
RUN apt-get install -y libmysqlclient-dev
RUN apt-get install -y python-mysqldb
RUN apt-get update && \
    apt-get upgrade -y && \
    apt-get install -y git

ENV HADOOP_HOME /opt/hadoop
ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64

# install packages
RUN \
  apt-get update && apt-get install -y \
  ssh \
  rsync \
  vim \
  openjdk-8-jdk

# create ssh keys
RUN \
  ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa && \
  cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys && \
  chmod 0600 ~/.ssh/authorized_keys

RUN DEBIAN_FRONTEND=noninteractive apt install -y mysql-client
RUN DEBIAN_FRONTEND=noninteractive apt install -y mysql-server
RUN chown -R mysql:mysql /var/lib/mysql /var/log/mysql

WORKDIR /opt

# download and extract hadoop, set JAVA_HOME in hadoop-env.sh, update path
RUN wget --no-check-certificate https://www-us.apache.org/dist/hadoop/common/hadoop-2.8.5/hadoop-2.8.5.tar.gz
RUN tar -xzf hadoop-2.8.5.tar.gz
RUN mv hadoop-2.8.5 $HADOOP_HOME
RUN echo "export JAVA_HOME=$JAVA_HOME" >> $HADOOP_HOME/etc/hadoop/hadoop-env.sh
RUN echo "PATH=$PATH:$HADOOP_HOME/bin" >> ~/.bashrc
RUN rm hadoop-2.8.5.tar.gz

ARG HIVE_VERSION
# Set HIVE_VERSION from arg if provided at build, env if provided at run, or default
# https://docs.docker.com/engine/reference/builder/#using-arg-variables
# https://docs.docker.com/engine/reference/builder/#environment-replacement
ENV HIVE_VERSION=${HIVE_VERSION:-2.3.2}

ENV HIVE_HOME /opt/hive
ENV PATH $HIVE_HOME/bin:$PATH
RUN echo "PATH=$PATH:$HIVE_HOME/bin" >> ~/.bashrc

#Install Hive and PostgreSQL JDBC
RUN apt-get update && apt-get install -y wget procps && \
    wget --no-check-certificate https://archive.apache.org/dist/hive/hive-$HIVE_VERSION/apache-hive-$HIVE_VERSION-bin.tar.gz && \
    tar -xzvf apache-hive-$HIVE_VERSION-bin.tar.gz && \
    mv apache-hive-$HIVE_VERSION-bin hive && \
    rm apache-hive-$HIVE_VERSION-bin.tar.gz && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

ADD mysql-connector-java-5.1.47.jar /opt/hive/lib
ADD hive-site.xml /opt/hive/conf

这里是 hive-site.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>

<property>
  <name>javax.jdo.option.ConnectionURL</name>
  <value>jdbc:mysql://localhost:3306/metastore?createDatabaseIfNotExist=true&amp;autoReconnect=true&amp;useSSL=false</value>
</property>


<property>
  <name>javax.jdo.option.ConnectionDriverName</name>
  <value>com.mysql.jdbc.Driver</value>
</property>

<property>
  <name>javax.jdo.option.ConnectionUserName</name>
  <value>root</value>
</property>

<property>
  <name>javax.jdo.option.ConnectionPassword</name>
  <value>password</value>
</property>

<property>
  <name>datanucleus.autoCreateSchema</name>
  <value>true</value>
</property>

<property>
  <name>datanucleus.fixedDatastore</name>
  <value>true</value>
</property>

<property>
 <name>datanucleus.autoCreateTables</name>
 <value>True</value>
 </property>

</configuration>

我能够构建和运行容器。当我执行到容器中时,我可以轻松地做到

root@c29fa91db6a2:/opt/hive/conf# mysql -uroot -ppassword
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 191
Server version: 5.7.25-0ubuntu0.18.04.2 (Ubuntu)

Copyright (c) 2000, 2019, 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>

但是当我尝试执行 hive --service metastore 时,我得到了

Caused by: java.sql.SQLException: Unable to open a test connection to the given database. JDBC url = jdbc:mysql://localhost:3306/metastore?createDatabaseIfNotExist=true&autoReconnect=true&useSSL=false, username = root. Terminating connection pool (set lazyInit to true if you expect to start your database after your app). Original Exception: ------
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.

在堆栈跟踪的下方,我看到了

Caused by: java.sql.SQLException: Access denied for user 'root'@'localhost'
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965) ~[mysql-connector-java-5.1.47.jar:5.1.47]

但是,这里是 root 用户拥有的授权

root@c29fa91db6a2:/opt/hive/conf# mysql -uroot -ppassword
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 205
Server version: 5.7.25-0ubuntu0.18.04.2 (Ubuntu)

Copyright (c) 2000, 2019, 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> show grants;
+---------------------------------------------------------------------+
| Grants for root@localhost                                           |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION        |
+---------------------------------------------------------------------+
2 rows in set (0.00 sec)

mysql>

我做错了什么?为了在 docker 容器中使用 hive,我还需要做什么?

另外,我看到mysql正在运行

root@7bfa781e1a2c:/opt/hive/conf# ps aux | grep mysql
mysql     1931  0.0  0.0   4624  1768 ?        S    20:19   0:00 /bin/sh /usr/bin/mysqld_safe
mysql     2282  0.2  8.7 1416908 179700 ?      Sl   20:19   0:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --log-error=/var/log/mysql/error.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/run/mysqld/mysqld.sock --port=3306 --log-syslog=1 --log-syslog-facility=daemon --log-syslog-tag=
root      2594  0.0  0.0  11460  1084 pts/1    S+   20:24   0:00 grep --color=auto mysql
root@7bfa781e1a2c:/opt/hive/conf#

最佳答案

有几个问题需要解决:

  1. 安装 mysql 时默认的 root 用户使用 auth_socket 作为认证插件。这意味着它没有密码。尝试使用 mysql -uroot 登录,它仍然可以登录。
mysql> select * from mysql.user\G
*************************** 1. row ***************************
                  Host: localhost
                  User: root
           Select_priv: Y
           Insert_priv: Y
           Update_priv: Y
           Delete_priv: Y
           Create_priv: Y
             Drop_priv: Y
           Reload_priv: Y
         Shutdown_priv: Y
          Process_priv: Y
             File_priv: Y
            Grant_priv: Y
       References_priv: Y
            Index_priv: Y
            Alter_priv: Y
          Show_db_priv: Y
            Super_priv: Y
 Create_tmp_table_priv: Y
      Lock_tables_priv: Y
          Execute_priv: Y
       Repl_slave_priv: Y
      Repl_client_priv: Y
      Create_view_priv: Y
        Show_view_priv: Y
   Create_routine_priv: Y
    Alter_routine_priv: Y
      Create_user_priv: Y
            Event_priv: Y
          Trigger_priv: Y
Create_tablespace_priv: Y
              ssl_type:
            ssl_cipher:
           x509_issuer:
          x509_subject:
         max_questions: 0
           max_updates: 0
       max_connections: 0
  max_user_connections: 0
                plugin: auth_socket
 authentication_string:
      password_expired: N
 password_last_changed: 2019-04-03 07:43:20
     password_lifetime: NULL
        account_locked: N
  1. 因此,在更改 mysql 中的任何内容之前,hive 服务会抛出以下错误:

Caused by: java.sql.SQLException: Access denied for user 'root'@'localhost'

  1. 我们需要设置 root@localhost 以使用 mysql_native_password 身份验证并设置密码,如@rajesh 所述:
mysql> alter user root@localhost identified with mysql_native_password by 'password';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> \q
Bye
  1. hive 仍然为我抛出错误,因为加载的 mysql 模式没有更新。因此必须执行 Hive 2.1.1 MetaException(message:Version information not found in metastore. ) 中提到的一些步骤
cd $HIVE_HOME/scripts/metastore/upgrade/mysql/

mysql -uroot -ppassword

mysql> use metastore;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> source hive-schema-2.3.0.mysql.sql;
  1. 现在 hive 开始正常了。
2019-04-03T08:58:01,442 INFO [main] org.apache.hadoop.hive.metastore.HiveMetaStore - Starting hive metastore on port 9083
2019-04-03T08:58:01,687 INFO [main] org.apache.hadoop.hive.metastore.HiveMetaStore - 0: Opening raw store with implementation class:org.apache.hadoop.hive.metastore.ObjectStore
2019-04-03T08:58:05,535 INFO [main] org.apache.hadoop.hive.metastore.HiveMetaStore - Added admin role in metastore
2019-04-03T08:58:05,546 INFO [main] org.apache.hadoop.hive.metastore.HiveMetaStore - Added public role in metastore
2019-04-03T08:58:05,594 INFO [main] org.apache.hadoop.hive.metastore.HiveMetaStore - No user is added in admin role, since config is empty
2019-04-03T08:58:05,804 INFO [main] org.apache.hadoop.hive.metastore.HiveMetaStore - Starting DB backed MetaStore Server with SetUGI enabled
2019-04-03T08:58:05,814 INFO [main] org.apache.hadoop.hive.metastore.HiveMetaStore - Started the new metaserver on port [9083]...
2019-04-03T08:58:05,815 INFO [main] org.apache.hadoop.hive.metastore.HiveMetaStore - Options.minWorkerThreads = 200
2019-04-03T08:58:05,816 INFO [main] org.apache.hadoop.hive.metastore.HiveMetaStore - Options.maxWorkerThreads = 1000
2019-04-03T08:58:05,816 INFO [main] org.apache.hadoop.hive.metastore.HiveMetaStore - TCP keepalive = true

关于mysql - Hive:无法连接到 Docker 内的 SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55388103/

相关文章:

python - 容器的 Docker 超时?

docker - 在 Docker 容器中启用 systemctl

php - 在 Ubuntu 18.04 crontab 中执行 PHP

MySQL 查询根据频率选择前 50 行的子集

mysql - 如何使用mysql从两个表中查找最新插入的数据

node.js - 尝试使用 PostgreSQL 对 Sails 1.0.2 进行 dockerize,无法以任何方式连接到数据库

php - Laravel 找不到 css 文件:GET http://localhost:8000/cssFile net::ERR_ABORTED

php - 变量的乘法和除法

mysql - 如何以逗号分隔获取重复的列值

ubuntu - 在 Ubuntu 上编译 Octave 脚本时出现解析错误