卸载mysql和 Access denied for user root@localhost 解决

1.为什么会出现这种异常?
    原因:Mysql有自己的权限控制,Mysql为了安全性,在默认情况下用户只允许在本地登录。但是localhost连接不可以可能是由于root用户被修改的缘故吧。Mysql有自带的数据库mysql,其中有表user是存放用户相关信息的。所以想办法连接上mysql 修改user表即可。
2.通用的解决办法

# /etc/init.d/mysql stop
# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
# mysql -u root mysql
mysql> UPDATE user SET Password=PASSWORD('newpassword') where USER='root';
mysql> FLUSH PRIVILEGES;
mysql> quit
# /etc/init.d/mysql restart
# mysql -uroot -p
Enter password: <输入新设的密码newpassword>

3.将mysql完全卸载
如果是结合php环境安装的话需要卸载apache mysql php

yum -y remove httpd会将和httpd有依赖关系的包也一并删除,算是比较干净和省事的卸载方式了,但依然会有遗漏的可能。可以用 rpm -qa | grep httpd 查看是否有遗漏。 如果有,可以用 rpm -e 包名 删除。终极查漏的方式: httpd -v 还有 which httpdmysql 和 php 也用类似方式删除。php的依赖关系比较多,也可以先用rpm -qa | grep php 查看全部依赖包,再用命令 rpm -e 包名 将它们一一删除。 删除时会提示依赖关系,提示那个包就先那个包即可。大概顺序如下:rpm -e php-fpm-5.3.3-22.el6.x86_64rpm-e php-pdo-5.3.3-22.el6.x86_64
php-gd-5.3.3-23.el6_4.x86_64
php-cli-5.3.3-23.el6_4.x86_64
php-mysql-5.3.3-23.el6_4.x86_64
php-common-5.3.3-23.el6_4.x86_64
php-pdo-5.3.3-23.el6_4.x86_64
干净删除mysql
yum remove mysql mysql-server mysql-libs compat-mysql51
rm -rf /var/lib/mysql
rm /etc/my.cnf
查看是否还有mysql软件:
rpm -qa|grep mysql
which mysql
mysql -V

[root@l-pub1 siremis]# yum -y remove httpd
Loaded plugins: fastestmirror, security
Setting up Remove Process
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.2.15-29.el6.centos will be erased
--> Processing Dependency: httpd-mmn = 20051115 for package: php-5.3.3-23.el6_4.x86_64
--> Running transaction check
---> Package php.x86_64 0:5.3.3-23.el6_4 will be erased
--> Finished Dependency Resolution    
base                                                                                                                                                                                       | 3.7 kB     00:00     
extras                                                                                                                                                                                     | 3.4 kB     00:00     
updates                                                                                                                                                                                    | 3.4 kB     00:00     
webtatic                                                                                                                                                                                   | 3.6 kB     00:00     
webtatic/primary_db                                                                                                                                                                        | 248 kB     00:00     

Dependencies Resolved

==================================================================================================================================================================================================================
 Package                                       Arch                                           Version                                                      Repository                                        Size
==================================================================================================================================================================================================================
Removing:
 httpd                                         x86_64                                         2.2.15-29.el6.centos                                         @updates                                         2.9 M
Removing for dependencies:
 php                                           x86_64                                         5.3.3-23.el6_4                                               @updates                                         3.5 M

Transaction Summary
==================================================================================================================================================================================================================
Remove        2 Package(s)

Installed size: 6.5 M
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Erasing    : php-5.3.3-23.el6_4.x86_64                                                                                                                                                                      1/2 
  Erasing    : httpd-2.2.15-29.el6.centos.x86_64                                                                                                                                                              2/2 
warning: /etc/httpd/conf/httpd.conf saved as /etc/httpd/conf/httpd.conf.rpmsave
  Verifying  : httpd-2.2.15-29.el6.centos.x86_64                                                                                                                                                              1/2 
  Verifying  : php-5.3.3-23.el6_4.x86_64                                                                                                                                                                      2/2 

