通过yum安装 官网
总体步骤分三步:
1.添加MySQL相关yum源
将mysql的yum源添加到本地yum仓库列表,需要做下面三件事。
a.下载yum源安装包
sudo wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
b.安装yum源
sudo yum install mysql80-community-release-el7-3.noarch.rpm
安装完成,可以通过下面命令查看,可以看到已经成功添加相关仓库列表。
[xiaomao@bt soft]$ yum repolist enabled | grep "mysql.*-community.*"
mysql-connectors-community/x86_64 MySQL Connectors Community 175
mysql-tools-community/x86_64 MySQL Tools Community 120
mysql80-community/x86_64 MySQL 8.0 Community Server 211
查看仓库中mysql的版本
[xiaomao@bt soft]$ yum repolist all | grep mysql
mysql-cluster-7.5-community/x86_64 MySQL Cluster 7.5 Community disabled
mysql-cluster-7.5-community-source MySQL Cluster 7.5 Community - disabled
mysql-cluster-7.6-community/x86_64 MySQL Cluster 7.6 Community disabled
mysql-cluster-7.6-community-source MySQL Cluster 7.6 Community - disabled
mysql-cluster-8.0-community/x86_64 MySQL Cluster 8.0 Community disabled
mysql-cluster-8.0-community-source MySQL Cluster 8.0 Community - disabled
mysql-connectors-community/x86_64 MySQL Connectors Community enabled: 175
mysql-connectors-community-source MySQL Connectors Community - disabled
mysql-tools-community/x86_64 MySQL Tools Community enabled: 120
mysql-tools-community-source MySQL Tools Community - Sourc disabled
mysql-tools-preview/x86_64 MySQL Tools Preview disabled
mysql-tools-preview-source MySQL Tools Preview - Source disabled
mysql55-community/x86_64 MySQL 5.5 Community Server disabled
mysql55-community-source MySQL 5.5 Community Server - disabled
mysql56-community/x86_64 MySQL 5.6 Community Server disabled
mysql56-community-source MySQL 5.6 Community Server - disabled
mysql57-community/x86_64 MySQL 5.7 Community Server disabled
mysql57-community-source MySQL 5.7 Community Server - disabled
mysql80-community/x86_64 MySQL 8.0 Community Server enabled: 211
mysql80-community-source MySQL 8.0 Community Server - disabled
2.配置需要的版本
编辑配置文件:/etc/yum.repos.d/mysql-community.repo
(默认会启用最新的版本)
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/6/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
enable=1 //表示启用
enable=0 //表示禁止
启用自己想安装的版本后报存
3.开始下载安装:
sudo yum install mysql-community-server
报错:
报错原文
Public key for mysql-community-server-5.7.37-1.el7.x86_64.rpm is not installed
Failing package is: mysql-community-server-5.7.37-1.el7.x86_64
GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Mysql安装失败
操作系统:CentOS 7.6
Mysql版本:mysql5.7
CentOS7.6 安装mysql5.7的时候报错,提示某一个包安装不成功。
失败原因
GPG对于包的源key的验证没有通过
解决办法
在yum install 版本后面加上 --nogpgcheck,即可绕过GPG验证成功安装。比如yum install mysql-community-server --nogpgcheck
4.启动Mysql:
sudo service mysqld start
sudo systemctl start mysqld.service //此语法是EL7以上
# 如果报错:先进行授权
chmod -R 777 /var/lib/mysql
查看启动状态:
sudo service mysqld status
sudo systemctl status mysqld.service //EL7 preferred
5.初始用户:
启动mysql后,会默认创建一个 root@localhost用户,密码可以通过如下命令查看(g6atVjN1!,yJ):
sudo grep 'temporary password' /var/log/mysqld.log
#如果上面查看没有密码,说明没有初始密码,直接回车就能登录
一般密码比较复杂,通过如下命令重置新密码:
mysql -uroot -p //先登录
set password=password("root@123A"); //修改密码
或通过该语句也可修改密码:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'root@123B';
6.配置可以远程连接数据库:
update user set host="%" where user="root";
flush privileges; //更新
上述操作一般完成就可以,如果不行,再执行下面的授权操作。
授权:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
重载授权表:
FLUSH PRIVILEGES;
7.Mysql8.0相关注意:
- 加密规则改变,用客户端连接出错,比如用navicat连接:
Client does not support authentication protocol requested by server.....
Authentication plugin 'caching_sha2_password' cannot be loaded
查看数据库加密插件:
--5.7版本中可以看到加密采用的是:mysql_native_password
--8.0中是caching_sha2_password
select user,host,plugin from user;
为了兼容,修改8.0的加密方式(也可以升级客户端):
-- 修改密码为永不过期
mysql> ALTER USER 'root'@'%' IDENTIFIED BY '你的密码' PASSWORD EXPIRE NEVER;
Query OK, 0 rows affected (0.02 sec)
-- 修改密码并指定加密规则为mysql_native_password
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.01 sec)
-- 刷新权限
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql>