操作系统:centos7
mysql版本:5.7
目录
一、卸载MySQL环境
1 查看yum是否安装过mysql
yum list installed mysql*
可以看到如下信息
>>
已安装的软件包
mysql-community-client.x86_64 5.7.28-1.el7 @mysql57-community
mysql-community-common.x86_64 5.7.28-1.el7 @mysql57-community
mysql-community-libs.x86_64 5.7.28-1.el7 @mysql57-community
mysql-community-libs-compat.x86_64 5.7.28-1.el7 @mysql57-community
mysql-community-server.x86_64 5.7.28-1.el7 @mysql57-community
mysql57-community-release.noarch el7-8 @/mysql57-community-release-el7-8.noarch
2 删除已安装mysql包
yum remove mysql-community-client mysql-community-common mysql-community-libs mysql-community-libs-compat mysql-community-server mysql57-community-release
3 查看rpm下载的mysql包
rpm -qa | grep -i mysql
可以看到如下信息
>>>
mysql-community-libs-5.7.28-1.el7.x86_64
mysql57-community-release-el7-8.noarch
mysql-community-common-5.7.28-1.el7.x86_64
mysql-community-client-5.7.28-1.el7.x86_64
mysql-community-libs-compat-5.7.28-1.el7.x86_64
mysql-community-server-5.7.28-1.el7.x86_64
4 卸载rpm包
rpm -e mysql57-community-release-el7-9.noarch
rpm -e mysql-community-server-5.7.17-1.el7.x86_64
rpm -e mysql-community-libs-5.7.17-1.el7.x86_64
rpm -e mysql-community-libs-compat-5.7.17-1.el7.x86_64
rpm -e mysql-community-common-5.7.17-1.el7.x86_64
rpm -e mysql-community-client-5.7.17-1.el7.x86_64
5 删除配置的mysql空间
3-1 先查看my.cnf中配置的datadir,不要删错了文件(默认为:/var/lib/mysql)
3-2 删除datadir: rm -rf /var/lib/mysql/ (!记得要删除mysql这个文件夹)
3-3 删除配置:rm /etc/my.cnf
6 检查剩余的mysql信息
6-1 whereis mysql
6-2 将搜索出的地方,按需删除(一般都是全部删除)
6-3 删除一些mysql用户配置
rm –rf /usr/my.cnf
rm -rf /root/.mysql_sercret
6-4 列举一下mysql的依赖,一般到这里就删除的比较彻底礼
systemctl list-dependencies mysqld
7 mysql卸载完成
二、安装MySQL
在MySQL官网中下载YUM源rpm安装包:http://dev.mysql.com/downloads/repo/yum/
1、下载并安装MySQL官方的 Yum Repository(可以去官网找到自己想要的版本,复制链接地址)
wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
2、yum安装mysql
yum -y install mysql57-community-release-el7-8.noarch.rpm
3、安装mysql服务器
yum -y install mysql-community-server
4、去喝杯茶,等上一段时间
5、下载好之后,直接启动mysql服务,先不去配置
5-1、systemctl start mysqld
5-2、出现:Active: active (running)代表启动成功
5-3、如果为其他状态,仔仔细细的看下提示信息,在看下mysql日志(从/etc/my.cnf中找到配置的日志路径),关注 [ERROR]信息,一般挺好排查的
6、mysql启动之后,获取初始化的root密码
6-1、从/etc/my.cnf中找到配置的日志路径(默认为:/var/log/mysqld.log)
6-2、grep "password" /var/log/mysqld.log
7、登录mysql,修改密码,不修改干不了其他的事
7-1、mysql -uroot -p
7-2、输入步骤6中的密码,回车登录
7-3、修改密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';
7-4、如果提示 ERROR 1819: 密码强度太弱,执行下面的命令,降低密码策略
7-4-1: mysql> set global validate_password_policy=0;
7-4-2: mysql> set global validate_password_length=1;
7-5、再重新设置密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';
7-6、接下来配置mysql服务,让其支撑远程访问,避免出现 Host 'XXX' is not allowed to connect to this MySQL server
mysql> grant all privileges on *.* to root@'%' identified by '你设置的root密码';
7-7、刷新
mysql> flush privileges;
7-8、退出
mysql> exit;
8、[非必须] 用root用户,修改mysql配置文件
8-1、vi /etc/my.cnf
8-2、在my.conf [mysqld]的文末加入
# 这一步不建议操作,不安全,且配置麻烦
#validate_password = off # 关闭密码策略
character_set_server=utf8 # 编码设置
init_connect='SET NAMES utf8' # 编码设置
lower_case_table_names=1 # 在SQL查询中忽略表的大小写,避免出现:Table '***_db.***_table' doesn't exist
8-3、保存退出
8-4、重启mysql服务
systemctl restart mysqld
9、结束,继续喝茶