1. 验证 mysql 是否安装
2. 卸载之前的 mysql
3. 安装新的 mysql
4. 配置 root 用户密码
5. 总结 Linux 重新安装 Mysql 的步骤
1. 验证 mysql 是否安装
rpm -qa | grep -i mysql
# -i:不区分大小写
- 验证 mysql 是否安装:
[root@localhost ~]# rpm -qa | grep -i mysql
mysql-server-5.1.73-5.el6_6.i686
mysql-libs-5.1.73-5.el6_6.i686
mysql-5.1.73-5.el6_6.i686
perl-DBD-MySQL-4.013-3.el6.i686
2. 卸载之前的 mysql
rpm -e --nodeps xx
# -e:卸载
# --nodeps:强制性
- 卸载 mysql :
[root@localhost ~]# rpm -qa | grep -i mysql
mysql-server-5.1.73-5.el6_6.i686
mysql-libs-5.1.73-5.el6_6.i686
mysql-5.1.73-5.el6_6.i686
perl-DBD-MySQL-4.013-3.el6.i686
[root@localhost ~]# rpm -e --nodeps mysql-server-5.1.73-5.el6_6.i686
[root@localhost ~]# rpm -e --nodeps mysql-5.1.73-5.el6_6.i686
[root@localhost ~]# rpm -e --nodeps perl-DBD-MySQL-4.013-3.el6.i686
[root@localhost ~]# rpm -e --nodeps mysql-libs-5.1.73-5.el6_6.i686
3. 安装新的 mysql
cd /opt/mysql
ls -al
rpm -ivh xx
# i:安装
# v:显示安装进度
# h:显示安装过程
service mysqld status
# 查看 mysql 服务状态
-
在 /opt 目录下创建 mysql 目录,并上传 mysql 安装包
-
进入 /opt/mysql 目录,安装 mysql
[root@localhost ~]# cd /opt/mysql/
[root@localhost mysql]# ls -al
total 89216
drwxr-xr-x. 2 root root 4096 Mar 3 13:38 .
drwxr-xr-x. 4 root root 4096 Mar 3 13:38 ..
-rw-r--r--. 1 root root 20893788 Mar 11 2020 MySQL-client-5.6.48-1.el6.i686.rpm
-rw-r--r--. 1 root root 64038720 Mar 11 2020 MySQL-server-5.6.48-1.el6.i686.rpm
-rw-r--r--. 1 root root 2235116 Mar 11 2020 MySQL-shared-5.6.48-1.el6.i686.rpm
-rw-r--r--. 1 root root 4173024 Mar 11 2020 MySQL-shared-compat-5.6.48-1.el6.i686.rpm
[root@localhost mysql]# rpm -ivh MySQL-client-5.6.48-1.el6.i686.rpm
warning: MySQL-client-5.6.48-1.el6.i686.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ########################################### [100%]
1:MySQL-client ########################################### [100%]
[root@localhost mysql]# rpm -ivh MySQL-shared-5.6.48-1.el6.i686.rpm
warning: MySQL-shared-5.6.48-1.el6.i686.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ########################################### [100%]
1:MySQL-shared ########################################### [100%]
[root@localhost mysql]# rpm -ivh MySQL-shared-compat-5.6.48-1.el6.i686.rpm
warning: MySQL-shared-compat-5.6.48-1.el6.i686.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ########################################### [100%]
1:MySQL-shared-compat ########################################### [100%]
[root@localhost mysql]# rpm -ivh MySQL-server-5.6.48-1.el6.i686.rpm
warning: MySQL-server-5.6.48-1.el6.i686.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ########################################### [100%]
1:MySQL-server ########################################### [100%]
...... 初始化数据库 ......
- 验证 mysql 是否被安装成功,查看 mysql 服务状态
# mysql 安装成功,mysql 服务非启动状态
[root@localhost mysql]# service mysqld status
MySQL is not running[FAILED]
# mysql 安装失败,mysql 服务无法识别
[root@localhost mysql]# service mysqld status
mysql: unrecognized service
- 若 mysql 安装失败,卸载所有 mysql 相关包,重新安装 mysql
[root@localhost mysql]# service mysqld status
mysql: unrecognized service
[root@localhost mysql]# rpm -qa | grep -i mysql
mysql-libs-5.1.73-5.el6_6.i686
MySQL-shared-5.6.48-1.el6.i686
MySQL-client-5.6.48-1.el6.i686
MySQL-shared-compat-5.6.48-1.el6.i686
[root@localhost mysql]# rpm -e --nodeps MySQL-shared-5.6.48-1.el6.i686
[root@localhost mysql]# rpm -e --nodeps MySQL-client-5.6.48-1.el6.i686
[root@localhost mysql]# rpm -e --nodeps MySQL-shared-compat-5.6.48-1.el6.i686
[root@localhost mysql]# rpm -e --nodeps mysql-libs-5.1.73-5.el6_6.i686
[root@localhost mysql]# rpm -ivh MySQL-client-5.6.48-1.el6.i686.rpm
[root@localhost mysql]# rpm -ivh MySQL-shared-5.6.48-1.el6.i686.rpm
[root@localhost mysql]# rpm -ivh MySQL-shared-compat-5.6.48-1.el6.i686.rpm
[root@localhost mysql]# rpm -ivh MySQL-server-5.6.48-1.el6.i686.rpm
[root@localhost mysql]# service mysqld status
MySQL is not running[FAILED]
4. 配置 root 用户密码
service mysqld start
# 启动 mysql 服务
# status:状态
# start:启动
# stop:关闭
# restart:重启
cd /root
ls -al .mysql_secret
# 验证 .mysql_secret 是否存在
cat /root/.mysql_secret
# 查看 root 用户的密码
mysql -u root -p
# 进入 mysql 修改密码
set password for root@localhost=password('123456');
flush privileges;
# 修改密码,刷新授权表
- 启动 mysql 服务,查看 root 用户的密码:ROvVVC_2cf9jIXUX
[root@localhost ~]# service mysqld start
Starting MySQL.[ OK ]
[root@localhost mysql]# cd /root
[root@localhost ~]# ls -al .mysql_secret
-rw-------. 1 root root 104 Apr 18 18:45 .mysql_secret
[root@localhost ~]# cat /root/.mysql_secret
# The random password set for the root user
at Sun Apr 18 18:45:25 2021 (local time): ROvVVC_2cf9jIXUX
- 使用该密码,进入 mysql 修改密码,刷新授权表
[root@localhost ~]# mysql -u root -p
Enter password: ROvVVC_2cf9jIXUX
Welcome to the MySQL monitor.
mysql> set password for root@localhost=password('123456');
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> Ctrl-C -- exit!
Aborted
- 退出 mysql(Ctrl + C),再以新密码进入 mysql,验证密码是否修改成功
[root@localhost ~]# mysql -u root -p
Enter password: 123456
Welcome to the MySQL monitor.
到此,新的 Mysql 安装完成
5. 总结 Linux 重新安装 Mysql 的步骤
# 验证 mysql 是否安装
1、rpm -qa | grep -i mysql
# 卸载之前的 mysql
2、 rpm -e --nodeps mysql-server-5.1.73-5.el6_6.i686
rpm -e --nodeps mysql-5.1.73-5.el6_6.i686
rpm -e --nodeps perl-DBD-MySQL-4.013-3.el6.i686
rpm -e --nodeps mysql-libs-5.1.73-5.el6_6.i686
# 上传 mysql 安装包,安装新的 mysql
3、 cd /opt/mysql
ls -al
rpm -ivh MySQL-client-5.6.48-1.el6.i686.rpm
rpm -ivh MySQL-shared-5.6.48-1.el6.i686.rpm
rpm -ivh MySQL-shared-compat-5.6.48-1.el6.i686.rpm
rpm -ivh MySQL-server-5.6.48-1.el6.i686.rpm
# 验证 mysql 是否安装成功,成功显示:MySQL is not running[FAILED]
4、service mysqld status
# 启动 mysql 服务,查看 root 用户的密码
# 使用该密码,进入 mysql 修改密码,刷新授权表
# 退出 mysql,再以新密码进入 mysql,验证密码是否修改成功
5、 service mysqld start
cat /root/.mysql_secret
mysql -u root -p
set password for root@localhost=password('123456');
flush privileges;
mysql -u root -p
# mysql 服务命令
service mysqld status / start / stop / restart