当前位置:   article > 正文

Centos7上使用yum安装mysql8.x_centos7 yum mysql8

centos7 yum mysql8


前言

最近在家里想搭建一个开发环境玩玩,没想到在虚拟机Centos7上使用yum安装mysql8.x竟然折腾了好久。
这次官网的文档都没有拯救我,踩了好多坑,所以把安装过程整理一下,避免以后重复踩坑。


一、官方文档

官网YUM安装:https://dev.mysql.com/doc/refman/8.0/en/linux-installation-yum-repo.html

二、安装步骤

1.安装MySQL Yum Repository

下载地址:https://dev.mysql.com/downloads/repo/yum/
在这里插入图片描述
注意:
mysql80-community-release-el7-6.noarch.rpm中的el7表示是操作系统为Centos7。

将下载的rpm文件上传到虚拟机上的指定目录。然后执行yum源仓库的安装命令:

sudo yum install  mysql80-community-release-el7-6.noarch.rpm
  • 1

官网默认的yum源由于是海外的,在国内网络下载安装时限速严重,这里推荐改为OpenTuna源,速度会快很多。

cd /etc/yum.repos.d/
mv mysql-community.repo mysql-community.repo.bak
vi mysql-community.repo
  • 1
  • 2
  • 3
# Enable to use MySQL 5.5
[mysql55-community]
name=MySQL 5.5 Community Server
baseurl=https://opentuna.cn/mysql/yum/mysql-5.5-community-el7-$basearch/
enabled=0
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql
 
# Enable to use MySQL 5.6
[mysql56-community]
name=MySQL 5.6 Community Server
baseurl=https://opentuna.cn/mysql/yum/mysql-5.6-community-el7-$basearch/
enabled=0
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql
 
# Enable to use MySQL 5.7
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=https://opentuna.cn/mysql/yum/mysql-5.7-community-el7-$basearch/
enabled=0
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql
 
