Centos7.9系统yum安装mysql5.7版本

Posted 花开花落的个人博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Centos7.9系统yum安装mysql5.7版本相关的知识,希望对你有一定的参考价值。

CentOS中默认安装有MariaDB,安装mysql会覆盖掉MariaDB;

环境说明:

centos7.9 64位系统

下载mysql社区版本源并安装

wget https://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
yum install -y mysql57-community-release-el7-10.noarch.rpm

安装之后 #yum repolist 会更新mysql repo源

yum安装mysql server

yum install -y mysql-community-server

出现如下错误

Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql


The GPG keys listed for the "MySQL 5.7 Community Server" repository are already installed but they are not correct for this package.
Check that the correct key URLs are configured for this repository.

GPG对于包的源key的验证没有通过

解决办法

在yum install 版本后面加上 --nogpgcheck,即可绕过GPG验证成功安装。

yum install mysql-community-server --nogpgcheck

启动mysql服务

systemctl start mysqld

查看版本

mysql --version

mysql  Ver 14.14 Distrib 5.7.38, for Linux (x86_64) using  EditLine wrapper

Mysql的使用和授权

查找mysql的登陆临时密码

grep 'password' /var/log/mysqld.log

使用临时密码登陆mysql,登陆后第一件事需要修改密码,必须符合密码复杂度 大小写字母数字加特殊字符组合;

mysql -uroot -p
mysql> alter user root@'localhost' identified by '密码';

测试1:创建一个新用户,只能从本地访问mysql,只授权mysql数据库下的所有表的select权限;

mysql> grant select on mysql.* to guo@localhost identified by '密码';

使用新用户登陆并测试删除表,提示没有权限;

mysql -u guo -h localhost -p
mysql> use mysql;
mysql> drop table user;
ERROR 1142 (42000): DROP command denied to user 'guo'@'localhost' for table 'user'

测试2:

授权star用户限制只能从172.16.0.21 地址访问,拥有最高权限,可以访问所有库所有表;

mysql> grant all privileges on *.* to 'star'@'172.16.0.21' identified by '密码';
mysql> flush privileges;
mysql> select user,host from mysql.user;
+---------------+--------------+
| user          | host         |
+---------------+--------------+
| star          | 172.16.0.21 |
| mysql.session | localhost    |
| mysql.sys     | localhost    |
| root          | localhost    |
| guo           | localhost    |
+---------------+--------------+

使用新用户star本地登陆,提示访问拒绝,使用密码正确;

mysql -ustar -p
Enter password:
ERROR 1045 (28000): Access denied for user 'star'@'localhost' (using password: YES)

登陆172.16.0.21 服务器指定 -h mysql服务器IP访问成功(只安装mysql client客户端即可)

wget https://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
yum install -y mysql57-community-release-el7-10.noarch.rpm
yum install -y mysql-community-client
mysql -ustar -p -h 172.16.0.22

密码策略

在命令行模式使用-e  参数,查看密码执行策略:

修改密码策略级别:

mysql -uroot -p -e "set global validate_password_policy=LOW;"

修改密码策略长度:

mysql -uroot -p -e "set global validate_password_length=6;"

编辑mysql配置文件,添加以下代码,设置字符集为utf8

vi /etc/my.cnf
[client]
default-character-set=utf8
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci

重启mysql生效

systemctl restart mysqld

再次登录数据库查看status状态,已更改为utf8

mysql -uroot -p

设置root远程访问,生产环境请不要这样使用,非常危险

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '密码' 

以上是关于Centos7.9系统yum安装mysql5.7版本的主要内容,如果未能解决你的问题,请参考以下文章

centos7 yum安装mysql5.7

CentOS7.9安装MySQL5.7

CentOS7.9安装MySQL5.7

CentOS7.9安装MySQL5.7

CentOS7.9安装MySQL5.7

安装MySQL5.7 安装环境:CentOS7 64位 MINI版,