centos8上通过yum安装MySQL,过程简单,不易出错
1.检查系统是否已安装MySQL相关,如果有则全部清除干净
#列出MySQL相关的安装包
rpm -qa | grep mysql
#依次删除安装包
yum remove xxx
2.安装centos8下MySQL8的yum源
mk /usr/local/mysql8/
cd /usr/local/mysql8/
#下载源
wget https://dev.mysql.com/get/mysql80-community-release-el8-4.noarch.rpm
#安装源
sudo yum install mysql80-community-release-el8-4.noarch.rpm
#检查
yum repolist enabled | grep "mysql.*-community.*"
3.关闭默认的MySQL(一定要执行,不然安装时会提示找不到包)
yum module disable mysql -y
4.安装MySQL
#安装
yum install mysql-community-server -y
#修改配置
vi /etc/my.cnf
wait_timeout=32000
slow_query_log = ON
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 2
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
init_connect='SET NAMES utf8mb4'
max_connections=1000
5.启动MySQL服务,查看运行状态
# 设置开机启动
systemctl enable mysqld
systemctl daemon-reload
#启动mysql服务
systemctl start mysqld
#查看mysql运行状态
systemctl status mysqld
6.查找初始密码并登录修改密码
#查找初始密码
grep 'temporary password' /var/log/mysqld.log
#通过初始密码登录
mysql -uroot -p
#修改成自己的密码
ALTER USER 'root'@'localhost' IDENTIFIED BY '自己的密码';
#刷新权限
flush privileges;
#退出MySQL命令行
exit;
#bash下重启MySQL服务
systemctl restart mysqld;
7.配置root远程登录
如果远程登录连接失败,请先检查系统云服务商(例如阿里云,腾讯云,华为云)控制台上的防火墙配置,一般不需要配置系统本身的防火墙
#登录mysql
mysql -uroot -p
#切换到mysql数据库
use mysql;
#让root可以通过任意IP登录
update user set host='%' where user='root';