如何在centos中完全彻底卸载mysql

本文提供了一种在CentOS 6.5系统上彻底卸载MySQL 5.6的方法,包括卸载通过rpm安装的MySQL服务,确保所有相关文件及配置被清除,并验证卸载是否成功。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



CentOS 下如何完全卸载MySQL?解决卸载不干净的问题

系统:CentOS 6.5,MySQL:MySql 5.6

这里主要解决使用rpm安装的卸载问题,安装方法见:CentOS安装MySQL*.rpm提示conflicts with file from package的解决办法 见 http://www.linuxidc.com/Linux/2016-12/137940.htm

虽然说这里的是使用rpm安装的卸载问题,我觉得使用yum方式安装的如果遇到相同的问题也一样适用。 

查看已经安装的服务 
rpm –qa|grep -i mysql 
-i 作用是不区分大小写

yum remove mysql mysql-server mysql-libs compat-mysql51
rm -rf /var/lib/mysql
rm /etc/my.cnf
查看是否还有mysql软件:
rpm -qa|grep mysql
有的话继续删除

可以看到有两个安装包 
MySQL-server-5.6.19-1.linux_glibc2.5.x86_64.rpm
MySQL-client-5.6.19-1.linux_glibc2.5.x86_64.rpm

删除这两个服务(去掉后缀) 
rpm –e MySQL-client-5.6.19-1.linux_glibc2.5.x86_64
rpm -e MySQL-server-5.6.19-1.linux_glibc2.5.x86_64  
查看残留的目录:

whereis mysql 

然后删除mysql目录:

rm –rf /usr/lib64/mysql 

删除相关文件: 
rm –rf /usr/my.cnf
rm -rf /root/.mysql_sercret  
最关键的:

rm -rf /var/lib/mysql

如果这个目录如果不删除,再重新安装之后,密码还是之前的密码,不会重新初始化!

网上查了很久都没有文章提到这个,最后还是自己摸索找出来的。


卸载完成!怎么确定是不是真的卸载干净了呢?

一是看安装输出:

如果没有卸载干净,安装server时输入只有两行:

[root @localhost opt]# rpm -ivh MySQL-server-5.6.19-1.linux_glibc2.5.x86_64.rpm 
Preparing...                ########################################### [100%] 
1:MySQL-server          ########################################### [100%]

卸载干净了安装输入如下:

[root @localhost opt]# rpm -ivh MySQL-server-5.6.19-1.linux_glibc2.5.x86_64.rpm 
Preparing...                ########################################### [100%] 
1:MySQL-server          ########################################### [100%]

2014-09-23 07:22:43 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 
2014-09-23 07:22:43 26041 [Note] InnoDB: Using atomics to ref count buffer pool pages 
2014-09-23 07:22:43 26041 [Note] InnoDB: The InnoDB memory heap is disabled 
2014-09-23 07:22:43 26041 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins 
2014-09-23 07:22:43 26041 [Note] InnoDB: Compressed tables use zlib 1.2.3 
2014-09-23 07:22:43 26041 [Note] InnoDB: Using Linux native AIO 
2014-09-23 07:22:43 26041 [Note] InnoDB: Using CPU crc32 instructions 
2014-09-23 07:22:43 26041 [Note] InnoDB: Initializing buffer pool, size = 128.0M 
2014-09-23 07:22:43 26041 [Note] InnoDB: Completed initialization of buffer pool 
2014-09-23 07:22:43 26041 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created! 
2014-09-23 07:22:43 26041 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB 
2014-09-23 07:22:43 26041 [Note] InnoDB: Database physically writes the file full: wait... 
2014-09-23 07:22:43 26041 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB 
2014-09-23 07:22:43 26041 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB 
2014-09-23 07:22:45 26041 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0 
2014-09-23 07:22:45 26041 [Warning] InnoDB: New log files created, LSN=45781 
2014-09-23 07:22:45 26041 [Note] InnoDB: Doublewrite buffer not found: creating new 
2014-09-23 07:22:45 26041 [Note] InnoDB: Doublewrite buffer created 
2014-09-23 07:22:45 26041 [Note] InnoDB: 128 rollback segment(s) are active. 
2014-09-23 07:22:45 26041 [Warning] InnoDB: Creating foreign key constraint system tables. 
2014-09-23 07:22:45 26041 [Note] InnoDB: Foreign key constraint system tables created 
2014-09-23 07:22:45 26041 [Note] InnoDB: Creating tablespace and datafile system tables. 
2014-09-23 07:22:45 26041 [Note] InnoDB: Tablespace and datafile system tables created. 
2014-09-23 07:22:45 26041 [Note] InnoDB: Waiting for purge to start 
2014-09-23 07:22:45 26041 [Note] InnoDB: 5.6.19 started; log sequence number 0 
A random root password has been set. You will find it in '/root/.mysql_secret'. 
2014-09-23 07:22:46 26041 [Note] Binlog end 
2014-09-23 07:22:46 26041 [Note] InnoDB: FTS optimize thread exiting. 
2014-09-23 07:22:46 26041 [Note] InnoDB: Starting shutdown... 
2014-09-23 07:22:48 26041 [Note] InnoDB: Shutdown completed; log sequence number 1625977

