Linux下卸载重装MySQL(基于CentOS8)
背景:卸载宝塔面板后,MySQL服务起不来,尝试修复无果,直接重装
1.卸载MySQL组件
$ rpm -e --nodeps mysql-community-common
$ rpm -e --nodeps mysql-community-libs
$ rpm -e --nodeps mysql-community-libs-compat
$ rpm -e --nodeps mysql-community-client
$ rpm -e --nodeps mysql-community-server
2.检查是否卸载干净
$ rpm -qa | grep -i mysql
3.查看mysql相关文件
$ whereis mysql
4.删除mysql相关文件
$ rm -rvf /usr/lib64/mysql*
$ rm -rvf /usr/include/mysql*
$ rm -rvf /usr/share/mysql*
5.查找mysql文件
$ find / -name mysql
6.删除相关文件
$ rm -rvf /var/lib/selinux/targeted/active/modules/100/mysql*
$ rm -rvf /var/lib/selinux/targeted/tmp/modules/100/mysql*
$ rm -rvf /var/lib/mysql*
$ rm -rvf /usr/share/bash-completion/completions/mysql*
$ rm -rvf /usr/share/selinux/targeted/default/active/modules/100/mysql*
7.重新安装MySQL组件
$ sudo rpm -ivh mysql-community-common-5.7.28-1.el7.x86_64.rpm
$ sudo rpm -ivh mysql-community-libs-5.7.28-1.el7.x86_64.rpm
$ sudo rpm -ivh mysql-community-libs-compat-5.7.28-1.el7.x86_64.rpm
$ sudo rpm -ivh mysql-community-client-5.7.28-1.el7.x86_64.rpm
$ sudo rpm -ivh mysql-community-server-5.7.28-1.el7.x86_64.rpm
8.初始化数据库
$ sudo mysqld --initialize --user=mysql
9.查看临时生成的root用户的密码
$ sudo grep 'temporary password' /var/log/mysqld.log
# vhep<My(Z0hx
10.启动 MySQL 服务
$ sudo systemctl start mysqld
11.登录 MySQL 数据库
$ mysql -uroot -p
密码:vhep<My(Z0hx
12.修改数据库root用户的登录密码
mysql> set password = password("000000");
MySQL8版本:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '000000';
-- 更新权限表: 更新权限表,使更改生效
FLUSH PRIVILEGES;
-- 重启MySQL服务
sudo systemctl restart mysqld
13.修改 mysql 库下的user表中的 root 用户允许任意 ip 连接
mysql> update mysql.user set host='%' where user='root';
mysql> flush privileges;