3. 安装配置MySQL数据库

一、VMware虚拟机使用建议

1、 建议每次配置使用快照进行记录;

点击虚拟机,点击快照,名字根据当前配置到哪里定,后续的操作导致系统出问题,直接使用快照恢复,极好!

在这里插入图片描述

2、启动虚拟机后就使用finalshell链接操作,VM感觉老笨重了

可以下载,可以上传,内部文件可视化操作在这里插入图片描述

二、配置MySQL(hive环境之一)

MySQL数据安装方式:1. RPM 2. YUM;下边我们都玩一下;

注意:先做个快照

1、 使用RPM安装

安装MySQL5.7版本,我们课程学习的版本

  1. 先检测HadoopMaster服务器上是否已经安装过MySQL及相关的RPM安装包,有则先删除
    好吧!我这里没有安装Hadoop环境
  • 在root用户下操作,不是的建议切换
[root@localhost ~]# su root
  • 查询rpm相关的安装包; 如下是啥都没;centos7默认支持MariaDB数据库
[root@localhost ~]# rpm -qa | grep -i mysql
[root@localhost ~]# 
  • 查看下载地址:https://downloads.mysql.com/archives/community/
  • 这里选择最新版本5.7.43;选择红帽子那个;centos是人家公司的
    在这里插入图片描述
  • 将下载的rpm安装包上传到系统目录下;上传后解压,这里先解压了
    命令:tar -xvf mysql-5.7.43-1.el7.x86_64.rpm-bundle.tar
    在这里插入图片描述
  • 上传到/home/hadoop目录下边,第一章已经创建了用户Hadoop这个目录在
[root@localhost hadoop]# ls
mysql-community-client-5.7.43-1.el7.x86_64.rpm  
mysql-community-devel-5.7.43-1.el7.x86_64.rpm  
mysql-community-server-5.7.43-1.el7.x86_64.rpm
  • 解压他们; 这里发出警告需要密钥
[root@localhost hadoop]# rpm -ivh mysql-community-server-5.7.43-1.el7.x86_64.rpm 
警告:mysql-community-server-5.7.43-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
错误:依赖检测失败:
        mysql-community-client(x86-64) >= 5.7.9 被 mysql-community-server-5.7.43-1.el7.x86_64 需要
        mysql-community-common(x86-64) = 5.7.43-1.el7 被 mysql-community-server-5.7.43-1.el7.x86_64 需要
  • 命令后边添加: --force --nodeps
[root@localhost hadoop]# rpm -ivh mysql-community-server-5.7.43-1.el7.x86_64.rpm --force --nodeps
警告:mysql-community-server-5.7.43-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:mysql-community-server-5.7.43-1.e################################# [100%]
  • 依次将这几个文件进行解压安装

22 rpm -ivh mysql-community-common-5.7.43-1.el7.x86_64.rpm
24 rpm -ivh mysql-community-libs-5.7.43-1.el7.x86_64.rpm --force --nodeps
25 rpm -ivh mysql-community-libs-compat-5.7.43-1.el7.x86_64.rpm --force --nodeps
26 rpm -ivh mysql-community-client-5.7.43-1.el7.x86_64.rpm --force --nodeps
27 rpm -ivh mysql-community-server-5.7.43-1.el7.x86_64.rpm --force --nodeps

  • 初始化数据库
    mysqld --initialize --user=mysql
  • 启动数据库
    systemctl start mysqld.service
  • 查看运行状态
    systemctl status mysqld.service
[root@localhost hadoop]# mysqld --initialize --user=mysql
[root@localhost hadoop]# systemctl start mysqld.service
[root@localhost hadoop]# systemctl status mysqld.service
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   # 如果是使用命令查看,这里的active(running)是绿色的,否则。。。
   Active: active (running) since 日 2023-11-19 16:20:31 CST; 4s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 61226 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 61205 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 61229 (mysqld)
    Tasks: 27
   CGroup: /system.slice/mysqld.service
           └─61229 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

1119 16:20:30 localhost.localdomain systemd[1]: Starting MySQL Server...
1119 16:20:31 localhost.localdomain systemd[1]: Started MySQL Server.

2、修改数据库密码

  • 查看数据库自动生成的密码;命令登录
[root@localhost hadoop]# grep "password" /var/log/mysqld.log
2023-11-19T08:20:22.707739Z 1 [Note] A temporary password is generated for root@localhost: p.Pwi7rQ7.B5
[root@localhost hadoop]# mysql -u root -p
Enter password: # 密码看不到,直接复制上边密码粘贴过来
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.43

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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> 

  • 使用SQL语句修改密码