Removed:
  httpd.x86_64 0:2.2.15-29.el6.centos                                                                                                                                                                             

Dependency Removed:
  php.x86_64 0:5.3.3-23.el6_4                                                                                                                                                                                     

Complete!
[root@l-pub1 siremis]# httpd -v
httpd: Command not found.
[root@l-pub1 siremis]# which httpd
httpd: Command not found.
[root@l-pub1 siremis]# rpm -qa | grep httpd
httpd-tools-2.2.15-29.el6.centos.x86_64
[root@l-pub1 siremis]# rpm -e httpd-tools-2.2.15-29.el6.centos.x86_64
[root@l-pub1 siremis]# yum -y remove php
Loaded plugins: fastestmirror, security
Setting up Remove Process
No Match for argument: php
Determining fastest mirrors
 * webtatic: us-east.repo.webtatic.com
Package(s) php available, but not installed.
No Packages marked for removal
[root@l-pub1 siremis]# yum -y remove php5
Loaded plugins: fastestmirror, security
Setting up Remove Process
No Match for argument: php5
Loading mirror speeds from cached hostfile
 * webtatic: us-east.repo.webtatic.com
No Packages marked for removal
[root@l-pub1 siremis]# yum -y remove php55w
Loaded plugins: fastestmirror, security
Setting up Remove Process
No Match for argument: php55w
Loading mirror speeds from cached hostfile
 * webtatic: us-east.repo.webtatic.com
Package(s) php55w available, but not installed.
No Packages marked for removal
[root@l-pub1 siremis]# yum -y remove mysql
Loaded plugins: fastestmirror, security
Setting up Remove Process
Resolving Dependencies
--> Running transaction check
---> Package mysql.x86_64 0:5.1.69-1.el6_4 will be erased
--> Processing Dependency: mysql = 5.1.69-1.el6_4 for package: mysql-server-5.1.69-1.el6_4.x86_64
--> Processing Dependency: mysql = 5.1.69-1.el6_4 for package: mysql-devel-5.1.69-1.el6_4.x86_64
--> Running transaction check
---> Package mysql-devel.x86_64 0:5.1.69-1.el6_4 will be erased
--> Processing Dependency: mysql-devel for package: q-python27-mysql-1.2.4b5-2.el6.x86_64
---> Package mysql-server.x86_64 0:5.1.69-1.el6_4 will be erased
--> Running transaction check
---> Package q-python27-mysql.x86_64 0:1.2.4b5-2.el6 will be erased
--> Finished Dependency Resolution

Dependencies Resolved

==================================================================================================================================================================================================================
 Package                                               Arch                                        Version                                               Repository                                          Size
==================================================================================================================================================================================================================
Removing:
 mysql                                                 x86_64                                      5.1.69-1.el6_4                                        @updates                                           2.4 M
Removing for dependencies:
 mysql-devel                                           x86_64                                      5.1.69-1.el6_4                                        @updates                                           388 k
 mysql-server                                          x86_64                                      5.1.69-1.el6_4                                        @updates                                            25 M
 q-python27-mysql                                      x86_64                                      1.2.4b5-2.el6                                         @Qunar-x86_64                                      279 k

Transaction Summary
==================================================================================================================================================================================================================
Remove        4 Package(s)

Installed size: 28 M
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Warning: RPMDB altered outside of yum.
  Erasing    : q-python27-mysql-1.2.4b5-2.el6.x86_64                                                                                                                                                          1/4 
  Erasing    : mysql-devel-5.1.69-1.el6_4.x86_64                                                                                                                                                              2/4 
161115 10:56:26 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
  Erasing    : mysql-server-5.1.69-1.el6_4.x86_64                                                                                                                                                             3/4 