2014-09-23 07:22:48 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 
2014-09-23 07:22:48 26065 [Note] InnoDB: Using atomics to ref count buffer pool pages 
2014-09-23 07:22:48 26065 [Note] InnoDB: The InnoDB memory heap is disabled 
2014-09-23 07:22:48 26065 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins 
2014-09-23 07:22:48 26065 [Note] InnoDB: Compressed tables use zlib 1.2.3 
2014-09-23 07:22:48 26065 [Note] InnoDB: Using Linux native AIO 
2014-09-23 07:22:48 26065 [Note] InnoDB: Using CPU crc32 instructions 
2014-09-23 07:22:48 26065 [Note] InnoDB: Initializing buffer pool, size = 128.0M 
2014-09-23 07:22:48 26065 [Note] InnoDB: Completed initialization of buffer pool 
2014-09-23 07:22:48 26065 [Note] InnoDB: Highest supported file format is Barracuda. 
2014-09-23 07:22:48 26065 [Note] InnoDB: 128 rollback segment(s) are active. 
2014-09-23 07:22:48 26065 [Note] InnoDB: Waiting for purge to start 
2014-09-23 07:22:48 26065 [Note] InnoDB: 5.6.19 started; log sequence number 1625977 
2014-09-23 07:22:48 26065 [Note] Binlog end 
2014-09-23 07:22:48 26065 [Note] InnoDB: FTS optimize thread exiting. 
2014-09-23 07:22:48 26065 [Note] InnoDB: Starting shutdown... 
2014-09-23 07:22:50 26065 [Note] InnoDB: Shutdown completed; log sequence number 1625987

A RANDOM PASSWORD HAS BEEN SET FOR THE MySQL root USER ! 
You will find that password in '/root/.mysql_secret'.

You must change that password on your first connect, 
no other statement but 'SET PASSWORD' will be accepted. 
See the manual for the semantics of the 'password expired' flag.

 Also, the account for the anonymous user has been removed.

 In addition, you can run:

  /usr/bin/mysql_secure_installation

 which will also give you the option of removing the test database. 
This is strongly recommended for production servers.

 See the manual for more instructions.

 Please report any problems at http://bugs.mysql.com/

 The latest information about MySQL is available on the web at

  http://www.mysql.com

 Support MySQL by buying support/licenses at http://shop.mysql.com

 New default config file was created as /usr/my.cnf and 
will be used by default by the server when you start it. 
You may edit this file to change server settings

最后一段中提示了重要信息,很多人因为不喜欢读英文,导致接下来不知道怎么操作!

二就是安装完成,启动服务之后

CentOS下如何完全干净卸载MySQL

使用命令:“mysql -uroot -p”输入你之前安装后设置的密码,看能不能登录,如果还可以登录,说明没有卸载干净!

CentOS下如何完全卸载MySQL?解决卸载不干净的问题

系统:CentOS 6.5,MySQL:MySql 5.6

这里主要解决使用rpm安装的卸载问题,安装方法见:CentOS安装MySQL*.rpm提示conflicts with file from package的解决办法 见 http://www.linuxidc.com/Linux/2016-12/137940.htm

虽然说这里的是使用rpm安装的卸载问题,我觉得使用yum方式安装的如果遇到相同的问题也一样适用。 

查看已经安装的服务 
rpm –qa|grep -i mysql 
-i 作用是不区分大小写

yum remove mysql mysql-server mysql-libs compat-mysql51
rm -rf /var/lib/mysql
rm /etc/my.cnf
查看是否还有mysql软件:
rpm -qa|grep mysql
有的话继续删除

可以看到有两个安装包 
MySQL-server-5.6.19-1.linux_glibc2.5.x86_64.rpm
MySQL-client-5.6.19-1.linux_glibc2.5.x86_64.rpm

