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: 只允许登录–其它什么也不允许做。