mysql> set password = password("123456");
Query OK, 0 rows affected, 1 warning (0.00 sec)
  • 退出重新登录;如下密码已经重置
mysql> exit
Bye
[root@localhost hadoop]# mysql -u root -p
Enter password: 123456   # 这里是看不见的
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.43 MySQL Community Server (GPL)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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> 

3、允许远程登录

  • 先查看主机、用户、密码信息; 先使用命令:use mysql;
mysql> select host,user,authentication_string from user;
+-----------+---------------+-------------------------------------------+
| host      | user          | authentication_string                     |
+-----------+---------------+-------------------------------------------+
| localhost | root          | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| localhost | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost | mysql.sys     | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
+-----------+---------------+-------------------------------------------+
3 rows in set (0.00 sec)
  • 更新root用户密码为123456
mysql> update user set password=password('123456') where user='root';
ERROR 1054 (42S22): Unknown error 1054
mysql> update user set authentication_string=password('123456') where user='root';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> select host,user,authentication_string from user;
+-----------+---------------+-------------------------------------------+
| host      | user          | authentication_string                     |
+-----------+---------------+-------------------------------------------+
| localhost | root          | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| localhost | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost | mysql.sys     | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
+-----------+---------------+-------------------------------------------+
3 rows in set (0.00 sec)

  • 注意:上边是书上写的垃圾操作,因为密码在上边已经更新为123456;而且我user表中并没有password字段,只有authentication_string

  • 将host设置为 % 解决远程登录;设置完刷新退出

mysql> use mysql;
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> update user set host='%' where user='root' and host='localhost';
Query OK, 1 row affected (0.00 sec)

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> exit
Bye
  • 修改编码(可以以后修改)
mysql> status;   # 查看状态
--------------
mysql  Ver 14.14 Distrib 5.7.43, for Linux (x86_64) using  EditLine wrapper
。。。
  • 修改文件内容如下
[root@localhost hadoop]# vim /etc/my.cnf
[root@localhost hadoop]# 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
[client]
default-character-set=utf8
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
  • 验证
[root@localhost hadoop]# systemctl restart mysqld.service
[root@localhost hadoop]# mysql -uroot -p123456
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 2
Server version: 5.7.43 MySQL Community Server (GPL)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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> status;
--------------
mysql  Ver 14.14 Distrib 5.7.43, for Linux (x86_64) using  EditLine wrapper

Connection id:          2
Current database:       
Current user:           root@localhost
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server version:         5.7.43 MySQL Community Server (GPL)
Protocol version:       10
Connection:             Localhost via UNIX socket
Server characterset:    utf8
Db     characterset:    utf8
Client characterset:    utf8
Conn.  characterset:    utf8
UNIX socket:            /var/lib/mysql/mysql.sock
Uptime:                 27 sec

Threads: 1  Questions: 5  Slow queries: 0  Opens: 106  Flush tables: 1  Open tables: 99  Queries per second avg: 0.185
--------------

mysql> 

  • 使用navicat for mysql 测试一下;点击官网下载
  • 这是连不上的情况;为什么会连接不上??? 往下看
    在这里插入图片描述
  • 这是能够链接的情况;发生甚么事了?
    在这里插入图片描述
  • 先关闭防火墙,再试试, 使用下边的命令;不行就百度一下吧!

systemctl status firewalld.service #查看防火墙状态
systemctl stop firewalld.service #关闭防火墙:
systemctl disable firewalld.service #禁用防火墙开机自启

[root@localhost hadoop]# systemctl status firewalld.service #查看防火墙状态
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since 日 2023-11-19 12:37:08 CST; 5h 0min ago
     Docs: man:firewalld(1)
 Main PID: 793 (firewalld)
    Tasks: 2
   CGroup: /system.slice/firewalld.service
           └─793 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid

1119 12:37:07 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
1119 12:37:08 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
1119 12:37:08 localhost.localdomain firewalld[793]: WARNING: AllowZoneDrifting is enabled. This is considered an insecure configuration option. It will be removed in a future release. Please consider disabling it now.
[root@localhost hadoop]# 
[root@localhost hadoop]# systemctl stop firewalld.service #关闭防火墙: 
[root@localhost hadoop]# 
[root@localhost hadoop]# systemctl disable firewalld.service #禁用防火墙开机自启
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost hadoop]# 
  • 结束
    在这里插入图片描述