[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=https://opentuna.cn/mysql/yum/mysql-8.0-community-el7-$basearch/
enabled=1
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql
 
[mysql-connectors-community]
name=MySQL Connectors Community
baseurl=https://opentuna.cn/mysql/yum/mysql-connectors-community-el7-$basearch/
enabled=1
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql
 
[mysql-tools-community]
name=MySQL Tools Community
baseurl=https://opentuna.cn/mysql/yum/mysql-tools-community-el7-$basearch/
enabled=1
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql
 
[mysql-tools-preview]
name=MySQL Tools Preview
baseurl=https://opentuna.cn/mysql/yum/mysql-tools-preview/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql
 
[mysql-cluster-7.5-community]
name=MySQL Cluster 7.5 Community
baseurl=https://opentuna.cn/mysql/yum/mysql-cluster-7.5-community-el7-$basearch/
enabled=0
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql
 
[mysql-cluster-7.6-community]
name=MySQL Cluster 7.6 Community
baseurl=https://opentuna.cn/mysql/yum/mysql-cluster-7.6-community-el7-$basearch/
enabled=0
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql
 
[mysql-cluster-8.0-community]
name=MySQL Cluster 8.0 Community
baseurl=https://opentuna.cn/mysql/yum/mysql-cluster-8.0-community-el7-$basearch/
enabled=0
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72

清空并更新缓存:

yum clean all && yum makecache
  • 1

2.选择要安装的版本

查看所有mysql的yum源仓库:

 yum repolist all | grep mysql
  • 1

在这里插入图片描述

禁用和启用可以安装的mysql版本:

$> sudo yum-config-manager --disable mysql57-community
$> sudo yum-config-manager --enable mysql80-community
  • 1
  • 2

查看可以安装的mysql版本:

yum repolist enabled | grep mysql
  • 1

3.检查rpm的签名

如果直接进行安装,出现提示获取GPG密钥失败。

sudo yum install mysql-community-server
  • 1

在这里插入图片描述
检查签名:

[root@localhost ~]# cd /root
[root@localhost ~]# ls
anaconda-ks.cfg    mysql80-community-release-el7-6.noarch.rpm
[root@localhost ~]# rpm --checksig mysql80-community-release-el7-6.noarch.rpm
mysql80-community-release-el7-6.noarch.rpm: RSA sha1 (MD5) PGP md5 不正确
  • 1
  • 2
  • 3
  • 4
  • 5

导入密钥:

[root@localhost ~]# rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
[root@localhost ~]# rpm --checksig mysql80-community-release-el7-6.noarch.rpm
mysql80-community-release-el7-6.noarch.rpm: rsa sha1 (md5) pgp md5 确定
  • 1
  • 2
  • 3

4.安装Mysql

sudo yum -y  install mysql-community-server
  • 1

可以发现下载速度快了很多,并且安装过程中就没有出现签名、秘钥的错误提示了。
在这里插入图片描述

5.启动mysql服务

//启动服务
$> systemctl start mysqld
//设置开机自启动
$>systemctl enable nginx
//查看服务状态
$> systemctl status mysqld
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

在这里插入图片描述

6.修改初始密码

查看初始密码:sudo grep 'temporary password' /var/log/mysqld.log

[root@localhost yum.repos.d]# sudo grep 'temporary password' /var/log/mysqld.log
2022-07-05T15:21:05.759128Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: +9ee!Z?esjfe
  • 1
  • 2

修改密码:

$> mysql -uroot -p
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'laoWAN@123456';
  • 1
  • 2

默认情况下,mysql对密码的安全级别限制较高,所以密码需要设置的复杂点才能修改成功。
如果只是开发环境只用,可以通过降低密码校验级别然后将密码修改的简单点。

查看密码校验级别:

mysql> SHOW VARIABLES LIKE 'validate_password.%';
+--------------------------------------+--------+
| Variable_name                        | Value  |
+--------------------------------------+--------+
| validate_password.check_user_name    | ON     |
| 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      |
+--------------------------------------+--------+
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

修改密码校验级别:

//设置密码校验级别
mysql> SET GLOBAL validate_password.policy=LOW;
Query OK, 0 rows affected (0.00 sec)
//设置密码最短长度
mysql> SET GLOBAL validate_password.length=6;
Query OK, 0 rows affected (0.00 sec)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

查看是否配置成功:

mysql> SHOW VARIABLES LIKE 'validate_password.%';
+--------------------------------------+-------+
| Variable_name                        | Value |
+--------------------------------------+-------+
| validate_password.check_user_name    | ON    |
| validate_password.dictionary_file    |       |
| validate_password.length             | 6     |
| validate_password.mixed_case_count   | 1     |
| validate_password.number_count       | 1     |
| validate_password.policy             | LOW   |
| validate_password.special_char_count | 1     |
+--------------------------------------+-------+
7 rows in set (0.01 sec)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

修改root用户密码:

$> mysql -uroot -p
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
  • 1
  • 2

7.创建用户并赋权

CREATE USER 'root'@'%' IDENTIFIED BY '123456';
GRANT ALL ON *.*  TO 'root'@'%';
  • 1
  • 2

其他命令:

//删除用户
mysql> drop user root@'%';
Query OK, 0 rows affected (0.00 sec)

//刷新权限
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

//查看用户
use mysql;
select user,host from user;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

8.关闭防火墙

默认情况下,Centos7会开启firewalld防火墙,屏蔽所有非22端口的网络请求,导致通过mysql客户端连接mysql服务失败。
由于是开发环境,直接关闭firewalld防火墙。

//停止firewall
systemctl stop firewalld

//禁止firewall开机启动
systemctl disable firewalld
  • 1
  • 2
  • 3
  • 4
  • 5

总结

比较关键的几点:
1、不要使用官网默认的yum源,下载速度超级慢,建议切换成OpenTuna源。
2、需要给rpm文件导入GPG密钥,不然会出现获取GPG密钥失败的异常。
3、通过修改默认的密码校验级别,成功设置简单的密码。
4、通过Mysql客户端工具连接失败时,检查Mysql服务是否启动,防火墙是否关闭。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/955669
推荐阅读
相关标签
  

闽ICP备14008679号