MySQL5.7版本的yum安装方式

yum部署方式

从CentOS 7.0发布以来,yum源中开始使用Mariadb来代替MySQL的安装(这里什么是mariadb,和mysql有什么区别,有兴趣的小伙伴可以自行查阅)。即使你输入的是yum install -y mysql , 显示的也是Mariadb的安装内容。如果想安装Mysql就必须使用Mysql官方的yum源。

一、编写yum源配置文件

#官网yum源地址
https://dev.mysql.com/downloads/repo/yum/

#下载
[root@db01 ~]# wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm

[root@db01 ~]# yum install -y mysql80-community-release-el7-3.noarch.rpm

#生成yum源并检查是否存在
[root@db01 ~]# yum makecache
[root@db01 ~]# yum repolist enabled | grep mysql
mysql-connectors-community/x86_64 MySQL Connectors Community                 212
mysql-tools-community/x86_64      MySQL Tools Community                      132
mysql80-community/x86_64          MySQL 8.0 Community Server                 283

#安装yum源管理工具
[root@db01 ~]# yum install -y yum-utils

#默认安装最新版的MySQL版本,这里我只想安装MySQL-5.7版本的
[root@db01 ~]# yum-config-manager --disable mysql80-community
[root@db01 ~]# yum-config-manager --enable mysql57-community
#检查是否更换
[root@db01 ~]# yum repolist enabled | grep mysql
mysql-connectors-community/x86_64 MySQL Connectors Community                 212
mysql-tools-community/x86_64      MySQL Tools Community                      132
mysql57-community/x86_64          MySQL 5.7 Community Server                 524

[root@db01 ~]# yum install -y mysql  mysql-community-server

#启动
[root@db01 ~]# systemctl start mysqld
[root@db01 ~]# systemctl status mysqld

二、初始化

MySQL服务器初始化(从MySQL 5.7开始)
在 MySQL 服务器初始启动时,如果服务器的数据目录为空,则会发生以下情况:
MySQL 服务器已初始化。
在数据目录中生成SSL证书和密钥文件。
安装并启用该 validate_password 插件。
将创建一个超级用户 帐户'root'@'localhost'。并会设置超级用户的密码,将其存储在错误日志文件/var/log/mysqld.log中。

[root@db01 ~]# cat /var/log/mysqld.log | grep 'temporary password'
2021-09-22T11:01:44.455052Z 1 [Note] A temporary password is generated for root@localhost: ?+wdtroG!1<t

三、尝试连接并修改默认密码

[root@db01 ~]# mysql -uroot -p'?+wdtroG!1<t'
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

#报错。 解决:修改初始密码
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

#报错。密码太简单
mysql> alter user root@localhost identified by '12345';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

#修改密码并重新登陆
mysql> alter user root@localhost identified by 'Zhang.32@com';
Query OK, 0 rows affected (0.00 sec)


#要设置比较简单的密码就需要取消密码复杂度,编辑 /etc/my.cnf配置文件, 在 [mysqld]配置块儿中添加如下内容(企业生成环境不建议使用这种配置)
plugin-load=validate_password.so 
validate-password=OFF

四、远程连接

远程登录还需要授权远程登录
Mysql默认不允许远程登录,我们需要设置关闭selinux或者防火墙,不关防火墙就开放3306端口

#关闭防火墙firewalld或者iptables
[root@db01 ~]# systemctl stop firewalld
[root@db01 ~]# systemctl stop iptables

#授权
[root@db01 ~]# mysql -uroot -p
mysql> grant all privileges on *.* to root@'%' identified by 'Zhang,32';



#如有开启防火墙需开放3306端口
[root@db01 ~]# firewall-cmd --add-port=3306/tcp 

[root@db01 ~]# iptables -t filter -I INPUT -p tcp --dport 3306 -j ACCEPT

原文地址:https://www.cnblogs.com/backz/p/15321754.html