三、centos7配置yum源

1、yum配置

  • 进入yum的文件夹

cd /etc/yum.repos.d/

  • 删除所有源;

rm -rf /etc/yum.repos.d/*

  • 下载阿里源;

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

  • 清除及生成缓存

清除yum缓存
yum clean all
缓存阿里云镜像
yum makecache

[root@localhost yum.repos.d]# rm -rf /etc/yum.repos.d/*
[root@localhost yum.repos.d]#  wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
--2023-11-19 18:41:10--  http://mirrors.aliyun.com/repo/Centos-7.repo
正在解析主机 mirrors.aliyun.com (mirrors.aliyun.com)... 116.211.153.181, 119.96.33.219, 116.211.220.243, ...
正在连接 mirrors.aliyun.com (mirrors.aliyun.com)|116.211.153.181|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:2523 (2.5K) [application/octet-stream]
正在保存至: “/etc/yum.repos.d/CentOS-Base.repo”

100%[=================================================================================================================================================================================================================================================>] 2,523       --.-K/s 用时 0s      

2023-11-19 18:41:11 (505 MB/s) - 已保存 “/etc/yum.repos.d/CentOS-Base.repo” [2523/2523])

[root@localhost yum.repos.d]# yum clean all
已加载插件:fastestmirror, langpacks
正在清理软件源: base extras updates
Cleaning up list of fastest mirrors
[root@localhost yum.repos.d]# yum makecache
已加载插件:fastestmirror, langpacks
Determining fastest mirrors
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
base                                                                                                                                                                                                                                                                | 3.6 kB  00:00:00     
extras                                                                                                                                                                                                                                                              | 2.9 kB  00:00:00     
updates                                                                                                                                                                                                                                                             | 2.9 kB  00:00:00     
(1/10): base/7/x86_64/group_gz                                                                                                                                                                                                                                      | 153 kB  00:00:00     
(2/10): base/7/x86_64/primary_db                                                                                                                                                                                                                                    | 6.1 MB  00:00:01     
(3/10): base/7/x86_64/filelists_db                                                                                                                                                                                                                                  | 7.2 MB  00:00:01     
(4/10): base/7/x86_64/other_db                                                                                                                                                                                                                                      | 2.6 MB  00:00:00     
(5/10): extras/7/x86_64/filelists_db                                                                                                                                                                                                                                | 303 kB  00:00:00     
(6/10): extras/7/x86_64/primary_db                                                                                                                                                                                                                                  | 250 kB  00:00:00     
(7/10): extras/7/x86_64/other_db                                                                                                                                                                                                                                    | 150 kB  00:00:00     
(8/10): updates/7/x86_64/filelists_db                                                                                                                                                                                                                               |  13 MB  00:00:02     
(9/10): updates/7/x86_64/other_db                                                                                                                                                                                                                                   | 1.5 MB  00:00:00     
(10/10): updates/7/x86_64/primary_db                                                                                                                                                                                                                                |  24 MB  00:00:03     
元数据缓存已建立
  • 测试一下

yum -y install httpd

[root@localhost yum.repos.d]# yum -y install httpd
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com

2.、yum安装MySQL数据库(先恢复快照)

  • 配置mysql数据库yum源

wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm

  • 安装msyql源

yum localinstall mysql57-community-release-el7-8.noarch.rpm

  • 检查MySQL源是否安装成功

yum repolist enabled

若出现如下情况

[root@localhost ~]# yum repolist enabled
已加载插件:fastestmirror, langpacks
/var/run/yum.pid 已被锁定,PID 为 71263 的另一个程序正在运行。
Another app is currently holding the yum lock; waiting for it to exit...
  另一个应用程序是:PackageKit
    内存: 29 M RSS (445 MB VSZ)
    已启动: Sun Nov 19 19:46:30 2023 - 00:11之前
    状态  :睡眠中,进程ID:71263

干掉这个进程;再次查看

[root@localhost ~]# kill -9 71263
[root@localhost ~]# yum repolist enabled
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: ftp.sjtu.edu.cn
 * extras: ftp.sjtu.edu.cn
 * updates: ftp.sjtu.edu.cn
源标识                                                                                                                                        源名称                                                                                                                                 状态
base/7/x86_64                                                                                                                                 CentOS-7 - Base                                                                                                                        10,072
extras/7/x86_64                                                                                                                               CentOS-7 - Extras                                                                                                                         518
mysql-connectors-community/x86_64                                                                                                             MySQL Connectors Community                                                                                                                234
mysql-tools-community/x86_64                                                                                                                  MySQL Tools Community                                                                                                                     102
mysql57-community/x86_64                                                                                                                      MySQL 5.7 Community Server                                                                                                                696
updates/7/x86_64                                                                                                                              CentOS-7 - Updates                                                                                                                      5,434
repolist: 17,056

  • 修改mysql-community.repo源; 改变默认安装的mysql版本

vim /etc/yum.repos.d/mysql-community.repo

在这里插入图片描述

  • 使用yum安装

yum -y install mysql-community-server --nogpgcheck

注意:这里使用msyql官方源安装特别慢

  • 启动MySQL服务

systemctl start mysqld

  • 查看MySQL状态

systemctl status mysqld

[root@localhost ~]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since 日 2023-11-19 20:53:41 CST; 7s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 27584 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 27466 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 27587 (mysqld)
    Tasks: 27
   CGroup: /system.slice/mysqld.service
           └─27587 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

1119 20:53:37 localhost.localdomain systemd[1]: Starting MySQL Server...
1119 20:53:41 localhost.localdomain systemd[1]: Started MySQL Server.
  • 开机自启动

[root@localhost ~]# systemctl enable mysqld
[root@localhost ~]# systemctl daemon-reload

  • 查看安装自定义密码

grep ‘temporary password’ /var/log/mysqld.log

[root@localhost ~]# grep 'temporary password' /var/log/mysqld.log
2023-11-19T12:53:39.431859Z 1 [Note] A temporary password is generated for root@localhost: !p6BCHhw8Y)o
  • 命令登录,直接复制上边查询的密码:!p6BCHhw8Y)o 注意复制完整登录
[root@localhost ~]# mysql -uroot -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@localhost ~]# mysql -uroot -p!p6BCHhw8Y)o
-bash: !p6BCHhw8Y: event not found
[root@localhost ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.44

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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> 

  • 查看初始密码策略报错;使用命令

ALTER USER USER() IDENTIFIED BY ‘Admin2022!’;

mysql> SHOW VARIABLES LIKE 'validate_password%';
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> ALTER USER USER() IDENTIFIED BY 'Admin2022!';
Query OK, 0 rows affected (0.00 sec)
  • 处理掉错误再次查看
mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+--------+
| Variable_name                        | Value  |
+--------------------------------------+--------+
| validate_password_check_user_name    | OFF    |
| validate_password_dictionary_file    |        |
| validate_password_length             | 8      |
| validate_password_mixed_case_count   | 1      |
| validate_password_number_count       | 1      |
| validate_password_policy             | MEDIUM |
| validate_password_special_char_count | 1      |
+--------------------------------------+--------+
7 rows in set (0.00 sec)

  • 重置密码出错;这是由于密码策略问题,上一步:validate_password_policy MEDIUM
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
  • 修改密码强度;修改密码长度
mysql> set global validate_password_policy=LOW;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_length=4;
Query OK, 0 rows affected (0.00 sec)
  • 再次执行
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
  • 退出,并重启MySQL服务

service mysqld restart

[root@localhost ~]# service mysqld restart
Redirecting to /bin/systemctl restart mysqld.service
  • 登录验证

[root@localhost ~]# mysql -uroot -p123456

  • 配置默认字符集编码为utf8

vim /etc/my.cnf # 进入文件

[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'

注意:yum安装mysql默认的文件路径

配置文件:/etc/my.cnf
日志文件:/var/log/mysqld.log
服务启动脚本:/usr/lib/systemd/system/mysqld.service
socket文件:/var/run/mysqld/mysqld.pid

3、可视化工具登录

  • 设置允许远程登录MySQL;注意一定要使用刷新命令
mysql> use mysql;
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> update user set host='%' where user='root' and host='localhost';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

  • 关闭防火墙
systemctl status firewalld.service #查看防火墙状态
systemctl stop firewalld.service #关闭防火墙
systemctl disable firewalld.service #禁用防火墙开机自启
  • 成功如下
    在这里插入图片描述

四、总结

1、出错点很多,需要谨慎,以上我遇到的出错点已经写入文中
2、这里应当先安装Hadoop再安装MySQL,无所谓了,主要是过程
3、两种方法安装选择其中之一可以,注意留快照哦

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值