删除这两个服务(去掉后缀) 
rpm –e MySQL-client-5.6.19-1.linux_glibc2.5.x86_64
rpm -e MySQL-server-5.6.19-1.linux_glibc2.5.x86_64  
查看残留的目录:

whereis mysql 

然后删除mysql目录:

rm –rf /usr/lib64/mysql 

删除相关文件: 
rm –rf /usr/my.cnf
rm -rf /root/.mysql_sercret  
最关键的:

rm -rf /var/lib/mysql

如果这个目录如果不删除,再重新安装之后,密码还是之前的密码,不会重新初始化!

网上查了很久都没有文章提到这个,最后还是自己摸索找出来的。


卸载完成!怎么确定是不是真的卸载干净了呢?

一是看安装输出:

如果没有卸载干净,安装server时输入只有两行:

[root @localhost opt]# rpm -ivh MySQL-server-5.6.19-1.linux_glibc2.5.x86_64.rpm 
Preparing...                ########################################### [100%] 
1:MySQL-server          ########################################### [100%]

卸载干净了安装输入如下:

[root @localhost opt]# rpm -ivh MySQL-server-5.6.19-1.linux_glibc2.5.x86_64.rpm 
Preparing...                ########################################### [100%] 
1:MySQL-server          ########################################### [100%]

2014-09-23 07:22:43 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 
2014-09-23 07:22:43 26041 [Note] InnoDB: Using atomics to ref count buffer pool pages 
2014-09-23 07:22:43 26041 [Note] InnoDB: The InnoDB memory heap is disabled 
2014-09-23 07:22:43 26041 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins 
2014-09-23 07:22:43 26041 [Note] InnoDB: Compressed tables use zlib 1.2.3 
2014-09-23 07:22:43 26041 [Note] InnoDB: Using Linux native AIO 
2014-09-23 07:22:43 26041 [Note] InnoDB: Using CPU crc32 instructions 
2014-09-23 07:22:43 26041 [Note] InnoDB: Initializing buffer pool, size = 128.0M 
2014-09-23 07:22:43 26041 [Note] InnoDB: Completed initialization of buffer pool 
2014-09-23 07:22:43 26041 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created! 
2014-09-23 07:22:43 26041 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB 
2014-09-23 07:22:43 26041 [Note] InnoDB: Database physically writes the file full: wait... 
2014-09-23 07:22:43 26041 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB 
2014-09-23 07:22:43 26041 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB 
2014-09-23 07:22:45 26041 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0 
2014-09-23 07:22:45 26041 [Warning] InnoDB: New log files created, LSN=45781 
2014-09-23 07:22:45 26041 [Note] InnoDB: Doublewrite buffer not found: creating new 
2014-09-23 07:22:45 26041 [Note] InnoDB: Doublewrite buffer created 
2014-09-23 07:22:45 26041 [Note] InnoDB: 128 rollback segment(s) are active. 
2014-09-23 07:22:45 26041 [Warning] InnoDB: Creating foreign key constraint system tables. 
2014-09-23 07:22:45 26041 [Note] InnoDB: Foreign key constraint system tables created 
2014-09-23 07:22:45 26041 [Note] InnoDB: Creating tablespace and datafile system tables. 
2014-09-23 07:22:45 26041 [Note] InnoDB: Tablespace and datafile system tables created. 
2014-09-23 07:22:45 26041 [Note] InnoDB: Waiting for purge to start 
2014-09-23 07:22:45 26041 [Note] InnoDB: 5.6.19 started; log sequence number 0 
A random root password has been set. You will find it in '/root/.mysql_secret'. 
2014-09-23 07:22:46 26041 [Note] Binlog end 
2014-09-23 07:22:46 26041 [Note] InnoDB: FTS optimize thread exiting. 
2014-09-23 07:22:46 26041 [Note] InnoDB: Starting shutdown... 
2014-09-23 07:22:48 26041 [Note] InnoDB: Shutdown completed; log sequence number 1625977