warning: /var/log/mysqld.log saved as /var/log/mysqld.log.rpmsave
  Erasing    : mysql-5.1.69-1.el6_4.x86_64                                                                                                                                                                    4/4 
  Verifying  : q-python27-mysql-1.2.4b5-2.el6.x86_64                                                                                                                                                          1/4 
  Verifying  : mysql-server-5.1.69-1.el6_4.x86_64                                                                                                                                                             2/4 
  Verifying  : mysql-5.1.69-1.el6_4.x86_64                                                                                                                                                                    3/4 
  Verifying  : mysql-devel-5.1.69-1.el6_4.x86_64                                                                                                                                                              4/4 

Removed:
  mysql.x86_64 0:5.1.69-1.el6_4                                                                                                                                                                                   

Dependency Removed:
  mysql-devel.x86_64 0:5.1.69-1.el6_4                                 mysql-server.x86_64 0:5.1.69-1.el6_4                                 q-python27-mysql.x86_64 0:1.2.4b5-2.el6                                

Complete!
[root@l-pub1 siremis]# rpm -qa | grep php
php-gd-5.3.3-23.el6_4.x86_64
php-cli-5.3.3-23.el6_4.x86_64
php-mysql-5.3.3-23.el6_4.x86_64
php-common-5.3.3-23.el6_4.x86_64
php-pdo-5.3.3-23.el6_4.x86_64
[root@l-pub1 siremis]# rpm -e php-gd-5.3.3-23.el6_4.x86_64
[root@l-pub1 siremis]# rpm -e php-cli-5.3.3-23.el6_4.x86_64
[root@l-pub1 siremis]# rpm -e php-mysql-5.3.3-23.el6_4.x86_64
[root@l-pub1 siremis]# rpm -e php-common-5.3.3-23.el6_4.x86_64
error: Failed dependencies:
    php-common(x86-64) = 5.3.3-23.el6_4 is needed by (installed) php-pdo-5.3.3-23.el6_4.x86_64
[root@l-pub1 siremis]# rpm -e php-pdo-5.3.3-23.el6_4.x86_64
[root@l-pub1 siremis]# rpm -e php-common-5.3.3-23.el6_4.x86_64
[root@l-pub1 siremis]# rpm -qa | grep php
[root@l-pub1 siremis]# yum remove mysql mysql-server mysql-libs compat-mysql51
Loaded plugins: fastestmirror, security
Setting up Remove Process
No Match for argument: mysql
Loading mirror speeds from cached hostfile
 * webtatic: us-east.repo.webtatic.com
Package(s) mysql available, but not installed.
No Match for argument: mysql-server
Package(s) mysql-server available, but not installed.
No Match for argument: compat-mysql51
Resolving Dependencies
--> Running transaction check
---> Package mysql-libs.x86_64 0:5.1.69-1.el6_4 will be erased
--> Processing Dependency: libmysqlclient.so.16()(64bit) for package: 2:postfix-2.6.6-2.2.el6_1.x86_64
--> Processing Dependency: libmysqlclient.so.16()(64bit) for package: perl-DBD-MySQL-4.013-3.el6.x86_64
--> Processing Dependency: libmysqlclient.so.16(libmysqlclient_16)(64bit) for package: 2:postfix-2.6.6-2.2.el6_1.x86_64
--> Processing Dependency: libmysqlclient.so.16(libmysqlclient_16)(64bit) for package: perl-DBD-MySQL-4.013-3.el6.x86_64
--> Processing Dependency: mysql-libs for package: 2:postfix-2.6.6-2.2.el6_1.x86_64
--> Running transaction check
---> Package perl-DBD-MySQL.x86_64 0:4.013-3.el6 will be erased
---> Package postfix.x86_64 2:2.6.6-2.2.el6_1 will be erased
--> Processing Dependency: /usr/sbin/sendmail for package: cronie-1.4.4-7.el6.x86_64
--> Running transaction check
---> Package cronie.x86_64 0:1.4.4-7.el6 will be erased
--> Processing Dependency: cronie = 1.4.4-7.el6 for package: cronie-anacron-1.4.4-7.el6.x86_64
--> Running transaction check
---> Package cronie-anacron.x86_64 0:1.4.4-7.el6 will be erased
--> Processing Dependency: /etc/cron.d for package: sysstat-9.0.4-20.el6.x86_64
--> Processing Dependency: /etc/cron.d for package: crontabs-1.10-33.el6.noarch
--> Restarting Dependency Resolution with new changes.
--> Running transaction check
---> Package crontabs.noarch 0:1.10-33.el6 will be erased
---> Package sysstat.x86_64 0:9.0.4-20.el6 will be erased
--> Finished Dependency Resolution

