CentOS 8.x 安装 MySQL 8.x


官网找 MySQL 最新版本

https://dev.mysql.com/downloads/mysql/

目前最新版本是 8.0.28,执行 curl 下载最小化安装的 6 个 rpm 包

MYSQL_VERSION=8.0.28
curl -O https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-8.0/mysql-community-common-${MYSQL_VERSION}-1.el8.$(uname -m).rpm
curl -O https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-8.0/mysql-community-client-plugins-${MYSQL_VERSION}-1.el8.$(uname -m).rpm
curl -O https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-8.0/mysql-community-libs-${MYSQL_VERSION}-1.el8.$(uname -m).rpm
curl -O https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-8.0/mysql-community-client-${MYSQL_VERSION}-1.el8.$(uname -m).rpm
curl -O https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-8.0/mysql-community-icu-data-files-${MYSQL_VERSION}-1.el8.$(uname -m).rpm
curl -O https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-8.0/mysql-community-server-${MYSQL_VERSION}-1.el8.$(uname -m).rpm

依次安装 rpm 包,中间可能会报缺少 perl 及 net-tools,dnf 安装即可

rpm -ivh mysql-community-common-${MYSQL_VERSION}-1.el8.$(uname -m).rpm
rpm -ivh mysql-community-client-plugins-${MYSQL_VERSION}-1.el8.$(uname -m).rpm
rpm -ivh mysql-community-libs-${MYSQL_VERSION}-1.el8.$(uname -m).rpm
rpm -ivh mysql-community-client-${MYSQL_VERSION}-1.el8.$(uname -m).rpm
rpm -ivh mysql-community-icu-data-files-${MYSQL_VERSION}-1.el8.$(uname -m).rpm
rpm -ivh mysql-community-server-${MYSQL_VERSION}-1.el8.$(uname -m).rpm

查看是否安装 mariadb,存在则删除,如:rpm -e --nodeps mariadb-libs

rpm -qa | grep mariadb

停止 mysqld 服务

systemctl stop mysqld

初始化数据库(时间有点长,耐心等待)

mysqld --initialize --console

目录授权

chown -R mysql:mysql /var/lib/mysql/

启动 mysqld 服务

systemctl start mysqld

查看临时密码

cat /var/log/mysqld.log

使用临时密码登录数据库

mysql -u root -p

修改 mysql 密码,并授权远程连接

alter USER 'root'@'localhost' IDENTIFIED BY '123456';
use mysql;
update user set host = "%" where user='root';
flush privileges;

关闭防火墙,使用 Navicat 测试数据库连接

systemctl stop firewalld
systemctl disable firewalld