2014-09-23 07:22:48 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 
2014-09-23 07:22:48 26065 [Note] InnoDB: Using atomics to ref count buffer pool pages 
2014-09-23 07:22:48 26065 [Note] InnoDB: The InnoDB memory heap is disabled 
2014-09-23 07:22:48 26065 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins 
2014-09-23 07:22:48 26065 [Note] InnoDB: Compressed tables use zlib 1.2.3 
2014-09-23 07:22:48 26065 [Note] InnoDB: Using Linux native AIO 
2014-09-23 07:22:48 26065 [Note] InnoDB: Using CPU crc32 instructions 
2014-09-23 07:22:48 26065 [Note] InnoDB: Initializing buffer pool, size = 128.0M 
2014-09-23 07:22:48 26065 [Note] InnoDB: Completed initialization of buffer pool 
2014-09-23 07:22:48 26065 [Note] InnoDB: Highest supported file format is Barracuda. 
2014-09-23 07:22:48 26065 [Note] InnoDB: 128 rollback segment(s) are active. 
2014-09-23 07:22:48 26065 [Note] InnoDB: Waiting for purge to start 
2014-09-23 07:22:48 26065 [Note] InnoDB: 5.6.19 started; log sequence number 1625977 
2014-09-23 07:22:48 26065 [Note] Binlog end 
2014-09-23 07:22:48 26065 [Note] InnoDB: FTS optimize thread exiting. 
2014-09-23 07:22:48 26065 [Note] InnoDB: Starting shutdown... 
2014-09-23 07:22:50 26065 [Note] InnoDB: Shutdown completed; log sequence number 1625987

A RANDOM PASSWORD HAS BEEN SET FOR THE MySQL root USER ! 
You will find that password in '/root/.mysql_secret'.

You must change that password on your first connect, 
no other statement but 'SET PASSWORD' will be accepted. 
See the manual for the semantics of the 'password expired' flag.

 Also, the account for the anonymous user has been removed.

 In addition, you can run:

  /usr/bin/mysql_secure_installation

 which will also give you the option of removing the test database. 
This is strongly recommended for production servers.

 See the manual for more instructions.

 Please report any problems at http://bugs.mysql.com/

 The latest information about MySQL is available on the web at

  http://www.mysql.com

 Support MySQL by buying support/licenses at http://shop.mysql.com

 New default config file was created as /usr/my.cnf and 
will be used by default by the server when you start it. 
You may edit this file to change server settings

最后一段中提示了重要信息,很多人因为不喜欢读英文,导致接下来不知道怎么操作!

二就是安装完成,启动服务之后

CentOS下如何完全干净卸载MySQL

使用命令:“mysql -uroot -p”输入你之前安装后设置的密码,看能不能登录,如果还可以登录,说明没有卸载干净!

