首页 > 技术文章 > yum安装mysql

shipment 2021-01-25 15:10 原文

yum安装mysql5.7

MySQL 5.7主要特性:

  • 更好的性能:对于多核CPU、固态硬盘、锁有着更好的优化,每秒100W QPS已不再是MySQL的追求,下个版本能否上200W QPS才是吾等用户更关心的
  • 更好的InnoDB存储引擎
  • 更为健壮的复制功能:复制带来了数据完全不丢失的方案,传统金融客户也可以选择使用MySQL数据库。此外,GTID在线平滑升级也变得可能
  • 更好的优化器:优化器代码重构的意义将在这个版本及以后的版本中带来巨大的改进,Oracle官方正在解决MySQL之前最大的难题
  • 原生JSON类型的支持
  • 更好的地理信息服务支持:InnoDB原生支持地理位置类型,支持GeoJSON,GeoHash特性
  • 新增sys库

在mysql8.0之后加密的方式也已经改变,在使用一些工具连接时除非使用最新版,否则要更改加密方式,例子

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your password';

yum的方式安装实际上就是下载一个rpm包,安装这个包后就有了mysql的各种repo源,包管理器通过repo源的地址去下载安装.

去官网可能访问比较慢,建议使用国内同步过来的源,比如阿里云镜像源清华大学镜像源中科大等等

可以用yum源中的mysql直接安装,但是版本不能自由选择

环境:

系统:centos7

#配置好yum源,包括epel源

[root@localhost ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
[root@localhost ~]# yum -y install vim wget


[root@localhost ~]#wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

使用官方源或者国内源安装

1.去官网下载yum仓库文件

官网下载连接:https://dev.mysql.com/downloads/repo/yum/

根据系统下载合适的文件

mysql_p1

mysql_p2

#将复制的连接地址下载
[root@localhost ~]# wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm

[root@localhost ~]# ls
anaconda-ks.cfg  mysql80-community-release-el7-3.noarch.rpm


###使用官方的源很慢,建议使用国内的源,清华大学的源:
https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql80-community-el7/mysql80-community-release-el7-3.noarch.rpm

2.安装yum仓库文件

#可是使用rpm -ivh或者是yum localinstall 去安装,两者实质是一样的
[root@localhost ~]# rpm -ivh mysql80-community-release-el7-3.noarch.rpm
warning: mysql80-community-release-el7-3.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql80-community-release-el7-3  ################################# [100%]

#安装完成后可以看到mysql的repo文件
[root@localhost ~]# ls /etc/yum.repos.d/
CentOS-Base.repo  epel.repo  mysql-community.repo  mysql-community-source.repo

3.版本选择

之前下载的默认开启的是mysql8.0

[root@localhost ~]# yum repolist enabled | grep mysql
mysql-connectors-community/x86_64 MySQL Connectors Community                 185
mysql-tools-community/x86_64      MySQL Tools Community                      123
mysql80-community/x86_64          MySQL 8.0 Community Server                 229

选择默认安装的版本

# 安装 YUM 管理工具包,此包提供了 yum-config-manager 命令工具
[root@localhost ~]# yum -y install yum-utils

[root@localhost ~]# yum-config-manager --disable mysql80-community
[root@localhost ~]# yum-config-manager --enable mysql57-community



查看默认启动的仓库

[root@localhost ~]# yum repolist enabled | grep mysql
mysql-connectors-community/x86_64 MySQL Connectors Community                 185
mysql-tools-community/x86_64      MySQL Tools Community                      123
mysql57-community/x86_64          MySQL 5.7 Community Server                 484

4.安装

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

5.设置默认配置

/etc/my.cnf的文件中设置

[mysqld]
#配置文件根据环境自行配置,或者保持默认
#例如添加下面几行,设置默认引擎编码和排序规则(根据情况设置合适的)
default-storage-engine=INNODB
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
skip-character-set-client-handshake
secure_file_priv=''


[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4

6.启动服务

# 启动
systemctl start mysqld

# 查看状态
systemctl status mysqld

# 开机自启动
systemctl enable mysqld

# 查看监听端口,默认 3306
ss -natl |grep 3306

7.初始化

MySQL服务器初始化(从MySQL 5.7开始):

在 MySQL 服务器初始启动时,如果服务器的数据目录为空,则会发生以下情况:

  • MySQL 服务器已初始化。
  • 在数据目录中生成SSL证书和密钥文件。
  • 安装并启用该 validate_password 插件。
  • 将创建一个超级用户 帐户'root'@'localhost'。并会设置超级用户的密码,将其存储在错误日志文件/var/log/mysqld.log中。
[root@localhost ~]# grep 'temporary password' /var/log/mysqld.log
2021-01-25T04:26:33.010077Z 1 [Note] A temporary password is generated for root@localhost: *sH2qhGeN(eB

#本地登录的root密码: *sH2qhGeN(eB

8.修改初始密码

使用日志中得到的密码登录

[root@localhost ~]# mysql -uroot -p'*sH2qhGeN(eB'
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 3
Server version: 5.7.33

Copyright (c) 2000, 2021, 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>

修改密码:

mysql> alter user   root@localhost   identified  by  '123456';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

#太过简单的密码会失败,因为不满足密码复杂度的要求

mysql> alter user   root@localhost   identified  by  'Test123!';
Query OK, 0 rows affected (0.00 sec)

要设置比较简单的密码就需要取消密码复杂度

编辑 my.cnf配置文件, 在 [mysqld]配置块儿中添加如下内容

plugin-load=validate_password.so 
validate-password=OFF

保存退出后,重启服务, 修改密码

9.远程连接

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

 mysql> grant all privileges on *.* to root@localhost identified by 'Test123!';
 Query OK, 0 rows affected, 1 warning (0.00 sec)

#允许任意IP连接
 mysql> grant all privileges on *.* to root@'%' identified by 'Test123!';
 Query OK, 0 rows affected, 1 warning (0.00 sec)

%:匹配任意长度的任意字符,常用于设置允许从任何主机登录
_:匹配任意单个字符

#放开3306端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload

#关闭防火墙和selinux一劳永逸

推荐阅读