1. 下载:

地址 https://dev.mysql.com/downloads/mysql/
MySQL - 图1

2. 解压

tar -xvf mysql-8.0.17-1.el7.x86_64.rpm-bundle.tar -C /usr/local/mysql/

解压后得到如下文件
mysql-community-client-8.0.17-1.el7.x86_64.rpm — 4
mysql-community-common-8.0.17-1.el7.x86_64.rpm — 1
mysql-community-devel-8.0.17-1.el7.x86_64.rpm —
mysql-community-embedded-compat-8.0.17-1.el7.x86_64.rpm — 5
mysql-community-libs-8.0.17-1.el7.x86_64.rpm — 2
mysql-community-libs-compat-8.0.17-1.el7.x86_64.rpm — 3
mysql-community-server-8.0.17-1.el7.x86_64.rpm — 6
mysql-community-test-8.0.17-1.el7.x86_64.rpm —

3. 安装

使用 rpm –vih XXX 命令安装,安装顺序在上一步标注

安装时出现的问题:
(1) 安装mysql-community-libs时报错:
error: Failed dependencies:
mariadb-libs is obsoleted by mysql-community-libs-8.0.17-1.el7.x8664
原因:CentOS 7默认的是mariadb数据库,所以要先卸载mariadb:
rpm -qa|grep mariadb — 查找mariadb数据库安装位置
rpm -e mariadb-libs-5.5.64-1.el7.x86_64 — 卸载
(2) 此时报错:_
error: Failed dependencies:
libmysqlclient.so.18()(64bit) is needed by (installed) postfix-2:2.10…
libmysqlclient.so.18(libmysqlclient_18)(64bit) is needed by (installed) …
原因:由于存在依赖关系,强制卸载:
rpm -e —nodeps mariadb-libs-5.5.64-1.el7.x86_64

(3) 安装mysql-community-embedded-compat时报错
error: Failed dependencies:
libnuma.so.1()(64bit) is needed by mysql-community-server-5.7.28-1.el7.x86_64
libnuma.so.1(libnuma_1.1)(64bit) is needed by mysql-community-server-5.7.28-1.el7.x86_64
libnuma.so.1(libnuma_1.2)(64bit) is needed by mysql-community-server-5.7.28-1.el7.x86_64
解决:安装numactl:
yum install numactl

(4) mysql-5.7.28-1.el7.x86_64.rpm-bundle安装需要的依赖
yum install libaio
yum install perl
yum install net-tools

4. 启动服务

  1. systemctl start mysqld

查看mysql服务状态
systemctl status mysqld

设置mysql随系统启动
systemctl enable mysqld

5. 修改root初始密码

查看初始密码:grep “password” /var/log/mysqld.log
2019-10-14T14:36:58.996882Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: lVg<J/UAg6fV

登录:mysql -uroot -p 输入密码: lVg<J/UAg6fV

修改密码:ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘mysql123456’;

修改密码时报错:ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
原因:设置的密码不符合mysql8的规范
解决办法:调整MySQL密码验证规则,修改 policy 和 length 的值:
set global validate_password.policy=0; —默认为1
set global validate_password.length=1; —默认为8
(查看密码相关安全设置: show variables like ‘validate_password%’;)

允许远程连接:

  • 授权法:

允许所有用户远程访问
mysql> GRANT ALL PRIVILEGES ON . TO ‘USERNAME’@’%’ IDENTIFIED BY ‘PASSWORD’ WITH GRANT OPTION; ~~(mysql8的分配权限不能带密码隐式创建账号了)
#允许单个ip 修改用户名和密码为你自己的
mysql> GRANT ALL PRIVILEGES ON . TO ‘USERNAME’@’1.2.3.4’ ~~IDENTIFIED BY ‘PASSWORD’ WITH GRANT OPTION;

mysql> FLUSH PRIVILEGES;

  • 该表法:

mysql> use mysql;
mysql> update user set host = ‘%’ where user = ‘root’; (需要重启mysql服务)

终极办法-修改mysql.cnf配置文件:
vim /etc/mysql/mysql.conf.d/mysqld.cnf
# bind-address = 127.0.0.1 #注释此行
重启服务

一次操作记录:
MySQL - 图2
错误:You are not allowed to create a user with GRANT
原因:当前user表中没有root - %记录; 可以更新root - localhost 为 root - %
MySQL - 图3

mysql> GRANT ALL PRIVILEGES ON . TO ‘root’@’%’;
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

此时用navicat连接还是报错:Client does not support authentication protocol requested by server;
原因:mysql8默认的加密方式为caching_sha2_password 与mysql5的加密方式mysql_native_password 不同

解决:更新用户加密方式:
ALTER USER ‘root’@’%’ IDENTIFIED WITH mysql_native_password BY ‘密码’;
MySQL - 图4