<think>首先,用户的问题是关于在CentOS 9上完全卸载MySQL的方法。我需要根据系统指令来构建我的回答。 系统指令包括: - 所有行内数学表达式使用$...$格式。 - 独立公式使用$$...$$格式并单独成段。 - LaTeX语法正确。 - 使用中文回答。 - 生成相关问题。 - 在回答中引用的段落末尾自然地添加引用标识。 用户的问题是关于卸载MySQL的,所以数学表达式可能不直接相关。我会确保如果有任何数学内容,就正确格式化。 参考站内引用: - 引用[1]: 列出了MySQL的一些文件和目录路径。 - 引用[2]: 描述了在CentOS7下安装和卸载MySQL的命令。 - 引用[3]: 关于如何完全卸载MySQL,包括卸载自带的MySQL。 - 引用[4]: 卸载MySQL并安装MariaDB的方法。 - 引用[5]: CentOS6.9安装与卸载MySQL的实战,包括权限命令。 用户指定了CentOS 9,但引用中提到了CentOS 6.9和7。我需要确保我的回答针对CentOS 9,但可以借鉴这些引用中的通用步骤。 关键点: - CentOS 9可能使用不同的包管理器或版本。CentOS 9基于RHEL 9,使用dnf或yum作为包管理器。 - 完全卸载MySQL包括删除所有相关文件、配置和数据。 标准卸载步骤: 1. 停止MySQL服务。 2. 卸载MySQL软件包。 3. 删除所有配置文件和数据目录。 4. 清理残留文件。 从引用中提取有用信息: - 引用[3]提到卸载自带的MySQL,并确保干净卸载。 - 引用[1]列出了可能存在的文件和目录路径。 - 引用[5]有权限命令,但主要针对安装。 我的回答结构: 1. **介绍**:解释为什么需要完全卸载。 2. **步骤**: - 停止MySQL服务。 - 卸载MySQL包。 - 删除残留文件和目录。 - 验证卸载。 3. **添加引用标识**:在引用相关部分时添加。 4. **生成相关问题**:在最后添加相关问题部分。 由于用户指定了CentOS 9,我需要确认命令。CentOS 9使用dnf作为默认包管理器,但yum可能仍然可用。 卸载命令: - 使用dnf或yum卸载包。 - 包名可能为mysql-server或mariadb-server,取决于安装的版本。 从引用[3]:rpm -ivh MySQL-server... 表示使用rpm安装,但卸载时可以用yum remove。 完整步骤: 1. 停止服务:sudo systemctl stop mysqld 2. 卸载包:sudo dnf remove mysql-server mysql-common 3. 删除文件和目录:如/etc/my.cnf, /var/lib/mysql等。 4. 清理其他文件:使用find命令或手动删除。 在回答中,自然地添加引用标识。例如,当提到文件路径时,引用[1];当提到卸载命令时,引用[3]。 使用中文回答。 生成相关问题: - 例如: 1. 如何安装MySQLCentOS 9上? 2. 卸载后如何安装MariaDB? 3. 如何确保所有MySQL数据被删除? 确保回答真实可靠。基于标准Linux管理实践。 最终回答结构: - 开头:简要说明。 - 步骤:编号或列表。 - 结尾:相关问题。</think>### CentOS 9 完全卸载 MySQL 方法 在 CentOS 9 上完全卸载 MySQL 需要彻底移除软件包、配置文件、数据目录和残留文件,以避免影响后续安装或系统稳定性。以下是详细步骤,基于标准 Linux 包管理实践和参考文档。操作前请确保以 root 用户或使用 `sudo` 权限执行命令,并备份重要数据(如数据库文件)。CentOS 9 默认使用 `dnf` 包管理器(兼容 `yum`),MySQL 包名通常为 `mysql-server` 或 `mariadb-server`(取决于安装版本)。 #### 步骤 1: 停止 MySQL 服务 首先,停止正在运行的 MySQL 服务,防止卸载过程中数据损坏。 ```bash sudo systemctl stop mysqld # 停止服务 sudo systemctl disable mysqld # 禁用开机自启 ``` 如果服务不存在(如已部分卸载),可忽略错误提示。此步骤确保卸载过程安全[^3]。 #### 步骤 2: 卸载 MySQL 软件包 使用 `dnf` 卸载 MySQL 核心包及其依赖。列出已安装的 MySQL 相关包,然后批量移除。 ```bash # 列出所有 MySQL/MariaDB 相关包 sudo dnf list installed | grep -i mysql sudo dnf list installed | grep -i mariadb # 卸载包(根据查询结果调整包名) sudo dnf remove mysql-server mysql-common mariadb-server mariadb-common ``` 卸载后,系统会提示删除依赖包,确认即可。如果使用 RPM 包安装(如从官网下载),需额外运行 `sudo rpm -e <package-name>` 移除残留包[^3]。 #### 步骤 3: 删除配置文件和残留目录 卸载包后,手动删除配置文件、数据目录和日志文件,这是完全卸载的关键。参考常见路径(基于 CentOS 默认安装): ```bash # 删除配置文件和目录 sudo rm -rf /etc/my.cnf /etc/my.cnf.d /etc/mysql /var/lib/mysql /var/log/mysql # 删除用户和组(如果存在) sudo userdel mysql sudo groupdel mysql # 清理临时文件和缓存 sudo rm -rf /tmp/mysql.sock /var/lock/subsys/mysql ``` 这些路径包括配置文件(如 `/etc/my.cnf`)、数据存储(如 `/var/lib/mysql`)和日志文件。您可以使用 `find` 命令搜索残留文件:`sudo find / -name "*mysql*"`,并手动删除输出结果[^1][^3]。 #### 步骤 4: 验证卸载 检查是否卸载干净: ```bash # 验证服务状态 sudo systemctl status mysqld # 应提示 "not found" # 检查包列表 sudo dnf list installed | grep -i mysql # 应无输出 # 尝试运行 MySQL 命令 mysql --version # 应提示 "command not found" ``` 如果仍有残留,重复步骤 2-3。完全卸载后,系统将无 MySQL 痕迹。 #### 注意事项 - **权限问题**:如果卸载后安装新数据库(如 MariaDB),需重新配置权限。例如,使用 `GRANT ALL PRIVILEGES` 命令授权远程访问[^4][^5]。 - **依赖冲突**:CentOS 9 可能预装 MariaDB(MySQL 分支)。卸载时指定 `mariadb-server` 代替 `mysql-server`。 - **数据备份**:卸载前备份 `/var/lib/mysql` 目录,以防数据丢失。 - **自动清理**:工具如 `dnf autoremove` 可辅助清理未用依赖。 通过以上步骤,您可以在 CentOS 9 上彻底卸载 MySQL。如果遇到问题,请检查系统日志(`journalctl -xe`)[^2][^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值