Dependencies Resolved

==================================================================================================================================================================================================================
 Package                                      Arch                                 Version                                           Repository                                                              Size
==================================================================================================================================================================================================================
Removing:
 mysql-libs                                   x86_64                               5.1.69-1.el6_4                                    @updates                                                               4.0 M
Removing for dependencies:
 cronie                                       x86_64                               1.4.4-7.el6                                       @anaconda-CentOS-201303020151.x86_64/6.4                               166 k
 cronie-anacron                               x86_64                               1.4.4-7.el6                                       @anaconda-CentOS-201303020151.x86_64/6.4                                43 k
 crontabs                                     noarch                               1.10-33.el6                                       @anaconda-CentOS-201303020151.x86_64/6.4                               2.4 k
 perl-DBD-MySQL                               x86_64                               4.013-3.el6                                       @base                                                                  338 k
 postfix                                      x86_64                               2:2.6.6-2.2.el6_1                                 @anaconda-CentOS-201303020151.x86_64/6.4                               9.7 M
 sysstat                                      x86_64                               9.0.4-20.el6                                      @anaconda-CentOS-201303020151.x86_64/6.4                               807 k

Transaction Summary
==================================================================================================================================================================================================================
Remove        7 Package(s)

Installed size: 15 M
Is this ok [y/N]: y
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Warning: RPMDB altered outside of yum.
  Erasing    : sysstat-9.0.4-20.el6.x86_64                                                                                                                                                                    1/7 
  Erasing    : cronie-1.4.4-7.el6.x86_64                                                                                                                                                                      2/7 
  Erasing    : cronie-anacron-1.4.4-7.el6.x86_64                                                                                                                                                              3/7 
  Erasing    : crontabs-1.10-33.el6.noarch                                                                                                                                                                    4/7 
  Erasing    : 2:postfix-2.6.6-2.2.el6_1.x86_64                                                                                                                                                               5/7 
warning: /etc/postfix/transport saved as /etc/postfix/transport.rpmsave
warning: /etc/postfix/main.cf saved as /etc/postfix/main.cf.rpmsave
  Erasing    : perl-DBD-MySQL-4.013-3.el6.x86_64                                                                                                                                                              6/7 
  Erasing    : mysql-libs-5.1.69-1.el6_4.x86_64                                                                                                                                                               7/7 
warning: /etc/my.cnf saved as /etc/my.cnf.rpmsave
  Verifying  : 2:postfix-2.6.6-2.2.el6_1.x86_64                                                                                                                                                               1/7 
  Verifying  : mysql-libs-5.1.69-1.el6_4.x86_64                                                                                                                                                               2/7 
  Verifying  : perl-DBD-MySQL-4.013-3.el6.x86_64                                                                                                                                                              3/7 
  Verifying  : cronie-anacron-1.4.4-7.el6.x86_64                                                                                                                                                              4/7 
  Verifying  : crontabs-1.10-33.el6.noarch                                                                                                                                                                    5/7 
  Verifying  : sysstat-9.0.4-20.el6.x86_64                                                                                                                                                                    6/7 
  Verifying  : cronie-1.4.4-7.el6.x86_64                                                                                                                                                                      7/7 

Removed:
  mysql-libs.x86_64 0:5.1.69-1.el6_4                                                                                                                                                                              

