所有操作开始前,先确定hadoop版本已经装好。具体可以看我的另一篇博客
搭建hadoop3.x
先把安装包放到software文件夹,并解压到mysql-lib中
[root@master software]# ll
总用量 1422372
-rw-r--r-- 1 root root 312850286 7月 2 15:00 apache-hive-3.1.2-bin.tar.gz
-rw-r--r-- 1 root root 338075860 7月 2 09:39 hadoop-3.1.3.tar.gz
-rw-r--r-- 1 root root 195013152 7月 2 09:36 jdk-8u212-linux-x64.tar.gz
-rw-r--r-- 1 root root 609556480 7月 2 15:08 mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar
-rw-r--r-- 1 root root 985600 7月 2 14:44 mysql-connector-java-5.1.37.jar
[root@master software]# mkdir mysql-lib
[root@master software]# tar -xf mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar -C ./mysql-lib/
[root@master software]# cd mysql-lib/
[root@master mysql-lib]# ll
总用量 595272
-rw-r--r-- 1 7155 31415 45109364 9月 30 2019 mysql-community-client-5.7.28-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415 318768 9月 30 2019 mysql-community-common-5.7.28-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415 7037096 9月 30 2019 mysql-community-devel-5.7.28-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415 49329100 9月 30 2019 mysql-community-embedded-5.7.28-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415 23354908 9月 30 2019 mysql-community-embedded-compat-5.7.28-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415 136837816 9月 30 2019 mysql-community-embedded-devel-5.7.28-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415 4374364 9月 30 2019 mysql-community-libs-5.7.28-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415 1353312 9月 30 2019 mysql-community-libs-compat-5.7.28-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415 208694824 9月 30 2019 mysql-community-server-5.7.28-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415 133129992 9月 30 2019 mysql-community-test-5.7.28-1.el7.x86_64.rpm
先处理一下环境,因为centos自带mariadb,会产生冲突,所以要先卸载
[root@master mysql-lib]# rpm -qa|grep mariadb
mariadb-libs-5.5.56-2.el7.x86_64
[root@master mysql-lib]# sudo rpm -e --nodeps mariadb-libs
# 查看一下是不是卸载干净了
[root@master mysql-lib]# rpm -qa|grep mariadb
[root@master mysql-lib]# rpm -qa|grep mysql
没什么问题了就可以安装了,一定要按顺序安装
[root@master mysql-lib]# ll
总用量 595284
-rw-r--r-- 1 7155 31415 45109364 9月 30 2019 mysql-community-client-5.7.28-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415 318768 9月 30 2019 mysql-community-common-5.7.28-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415 7037096 9月 30 2019 mysql-community-devel-5.7.28-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415 49329100 9月 30 2019 mysql-community-embedded-5.7.28-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415 23354908 9月 30 2019 mysql-community-embedded-compat-5.7.28-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415 136837816 9月 30 2019 mysql-community-embedded-devel-5.7.28-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415 4374364 9月 30 2019 mysql-community-libs-5.7.28-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415 1353312 9月 30 2019 mysql-community-libs-compat-5.7.28-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415 208694824 9月 30 2019 mysql-community-server-5.7.28-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415 133129992 9月 30 2019 mysql-community-test-5.7.28-1.el7.x86_64.rpm
[root@master mysql-lib]# sudo rpm -ivh mysql-community-common-5.7.28-1.el7.x86_64.rpm
警告:mysql-community-common-5.7.28-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-common-5.7.28-1.e################################# [100%]
[root@master mysql-lib]# sudo rpm -ivh mysql-community-libs-5.7.28-1.el7.x86_64.rpm
警告:mysql-community-libs-5.7.28-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-libs-5.7.28-1.el7################################# [100%]
[root@master mysql-lib]# sudo rpm -ivh mysql-community-libs-compat-5.7.28-1.el7.x86_64.rpm
警告:mysql-community-libs-compat-5.7.28-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-libs-compat-5.7.2################################# [100%]
[root@master mysql-lib]# sudo rpm -ivh mysql-community-client-5.7.28-1.el7.x86_64.rpm
警告:mysql-community-client-5.7.28-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-client-5.7.28-1.e################################# [100%]
安装到第5个时,如果centos是最小化安装的,那会报这个问题
[root@master mysql-lib]# sudo rpm -ivh mysql-community-server-5.7.28-1.el7.x86_64.rpm
警告:mysql-community-server-5.7.28-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
错误:依赖检测失败:
libnuma.so.1()(64bit) 被 mysql-community-server-5.7.28-1.el7.x86_64 需要
libnuma.so.1(libnuma_1.1)(64bit) 被 mysql-community-server-5.7.28-1.el7.x86_64 需要
libnuma.so.1(libnuma_1.2)(64bit) 被 mysql-community-server-5.7.28-1.el7.x86_64 需要
[root@master mysql-lib]# yum install -y libaio
[root@master mysql-lib]# yum install -y numactl
然后再尝试安装就没问题了
[root@master mysql-lib]# sudo rpm -ivh mysql-community-server-5.7.28-1.el7.x86_64.rpm
警告:mysql-community-server-5.7.28-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-server-5.7.28-1.e################################# [100%]
[root@master mysql-lib]# cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
看到了mysql目录是/var/lib/mysql
#进入这个地址 看看是否有历史残余文件
[root@master mysql-lib]# cd /var/lib/mysql
# 没东西就没问题,有就都删了
[root@master mysql]# ll
总用量 0
[root@master mysql]# sudo mysqld --initialize --user=mysql
看下默认密码
[root@master mysql]# cat /var/log/mysqld.log
2020-07-02T07:29:10.095244Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-07-02T07:29:11.331738Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-07-02T07:29:11.498492Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-07-02T07:29:11.590436Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: b94654a6-bc35-11ea-a160-525400f3e388.
2020-07-02T07:29:11.600554Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-07-02T07:29:13.093996Z 0 [Warning] CA certificate ca.pem is self signed.
2020-07-02T07:29:13.522160Z 1 [Note] A temporary password is generated for root@localhost: p;oK<M)<_8p/
密码是p;oK
启动mysql服务
[root@master mysql]# systemctl start mysqld
[root@master mysql]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since 四 2020-07-02 15:31:00 CST; 10s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 5617 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 5497 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 5624 (mysqld)
Tasks: 27
Memory: 205.4M
CGroup: /system.slice/mysqld.service
└─5624 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.p...
7月 02 15:30:55 master systemd[1]: Starting MySQL Server...
7月 02 15:31:00 master systemd[1]: Started MySQL Server.
进入mysql
$ mysql -u root -p
提示输入密码,输入刚刚复制的密码
成功进入mysql后,设置我们的mysql密码,这个自己记住哈~
#设置密码为你的密码
mysql> set password = password("你的密码");
接下来设置root用户的权限,允许任意ip连接,为后面的hive连接做准备
mysql> use mysql;
mysql> select host,user from user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+---------------+
3 rows in set (0.00 sec)
mysql> update mysql.user set host='%' where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
# 权限高的在最上面(新版本优化的)
mysql> select host,user from user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| % | root |
| localhost | mysql.session |
| localhost | mysql.sys |
+-----------+---------------+
3 rows in set (0.00 sec)
--这步一定要做,是刷新配置的,不然密码无法生效
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
然后再尝试用更改后的密码登录一下,成功了就是配置好了。
到/home/centos/software目录下,解压apache-hive-3.1.2-bin.tar.gz到/home/centos/module/目录下面
[root@master software]# tar -zxvf apache-hive-3.1.2-bin.tar.gz -C /home/centos/module/
进入module文件夹,把文件名改一下
[root@master module]# ll
总用量 12
drwxr-xr-x 9 root root 4096 7月 2 15:40 apache-hive-3.1.2-bin
drwxr-xr-x 11 centos centos 4096 7月 2 11:51 hadoop-3.1.3
drwxr-xr-x 7 10 143 4096 4月 2 2019 jdk1.8.0_212
[root@master module]# mv apache-hive-3.1.2-bin/ hive
[root@master module]# ll
总用量 12
drwxr-xr-x 11 centos centos 4096 7月 2 11:51 hadoop-3.1.3
drwxr-xr-x 9 root root 4096 7月 2 15:40 hive
drwxr-xr-x 7 10 143 4096 4月 2 2019 jdk1.8.0_212
配置环境变量
[root@master module]# vim /etc/profile.d/my_env.sh
#HIVE_HOME
export HIVE_HOME=/home/centos/module/hive
export PATH=$PATH:$HIVE_HOME/bin
加载环境变量,并验证是否成功
[root@master module]# source /etc/profile
[root@master module]# echo $HIVE_HOME
/home/centos/module/hive
解决日志Jar包冲突
hadoop和hive之间的log4j版本冲突,要把hive的lib文件夹中的jar包名字改一下,无法启用即可。就直接在文件名后加.bak即可。
[atguigu@hadoop102 lib]$ mv log4j-slf4j-impl-2.10.0.jar log4j-slf4j-impl-2.10.0.jar.bak
[atguigu@hadoop102 lib]$ ll | grep log4j
-rw-rw-r--. 1 atguigu atguigu 63835 4月 15 00:34 log4j-1.2-api-2.10.0.jar
-rw-rw-r--. 1 atguigu atguigu 255485 4月 15 00:24 log4j-api-2.10.0.jar
-rw-rw-r--. 1 atguigu atguigu 1597622 4月 15 00:24 log4j-core-2.10.0.jar
-rw-rw-r--. 1 atguigu atguigu 24173 4月 15 00:24 log4j-slf4j-impl-2.10.0.jar.bak
-rw-rw-r--. 1 atguigu atguigu 32060 4月 15 00:34 log4j-web-2.10.0.jar
将MySQL的JDBC驱动拷贝到Hive的lib目录下
[root@master lib]# cp /home/centos/software/mysql-connector-java-5.1.37.jar ./
看看是否拷贝成功
[root@master lib]# ll | grep mysql-con
-rw-r--r-- 1 root root 985600 7月 2 15:47 mysql-connector-java-5.1.37.jar
在$HIVE_HOME/conf目录下新建hive-site.xml文件,
[root@master conf]# vim hive-site.xml
有三个值需要你改一下
<configuration>
<property>
<name>javax.jdo.option.ConnectionURLname>
<value>jdbc:mysql://腾讯云内网ip:3306/metastore?useSSL=falsevalue>
property>
<property>
<name>javax.jdo.option.ConnectionDriverNamename>
<value>com.mysql.jdbc.Drivervalue>
property>
<property>
<name>javax.jdo.option.ConnectionUserNamename>
<value>rootvalue>
property>
<property>
<name>javax.jdo.option.ConnectionPasswordname>
<value>你的mysql密码value>
property>
<property>
<name>hive.metastore.warehouse.dirname>
<value>/user/hive/warehousevalue>
property>
<property>
<name>hive.metastore.schema.verificationname>
<value>falsevalue>
property>
<property>
<name>hive.metastore.urisname>
<value>thrift://腾讯云内网ip:9083value>
property>
<property>
<name>hive.server2.thrift.portname>
<value>10000value>
property>
<property>
<name>hive.server2.thrift.bind.hostname>
<value>你本机的hostnamevalue>
property>
<property>
<name>hive.metastore.event.db.notification.api.authname>
<value>falsevalue>
property>
<property>
<name>hive.server2.active.passive.ha.enablename>
<value>truevalue>
property>
configuration>
把改好的内容粘进去后,保存退出
登录mysql,创建hive元数据库
mysql> create database metastore;
Query OK, 1 row affected (0.00 sec)
退出,初始化hive元数据库
# 让hive在metastore中创建初始化的表
[atguigu@hadoop102 software]$ schematool -initSchema -dbType mysql -verbos
再次进入mysql,检查一下,可以看到已经创建好了(有74张表)
mysql> use metastore;
mysql> show tables;
创建一个启动脚本,不然每次写那么长让它后台启动实在太烦了。
[root@master bin]# vim hs.sh
添加如下内容
#!/bin/bash
HIVE_LOG_DIR=$HIVE_HOME/logs
#判断有没有这个目录,没有就创建
if [ ! -d $HIVE_LOG_DIR ]
then
mkdir -p $HIVE_LOG_DIR
fi
#检查进程是否运行正常,参数1为进程名,参数2为进程端口
#status
function check_process()
{
#/dev/null 黑洞 不要的流都放到这里销毁
pid=$(ps -ef 2>/dev/null | grep -v grep | grep -i $1 | awk '{print $2}')
ppid=$(netstat -nltp 2>/dev/null | grep $2 | awk '{print $7}' | cut -d '/' -f 1)
echo $pid
[[ "$pid" =~ "$ppid" ]] && [ "$ppid" ] && return 0 || return 1
}
#start
function hive_start()
{
metapid=$(check_process HiveMetastore 9083)
#看懂这行即可 ,这行是让服务后台启动的
cmd="nohup hive --service metastore >$HIVE_LOG_DIR/metastore.log 2>&1 &"
cmd=$cmd" sleep 4; hdfs dfsadmin -safemode wait >/dev/null 2>&1"
[ -z "$metapid" ] && eval $cmd || echo "Metastroe服务已启动"
server2pid=$(check_process HiveServer2 10000)
#看懂这行即可 ,这行是让服务后台启动的
cmd="nohup hive --service hiveserver2 >$HIVE_LOG_DIR/hiveServer2.log 2>&1 &"
[ -z "$server2pid" ] && eval $cmd || echo "HiveServer2服务已启动"
}
#stop
function hive_stop()
{
metapid=$(check_process HiveMetastore 9083)
[ "$metapid" ] && kill $metapid || echo "Metastore服务未启动"
server2pid=$(check_process HiveServer2 10000)
[ "$server2pid" ] && kill $server2pid || echo "HiveServer2服务未启动"
}
case $1 in
"start")
hive_start
;;
"stop")
hive_stop
;;
"restart")
hive_stop
sleep 2
hive_start
;;
"status")
check_process HiveMetastore 9083 >/dev/null && echo "Metastore服务运行正常" || echo "Metastore服务运行异常"
check_process HiveServer2 10000 >/dev/null && echo "HiveServer2服务运行正常" || echo "HiveServer2服务运行异常"
;;
*)
echo Invalid Args!
echo 'Usage: '$(basename $0)' start|stop|restart|status'
;;
esac
保存退出,给脚本加权限
$ chmod +x hs.sh
现在来启动吧(启动前要确认hadoop是已启动,且运行正常的哦)
[root@master bin]# hs.sh start
[root@master bin]# jps
32177 SecondaryNameNode
514 NodeManager
20788 Jps
31396 NameNode
32652 ResourceManager
20253 RunJar
31757 DataNode
991 JobHistoryServer
可以看到启动成功了,我们用脚本来看下状态
[root@master bin]# hs.sh status
Metastore服务运行异常
HiveServer2服务运行异常
你会惊奇的发现,居然有一个运行异常,这是怎么回事?其实是因为它启动比较慢,不信你过了两分钟再看
[root@master bin]# hs.sh status
Metastore服务运行正常
HiveServer2服务运行正常
如果你不是用root来搭建的hadoop,那这里根据自己的设置更改
#必须用root访问,因为jdbc的管理员是root,不然没权限 -n后面是要登录的用户
[atguigu@hadoop102 bin]$ beeline -u jdbc:hive2://master:10000 -n root
Connecting to jdbc:hive2://master:10000
Connected to: Apache Hive (version 3.1.2)
Driver: Hive JDBC (version 3.1.2)
Transaction isolation: TRANSACTION_REPEATABLE_READ
Beeline version 3.1.2 by Apache Hive
0: jdbc:hive2://master:10000>
这样就是成功进入了,退出的话
0: jdbc:hive2://master:10000> !quit
Closing: 0: jdbc:hive2://master:10000
[atguigu@hadoop102 bin]$ hive
#一堆信息
hive>
这样就是成功进入了,退出的话是quit;
接下来就可以玩hive啦!
他俩的历史记录都在家目录中
$ cd ~
$ ls -a
可以到有一个.hivehistory
文件和.beeline
文件夹,历史记录会在你quit这个JDBC后写入。
Hive的log默认存放在当前用户名下下,我们把hive本身的运行日志也放到这个logs里,方便以后查看。
(1)修改$HIVE_HOME/conf/hive-log4j.properties.template
文件名称为hive-log4j.properties
$ mv hive-log4j2.properties.template hive-log4j.properties
$ vim hive-log4j.properties
(2)在hive-log4j.properties文件中修改log存放位置
property.hive.log.dir=/home/centos/module/hive/logs
新版本的hive启动的时候,默认申请的jvm堆内存大小为256M,jvm堆内存申请的太小,导致后期开启本地模式,执行复杂的sql时经常会报错:java.lang.OutOfMemoryError: Java heap space
,因此最好提前调整一下HADOOP_HEAPSIZE
这个参数。
[atguigu@hadoop102 conf]$ cp hive-env.sh.template hive-env.sh
[atguigu@hadoop102 conf]$ vim hive-env.sh
将hive-env.sh其中的参数 export HADOOP_HEAPSIZE=1024
的注释放开
做完以上所有配置,重启hive。
推荐dbeaver,连接前记得来到安全组,开放10000端口
具体配置步骤如下
然后
测试连接,等个1分钟左右基本上就可以成功了
后面就在Windows上好好玩hive吧~