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