Dependency Removed:
  cronie.x86_64 0:1.4.4-7.el6   cronie-anacron.x86_64 0:1.4.4-7.el6   crontabs.noarch 0:1.10-33.el6   perl-DBD-MySQL.x86_64 0:4.013-3.el6   postfix.x86_64 2:2.6.6-2.2.el6_1   sysstat.x86_64 0:9.0.4-20.el6  

Complete!
[root@l-pub1 siremis]# rm -rf /var/lib/mysql
[root@l-pub1 siremis]# rm /etc/my.cnf
/bin/rm: cannot remove `/etc/my.cnf': No such file or directory
[root@l-pub1 siremis]# rpm -qa|grep mysql
[root@l-pub1 siremis]# which mysql
mysql: Command not found.
[root@l-pub1 siremis]# mysql -v
mysql: Command not found.
[root@l-pub1 siremis]# 

4.卸载完成了重装即可

sudo yum install php55w php55w-mysql php55w-gd php55w-curl

5.管理mysql权限控制 设置用户连接

一、允许root用户在任何地方进行远程登录,并具有所有库任何操作权限,
具体操作如下:
在本机先使用root用户登录mysql: mysql -u root -p"youpassword" 进行授权操作:
mysql>GRANT ALL PRIVILEGES ON . TO 'root'@'%' IDENTIFIED BY 'youpassword' WITH GRANT OPTION;
重载授权表:
FLUSH PRIVILEGES;
退出mysql数据库:
exit
二、允许root用户在一个特定的IP进行远程登录,并具有所有库任何操作权限,具体操作如下: 在本机先使用root用户登录mysql: mysql -u root -p"youpassword" 进行授权操作: GRANT ALL PRIVILEGES ON . TO root@"172.16.16.152" IDENTIFIED BY "youpassword" WITH GRANT OPTION; 重载授权表: FLUSH PRIVILEGES; 退出mysql数据库: exit
三、允许root用户在一个特定的IP进行远程登录,并具有所有库特定操作权限,具体操作如下: 在本机先使用root用户登录mysql: mysql -u root -p"youpassword" 进行授权操作: GRANT select,insert,update,delete ON . TO root@"172.16.16.152" IDENTIFIED BY "youpassword"; 重载授权表: FLUSH PRIVILEGES; 退出mysql数据库: exit
四、删除用户授权,需要使用REVOKE命令,具体命令格式为: REVOKE privileges ON 数据库[.表名] FROM user-name; 具体实例,先在本机登录mysql: mysql -u root -p"youpassword" 进行授权操作: GRANT select,insert,update,delete ON TEST-DB TO test-user@"172.16.16.152" IDENTIFIED BY "youpassword"; 再进行删除授权操作: REVOKE all on TEST-DB from test-user; ****注:该操作只是清除了用户对于TEST-DB的相关授权权限,但是这个“test-user”这个用户还是存在。 最后从用户表内清除用户: DELETE FROM user WHERE user="test-user"; 重载授权表: FLUSH PRIVILEGES; 退出mysql数据库: exit
五、MYSQL权限详细分类: 全局管理权限: FILE: 在MySQL服务器上读写文件。 PROCESS: 显示或杀死属于其它用户的服务线程。 RELOAD: 重载访问控制表,刷新日志等。 SHUTDOWN: 关闭MySQL服务。 数据库/数据表/数据列权限: ALTER: 修改已存在的数据表(例如增加/删除列)和索引。 CREATE: 建立新的数据库或数据表。 DELETE: 删除表的记录。 DROP: 删除数据表或数据库。 INDEX: 建立或删除索引。 INSERT: 增加表的记录。 SELECT: 显示/搜索表的记录。 UPDATE: 修改表中已存在的记录。 特别的权限: ALL: 允许做任何事(和root一样)。 USAGE: 只允许登录--其它什么也不允许做。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,547评论 6 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,399评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,428评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,599评论 1 274
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,612评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,577评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,941评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,603评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,852评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,605评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,693评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,375评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,955评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,936评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,172评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,970评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,414评论 2 342

推荐阅读更多精彩内容